Skip to content

Commit 6b7204b

Browse files
Switcher manage Sending and Receiving Clients over Network
If Client is the first, they are the sending client. If Client is the second, they are the receiving client. The final router will send data to this Client stored in the routerCount object.
1 parent 0fb4ec1 commit 6b7204b

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

Server/src/Switcher.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ let FlowTable = new Map<String, String[]>([
2424
["R3", ["R2", "E2"]],
2525
]);
2626

27+
let receivingClient = { ip: "", port: 0 };
28+
2729
/** Receive Messages from Routers */
28-
let routerCount = 0;
30+
let routerCount: number = 0, clientCount: number = 0;
31+
2932
Switcher.on('message', (msg, rinfo) => {
3033
console.log(`Server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
3134

@@ -69,17 +72,29 @@ Switcher.on('message', (msg, rinfo) => {
6972
case 3:
7073
// Type 3: Client connecting to Router to get information about Forwarding Table
7174
console.log("Client connecting to Switcher");
72-
73-
// Get first Router in Routers list
74-
var it = Routers.values();
75-
76-
let firstRouter = it.next().value;
77-
// TODO: If no Routers are active, save Client details and notify once a Router is connected
78-
if(!firstRouter) sendMessage = { message: "No Routers active on Network" };
79-
else sendMessage = JSON.parse(firstRouter);
80-
8175
messageType = 3;
8276

77+
// Get first Router in Routers list
78+
var it = Routers.values();
79+
80+
let firstRouter = it.next().value;
81+
// TODO: If no Routers are active, save Client details and notify once a Router is connected
82+
if(!firstRouter) return sendMessage = { message: "No Routers active on Network" };
83+
else if(clientCount == 0) {
84+
console.log("Initial Client Connected");
85+
sendMessage = JSON.parse(firstRouter);
86+
} else if(clientCount == 1) {
87+
console.log("Receiving Client Connected");
88+
// Store Client details for later use by last router
89+
receivingClient.ip = address;
90+
receivingClient.port = port;
91+
sendMessage = "You are the receiving Client";
92+
} else {
93+
console.log("Additional Client connected, no space in Flow Table");
94+
sendMessage = "You are not connected to the Network, no space.";
95+
}
96+
clientCount++;
97+
8398
}
8499

85100
// Send message to Client

0 commit comments

Comments
 (0)