-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISP.java
More file actions
111 lines (97 loc) · 3.57 KB
/
Copy pathISP.java
File metadata and controls
111 lines (97 loc) · 3.57 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
/**
* @author Thomas Moroney
*/
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Random;
public class ISP extends Node {
static final int CONTROLLER_PORT = 50000;
static final int GW1_PORT = 50003;
static final int GW2_PORT = 50004;
static final int ISP_PORT = 50005; // <---- current node
static final int CP_PORT = 50006;
static final String CONTROLLER_NODE = "Controller";
static final String GW1_NODE = "GW1";
static final String GW2_NODE = "GW2";
static final String CP_NODE = "CP";
InetSocketAddress dstAddress;
HashMap<String, Integer> nextJump = new HashMap<>(); // stores the jump after asking the controller so there is no
// need to ask again
HashMap<Integer, String> nodeList = new HashMap<>(); // map each port to a node name
DatagramPacket currentPacket;
String dest; // destination of incoming packet
ISP(int port) {
try {
nodeList.put(GW1_PORT, GW1_NODE);
nodeList.put(GW2_PORT, GW2_NODE);
nodeList.put(CP_PORT, CP_NODE);
socket = new DatagramSocket(port);
listener.go();
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
// handle incoming packets
public void onReceipt(DatagramPacket packet) {
try {
PacketContent content = PacketContent.fromDatagramPacket(packet);
if (content.getType() == PacketContent.TEXTPACKET) {
System.out.println("Received packet");
currentPacket = packet;
TextPacket inPacket = ((TextPacket) content);
dest = (inPacket.text).substring(0, 3); // isolate destination of packet
if (nextJump.containsKey(dest)) {
int destPort = nextJump.get(dest);
System.out.println("Found next jump in forwarding table.");
System.out.println("Forwarded packet to " + nodeList.get(destPort));
dstAddress = new InetSocketAddress(nodeList.get(destPort), destPort);
packet.setSocketAddress(dstAddress);
socket.send(packet);
} else {
System.out.println("Next jump not stored. Requesting next jump from Controller...");
dstAddress = new InetSocketAddress(CONTROLLER_NODE, CONTROLLER_PORT);
packet.setSocketAddress(dstAddress);
socket.send(packet);
}
} else if (content.getType() == PacketContent.NEXTNODE) {
Random random = new Random();
int num = random.nextInt(6);
if (num != 0) {
System.out.println("Received packet");
JumpPacket inPacket = ((JumpPacket) content);
int port = inPacket.getPort();
DatagramPacket ackPacket;
ackPacket = new AckPacketContent("Updated ISP forwarding table").toDatagramPacket();
dstAddress = new InetSocketAddress(CONTROLLER_NODE, CONTROLLER_PORT);
ackPacket.setSocketAddress(dstAddress);
socket.send(ackPacket);
System.out.println("Controller returned next jump - updated forwarding table.");
System.out.println("Forwarded packet to " + nodeList.get(port));
nextJump.put(dest, port); // adds next node to hashmap for later use
dstAddress = new InetSocketAddress(nodeList.get(port), port);
currentPacket.setSocketAddress(dstAddress);
socket.send(currentPacket);
}
else {
System.out.println("There was disturbance in the network - next node was not received!!!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void start() throws Exception {
System.out.println("Waiting for contact");
this.wait();
}
public static void main(String[] args) {
try {
(new ISP(ISP_PORT)).start();
System.out.println("Program completed");
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}