Repository navigation
Expand file tree
/
Copy pathBitField.java
More file actions
314 lines (244 loc) · 6.73 KB
/
Copy pathBitField.java
File metadata and controls
314 lines (244 loc) · 6.73 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.RandomAccessFile;
public class BitField implements MessageConstants
{
public Piece[] pieces;
public int size;
public BitField()
{
size = (int) Math.ceil(((double) CommonProperties.fileSize / (double) CommonProperties.pieceSize));
this.pieces = new Piece[size];
for (int i = 0; i < this.size; i++)
this.pieces[i] = new Piece();
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Piece[] getPieces() {
return pieces;
}
public void setPieces(Piece[] pieces) {
this.pieces = pieces;
}
public byte[] encode()
{
return this.getBytes();
}
public static BitField decode(byte[] b)
{
BitField returnBitField = new BitField();
for(int i = 0 ; i < b.length; i ++)
{
int count = 7;
while(count >=0)
{
int test = 1 << count;
if(i * 8 + (8-count-1) < returnBitField.size)
{
if((b[i] & (test)) != 0)
returnBitField.pieces[i * 8 + (8-count-1)].isPresent = 1;
else
returnBitField.pieces[i * 8 + (8-count-1)].isPresent = 0;
}
count--;
}
}
return returnBitField;
}
public synchronized boolean compare(BitField yourBitField) {
int yourSize = yourBitField.getSize();
for (int i = 0; i < yourSize; i++) {
if (yourBitField.getPieces()[i].getIsPresent() == 1
&& this.getPieces()[i].getIsPresent() == 0) {
return true;
} else
continue;
}
return false;
}
// find first bit number i don't have but you have
public synchronized int returnFirstDiff(BitField yourBitField)
{
int mySize = this.getSize();
int yourSize = yourBitField.getSize();
if (mySize >= yourSize) {
for (int i = 0; i < yourSize; i++) {
if (yourBitField.getPieces()[i].getIsPresent() == 1
&& this.getPieces()[i].getIsPresent() == 0) {
return i;
}
}
} else {
for (int i = 0; i < mySize; i++) {
if (yourBitField.getPieces()[i].getIsPresent() == 1
&& this.getPieces()[i].getIsPresent() == 0) {
return i;
}
}
}
return -1;
}
public byte[] getBytes()
{
int s = this.size / 8;
if (size % 8 != 0)
s = s + 1;
byte[] iP = new byte[s];
int tempInt = 0;
int count = 0;
int Cnt;
for (Cnt = 1; Cnt <= this.size; Cnt++)
{
int tempP = this.pieces[Cnt-1].isPresent;
tempInt = tempInt << 1;
if (tempP == 1)
{
tempInt = tempInt + 1;
} else
tempInt = tempInt + 0;
if (Cnt % 8 == 0 && Cnt!=0) {
iP[count] = (byte) tempInt;
count++;
tempInt = 0;
}
}
if ((Cnt-1) % 8 != 0)
{
int tempShift = ((size) - (size / 8) * 8);
tempInt = tempInt << (8 - tempShift);
iP[count] = (byte) tempInt;
}
return iP;
}
static String byteArrayToHexString(byte in[])
{
byte ch = 0x00;
int i = 0;
if (in == null || in.length <= 0)
return null;
String pseudo[] = {"0", "1", "2","3", "4", "5", "6", "7", "8","9", "A", "B", "C", "D", "E","F"};
StringBuffer out = new StringBuffer(in.length * 2);
while (i < in.length)
{
ch = (byte) (in[i] & 0xF0);
ch = (byte) (ch >>> 4);
// shift the bits down
ch = (byte) (ch & 0x0F);
// must do this is high order bit is on!
out.append(pseudo[ (int) ch]);
ch = (byte) (in[i] & 0x0F);
out.append(pseudo[ (int) ch]);
i++;
}
String rslt = new String(out);
return rslt;
}
public void initOwnBitfield(String OwnPeerId, int hasFile) {
if (hasFile != 1) {
// If the file not exists
for (int i = 0; i < this.size; i++) {
this.pieces[i].setIsPresent(0);
this.pieces[i].setFromPeerID(OwnPeerId);
}
} else {
// If the file exists
for (int i = 0; i < this.size; i++) {
this.pieces[i].setIsPresent(1);
this.pieces[i].setFromPeerID(OwnPeerId);
}
}
}
// Update the bit field class and piece information
public synchronized void updateBitField(String peerId, Piece piece) {
try
{
if (peerProcess.ownBitField.pieces[piece.pieceIndex].isPresent == 1) {
peerProcess.showLog(peerId + " Piece already received!!");
}
else
{
String fileName = CommonProperties.fileName;
File file = new File(peerProcess.peerID, fileName);
int off = piece.pieceIndex * CommonProperties.pieceSize;
RandomAccessFile raf = new RandomAccessFile(file, "rw");
byte[] byteWrite;
byteWrite = piece.filePiece;
raf.seek(off);
raf.write(byteWrite);
this.pieces[piece.pieceIndex].setIsPresent(1);
this.pieces[piece.pieceIndex].setFromPeerID(peerId);
raf.close();
peerProcess.showLog(peerProcess.peerID
+ " has downloaded the PIECE " + piece.pieceIndex
+ " from Peer " + peerId
+ ". Now the number of pieces it has is "
+ peerProcess.ownBitField.ownPieces());
if (peerProcess.ownBitField.isCompleted()) {
peerProcess.remotePeerInfoHash.get(peerProcess.peerID).isInterested = 0;
peerProcess.remotePeerInfoHash.get(peerProcess.peerID).isCompleted = 1;
peerProcess.remotePeerInfoHash.get(peerProcess.peerID).isChoked = 0;
updatePeerInfo(peerProcess.peerID, 1);
peerProcess.showLog(peerProcess.peerID + " has DOWNLOADED the complete file.");
}
}
} catch (Exception e) {
peerProcess.showLog(peerProcess.peerID
+ " EROR in updating bitfield " + e.getMessage());
}
}
public int ownPieces()
{
int count = 0;
for (int i = 0; i < this.size; i++)
if (this.pieces[i].isPresent == 1)
count++;
return count;
}
public boolean isCompleted() {
for (int i = 0; i < this.size; i++) {
if (this.pieces[i].isPresent == 0) {
return false;
}
}
return true;
}
// Updates PeerInfo.cfg
public void updatePeerInfo(String clientID, int hasFile)
{
BufferedWriter out = null;
BufferedReader in = null;
try
{
in= new BufferedReader(new FileReader("PeerInfo.cfg"));
String line;
StringBuffer buffer = new StringBuffer();
while((line = in.readLine()) != null)
{
if(line.trim().split("\\s+")[0].equals(clientID))
{
buffer.append(line.trim().split("\\s+")[0] + " " + line.trim().split("\\s+")[1] + " " + line.trim().split("\\s+")[2] + " " + hasFile);
}
else
{
buffer.append(line);
}
buffer.append("\n");
}
in.close();
out= new BufferedWriter(new FileWriter("PeerInfo.cfg"));
out.write(buffer.toString());
out.close();
}
catch (Exception e)
{
peerProcess.showLog(clientID + " Error in updating the PeerInfo.cfg " + e.getMessage());
}
}
}