-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.java
290 lines (238 loc) · 9.53 KB
/
Router.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package project3ccn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.io.*;
import java.net.*;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.misc.IOUtils;
/**
*
* @author shirupa chowdhury
* @author lizzy thomas
*/
public class Router {
static String fileName = "";
static FileInputStream file = null;
static InputStreamReader input = null;
static BufferedReader fileRead = null;
static String routerName;
static ArrayList<String> neighbors = new ArrayList<String>();
static DistanceVector dv = new DistanceVector();
static ArrayList<HashMap<String, String>> buffer = new ArrayList<HashMap<String, String>>();
static DatagramSocket Sendsock;
static DatagramSocket Rcvsock;
static int port;
static InetAddress ip;
static HashMap<String, Integer> portTable = new HashMap<>();
static int count = 0;
static Scheduler scheduler;
static boolean vectorMade = false;
static byte[] dataOut;
public static void read_file(String filename, boolean resetDV) throws IOException {
//update DistanceVector
neighbors.clear();
try {
file = new FileInputStream(filename);
input = new InputStreamReader(file, "UTF-8");
fileRead = new BufferedReader(input);
} catch (Exception e) {
}
try {
int numNeighbors = Integer.parseInt(fileRead.readLine());
} catch (IOException ex) {
Logger.getLogger(Router.class.getName()).log(Level.SEVERE, null, ex);
}
HashMap<String, String> tempMap = new HashMap<String, String>();
String line;
while ((line = fileRead.readLine()) != null) {
String[] inputs = line.split(" ");
String name = inputs[0].toUpperCase();
String value = inputs[1];
neighbors.add(name);
tempMap.put(name, value + "," + routerName);
}
ArrayList<String> abcd = new ArrayList<>();
ArrayList<String> oldValues = new ArrayList<>();
if (resetDV) {
dv.vector = (HashMap<String, String>) tempMap.clone();
vectorMade = true;
} else{
Set keysone = dv.vector.keySet();
for (Iterator i = keysone.iterator(); i.hasNext();) {
String key = (String) i.next();
String value = dv.vector.get(key);
if (tempMap.get(key) != value && tempMap.get(key)!=null && DistanceVector.getThrough(value).equals(routerName))
{
abcd.add(key);
String oldValue = value;
oldValues.add(oldValue);
dv.vector.put(key, tempMap.get(key));
}
}
for (int i = 0; i < abcd.size(); i++)
{
Set keystwo = dv.vector.keySet();
for (Iterator j = keystwo.iterator(); j.hasNext();) {
String key2 = (String) j.next();
String value2 = dv.vector.get(key2);
System.out.println(value2);
System.out.println(abcd.get(i));
System.out.println(oldValues.get(i));
//System.out.println(value);
if (DistanceVector.getThrough(value2).equals(abcd.get(i)))
//&& Math.abs((DistanceVector.getDistance(oldValue) - DistanceVector.getDistance(value))) > 0.1)
{
double newD = DistanceVector.getDistance(value2)
- DistanceVector.getDistance(oldValues.get(i)) + DistanceVector.getDistance(dv.vector.get(abcd.get(i)));
//test
//System.out.println("key2: " + key2 + " value2: " + DistanceVector.getDistance(value2) + " oldValue: "
// + DistanceVector.getDistance(oldValue) + " value: " + DistanceVector.getDistance(value));
dv.vector.put(key2, DistanceVector.createValue(newD, abcd.get(i)));
}
}
}
}
tempMap.clear();
fileRead.close();
input.close();
file.close();
}
public static void send_update() {
count++;
try {
read_file(fileName, false);
} catch (IOException ex) {
Logger.getLogger(Router.class.getName()).log(Level.SEVERE, null, ex);
}
if (!vectorMade) {
try {
read_file(fileName, true);
} catch (IOException ex) {
Logger.getLogger(Router.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
for (int i = 0; i < neighbors.size(); i++) {
DistanceVector dvCopy = DistanceVector.copy(dv);
if (!neighbors.get(i).equals(routerName)) {
poisonReverse(neighbors.get(i), dvCopy);
dataOut = DistanceVector.convertToBytes(dvCopy);
int portno = portTable.get(neighbors.get(i)) + 1;
DatagramPacket pack = new DatagramPacket(dataOut, dataOut.length, ip, portno);
Sendsock.send(pack);
System.out.println(" Sending to port " + portno);
}
}
} catch (IOException e) {
System.out.print(e);
}
display();
scheduler = new Scheduler(15, routerName);
}
public static void receive_update() {
//receive a packet and create dv
byte[] dataIn = new byte[1024];
DatagramPacket pack = new DatagramPacket(dataIn, 1024);
try {
System.out.println("Waiting still!!");
Rcvsock.receive(pack);
} catch (IOException ex) {
Logger.getLogger(Router.class.getName()).log(Level.SEVERE, null, ex);
}
DistanceVector incomingDV = new DistanceVector();
try {
incomingDV = DistanceVector.convertFromBytes(dataIn);
} catch (IOException ex) {
Logger.getLogger(Router.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Router.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Received update from " + incomingDV.source);
dv = DVAlgorithm.doDV(dv, incomingDV);
}
public static void readPorts() throws FileNotFoundException, IOException {
File file = new File("Ports.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null) {
String[] str_array = st.split(" ");
portTable.put(str_array[0], Integer.parseInt(str_array[1]));
}
}
public static void display() {
System.out.println("Output Number: " + count);
Set keysone = dv.vector.keySet();
for (Iterator i = keysone.iterator(); i.hasNext();) {
String key = (String) i.next();
String value = dv.vector.get(key);
System.out.println("Shortest path " + dv.source + "-" + key
+ ": the next hop is " + DistanceVector.getThrough(value) + " and the cost is " + DistanceVector.getDistance(value));
}
}
public static void poisonReverse(String SendTo, DistanceVector d) {
Set keysone = d.vector.keySet();
for (Iterator i = keysone.iterator(); i.hasNext();) {
String key = (String) i.next();
String value = d.vector.get(key);
if (SendTo.equals(DistanceVector.getThrough(value))) {
d.vector.put(key, DistanceVector.createValue(Double.MAX_VALUE, value));
}
}
}
public static void main(String[] args) {
boolean inputSuccess = false;
try {
ip = InetAddress.getByName("localhost");
//port number = args[1]
//filename = args[0]
//get arguments
} catch (UnknownHostException ex) {
Logger.getLogger(Router.class.getName()).log(Level.SEVERE, null, ex);
}
while (!inputSuccess) {
try {
fileName = args[0]; //path of dat file for router
port = Integer.parseInt(args[1]); //router port
routerName = args[2];
inputSuccess = true;
} catch (Exception e) {
System.out.println("Invalid arguments. <FileName> <portNumber>");
}
}
//get router name and save in its distance vector
// String temp[] = fileName.split(".");
//routerName = temp[0].toUpperCase();
dv.setSource(routerName);
try {
Sendsock = new DatagramSocket(port);
Rcvsock = new DatagramSocket(port + 1);
} catch (SocketException ex) {
Logger.getLogger(Router.class.getName()).log(Level.SEVERE, null, ex);
}
try {
read_file(fileName, true);
} catch (IOException ex) {
Logger.getLogger(Router.class.getName()).log(Level.SEVERE, null, ex);
}
try {
readPorts();
} catch (IOException ex) {
Logger.getLogger(Router.class.getName()).log(Level.SEVERE, null, ex);
}
scheduler = new Scheduler(15, routerName);
while (true) {
//receive a packet and create dv
System.out.println("Waiting for an update!! ");
receive_update();
}
}
}