-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMuxDemuxSimple.java
169 lines (147 loc) · 5.9 KB
/
MuxDemuxSimple.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
/* Le Hénaff Pablo ; Basudan Hossam*/
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingDeque;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
public class MuxDemuxSimple implements Runnable {
private DatagramSocket mySocket = null;
private SimpleMessageHandler[] myMessageHandlers;
private LinkedBlockingDeque<String> outgoing = new LinkedBlockingDeque<String>(20);
private LinkedBlockingDeque<PeerRecord> peerTable = new LinkedBlockingDeque<PeerRecord>(20);
private String myID =Test.MYID;
LinkedBlockingDeque<DownloadObject> downloadQueue = new LinkedBlockingDeque<DownloadObject>(20);
//TODO questions:
// "create and test the databases of my peers?"
// H1 : Do we all have the same database with different versions? --> only update
// H2 : Each machine is responsible of a small part of the database and I keep a backup?
// I will code ListReceiver based on H2 after discussion robin
public Database myDatabase = new Database(20);
private ConcurrentHashMap<String, Database> othersDatabases= new ConcurrentHashMap<String, Database> ();
public MuxDemuxSimple (SimpleMessageHandler[] h, DatagramSocket s){
if(s==null)
try {
exit(1);
} catch (Exception e) {
e.printStackTrace();
}
mySocket = s;
myMessageHandlers = h;
Thread senderThread = new Thread(new Runnable() {
@Override
public void run() {
byte [] byteArray;
DatagramPacket dp = null;
String msg = "";
while (true){
try {
msg = outgoing.take();
//InetAddress.getLocalHost();
} catch (InterruptedException e) {
System.err.println(e);
}
byteArray =msg.getBytes();
try {
dp = new DatagramPacket(byteArray, byteArray.length, InetAddress.getByName("255.255.255.255"), Test.PORTNO);
} catch (UnknownHostException e) {
System.err.println(e);
}
try {
mySocket.send(dp);
} catch (IOException e) {
System.err.println(e);
}
}
}
});
senderThread.start();
}
//Receiver Thread
public void run(){
for (int i=0; i<myMessageHandlers.length; i++){
myMessageHandlers[i].setMuxDemux(this);
}
byte[] buffer = new byte[1024];
DatagramPacket receiver = new DatagramPacket(buffer, buffer.length);
while(true) {
try {
mySocket.receive(receiver);
//TODO how to ip adresse to peer record
String data = new String(receiver.getData(),0, receiver.getLength(), StandardCharsets.UTF_8);
for (int i=0; i<myMessageHandlers.length; i++){
myMessageHandlers[i].handleMessage( data+ receiver.getAddress().toString());
// myMessageHandlers[i].handleMessage( data);
}
} catch (Exception e) {
System.err.println(e);
}
}
//TODO Close the socket
// try{
// mySocket.close();
// }catch(IOException e){ }
}
public void send(String s){
try {
outgoing.put(s);
} catch (InterruptedException e) {
System.err.println(e);
}
}
//TODO verifie loop
public synchronized LinkedBlockingDeque<PeerRecord> getPeerTable(){
for(PeerRecord pr : this.peerTable) {
if (pr.getExpirationTime().compareTo(Instant.now())<0) {
peerTable.remove(pr);
if(Test.DEBUG)
System.out.println("In_getPeerTable()_Deleting " + pr.getPeerID() + " peer record (expired).");
}
}
return this.peerTable;
}
public String getMyID(){
return this.myID;
}
public synchronized PeerRecord getbyID(String ID){
for(PeerRecord pr : this.getPeerTable()) {
if (pr.getPeerID().equals(ID)) return pr;
}
return null;
}
public String toStringPeerTable(){
String res="\n**********************************************************************\n";
res+="peerID;peerIPAddress;peerSeqNum;expirationTime;peerState\n";
res+="**********************************************************************\n";
for(PeerRecord pr : this.peerTable) {
res+=pr.toString();
}
res+="**********************************************************************\n\n";
return res;
}
public ConcurrentHashMap<String, Database> getOthersDatabases() {
return othersDatabases;
}
public String toStringOthersDatabases(){
String s="";
if(othersDatabases.size()>=1){
s= "\n\n***********************************************************************\n";
for(Map.Entry<String,Database> db : othersDatabases.entrySet()){
s+="<"+db.getKey()+";"+db.getValue().getDatabaseSequenceNumber()+">\n";
s+= db.getValue().toStringofStringQueue();
}
s+= "***********************************************************************\n\n";
}
return s;
}
}