-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.java
238 lines (209 loc) · 8.08 KB
/
Node.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
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;
import java.util.regex.*;
public class Node {
// Node
int id;
String name;
String port;
List<List<Integer>> neighbours = new ArrayList<>();
// Config
int totalNodes;
int minPerActive;
int maxPerActive;
int minSendDelay;
int snapshotDelay;
int maxNumber;
// Variable
int msgSent = 0;
int msgReceived = 0;
int custom_end = 0;
boolean state = false;
boolean pem_passive = false;
Vector<Integer> clock = new Vector<>();
Vector<Integer> sndClk = new Vector<>();
Vector<Integer> rcvClk = new Vector<>();
// Components
Server server;
Client client;
ChandyLamport snapshot;
// Helper
Map<String, List<Integer>> hostToId_PortMap = new HashMap<>();
Map<Integer, List<String>> idToHost_PortMap = new HashMap<>();
Map<Integer, Socket> idToChannelMap = new HashMap<>();
public Node(int id) {
this.id = id;
}
public static void main(String[] args) {
// Init Node
Node node;
// if (args.length > 0)
// node = new Node(Integer.parseInt(args[0]));
// else
node = new Node(-1);
// Parse the config file
node.readConfig();
// Init Vector Clock;
node.initVectorClock();
// Print details
node.printNodeConfig();
node.printNodeNeighbours();
// Chandy Lamport Protocol
node.snapshot = new ChandyLamport(node);
// Server
node.server = new Server(node.getPort(), node);
node.server.init();
try {
System.out.println("[SERVER] Loading... Wait 10s");
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Client
node.client = new Client(node);
try {
System.out.println("[CLIENT] Loading... Wait 10s");
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
node.client.init();
// Starting Chandy Lamport
try {
if (node.id == 0) {
node.snapshot.initSpanningTree();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void readConfig() {
// Declring Variables
String CONFIG_FILE_NAME = "aos-project1/config.txt";
String line;
int configLine = 0;
String localHost = "";
// Get Host Name
try {
localHost = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
System.out.println(e);
}
// REGEX Pattern
Pattern REGEX_PATTERN_CONFIG = Pattern.compile("^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
try {
// Creating a reader for config file
BufferedReader reader = new BufferedReader(new FileReader(CONFIG_FILE_NAME));
// Looping over the whole file, reading line by line
while ((line = reader.readLine()) != null) {
// Ignoring comments and empty line
line = line.split("#")[0].trim();
if (line.isEmpty())
continue;
// Match for config file.
Matcher configMatcher = REGEX_PATTERN_CONFIG.matcher(line);
if (configMatcher.matches()) {
this.totalNodes = Integer.parseInt(configMatcher.group(1));
this.minPerActive = Integer.parseInt(configMatcher.group(2));
this.maxPerActive = Integer.parseInt(configMatcher.group(3));
this.minSendDelay = Integer.parseInt(configMatcher.group(4));
this.snapshotDelay = Integer.parseInt(configMatcher.group(5));
this.maxNumber = Integer.parseInt(configMatcher.group(6));
} else if (configLine <= totalNodes) {
// All this lines are in format [ XXXX XXXX XXXX ]
String[] nodeConf = line.split(" ");
// Extracting data for read node.
int node_Id = Integer.parseInt(nodeConf[0]);
// String node_Host = nodeConf[1];
String node_Host = nodeConf[1] + ".utdallas.edu";
int node_Port = Integer.parseInt(nodeConf[2]);
if (this.id == -1 && node_Host.equals(localHost)) {
this.id = node_Id;
}
List<String> valueA = new ArrayList<>();
valueA.add(node_Host);
valueA.add(String.valueOf(node_Port));
List<Integer> valueB = new ArrayList<>();
valueB.add(node_Id);
valueB.add(node_Port);
this.idToHost_PortMap.put(node_Id, valueA);
this.hostToId_PortMap.put(node_Host, valueB);
} else {
// We know top (n + 1) lines are for config. Thenafter nth line contain nth node
String[] node_neighbours = line.split(" ");
List<Integer> valueA = new ArrayList<>();
for (String n : node_neighbours) {
valueA.add(Integer.parseInt(n));
}
this.neighbours.add(valueA);
}
configLine += 1;
}
// Closing reader
reader.close();
} catch (
IOException e) {
e.printStackTrace();
}
}
public String getHost() {
return idToHost_PortMap.get(id).get(0);
}
public String getHost(int id) {
return idToHost_PortMap.get(id).get(0);
}
public int getPort() {
return Integer.parseInt(idToHost_PortMap.get(id).get(1));
}
public int getPort(int id) {
return Integer.parseInt(idToHost_PortMap.get(id).get(1));
}
public void initVectorClock() {
for (int i = 0; i < totalNodes; i++) {
this.clock.add(0);
this.sndClk.add(0);
this.rcvClk.add(0);
}
}
public void changeState() {
this.state = !state;
}
/* ========== HELPER FUNCTIONS ========== */
public void printNodeConfig() {
System.out.println("========== Node Config ==========");
System.out.println("Node Id: " + id);
System.out.println("Node Host: " + getHost());
System.out.println("Node Port: " + getPort());
System.out.println("Total Nodes: " + totalNodes);
System.out.println("Min Per Active: " + minPerActive);
System.out.println("Max Per Active: " + maxPerActive);
System.out.println("Min Send Delay: " + minSendDelay);
System.out.println("Snapshot Delay: " + snapshotDelay);
System.out.println("Max Number: " + maxNumber);
System.out.println("=================================\n");
}
public void printNodeNeighbours() {
System.out.println("======== Node Neighbours ========");
for (Integer neighbour : neighbours.get(id)) {
System.out.println("Id: " + neighbour + " | Host: " + idToHost_PortMap.get(neighbour).get(0) + " | Port: "
+ idToHost_PortMap.get(neighbour).get(1));
}
System.out.println("=================================\n");
}
public void printNodeVectorClock() {
int totalSent = 0, totalReceive = 0;
System.out.println("======= Node Vector Clock =======");
for (int i = 0; i < totalNodes; i++) {
System.out.println("NodeId: " + i + " | Msg: " + clock.get(i) + " | Send: " + sndClk.get(i) + " | Recieve: "
+ rcvClk.get(i));
totalSent += sndClk.get(i);
totalReceive += rcvClk.get(i);
}
System.out.println("Total Send: " + totalSent + " | Total Recieve: " + totalReceive + " | Diff: "
+ (totalSent - totalReceive));
System.out.println("=================================\n");
}
}