@@ -19,7 +19,7 @@ interface RouterGraphNode extends CommonGraphNode {
1919export interface RoutingTableEntry {
2020 ip : string ;
2121 mask : string ;
22- iface : string ;
22+ iface : DeviceId ;
2323}
2424
2525// Typescript type guard
@@ -162,7 +162,7 @@ export class DataGraph {
162162 device1 . routingTable . push ( {
163163 ip : device2 . ip . toString ( ) ,
164164 mask : device2 . mask ,
165- iface : `eth ${ n2Id } ` ,
165+ iface : n2Id ,
166166 } ) ;
167167 }
168168
@@ -173,20 +173,21 @@ export class DataGraph {
173173 device2 . routingTable . push ( {
174174 ip : device1 . ip . toString ( ) ,
175175 mask : device1 . mask ,
176- iface : `eth ${ n1Id } ` ,
176+ iface : n1Id ,
177177 } ) ;
178178 }
179179
180180 console . log (
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 ( "Device’ s 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