-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouter_qian.java
277 lines (235 loc) · 8.57 KB
/
Router_qian.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
package socs.network.node;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Map.Entry;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import socs.network.message.HeartBeats;
import socs.network.message.LSA;
import socs.network.message.LinkDescription;
import socs.network.message.SOSPFPacket;
import socs.network.util.Configuration;
public class Router {
static RouterDescription rd = new RouterDescription();
protected LinkStateDatabase lsd;
static Link[] ports = new Link[4];
Socket[] sockets = new Socket[4];
ObjectOutputStream[] oosList = new ObjectOutputStream[4];
public Router(Configuration config) {
rd.simulatedIPAddress = config.getString("socs.network.router.ip");
rd.processPortNumber = config.getShort("socs.network.router.port");
lsd = new LinkStateDatabase(rd);
//heartbeats
TimerTask heartbeat = new HeartBeats(ports);
Timer timer = new Timer();
timer.schedule(heartbeat, 300);
}
/**
* output the shortest path to the given destination ip
* <p/>
* format: source ip address -> ip address -> ... -> destination ip
*
* @param destinationIP
* the ip adderss of the destination simulated router
*/
private void processDetect(String destinationIP) {
System.out.println(this.lsd.getShortestPath(destinationIP));
}
/**
* disconnect with the router identified by the given destination ip address
* Notice: this command should trigger the synchronization of database
*
* @param portNumber
* the port number which the link attaches at
*/
private void processDisconnect(short portNumber) {
for (int i = 0; i < 4; i++) {
if (ports[i] != null && ports[i].router2.getProcessPortNumber() == portNumber) {
ports[i] = null;
}
}
}
/**
* attach the link to the remote router, which is identified by the given
* simulated ip; to establish the connection via socket, you need to
* indentify the process IP and process Port; additionally, weight is the
* cost to transmitting data through the link
* <p/>
* NOTE: this command should not trigger link database synchronization
* @throws IOException
*/
private void processAttach(String processIP, short processPort, String simulatedIP, short weight) throws IOException {
String msg = "Connection cannot be established.";
for (int i = 0; i < 4; i++) {
if(ports[i] == null){
RouterDescription r2 = new RouterDescription(processIP, processPort, simulatedIP);
ports[i] = new Link(rd, r2,weight);
Socket client2 = new Socket(r2.getProcessIPAddress(), r2.getProcessPortNumber());
sockets[i] = client2;
oosList[i] = new ObjectOutputStream(client2.getOutputStream());
msg = "Connection established.";
break;
}
}
System.out.println(msg);
}
/**
* broadcast Hello to neighbors
*/
private boolean processStart() {
boolean hasNeighbour = false;
boolean isUpdated = false;
try {
for (int i = 0; i < 4; i++) {
if(ports[i] != null){
hasNeighbour = true;
Socket client = new Socket(ports[i].router2.getProcessIPAddress(), ports[i].router2.getProcessPortNumber());
ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
SOSPFPacket pack = new SOSPFPacket();
pack.srcProcessIP = ports[i].router2.getProcessIPAddress();
pack.srcProcessPort = rd.getProcessPortNumber();
pack.srcIP = rd.getSimulatedIPAddress();
pack.weight = ports[i].weight;
pack.sospfType = 0;
oos.writeObject(pack);
SOSPFPacket backPack = (SOSPFPacket) ois.readObject();
if(backPack.sospfType == 2){
System.out.println("Already Started.");
//client.close();
continue;
}
System.out.println("Hello From " + ports[i].router2.getSimulatedIPAddress() + " \nSet " + ports[i].router2.getSimulatedIPAddress() + " state to TWO_WAY");
ports[i].router2.setStatus(RouterStatus.TWO_WAY);
oos.writeObject(pack);
//1.creates linkDescription for this new link
//2. adds this new link to the LSA
LinkDescription newLinkDes = new LinkDescription(ports[i].router2.getSimulatedIPAddress(), ports[i].router2.getProcessPortNumber(), ports[i].weight);
this.lsd.addLink(rd.getSimulatedIPAddress(), newLinkDes);
isUpdated = true;
//client.close();
}
}
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!hasNeighbour){
System.out.println("No Neighbour Connected.");
}
return isUpdated;
}
/**
* attach the link to the remote router, which is identified by the given
* simulated ip; to establish the connection via socket, you need to
* indentify the process IP and process Port; additionally, weight is the
* cost to transmitting data through the link
* <p/>
* This command does trigger the link database synchronization
*/
private void processConnect(String processIP, short processPort, String simulatedIP, short weight) {
}
/**
* output the neighbors of the routers
*/
private void processNeighbors() {
for (int i = 0; i < 4; i++) {
if (ports[i] != null) {
System.out.println("IP Address of the neighbor[" + i + "] : " + ports[i].router2.getSimulatedIPAddress());
}
}
}
/**
* disconnect with all neighbors and quit the program
*/
private void processQuit() {
System.out.println("Quitting client.");
System.exit(1);
}
public void terminal() {
try {
ServerSocket serverSocket = new ServerSocket(rd.getProcessPortNumber());
System.out.println("Router:[" + rd.getSimulatedIPAddress() + "] ready...");
startClient();
while (true) {
Socket socket = serverSocket.accept();
socket.getLocalSocketAddress();
new RouterThread(socket, rd, ports, lsd, sockets, oosList).start();
}
} catch (IOException e) {
System.out.println("Unable to read from standard in");
System.exit(1);
}
}
public void startClient() {
(new Thread() {
@Override
public void run() {
try {
BufferedReader br = new java.io.BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.print(">> ");
String command = br.readLine();
if (command.startsWith("detect ")) {
String[] cmdLine = command.split(" ");
processDetect(cmdLine[1]);
} else if (command.startsWith("disconnect ")) {
String[] cmdLine = command.split(" ");
processDisconnect(Short.parseShort(cmdLine[1]));
} else if (command.startsWith("quit")) {
processQuit();
} else if (command.startsWith("attach ")) {
String[] cmdLine = command.split(" ");
processAttach(cmdLine[1], Short.parseShort(cmdLine[2]), cmdLine[3], Short.parseShort(cmdLine[4]));
} else if (command.equals("start")) {
boolean isUpdated = processStart();
if(isUpdated){
sendNewPack();
}
} else if (command.equals("connect ")) {
String[] cmdLine = command.split(" ");
processConnect(cmdLine[1], Short.parseShort(cmdLine[2]), cmdLine[3], Short.parseShort(cmdLine[4]));
} else if (command.equals("neighbors")) {
// output neighbors
processNeighbors();
} else {
// invalid command
System.out.println("The interface does not support this command.");
break;
}
}
br.close();
//isReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
public void sendNewPack() throws IOException {
SOSPFPacket newPack = new SOSPFPacket();
newPack.srcIpList = new ArrayList<String>();
newPack.srcIpList.add(rd.getSimulatedIPAddress());
newPack.srcIP = rd.getSimulatedIPAddress();
newPack.sospfType = 1;
newPack.lsaArray = new Vector<LSA>();
for (Entry<String, LSA> lsa : lsd._store.entrySet()) {
newPack.lsaArray.addElement(lsa.getValue().copy());
}
for (int i = 0; i < 4; i++) {
if(ports[i] != null){
synchronized (sockets[i]) {
//System.out.println("as client Send pack to" + ports[i].router2.getSimulatedIPAddress());
oosList[i].writeObject(newPack);
}
}
}
}
}