-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path'
More file actions
204 lines (172 loc) · 5.41 KB
/
Copy path'
File metadata and controls
204 lines (172 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package network;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import models.Neighbour;
import models.Peer;
import utils.FileManager;
public class PeerNode {
private Peer peer;
private Server server;
private ClientManager clientManager;
private Set<Integer> requestedPeices;
private byte[] bitfield;
private FileManager fileManager;
private ConcurrentHashMap<Integer, Neighbour> otherPeerBitfield;
private Object pieceLock;
private Object bitfieldLock;
public PeerNode(Peer peer, FileManager fileManager, int noOfPieces) {
int bytefieldLength = Math.ceilDiv(noOfPieces, 8);
this.peer = peer;
this.fileManager = fileManager;
this.server = new Server(this);
this.clientManager = new ClientManager(this);
this.requestedPeices = new HashSet<>();
this.pieceLock = new Object();
this.bitfieldLock = new Object();
this.bitfield = new byte[bytefieldLength];
this.otherPeerBitfield = new ConcurrentHashMap<>();
if (peer.getisFilePresent()) {
fillBitfield(noOfPieces);
if (peer.getisFilePresent()) {
this.fileManager.breakFileIntoPeices();
this.fileManager.setNoOfMissingPeices(0);
}
} else {
Arrays.fill(this.bitfield, (byte) 0);
this.fileManager.setNoOfMissingPeices(noOfPieces);
}
}
public void setPeerInterested(int peerId) {
this.otherPeerBitfield.get(peerId).setInterestedInMe(true);
}
public void setPeerNotInterested(int peerId) {
this.otherPeerBitfield.get(peerId).setInterestedInMe(false);
}
public void setOtherPeerBitfield(int peerId, byte[] otherPeerBitfield) {
this.otherPeerBitfield.put(peerId, new Neighbour(otherPeerBitfield, peerId));
}
public void setBit(int peerId, int pieceIndex) {
synchronized (bitfieldLock) {
int byteIndex = pieceIndex / 8;
int bitIndex = 7 - pieceIndex % 8;
byte val = this.bitfield[byteIndex];
val = (byte) (val | (1 << bitIndex));
this.bitfield[byteIndex] = val;
}
removeInterestedPieces(peerId, pieceIndex);
this.requestedPeices.remove(pieceIndex);
}
public void setOtherPeerBit(int peerId, int pieceIndex) {
Neighbour neighbour = this.otherPeerBitfield.get(peerId);
byte[] bitfield = neighbour.getBitfield();
int byteIndex = pieceIndex / 8;
int bitIndex = 7 - pieceIndex % 8;
byte val = bitfield[byteIndex];
byte mask = (byte) (1 << bitIndex);
boolean alreadySet = (val & mask) != 0;
if (alreadySet) {
return;
}
val = (byte) (val | mask);
bitfield[byteIndex] = val;
neighbour.setBitfield(bitfield);
addInterestedPieces(peerId, pieceIndex);
}
private void addInterestedPieces(int peerId, int pieceIndex) {
Neighbour neighbour = this.otherPeerBitfield.get(peerId);
neighbour.addInterestingPieces(pieceIndex);
}
private void removeInterestedPieces(int peerId, int pieceIndex) {
Neighbour neighbour = this.otherPeerBitfield.get(peerId);
neighbour.removeInterestingPieces(pieceIndex);
}
public void braodcastToServers(byte[] payload) {
this.clientManager.broadcastToServers(payload);
}
public void broadcastToClients(byte[] payload) {
this.server.broadcastingToMyClients(payload);
}
public void setClientManager(ClientManager clientManager) {
this.clientManager = clientManager;
}
public void startServer() {
new Thread(this.server).start();
}
public void connectClient(int serverPort, int serverPeerId, String serverHost) {
System.out.println("inside peernode");
clientManager.connect(serverPort, serverPeerId, serverHost).start();
}
public boolean setInterestedPieces(int peerId, byte[] payload) {
List<Integer> interestedPieces = new ArrayList<>();
for (int i = 0; i < bitfield.length; i++) {
byte mine = this.bitfield[i];
byte theirs = payload[i];
for (int bit = 7; bit >= 0; bit--) {
int myBit = (mine >> bit) & 1;
int theirBit = (theirs >> bit) & 1;
if (myBit == 0 && theirBit == 1) {
int pieceIndex = i * 8 + (7 - bit);
interestedPieces.add(pieceIndex);
}
}
}
this.otherPeerBitfield.get(peerId).setInterestingPieces(interestedPieces);
return !interestedPieces.isEmpty();
}
public int getInterestedPiece(int peerId) {
synchronized (pieceLock) {
List<Integer> pieces = this.otherPeerBitfield.get(peerId).getInterestingPieces();
int ind = pieces.size() - 1;
while (ind >= 0) {
if (this.requestedPeices.contains(pieces.get(ind))) {
ind--;
} else {
this.requestedPeices.add(pieces.get(ind));
return pieces.get(ind);
}
}
return -1;
}
}
public Peer getPeer() {
return this.peer;
}
public Server getServer() {
return this.server;
}
public byte[] getBitfield() {
return this.bitfield;
}
public byte[] getOtherBitfield(int peerId) {
return this.otherPeerBitfield.get(peerId).getBitfield();
}
public FileManager getFileManager() {
return fileManager;
}
public void setFileManager(FileManager fileManager) {
this.fileManager = fileManager;
}
public Set<Integer> getRequestedPeices() {
return requestedPeices;
}
public void setRequestedPeices(Set<Integer> requestedPeices) {
this.requestedPeices = requestedPeices;
}
private void fillBitfield(int noOfPieces) {
int fullBytes = noOfPieces / 8;
Arrays.fill(this.bitfield, (byte) 0xFF);
int remainder = noOfPieces % 8;
if (remainder != 0) {
byte lastByte = 0;
for (int i = 0; i < remainder; i++) {
lastByte |= (1 << (7 - i));
}
bitfield[fullBytes] = lastByte;
}
}
}