-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
350 lines (300 loc) · 10.4 KB
/
Node.java
File metadata and controls
350 lines (300 loc) · 10.4 KB
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
import java.util.ArrayList;
import java.util.Iterator;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.net.ProtocolException;
/**
* <pre>
* Node -- Class defining the data structures and code for the
* protocol stack for a node participating in the fishnet
* This code is under student control; we provide a baseline
* "hello world" example just to show how it interfaces with the FishNet classes.
*
* NOTE: per-node global variables are prohibited.
* In simulation mode, we execute many nodes in parallel, and thus
* they would share any globals. Please put per-node state in class Node
* instead.
*
* In other words, all the code defined here should be generic -- it shouldn't
* matter to this code whether it is executing in a simulation or emulation
*
* This code must be written as a state machine -- each upcall must do its work and return so that
* other upcalls can be delivered
* </pre>
*/
public class Node {
private final long PingTimeout = 10000; // Timeout pings in 10 seconds
private Manager manager;
private int addr;
private ArrayList pings; // To store PingRequests.
// Fishnet reliable data transfer
// TCP manager
private TCPManager tcpMan;
/**
* Create a new node
*
* @param manager The manager that is managing Fishnet
* @param addr The address of this node
*/
public Node(Manager manager, int addr) {
this.manager = manager;
this.addr = addr;
this.pings = new ArrayList();
// Fishnet reliable data transfer
this.tcpMan = new TCPManager(this, addr, manager);
}
/**
* Called by the manager to start this node up.
*/
public void start() {
logOutput("started");
this.addTimer(PingTimeout, "pingTimedOut");
// Fishnet reliable data transfer
// Start TCP manager
// another thread...??????????????????????????????????????????????????????????????????????????????
tcpMan.start();
}
/**
* Called by the manager when a packet has arrived for this node
*
* @param from The address of the node that has sent this packet
* @param msg The serialized form of the packet.
*/
public void onReceive(Integer from, byte[] msg) {
Packet packet = Packet.unpack(msg);
// logOutput("received packet from " + from);
if (packet == null) {
logError("Unable to unpack message: " + Utility.byteArrayToString(msg) + " Received from " + from);
return;
}
this.receivePacket(from.intValue(), packet);
}
/**
* Called by the manager when there is a command for this node from the user.
* Command can be input either from keyboard or file. For now sending messages
* as ping to a neighbor.
*
* @param command The command for this node
*/
public void onCommand(String command) {
if (this.matchTransferCommand(command)) {
return;
}
if (this.matchServerCommand(command)) {
return;
}
if (this.matchPingCommand(command)) {
return;
}
logError("Unrecognized command: " + command);
}
/**
* Callback method given to manager to invoke when a timer fires.
*/
public void pingTimedOut() {
Iterator iter = this.pings.iterator();
while (iter.hasNext()) {
PingRequest pingRequest = (PingRequest) iter.next();
if ((pingRequest.getTimeSent() + PingTimeout) < this.manager.now()) {
try {
logOutput("Timing out ping: " + pingRequest);
iter.remove();
} catch (Exception e) {
logError("Exception occured while trying to remove an element from ArrayList pings. Exception: "
+ e);
return;
}
}
}
this.addTimer(PingTimeout, "pingTimedOut");
}
private boolean matchPingCommand(String command) {
int index = command.indexOf(" ");
if (index == -1) {
return false;
}
try {
int destAddr = Integer.parseInt(command.substring(0, index));
String message = command.substring(index + 1);
Packet packet = new Packet(destAddr, this.addr, Packet.MAX_TTL, Protocol.PING_PKT, 0,
Utility.stringToByteArray(message));
this.send(destAddr, packet);
this.pings.add(new PingRequest(destAddr, Utility.stringToByteArray(message), this.manager.now()));
return true;
} catch (Exception e) {
logError("Exception: " + e);
}
return false;
}
private void receivePacket(int from, Packet packet) {
switch (packet.getProtocol()) {
case Protocol.PING_PKT:
this.receivePing(packet);
break;
case Protocol.PING_REPLY_PKT:
this.receivePingReply(packet);
break;
case Protocol.TRANSPORT_PKT:
this.receiveTCP(packet);
break;
default:
logError("Packet with unknown protocol received. Protocol: " + packet.getProtocol());
}
}
// private void receiveTCP(Packet packet)
private void receiveTCP(Packet packet) {
//logOutput("Received TCP packet from " + packet.getSrc()
// );
tcpMan.dispatch(packet);
return;
}
private void receivePing(Packet packet) {
logOutput("Received Ping from " + packet.getSrc() + " with message: "
+ Utility.byteArrayToString(packet.getPayload()));
try {
Packet reply = new Packet(packet.getSrc(), this.addr, Packet.MAX_TTL, Protocol.PING_REPLY_PKT, 0,
packet.getPayload());
this.send(packet.getSrc(), reply);
} catch (IllegalArgumentException e) {
logError("Exception while trying to send a Ping Reply. Exception: " + e);
}
}
// Check that ping reply matches what was sent
private void receivePingReply(Packet packet) {
Iterator iter = this.pings.iterator();
String payload = Utility.byteArrayToString(packet.getPayload());
while (iter.hasNext()) {
PingRequest pingRequest = (PingRequest) iter.next();
if ((pingRequest.getDestAddr() == packet.getSrc())
&& (Utility.byteArrayToString(pingRequest.getMsg()).equals(payload))) {
logOutput("Got Ping Reply from " + packet.getSrc() + ": " + payload);
try {
iter.remove();
} catch (Exception e) {
logError(
"Exception occured while trying to remove an element from ArrayList pings while processing Ping Reply. Exception: "
+ e);
}
return;
}
}
logError("Unexpected Ping Reply from " + packet.getSrc() + ": " + payload);
}
private void send(int destAddr, Packet packet) {
try {
this.manager.sendPkt(this.addr, destAddr, packet.pack());
} catch (IllegalArgumentException e) {
logError("Exception: " + e);
}
}
// Adds a timer, to fire in deltaT milliseconds, with a callback to a public
// function of this class that takes no parameters
private void addTimer(long deltaT, String methodName) {
try {
Method method = Callback.getMethod(methodName, this, null);
Callback cb = new Callback(method, this, null);
this.manager.addTimer(this.addr, deltaT, cb);
} catch (Exception e) {
logError("Failed to add timer callback. Method Name: " + methodName + "\nException: " + e);
}
}
// Fishnet reliable data transfer
/**
* Send a transport segment to the specified node (network layer service entry
* point for the transport layer)
*
* @param srcAddr int Source node address
* @param destAddr int Sestination node address
* @param protocol int Transport layer protocol to use
* @param payload byte[] Payload to be sent
*/
public void sendSegment(int srcAddr, int destAddr, int protocol, byte[] payload) {
Packet packet = new Packet(destAddr, srcAddr, Packet.MAX_TTL, protocol, 0, payload);
this.send(destAddr, packet);
//logOutput("Send TCP packet to " + destAddr);
}
public int getAddr() {
return this.addr;
}
public void logError(String output) {
this.log(output, System.err);
}
public void logOutput(String output) {
//this.log(output, System.out);
}
private void log(String output, PrintStream stream) {
stream.println("Node " + this.addr + ": " + output);
}
private boolean matchTransferCommand(String command) {
// transfer command syntax:
// transfer dest port localPort amount [interval sz]
// Synopsis:
// Connect to a transfer server listening on port <port> at node
// <dest>, using local port <localPort>, and transfer <amount> bytes.
// Required arguments:
// dest: address of destination node
// port: destination port
// localPort: local port
// amount: number of bytes to transfer
// Optional arguments:
// interval: execution interval of the transfer client, default 1 second
// sz: buffer size of the transfer client, default 65536
String[] args = command.split(" ");
if (args.length < 5 || args.length > 7 || !args[0].equals("transfer")) {
return false;
}
try {
int destAddr = Integer.parseInt(args[1]);
int port = Integer.parseInt(args[2]);
int localPort = Integer.parseInt(args[3]);
int amount = Integer.parseInt(args[4]);
long interval = args.length >= 6 ? Integer.parseInt(args[5]) : TransferClient.DEFAULT_CLIENT_INTERVAL;
int sz = args.length == 7 ? Integer.parseInt(args[6]) : TransferClient.DEFAULT_BUFFER_SZ;
TCPSock sock = this.tcpMan.socket();
sock.bind(localPort);
sock.connect(destAddr, port);
TransferClient client = new TransferClient(manager, this, sock, amount, interval, sz);
client.start();
return true;
} catch (Exception e) {
logError("Exception: " + e);
}
return false;
}
private boolean matchServerCommand(String command) {
// server command syntax:
// server port backlog [servint workint sz]
// Synopsis:
// Start a transfer server at the local node, listening on port
// <port>. The server has a maximum pending (incoming) connection
// queue of length <backlog>.
// Required arguments:
// port: listening port
// backlog: maximum length of pending connection queue
// Optional arguments:
// servint: execution interval of the transfer server, default 1 second
// workint: execution interval of the transfer worker, default 1 second
// sz: buffer size of the transfer worker, default 65536
String[] args = command.split(" ");
if (args.length < 3 || args.length > 6 || !args[0].equals("server")) {
return false;
}
try {
int port = Integer.parseInt(args[1]);
int backlog = Integer.parseInt(args[2]);
long servint = args.length >= 4 ? Integer.parseInt(args[3]) : TransferServer.DEFAULT_SERVER_INTERVAL;
long workint = args.length >= 5 ? Integer.parseInt(args[4]) : TransferServer.DEFAULT_WORKER_INTERVAL;
int sz = args.length == 6 ? Integer.parseInt(args[5]) : TransferServer.DEFAULT_BUFFER_SZ;
TCPSock sock = this.tcpMan.socket();
sock.bind(port);
sock.listen(backlog);
TransferServer server = new TransferServer(manager, this, sock, servint, workint, sz);
server.start();
logOutput("server started, port = " + port);
return true;
} catch (Exception e) {
logError("Exception: " + e);
}
return false;
}
}