-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.java
executable file
·131 lines (105 loc) · 3.32 KB
/
server.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
/**
*
* server.java
*/
/**
*
* Program to initialize topology of every router
* @author Ishaan Thakker
* @author Amol Gaikwad
* @author Neel Desai
*/
// Importing packages
import java.util.*;
import java.net.*;
import java.io.*;
public class server extends Thread{
static Topology t;
static ArrayList<String> sources;
public static void main(String args[]){
t = new Topology();
sources = new ArrayList<>();
try{
//Read the topology from text file
File f = new File("topology.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while(line != null){
String[] content = line.split(",");
String source = content[0];
String destination = content[1];
double cost = Double.parseDouble(content[2]);
TopologyRow row = new TopologyRow(source, destination, cost, destination);
t.addRow(row);
if(!sources.contains(source))
sources.add(source);
line = br.readLine();
}
listener l = new listener();
System.out.println("Starting");
l.start();
System.out.println("Started");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
/**
* Class to listen all incoming routers and send them their all topologies
*/
static class listener extends Thread{
/**
* Function to initialize the topology of incoming routers
* @param none
* @return void
*/
public void run(){
try{
DatagramSocket send_socket = new DatagramSocket();
DatagramSocket ds = new DatagramSocket(6789);
while(true){
// Receive packets
byte[] b = new byte[3000];
DatagramPacket dp = new DatagramPacket(b, b.length);
ds.receive(dp);
byte[] data = dp.getData();
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
System.out.println("FROM: "+dp.getAddress().toString());
LinkState ls = (LinkState) is.readObject();
String source = ls.getSource();
boolean state = ls.getState();
System.out.println(source+" "+state);
ArrayList<TopologyRow> rows = t.getTopology();
ArrayList<TopologyRow> rows_to_send = new ArrayList<>();
// Getting the information of neighbours for connecting router
for(TopologyRow row: rows){
if(source.equals(row.getSource())){
// row.setStateActive(state);
}
if(source.equals(row.getSource())){
rows_to_send.add(row);
}
}
Topology topology_to_send = new Topology(rows_to_send);
t.setTopology(rows);
// Send information of neighbours to connecting routers
System.out.println("Broadcasting");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(outputStream);
os.writeObject(topology_to_send);
os.flush();
byte[] buf = outputStream.toByteArray();
//Send the packet to the router
DatagramPacket dp_send = new DatagramPacket(buf, buf.length, InetAddress.getByName(source), 6790);
System.out.println(source);
send_socket.send(dp_send);
System.out.println("sent");
}
}catch(Exception e){
System.out.println("In listener run: "+e.getMessage());
e.printStackTrace();
}
}
}
}