-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.java
420 lines (369 loc) · 13.3 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import javax.script.*;
import java.util.function.*;
import java.util.*;
import java.util.concurrent.*;
class Node {
public static final int GC_WAIT_MILLIS = 10000;
public static final long TIMER_WAIT_MAX_MILLIS = 100;
private String script = "";
private ScriptEngineManager manager = new ScriptEngineManager();
private ScriptEngine engine = manager.getEngineByName("nashorn");
protected Invocable inv;
private TCP tcp;
private UDP udp;
private NodeUtility nodeutility;
private Consumer<ScriptException> errorCallback;
private boolean working = false;
private String ip;
private Network net;
private Computer computer;
private EventWriter writer;
private ArrayList<Future<Void>> futures = new ArrayList<>();
private java.util.Timer gcTimer = new java.util.Timer();
public interface ReceiveFunction {
void accept(String str0, Integer int0, String str1);
}
public class TCP {
private ArrayList<TCPConnection> servers = new ArrayList<>();
private ArrayList<TCPConnection> clients = new ArrayList<>();
public TCPConnection createServer(int port) {
System.out.println("Creating server at " + port);
//If server already exists at that port
TCPConnection serv = getServerAtPort(port);
if(serv != null) {
return null;
}
TCPConnection con = new TCPConnection(port);
servers.add(con);
return con;
}
public TCPConnection connectToServer(String ip, int port) {
System.out.println("Connecting to server at: " + ip + ":" + port);
TCPConnection con = new TCPConnection(ip, port);
clients.add(con);
return con;
}
public TCPConnection getServerAtPort(int port) {
for(TCPConnection server : servers) {
if(server.getPort() == port) {
return server;
}
}
return null;
}
public TCPConnection getClientAtIPAndPort(String ip, int port) {
for(TCPConnection client : clients) {
if(client.getPort() == port && client.getRemoteIP().equals(ip)) {
return client;
}
}
return null;
}
//Acts as both a TCP client and TCP server
public class TCPConnection {
private int port;
private Consumer<String> onConnection;
private Consumer<String> onData;
private Consumer<Boolean> onDisconnect;
private boolean hasConnection = false;
private TCPConnectionData otherEnd;
private boolean isServer;
private ArrayList<TCPConnectionData> clients;
public class TCPConnectionData {
public String remoteIP;
public int port;
public TCPConnection object;
}
//Create server
public TCPConnection(int port) {
this.port = port;
isServer = true;
clients = new ArrayList<TCPConnectionData>();
}
public ArrayList<TCPConnectionData> getClients() {
if(isServer) {
return clients;
}
return null;
}
//Create client
public TCPConnection(String remoteIP, int port) {
this.otherEnd.remoteIP = remoteIP;
this.port = port;
isServer = false;
otherEnd = new TCPConnectionData();
}
public boolean isServer() {
return isServer;
}
public TCPConnection onConnection(Consumer<String> jso) {
onConnection = jso;
return this;
}
public TCPConnection onData(Consumer<String> jso) {
onData = jso;
return this;
}
public TCPConnection onDisconnect(Consumer<Boolean> jso) {
onDisconnect = jso;
return this;
}
public boolean hasConnection() {
return hasConnection;
}
public int getPort() {
return port;
}
public String getRemoteIP() {
return otherEnd.remoteIP;
}
public void disconnectByRemote() {
disconnectHelper(true);
}
public void disconnect() {
disconnectHelper(false);
otherEnd.object.disconnectByRemote();
}
private void disconnectHelper(boolean byRemote) {
otherEnd.remoteIP = null;
hasConnection = false;
if(onDisconnect != null) {
futures.add(Node.this.getNet().getExecutor().submit(() -> {
onDisconnect.accept(byRemote);
return null;
}));
}
}
//For clients
public void setOtherEnd(TCPConnection otherEnd, String remoteIP, int port) {
if(isServer) {
return;
}
this.otherEnd.port = port;
this.otherEnd.remoteIP = remoteIP;
this.otherEnd.object = otherEnd;
}
//For clients
public boolean connect(String remoteIP, int port) {
if(isServer) {
return false;
}
hasConnection = true;
//System.out.println("Connecting to: " + remoteIP + ":" + port);
if(onConnection != null) {
futures.add(Node.this.getNet().getExecutor().submit(() -> {
onConnection.accept(remoteIP);
return null;
}));
}
return true;
}
//For servers
public TCPConnection connect(String remoteIP, int port, TCPConnection otherEnd) {
if(port != port || hasConnection || !isServer) {
return null;
}
otherEnd.setOtherEnd(this, Node.this.getIP(), port);
TCPConnectionData data = new TCPConnectionData();
data.remoteIP = remoteIP;
data.port = port;
data.object = otherEnd;
clients.add(data);
hasConnection = true;
if(onConnection != null) {
futures.add(Node.this.getNet().getExecutor().submit(() -> {
onConnection.accept(remoteIP);
return null;
}));
}
return this;
}
public void receiveData(String data) {
if(onData != null) {
futures.add(Node.this.getNet().getExecutor().submit(() -> {
onData.accept(data);
return null;
}));
}
}
}
}
public class UDP {
private ReceiveFunction rf;
public void send(String ip, int port, String data) {
net.sendUDPData(ip, port, data, Node.this.getIP());
}
public void receive(String ip, int port, String data) {
//System.out.println("Receiving from: " + ip + ":" + port + " data: " + data);
if(rf != null) {
futures.add(Node.this.getNet().getExecutor().submit(() -> {
rf.accept(ip, port, data);
return null;
}));
}
}
public void onReceive(ReceiveFunction rf) {
this.rf = rf;
}
}
public class NodeUtility {
private ArrayList<java.util.Timer> timers = new ArrayList<>();
public String getIP() {
return ip;
}
public String getBroadcastAddr() {
return net.getBroadcastAddr();
}
public boolean pingIP(String ip) {
return Node.this.getNet().ping(ip);
}
public java.util.Timer delay(Runnable runnable, long milliseconds) {
java.util.Timer timer = new java.util.Timer();
timer.schedule(new TimerTask() {
public void run() {
runnable.run();
}
}, milliseconds);
timers.add(timer);
return timer;
}
public java.util.Timer interval(Runnable runnable, long milliseconds, long period) {
java.util.Timer timer = new java.util.Timer();
timer.schedule(new TimerTask() {
public void run() {
runnable.run();
}
}, milliseconds, period);
timers.add(timer);
return timer;
}
public ArrayList<java.util.Timer> getTimers() {
return timers;
}
public void removeTimers() {
for(java.util.Timer timer : timers) {
timer.cancel();
timer.purge();
}
timers = new ArrayList<>();
}
}
public void dispose() {
if(nodeutility != null) {
nodeutility.removeTimers();
}
boolean hitMaxWait = false;
//Destroy futures
for(int i = futures.size() - 1; i >= 0; i--) {
if(futures.get(i).isDone()) {
futures.remove(i);
} else {
//If we've already waited long enough
if(hitMaxWait) {
//Cancel and interrupt
futures.get(i).cancel(true);
} else {
//Cancel, but don't interrupt if running
futures.get(i).cancel(false);
if(!futures.get(i).isDone()) {
//If still not done, wait max of TIMER_WAIT_MAX_MILLIS
try {
futures.get(i).get(TIMER_WAIT_MAX_MILLIS, TimeUnit.MILLISECONDS);
} catch(InterruptedException | ExecutionException |
TimeoutException e) {
//do nothing
} finally {
//If still not done, cancel and interrupt
if(!futures.get(i).isDone()) {
hitMaxWait = true;
futures.get(i).cancel(true);
}
}
}
}
}
}
}
private boolean gcInit = false;
private void initGC() {
if(!gcInit) {
gcTimer.schedule(new TimerTask() {
public void run() {
Node.this.performGC();
}
}, GC_WAIT_MILLIS * 2, GC_WAIT_MILLIS);
gcInit = true;
}
}
private void performGC() {
//Garbage collection of futures
for(int i = futures.size() - 1; i >= 0; i--) {
if(futures.get(i).isDone()) {
futures.remove(i);
}
}
}
public Node(String ip, Network net, Computer computer) {
this.ip = ip;
this.net = net;
this.computer = computer;
initGC();
}
public void initializeWriterWithPrintCallback(BiConsumer<String, String> messageCallback) {
writer = new EventWriter(this, messageCallback);
engine.getContext().setWriter(writer);
}
public void setErrorCallback(Consumer<ScriptException> errorCallback) {
this.errorCallback = errorCallback;
}
public String getScript() {
return script;
}
public void setIP(String ip) {
this.ip = ip;
}
public String getIP() {
return ip;
}
public UDP getUDP() {
return udp;
}
public TCP getTCP() {
return tcp;
}
public synchronized Network getNet() {
return net;
}
public void setScript(String script) {
this.script = script;
initCommunications();
evalScript();
}
private void initCommunications() {
tcp = new TCP();
udp = new UDP();
if(nodeutility != null) {
nodeutility.removeTimers();
}
nodeutility = new NodeUtility();
}
private void evalScript() {
working = false;
engine.put("TCP", tcp);
engine.put("UDP", udp);
engine.put("Utility", nodeutility);
futures.add(getNet().getExecutor().submit(() -> {
try {
engine.eval(script);
} catch(ScriptException se) {
errorCallback.accept(se);
}
return null;
}));
inv = (Invocable)engine;
working = true;
}
public boolean isWorking() {
return working;
}
}