-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynReceiver.java
92 lines (62 loc) · 3.14 KB
/
SynReceiver.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
/* Le Hénaff Pablo ; Basudan Hossam*/
import java.util.concurrent.LinkedBlockingDeque;
public class SynReceiver implements SimpleMessageHandler, Runnable {
private LinkedBlockingDeque<String> incoming = new LinkedBlockingDeque<String>(20);
private MuxDemuxSimple myMuxDemux = null;
@Override
public void run() {
// we need to check that m corresponds to a SYN message
// maybe change the structure and add a simple Message class ? or just handled through custom exceptions, as explained below
while(true){
try {
String[] split = incoming.take().split("/");
SynMessage synMessage = new SynMessage(split[0]);
// TODO: how to ignore syn messages received while sending list messages to that peer ?
// ie how to check that the last LIST message has already been sent ? -> peer state updated ?
// Hossam : if the syn message received while we send List messages it will be ignored be the process
// busy sending the list messages, after sending the list messages we change the state
// We check the state before processing to ignore already syn peers?
//IS SYN message for me?
//Are't we already synchronized?
//Do I have the version he needs?
if(synMessage.getPeerId().equals(myMuxDemux.getMyID()) &&
synMessage.getSequenceN()==myMuxDemux.myDatabase.getDatabaseSequenceNumber()) {
synchronized (myMuxDemux.myDatabase.stringQueue) { // used this new design: database is public and we need to add synchronized block each time we access it
int index = 0;
for (String dataString : myMuxDemux.myDatabase.stringQueue) {
ListMessage listMessage = new ListMessage(myMuxDemux.getMyID(), synMessage.getSenderID(), synMessage.getSequenceN(), myMuxDemux.myDatabase.size());
// tell to listMessage what is the data and which part it contains
listMessage.setPartNoAndData((index++), dataString);
myMuxDemux.send(listMessage.getListMessageAsEncodedString());
}
//After sending we update peer state without verifying that the messages were really sent!
//TODO questions:
// Do I need to update the peerSyn# in my peerTable?
// how do we get out of synchronized? only way by hello message to "inconsistent"
// how do we get from inconsistent to heard ? after expiration, we remove then we receive new hello message?
// what about dying? why do we use it? isn't dying means remove? or it is more than that?
myMuxDemux.getbyID(synMessage.getSenderID()).setPeerState("synchronized");
}
}
} catch (NotRightTypeException e) {
} catch (Exception e) {
// useless : SynMessage constructor receiving a HelloMessage string is normal
// another idea ? maybe SynMessage should raise a different exception when not the right type of message, so we can do nothing in that case
e.printStackTrace();
}
}
}
@Override
public void handleMessage(String m) {
try {
incoming.put(m);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void setMuxDemux(MuxDemuxSimple muxDemuxSimple) {
myMuxDemux = muxDemuxSimple;
}
}