-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
131 lines (113 loc) · 4.44 KB
/
Server.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
import java.io.*;
import java.net.*;
public class Server {
int port;
Node node;
ServerSocket server;
public Server(int port, Node node) {
this.port = port;
this.node = node;
}
public void wakeNodeIfPassive(Message msg) {
if (!node.state && node.msgSent < node.maxNumber) {
node.changeState();
}
}
public void handleMessage(Message msg) {
if (msg.id != -1)
System.out.println("[SERVER] Message received from Node " + msg.id);
// Message Handler
if (msg.messageType == MessageType.APPLICATION) {
wakeNodeIfPassive(msg);
for (int i = 0; i < node.totalNodes; i++) {
int value = Math.max(node.clock.get(i), msg.clock.get(i));
node.clock.set(i, value);
}
node.rcvClk.set(msg.id, node.rcvClk.get(msg.id) + 1);
node.msgReceived += 1;
} else if (msg.messageType == MessageType.CUSTOM_END) {
node.custom_end++;
if (node.pem_passive && node.custom_end == node.neighbours.get(node.id).size())
node.printNodeVectorClock();
} else if (msg.messageType == MessageType.MARKER) {
System.out.println("[SERVER] Message type: MARKER");
try {
node.snapshot.handleMarkerMessageFromParent(msg);
} catch (Exception e) {
e.printStackTrace();
}
} else if (msg.messageType == MessageType.MARKER_REJECTION) {
System.out.println("[SERVER] Message type: MARKER_REJECTION");
try {
node.snapshot.handleMarkerRejectionMsg(msg);
} catch (Exception e) {
e.printStackTrace();
}
} else if (msg.messageType == MessageType.MARKER_REPLY) {
System.out.println("[SERVER] Message type: MARKER_REPLY");
try {
node.snapshot.handleMarkerRepliesFromChild(msg);
} catch (Exception e) {
e.printStackTrace();
}
} else if (msg.messageType == MessageType.END_SNAPSHOT) {
System.out.println("[SERVER] Message type: END_SNAPSHOT");
try {
node.snapshot.handleSnapshotResetMsg(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void listen() {
try {
this.server = new ServerSocket(port);
System.out.println("[SERVER] Started @ port: " + port);
while (true) {
Socket client = server.accept();
// Start a new thread to handle the client connection
Thread listener = new Thread(() -> {
try {
InputStream clientInputStream = client.getInputStream();
DataInputStream dataInputStream = new DataInputStream(clientInputStream);
while (!client.isClosed()) {
try {
// Reading Incoming Message.
int length = dataInputStream.readInt();
byte[] buffer = new byte[length];
dataInputStream.readFully(buffer);
Message msg = Message.fromByteArray(buffer);
synchronized (node) {
handleMessage(msg);
}
} catch (EOFException e) {
System.out.println("[SERVER] Connection closed by client");
break;
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
});
listener.start();
}
} catch (
IOException e) {
e.printStackTrace();
}
}
public void init() {
Thread server = new Thread(() -> {
System.out.println("[SERVER] Starting...");
try {
node.server.listen();
} catch (Exception e) {
e.printStackTrace();
}
});
server.start();
}
}