-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommunicationManager.java
196 lines (160 loc) · 4.89 KB
/
CommunicationManager.java
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
import java.io.IOException;
import java.util.Map;
public class CommunicationManager {
// This class is to help us manage the communication( store the necessage information such as
//the negborhood list, piecenum, my bitfield and so on)
private int myPeerID;
private Map<Integer, NeighborInfo> neighborList;
private Commoncfg commoncfg;
private int pieceNum;
private int pieceSize;
private int lastPieceSize;
//private boolean hasFile;
private Bitfield myBitfield;
private FileManager file;
private Map<Integer, PeerInfo> peers;
public CommunicationManager(int myPeerID, Commoncfg commoncfg, Map<Integer, NeighborInfo> neighborList, Bitfield bitfield, Map<Integer, PeerInfo> peers) {
this.commoncfg = commoncfg;
this.neighborList = neighborList;
this.myPeerID = myPeerID;
//this.hasFile = hasFile;
this.pieceSize = commoncfg.getPieceSize();
int fileSize = commoncfg.getFileSize();
this.pieceNum = fileSize / this.pieceSize;
int value = fileSize % this.pieceSize;
if (value == 0) {
this.lastPieceSize = this.pieceSize;
} else {
this.lastPieceSize = value;
this.pieceNum = this.pieceNum + 1;
}
this.myBitfield = bitfield;
try {
this.file = new FileManager(this.myPeerID, this.commoncfg.getFileName(), this.commoncfg.getFileSize(), this.pieceSize);
this.file.createFile();
} catch (IOException e) {
e.printStackTrace();
}
this.peers = peers;
}
public Map<Integer, PeerInfo> getPeers() {
return peers;
}
public void setPeers(Map<Integer, PeerInfo> peers) {
this.peers = peers;
}
public FileManager getFile() {
return file;
}
public void setFile(FileManager file) {
this.file = file;
}
public Commoncfg getCommoncfg() {
return commoncfg;
}
public void setCommoncfg(Commoncfg commoncfg) {
this.commoncfg = commoncfg;
}
public Bitfield getMyBitfield() {
return this.myBitfield;
}
public void setMyBitfield(Bitfield myBitfield) {
this.myBitfield = myBitfield;
}
public void AddClient(int peerID, Client client) {
this.neighborList.get(peerID).setClient(client);
}
public void sendToAll(Message msg) {
for (Integer peerID : this.neighborList.keySet()) {
this.neighborList.get(peerID).getClient().send(msg.getMessageBytes());
}
}
public boolean isQuit() {
boolean quit;
if (this.neighborList.size() != this.peers.size() - 1) {
quit = false;
return quit;
}
if (this.myBitfield.isHasCompleteFile()) {
quit = true;
} else {
quit = false;
}
for (Integer peerID : this.neighborList.keySet()) {
if (!this.neighborList.get(peerID).isHasCompleteFile()) {
quit = false;
}
}
return quit;
}
public Map<Integer, NeighborInfo> getNeighborList() {
boolean a = false;
if (a) {
System.out.println("Getting neighbor list");
}
return neighborList;
}
public void setNeighborList(Map<Integer, NeighborInfo> neighborList) {
boolean a = false;
if (a) {
System.out.println("Setting neighbor list");
}
this.neighborList = neighborList;
}
public int getPieceNum() {
boolean a = false;
if (a) {
System.out.println("Get piece number");
}
return pieceNum;
}
public void setPieceNum(int pieceNum) {
boolean a = false;
if (a) {
System.out.println("Set piece number");
}
this.pieceNum = pieceNum;
}
public int getPieceSize() {
boolean a = false;
if (a) {
System.out.println("Get piece size");
}
return pieceSize;
}
public void setPieceSize(int pieceSize) {
boolean a = false;
if (a) {
System.out.println("Set piece number");
}
this.pieceSize = pieceSize;
}
public int getLastPieceSize() {
boolean a = false;
if (a) {
System.out.println("Get last piece size");
}
return lastPieceSize;
}
public void setLastPieceSize(int lastPieceSize) {
boolean a = false;
if (a) {
System.out.println("set last piece size");
}
this.lastPieceSize = lastPieceSize;
}
public void setMyPeerID(int myPeerID) {
boolean a = false;
if (a) {
System.out.println("Set myPeerID");
}
this.myPeerID = myPeerID;
}
public int getMyPeerID() {
boolean a = false;
if (a) {
System.out.println("Get myPeerID");
}
return myPeerID;
}
}