Skip to content

Commit be89df4

Browse files
committed
feat: regenerate routing tables on each change
1 parent 43cfff9 commit be89df4

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class GlobalContext {
1919
initialize(viewport: Viewport) {
2020
this.viewport = viewport;
2121

22-
const baseIp = "192.168.1.0";
22+
const baseIp = "10.0.0.0";
2323
const mask = "255.255.255.255";
2424
this.ipGenerator = new IpAddressGenerator(baseIp, mask);
2525
loadFromLocalStorage(this);

src/types/graphs/datagraph.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface RouterGraphNode extends CommonGraphNode {
1919
export interface RoutingTableEntry {
2020
ip: string;
2121
mask: string;
22-
iface: number;
22+
iface: DeviceId;
2323
}
2424

2525
// Typescript type guard
@@ -181,12 +181,13 @@ export class DataGraph {
181181
`Connection created between devices ID: ${n1Id} and ID: ${n2Id}`,
182182
);
183183
this.notifyChanges();
184+
this.regenerateAllRoutingTables();
184185
}
185186

186187
updateDevicePosition(id: DeviceId, newValues: { x?: number; y?: number }) {
187188
const deviceGraphNode = this.devices.get(id);
188189
if (!deviceGraphNode) {
189-
console.warn("Devices id is not registered");
190+
console.warn("Device's id is not registered");
190191
return;
191192
}
192193
this.devices.set(id, { ...deviceGraphNode, ...newValues });
@@ -237,6 +238,7 @@ export class DataGraph {
237238
this.devices.delete(id);
238239
console.log(`Device with ID ${id} and its connections were removed.`);
239240
this.notifyChanges();
241+
this.regenerateAllRoutingTables();
240242
}
241243

242244
// Method to remove a connection (edge) between two devices by their IDs
@@ -270,6 +272,7 @@ export class DataGraph {
270272
`Connection removed between devices ID: ${n1Id} and ID: ${n2Id}`,
271273
);
272274
this.notifyChanges();
275+
this.regenerateAllRoutingTables();
273276
}
274277

275278
subscribeChanges(callback: () => void) {
@@ -279,4 +282,56 @@ export class DataGraph {
279282
notifyChanges() {
280283
this.onChanges.forEach((callback) => callback());
281284
}
285+
286+
regenerateAllRoutingTables() {
287+
console.log("Regenerating all routing tables");
288+
this.devices.forEach((_, id) => this.regenerateRoutingTable(id));
289+
}
290+
291+
regenerateRoutingTable(id: DeviceId) {
292+
const router = this.devices.get(id);
293+
if (!isRouter(router)) {
294+
return;
295+
}
296+
console.log(`Regenerating routing table for ID ${id}`);
297+
const parents = new Map<DeviceId, DeviceId>();
298+
parents.set(id, id);
299+
const queue = Array.from([id]);
300+
while (queue.length > 0) {
301+
const currentId = queue.shift();
302+
const current = this.devices.get(currentId);
303+
if (!isRouter(current)) {
304+
// Don't route packets on hosts
305+
continue;
306+
}
307+
current.connections.forEach((connectedId) => {
308+
if (!parents.has(connectedId)) {
309+
parents.set(connectedId, currentId);
310+
queue.push(connectedId);
311+
}
312+
});
313+
}
314+
315+
console.log(parents);
316+
317+
const table: RoutingTableEntry[] = [];
318+
parents.forEach((currentId, childId) => {
319+
const dstId = childId;
320+
if (dstId === id) {
321+
return;
322+
}
323+
324+
while (currentId !== id) {
325+
const parentId = parents.get(currentId);
326+
childId = currentId;
327+
currentId = parentId;
328+
}
329+
// Here the currentId is the router, and the childId
330+
// is the first step towards dstId
331+
const dst = this.devices.get(dstId);
332+
const entry = { ip: dst.ip, mask: dst.mask, iface: childId };
333+
table.push(entry);
334+
});
335+
router.routingTable = table;
336+
}
282337
}

0 commit comments

Comments
 (0)