-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeData.java
More file actions
97 lines (80 loc) · 3.32 KB
/
NodeData.java
File metadata and controls
97 lines (80 loc) · 3.32 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
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.Timer;
public class NodeData extends Thread{
private final String nodeName;
private final String nodeType; // Host, Router, or Controller
private final int dstPort;
private final DatagramSocket thisSocket;
private final InetSocketAddress dstAddress;
private int nextSentPackNum; //keeping track of sender window (from perspective of Broker)
private int nextExpectedPackNum; //keeping track of receiver window (from perspective of Broker)
public String nodeID; //Node's name, e.g. H3 for a host, R5 for a router.
public Timer[] goBackNWindow = new Timer[16];
int goBackNWindowSize = 0;
int distanceToThisRouter;
// This version of the NodeData class is a slightly reduced version of the one used in Assignment 1.
// This class will be used by Controller and Routers, and is used solely for communication between
// nodes. I decided to reuse the Go-Back-N model of my last assignment, as it will greatly
// simplify setting up communication between nodes for this assignment.
NodeData(DatagramSocket thisSocket, int connectedNodePort, String nodeType, String nodeName, int distanceToThisRouter)
{
this.nodeName = nodeName;
this.nodeType = nodeType;
nextSentPackNum = 0;
nextExpectedPackNum = 0;
dstPort = connectedNodePort;
dstAddress = new InetSocketAddress("localhost", dstPort);
this.distanceToThisRouter = distanceToThisRouter;
this.thisSocket = thisSocket;
}
public String getNodeName() {
return nodeName;
}
public int getDSTPort() {
return dstPort;
}
public InetSocketAddress getDstAddress() {
return dstAddress;
}
public String getNodeType() {
return nodeType;
}
public int getNextSentPackNum() {
return nextSentPackNum;
}
public String getNextSentPackNumToString() {
return "" + ((nextSentPackNum < 10) ? "0" + nextSentPackNum : nextSentPackNum);
}
public int getNextExpectedPackNum() {
return nextExpectedPackNum;
}
public String getNextExpectedPackNumToString() {
return "" + ((nextExpectedPackNum < 10) ? "0" + nextExpectedPackNum : nextExpectedPackNum);
}
public void incrementNextExpectedPackNum()
{
nextExpectedPackNum = (nextExpectedPackNum + 1) % 16; // iterate nextSentPackNum
}
public void sendPacket(SNDContent PacketContentToSend) {
// always one element on the goBackNWindow that is null.
while (goBackNWindowSize >= 15)
{
try {
Thread.sleep(500);
} catch (InterruptedException e) { e.printStackTrace(); }
}
// Reset packet number so it matches up with node's next expected packet number,
// and then create Datagram packet
PacketContentToSend.resetPacketNumber(nextSentPackNum);
DatagramPacket packetToSend = PacketContentToSend.toDatagramPacket();
packetToSend.setSocketAddress(dstAddress); // sets this node's dstAddress as the destination for this packet
Timer packetTimer = new Timer(); // start new timer
TimerFlowControl ARQ = new TimerFlowControl(thisSocket, packetToSend);
packetTimer.schedule(ARQ, 100, 5000); // delay of 2 seconds, repeat every 2 seconds
goBackNWindow[nextSentPackNum] = packetTimer;
nextSentPackNum = (nextSentPackNum + 1) % 16; // iterate nextSentPackNum
goBackNWindowSize++;
}
}