-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitfield.java
More file actions
132 lines (116 loc) · 3.86 KB
/
Bitfield.java
File metadata and controls
132 lines (116 loc) · 3.86 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
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
// This class is to deal with bit field such as set bit and generate bit field message.
public class Bitfield {
//
private int length;
private List<Integer> bits;
private boolean hasCompleteFile;
public Bitfield(int length, Boolean hasfile) {
// This is used at the beignning of the program when we need to check which peers have file and the other do not have.
this.length = length;
bits = new ArrayList<Integer>(length);
for (int i = 0; i < this.length; i++) {
if (hasfile) {
bits.add(i, 1);
this.hasCompleteFile = true;
} else {
bits.add(i, 0);
this.hasCompleteFile = false;
}
}
}
public Bitfield(int length, byte[] bitfieldBuffer) {
// This is to construct peer's bit field while sharing the file.
this.length = length;
bits = new ArrayList<Integer>(length);
for (int i = 0; i < this.length; i++) {
if (byteToInt(bitfieldBuffer[0]) != 0) {
bits.add(i, 1);
this.hasCompleteFile = true;
} else {
bits.add(i, 0);
this.hasCompleteFile = false;
}
}
System.out.println(bits.size());
}
public int getLength() {
return length;
}
synchronized public int getBit(int index) {
return bits.get(index);
}
synchronized public void setBit(int index, int value) {
// This is used to chage bit in the bitfiled when recieve have message and piece message.
bits.set(index, value);
//Each time we need to check whether have complete file.
if (this.hasCompleteFile == true) {
return;
} else {
this.hasCompleteFile = true;
for (int i = 0; i < bits.size(); i++) {
if (bits.get(i) == 0)
this.hasCompleteFile = false;
}
}
}
synchronized public boolean isHasCompleteFile() {
return this.hasCompleteFile;
}
synchronized public int getPieceNum() {
// To get how many pieces currently have.
int pieceNum = 0;
for (int i = 0; i < this.length; i++) {
if (bits.get(i) == 1)
pieceNum++;
}
return pieceNum;
}
synchronized public Message genBitFieldMessage() throws IOException {
int size = this.length;
int messagelength = this.length / 8;
int leftbits = this.length % 8;
byte[] data;
if (leftbits == 0) {
data = new byte[messagelength];
} else {
data = new byte[messagelength + 1];
}
for (int i = 0; i < messagelength; i++) {
int value = 0;
for (int j = 0; j < 8; j++) {
value = value * 2;
value = value + bits.get(i * 8 + j);
}
data[i] = intToByte(value);
}
System.out.println(messagelength);
if (leftbits != 0) {
int value = 0;
for (int j = 0; j < leftbits; j++) {
value = value * 2;
System.out.println(j);
value = value + bits.get(messagelength * 8 + j);
}
for (int j = 0; j < 8 - leftbits; j++) {
value = value * 2;
}
data[messagelength] = intToByte(value);
}
return new BitFieldMessage(data);
}
public static int byteArrayToInt(byte[] b) {
return b[3] & 0xFF |
(b[2] & 0xFF) << 8 |
(b[1] & 0xFF) << 16 |
(b[0] & 0xFF) << 24;
}
public static byte intToByte(int a){
return (byte) a;
}
public static int byteToInt(byte b){
return b & 0xFF;
}
}