-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCP.java
111 lines (97 loc) · 3.61 KB
/
CP.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
/**
* @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 CP extends Node {
static final int CONTROLLER_PORT = 50000;
static final int ISP_PORT = 50005;
static final int CP_PORT = 50006; // <---- current node
static final int DSERVER_PORT = 50007;
static final int DSERVER2_PORT = 50008;
static final String CONTROLLER_NODE = "Controller";
static final String ISP_NODE = "ISP";
static final String DSERVER_NODE = "DServer";
static final String DSERVER2_NODE = "DServer2";
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
CP(int port) {
try {
nodeList.put(ISP_PORT, ISP_NODE);
nodeList.put(DSERVER_PORT, DSERVER_NODE);
nodeList.put(DSERVER2_PORT, DSERVER2_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 CP 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 CP(CP_PORT)).start();
System.out.println("Program completed");
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}