-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemotePeerHandler.java
379 lines (337 loc) · 9.79 KB
/
RemotePeerHandler.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import java.net.*;
import java.io.*;
public class RemotePeerHandler implements Runnable, MessageConstants
{
private Socket peerSocket = null;
private InputStream in;
private OutputStream out;
private int connType;
private HandshakeMessage handshakeMessage;
String ownPeerId, remotePeerId;
final int ACTIVECONN = 1;
final int PASSIVECONN = 0;
public void openClose(InputStream i, Socket socket)
{
try {
i.close();
i = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
public RemotePeerHandler(Socket peerSocket, int connType, String ownPeerID) {
this.peerSocket = peerSocket;
this.connType = connType;
this.ownPeerId = ownPeerID;
try
{
in = peerSocket.getInputStream();
out = peerSocket.getOutputStream();
}
catch (Exception ex)
{
peerProcess.showLog(this.ownPeerId + " Error : " + ex.getMessage());
}
}
public RemotePeerHandler(String add, int port, int connType, String ownPeerID)
{
try
{
this.connType = connType;
this.ownPeerId = ownPeerID;
this.peerSocket = new Socket(add, port);
}
catch (UnknownHostException e)
{
peerProcess.showLog(ownPeerID + " RemotePeerHandler : " + e.getMessage());
}
catch (IOException e)
{
peerProcess.showLog(ownPeerID + " RemotePeerHandler : " + e.getMessage());
}
this.connType = connType;
try
{
in = peerSocket.getInputStream();
out = peerSocket.getOutputStream();
}
catch (Exception ex)
{
peerProcess.showLog(ownPeerID + " RemotePeerHandler : " + ex.getMessage());
}
}
public boolean SendHandshake()
{
try
{
out.write(HandshakeMessage.encodeMessage(new HandshakeMessage(MessageConstants.HANDSHAKE_HEADER, this.ownPeerId)));
}
catch (IOException e)
{
peerProcess.showLog(this.ownPeerId + " SendHandshake : " + e.getMessage());
return false;
}
return true;
}
public boolean ReceiveHandshake()
{
byte[] receivedHandshakeByte = new byte[32];
try
{
in.read(receivedHandshakeByte);
handshakeMessage = HandshakeMessage.decodeMessage(receivedHandshakeByte);
remotePeerId = handshakeMessage.getPeerIDString();
//populate peerID to socket mapping
peerProcess.peerIDToSocketMap.put(remotePeerId, this.peerSocket);
}
catch (IOException e)
{
peerProcess.showLog(this.ownPeerId + " ReceiveHandshake : " + e.getMessage());
return false;
}
return true;
}
public boolean SendRequest(int index)
{
try
{
out.write(DataMessage.encodeMessage(new DataMessage( DATA_MSG_REQUEST, ConversionUtil.intToByteArray(index))));
}
catch (IOException e)
{
peerProcess.showLog(this.ownPeerId + " SendRequest : " + e.getMessage());
return false;
}
return true;
}
public boolean SendInterested()
{
try{
out.write(DataMessage.encodeMessage(new DataMessage(DATA_MSG_INTERESTED)));
}
catch (IOException e){
peerProcess.showLog(this.ownPeerId + " SendInterested : " + e.getMessage());
return false;
}
return true;
}
public boolean SendNotInterested()
{
try{
out.write(DataMessage.encodeMessage(new DataMessage( DATA_MSG_NOTINTERESTED)));
}
catch (IOException e){
peerProcess.showLog(this.ownPeerId + " SendNotInterested : " + e.getMessage());
return false;
}
return true;
}
public boolean ReceiveUnchoke()
{
byte [] receiveUnchokeByte = null;
try
{
in.read(receiveUnchokeByte);
}
catch (IOException e){
peerProcess.showLog(this.ownPeerId + " ReceiveUnchoke : " + e.getMessage());
return false;
}
DataMessage m = DataMessage.decodeMessage(receiveUnchokeByte);
if(m.getMessageTypeString().equals(DATA_MSG_UNCHOKE)){
peerProcess.showLog(ownPeerId + "is unchoked by " + remotePeerId);
return true;
}
else
return false;
}
public boolean ReceiveChoke()
{
byte [] receiveChokeByte = null;
// Check whether the in stream has data to be read or not.
try{
if(in.available() == 0) return false;
}
catch (IOException e){
peerProcess.showLog(this.ownPeerId + " ReceiveChoke : " + e.getMessage());
return false;
}
try{
in.read(receiveChokeByte);
}
catch (IOException e){
peerProcess.showLog(this.ownPeerId + " ReceiveChoke : " + e.getMessage());
return false;
}
DataMessage m = DataMessage.decodeMessage(receiveChokeByte);
if(m.getMessageTypeString().equals(DATA_MSG_CHOKE))
{
// LOG 6:
peerProcess.showLog(ownPeerId + " is CHOKED by " + remotePeerId);
return true;
}
else
return false;
}
public boolean receivePeice()
{
byte [] receivePeice = null;
try
{
in.read(receivePeice);
}
catch (IOException e)
{
peerProcess.showLog(this.ownPeerId + " receivePeice : " + e.getMessage());
return false;
}
DataMessage m = DataMessage.decodeMessage(receivePeice);
if(m.getMessageTypeString().equals(DATA_MSG_UNCHOKE))
{
// LOG 5:
peerProcess.showLog(ownPeerId + " is UNCHOKED by " + remotePeerId);
return true;
}
else
return false;
}
public void run()
{
byte []handshakeBuff = new byte[32];
byte []dataBuffWithoutPayload = new byte[DATA_MSG_LEN + DATA_MSG_TYPE];
byte[] msgLength;
byte[] msgType;
DataMessageWrapper dataMsgWrapper = new DataMessageWrapper();
try
{
if(this.connType == ACTIVECONN)
{
if(!SendHandshake())
{
peerProcess.showLog(ownPeerId + " HANDSHAKE sending failed.");
System.exit(0);
}
else
{
peerProcess.showLog(ownPeerId + " HANDSHAKE has been sent...");
}
while(true)
{
in.read(handshakeBuff);
handshakeMessage = HandshakeMessage.decodeMessage(handshakeBuff);
if(handshakeMessage.getHeaderString().equals(MessageConstants.HANDSHAKE_HEADER))
{
remotePeerId = handshakeMessage.getPeerIDString();
peerProcess.showLog(ownPeerId + " makes a connection to Peer " + remotePeerId);
peerProcess.showLog(ownPeerId + " Received a HANDSHAKE message from Peer " + remotePeerId);
//populate peerID to socket mapping
peerProcess.peerIDToSocketMap.put(remotePeerId, this.peerSocket);
break;
}
else
{
continue;
}
}
// Sending BitField...
DataMessage d = new DataMessage(DATA_MSG_BITFIELD, peerProcess.ownBitField.encode());
byte []b = DataMessage.encodeMessage(d);
out.write(b);
peerProcess.remotePeerInfoHash.get(remotePeerId).state = 8;
}
//Passive connection
else
{
while(true)
{
in.read(handshakeBuff);
handshakeMessage = HandshakeMessage.decodeMessage(handshakeBuff);
if(handshakeMessage.getHeaderString().equals(MessageConstants.HANDSHAKE_HEADER))
{
remotePeerId = handshakeMessage.getPeerIDString();
peerProcess.showLog(ownPeerId + " makes a connection to Peer " + remotePeerId);
peerProcess.showLog(ownPeerId + " Received a HANDSHAKE message from Peer " + remotePeerId);
//populate peerID to socket mapping
peerProcess.peerIDToSocketMap.put(remotePeerId, this.peerSocket);
break;
}
else
{
continue;
}
}
if(!SendHandshake())
{
peerProcess.showLog(ownPeerId + " HANDSHAKE message sending failed.");
System.exit(0);
}
else
{
peerProcess.showLog(ownPeerId + " HANDSHAKE message has been sent successfully.");
}
peerProcess.remotePeerInfoHash.get(remotePeerId).state = 2;
}
// receive data messages continuously
while(true)
{
int headerBytes = in.read(dataBuffWithoutPayload);
if(headerBytes == -1)
break;
msgLength = new byte[DATA_MSG_LEN];
msgType = new byte[DATA_MSG_TYPE];
System.arraycopy(dataBuffWithoutPayload, 0, msgLength, 0, DATA_MSG_LEN);
System.arraycopy(dataBuffWithoutPayload, DATA_MSG_LEN, msgType, 0, DATA_MSG_TYPE);
DataMessage dataMessage = new DataMessage();
dataMessage.setMessageLength(msgLength);
dataMessage.setMessageType(msgType);
if(dataMessage.getMessageTypeString().equals(MessageConstants.DATA_MSG_CHOKE)
||dataMessage.getMessageTypeString().equals(MessageConstants.DATA_MSG_UNCHOKE)
||dataMessage.getMessageTypeString().equals(MessageConstants.DATA_MSG_INTERESTED)
||dataMessage.getMessageTypeString().equals(MessageConstants.DATA_MSG_NOTINTERESTED)){
dataMsgWrapper.dataMsg = dataMessage;
dataMsgWrapper.fromPeerID = this.remotePeerId;
peerProcess.addToMsgQueue(dataMsgWrapper);
}
else {
int bytesAlreadyRead = 0;
int bytesRead;
byte []dataBuffPayload = new byte[dataMessage.getMessageLengthInt()-1];
while(bytesAlreadyRead < dataMessage.getMessageLengthInt()-1){
bytesRead = in.read(dataBuffPayload, bytesAlreadyRead, dataMessage.getMessageLengthInt()-1-bytesAlreadyRead);
if(bytesRead == -1)
return;
bytesAlreadyRead += bytesRead;
}
byte []dataBuffWithPayload = new byte [dataMessage.getMessageLengthInt()+DATA_MSG_LEN];
System.arraycopy(dataBuffWithoutPayload, 0, dataBuffWithPayload, 0, DATA_MSG_LEN + DATA_MSG_TYPE);
System.arraycopy(dataBuffPayload, 0, dataBuffWithPayload, DATA_MSG_LEN + DATA_MSG_TYPE, dataBuffPayload.length);
DataMessage dataMsgWithPayload = DataMessage.decodeMessage(dataBuffWithPayload);
dataMsgWrapper.dataMsg = dataMsgWithPayload;
dataMsgWrapper.fromPeerID = remotePeerId;
peerProcess.addToMsgQueue(dataMsgWrapper);
dataBuffPayload = null;
dataBuffWithPayload = null;
bytesAlreadyRead = 0;
bytesRead = 0;
}
}
}
catch(IOException e){
peerProcess.showLog(ownPeerId + " run exception: " + e);
}
}
public void releaseSocket() {
try {
if (this.connType == PASSIVECONN && this.peerSocket != null) {
this.peerSocket.close();
}
if (in != null) {
in.close();
}
if (out != null)
out.close();
} catch (IOException e) {
peerProcess.showLog(ownPeerId + " Release socket IO exception: " + e);
}
}
}