-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRIPSender.java
58 lines (51 loc) · 1.87 KB
/
RIPSender.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
import java.io.IOException;
import java.net.*;
import java.util.Map;
public class RIPSender implements Runnable {
private int port;
private DatagramSocket socket;
RIPSender() {
this.port = 5521;
}
RIPSender(int port, DatagramSocket socket) {
this.port = port;
this.socket = socket;
}
public void SendUDPPacket() throws IOException {
// synchronized (RoutingTable.routeEntriesLock) {
for (Map.Entry<InetAddress, InetAddress> neighbor : LunarRover.NEIGHBORS_ENTRIES.entrySet()) {
RIP rip = new RIP();
for (Map.Entry<InetAddress, NextHopInfoTable> route : RoutingTable.ROUTE_ENTRIES.entrySet()) {
NextHopInfoTable nextHopeInfo = route.getValue();
// implement split horizon with poison reverse.
Integer hopCount = nextHopeInfo.hopCount;
if (neighbor.getValue().equals(nextHopeInfo.nextHopAddress) && hopCount > 1) {
hopCount = 16;
}
// add RIP entry
RIPEntry ripEntry = new RIPEntry(route.getKey(), nextHopeInfo.subnetMask, nextHopeInfo.nextHopAddress, hopCount);
rip.ripEntries.add(ripEntry);
}
// Packet setup
byte[] data = rip.RIPEncodeData();
DatagramPacket packet = new DatagramPacket(data, data.length, neighbor.getValue(), RIPReceiver.PORT);
RIPReceiver.SOCKET.send(packet);
}
// }
RoutingTable rt = new RoutingTable();
// rt.PrintRoutingTable();
}
@Override
public void run() {
while (true) {
try {
SendUDPPacket();
Thread.sleep(5000);
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
}
}