-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutingTable.java
More file actions
319 lines (270 loc) · 12.6 KB
/
RoutingTable.java
File metadata and controls
319 lines (270 loc) · 12.6 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
//Networks 4119 Ruchir Khaitan class to represent a routing table and perform all neccessary functions on it
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Collections;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Set;
import java.net.InetAddress;
import java.io.IOException;
import java.net.UnknownHostException;
import java.io.*;
import java.net.*;
public class RoutingTable {
private List<Neighbor> distanceVector = Collections.synchronizedList(new ArrayList<Neighbor>());
private Set<String> knownVertices = Collections.synchronizedSet(new HashSet<String>());
private int MAXSIZE = 512;
private byte[] vectorData = new byte[MAXSIZE];
private ByteBuffer currentVector = ByteBuffer.wrap(vectorData);
private int currentActive = 0;
private InetAddress myAddr;
private int myPort;
private long timeout;
public RoutingTable(int port, long time) throws UnknownHostException {
this.myAddr = InetAddress.getLocalHost();
this.myPort = port;
//System.out.println("The address is" + myAddr);
//System.out.format("The port is %d%n ", myPort);
this.timeout = time;
}
public void addNeighbor(String ipAddr, String portNum, String destCost) throws UnknownHostException, SocketException {
//used to read from commandLine
Neighbor myNewNeighbor = new Neighbor(ipAddr,portNum, destCost);
distanceVector.add(myNewNeighbor);
//knownVertices.add(ipAddr+portNum);
currentActive++;
}
public void addNewNeighbor(InetAddress ipAddr, int portNum, double destCost) throws UnknownHostException, SocketException {
//used to read from commandLine
Neighbor myNewNeighbor = new Neighbor(ipAddr,portNum, destCost);
distanceVector.add(myNewNeighbor);
//knownVertices.add(ipAddr+portNum);
currentActive++;
}
public void addNewLink(InetAddress destAddress, InetAddress linkAddr, int portNum, double linkCost){
//used to add a new connection when seen from a route update message
Neighbor myNewLink = new Neighbor(destAddress,linkAddr, portNum,linkCost);
distanceVector.add(myNewLink);
currentActive++;
}
public void printRoutingTable() {
//underlying logic of show-route
long currTime = System.currentTimeMillis();
System.out.format("<%d>Distance vector is: ", currTime);
for (Neighbor n : distanceVector) {
if (!n.isNeighbor() || n.isActive()){
n.printNeighbor();
}
}
}
public void generateDistanceVector(int updateType) throws SocketException {
//generates byte representation of current distance vector data to be sent to neighbors
currentVector.clear();
currentVector.put((byte) updateType);
currentVector.putInt(myPort);
//generates byte array to be sent to neighbors
currentVector.putInt(currentActive);
for (Neighbor n : distanceVector) {
if (!n.isNeighbor() || n.isActive()){
currentVector.put(n.getDestByteArray());
currentVector.putInt(n.getDestPort());
currentVector.putDouble(n.getLinkCost());
}
}
}
public void sendToAllNeighbors() throws IOException{
//underlying logic of route update and linkup
for (Neighbor n : distanceVector) {
if (n.isNeighbor() && n.isActive()){
n.sendToNeighbor(vectorData);
}
}
}
public void sendRouteUpdate() throws IOException {
this.generateDistanceVector(1);
this.sendToAllNeighbors();
}
public void initialLinkUp() throws IOException, SocketException {
this.generateDistanceVector(3);
this.sendToAllNeighbors();
}
public void handleReceivedRouteUpdate(byte[] recvData, InetAddress senderAddress) throws UnknownHostException {
//assumes that a distance vector and parses it updating its own data using Bellman Ford
ByteBuffer recvBuffer = ByteBuffer.wrap(recvData);
Byte check = recvBuffer.get();
if (check != ((byte) 1) && check != ((byte) 3)){
return;
}
int senderPort = recvBuffer.getInt();
//System.out.format("Received node from %d%n", senderPort);
//first determine who sent it and their link cost to you
double senderLinkCost = 0;
for (Neighbor n : distanceVector) {
if (n.equalsNeighbor(senderAddress, senderPort)) {
//update n's last heard from value
n.setActive(true);
n.setTime();
senderLinkCost = n.getLinkCost();
}
}
if (senderLinkCost == 0 ){
//we couldn't find matching sender
//error case
return;
}
byte[] linkAddr = new byte[4];
InetAddress linkAddress;
int linkPort = 0;
boolean foundLink = false;
double linkCost;
int numLinks = recvBuffer.getInt();
for(int i = 0; i< numLinks; i++) {
foundLink = false;
recvBuffer.get(linkAddr, 0, 4);
linkAddress = InetAddress.getByAddress(linkAddr);
linkPort = recvBuffer.getInt();
linkCost = recvBuffer.getDouble();
//use hashset to check contains first instead
for (Neighbor n : distanceVector) {
if (n.equalsNeighbor(linkAddress, linkPort)) {
foundLink = true;
if(/*n.isActive() && */ n.getLinkCost() > (senderLinkCost + linkCost)){
n.setLinkCost(senderLinkCost+linkCost);
}
}
}
if(!foundLink && !isMyAddress(linkAddress, linkPort)) {
//encountered a new node
// System.out.println("Making a new node");
this.addNewLink(linkAddress, senderAddress, linkPort, (senderLinkCost + linkCost));
}
}
}
public int modifyLink(InetAddress destAddr, int destPort, boolean value) {
//logical link down, applies to user inputted link downs, received link down messages, and timeouts
for (Neighbor n : distanceVector) {
if (n.isNeighbor() && n.equalsNeighbor(destAddr, destPort) ) {
n.setActive(value);
}
}
if(value){
currentActive++;
return currentActive;
}
currentActive--;
return currentActive;
}
public void userLinkCommand(String destAddress, String destPortNum, boolean value) throws IOException, UnknownHostException {
//handle user input case of linkdown
InetAddress destAddr = InetAddress.getByName(destAddress);
int destPort = Integer.parseInt(destPortNum);
int currActive = this.currentActive;
int result = this.modifyLink(destAddr, destPort, value);
if(currActive != (result + 1) && !value) {
//System.out.format("Sorry could not remove %s:%s not a direct neighbor", destAddress, destPortNum);
return;
}
if(currActive != (result - 1) && value) {
//System.out.format("Sorry could not add %s:%s not previously a direct neighbor", destAddress, destPortNum);
return;
}
//generate link down/up message, send it to neighbor
currentVector.clear();
if(!value){
currentVector.put((byte) 2);
}
else {
currentVector.put((byte) 4);
}
currentVector.putInt(myPort);
currentVector.putInt(myPort);
currentVector.putInt(-1);
for (Neighbor n : distanceVector) {
if (n.isNeighbor() && n.equalsNeighbor(destAddr, destPort) ) {
n.sendToNeighbor(vectorData);
return;
}
}
}
public void handleReceivedLinkMessage(byte[] recvData, InetAddress senderAddress) throws UnknownHostException{
//check that the received array is a valid linkup/linkdown
ByteBuffer recvBuffer = ByteBuffer.wrap(recvData);
boolean value = false;
if (recvBuffer.get() == (byte) 4){
value = true;
}
int senderPort = recvBuffer.getInt();
if(senderPort != recvBuffer.getInt()){
return;
}
if (recvBuffer.getInt() == -1) {
modifyLink(senderAddress, senderPort, value);
}
}
public void handleReceivedInitLinkUp(byte[] recvData, InetAddress senderAddress) throws UnknownHostException, SocketException {
ByteBuffer recvBuffer = ByteBuffer.wrap(recvData);
// System.out.println("In handle init linkup");
if (recvBuffer.get() != (byte) 3){
return;
}
int senderPort = recvBuffer.getInt();
//System.out.format("Init linkup from port %d%n", senderPort);
for (Neighbor n : distanceVector) {
if (n.equalsNeighbor(senderAddress, senderPort)) {
//you already have this node stored
handleReceivedRouteUpdate(recvData, senderAddress);
return;
}
}
//first determine who sent it and their link cost to you
double senderLinkCost = 0;
byte[] linkAddr = new byte[4];
InetAddress linkAddress;
int linkPort = 0;
//boolean foundLink = false;
int numLinks = recvBuffer.getInt();
for(int i = 0; i< numLinks; i++) {
recvBuffer.get(linkAddr, 0, 4);
linkAddress = InetAddress.getByAddress(linkAddr);
linkPort = recvBuffer.getInt();
senderLinkCost = recvBuffer.getDouble();
if(isMyAddress(linkAddress, linkPort)) {
this.addNewNeighbor(senderAddress, senderPort, senderLinkCost);
recvBuffer.rewind();
handleReceivedRouteUpdate(recvData, senderAddress);
return;
}
}
}
public void setMyAddress(InetAddress address){
this.myAddr = address;
}
public boolean isMyAddress(InetAddress address, int port){
if (this.myAddr.equals(address) && this.myPort == port){
return true;
}
return false;
}
public void handleReceivedInputs(byte[] data, InetAddress sender) throws UnknownHostException, SocketException {
Byte indexByte = data[0];
//System.out.format("In received handler start byte is %d%n", indexByte);
if(indexByte == (byte) 1) {
this.handleReceivedRouteUpdate(data, sender);
return;
}
if(indexByte == (byte) 3) {
this.handleReceivedInitLinkUp(data, sender);
return;
}
if(indexByte == (byte) 2 || indexByte == (byte) 4){
handleReceivedLinkMessage(data, sender);
}
}
public void scanForNeighborTimeout(long time){
for (Neighbor n : distanceVector) {
if (n.isNeighbor() && n.isActive()){
n.handleTimeout(timeout, time);
}
}
}
}