@@ -13,11 +13,25 @@ interface CommonGraphNode {
1313
1414interface RouterGraphNode extends CommonGraphNode {
1515 type : DeviceType . Router ;
16+ routingTable : RoutingTableEntry [ ] ;
17+ }
18+
19+ export interface RoutingTableEntry {
20+ ip : string ;
21+ mask : string ;
22+ iface : string ;
23+ }
24+
25+ // Typescript type guard
26+ export function isRouter ( node : GraphNode ) : node is RouterGraphNode {
27+ return node . type === DeviceType . Router ;
1628}
1729
1830export type GraphNode = CommonGraphNode | RouterGraphNode ;
1931
20- export interface GraphDataNode {
32+ export type GraphDataNode = CommonDataNode | RouterDataNode ;
33+
34+ interface CommonDataNode {
2135 id : DeviceId ;
2236 x : number ;
2337 y : number ;
@@ -27,6 +41,11 @@ export interface GraphDataNode {
2741 connections : DeviceId [ ] ;
2842}
2943
44+ interface RouterDataNode extends CommonDataNode {
45+ type : DeviceType . Router ;
46+ routingTable : RoutingTableEntry [ ] ;
47+ }
48+
3049export type GraphData = GraphDataNode [ ] ;
3150
3251export interface NewDevice {
@@ -49,11 +68,7 @@ export class DataGraph {
4968 console . log ( nodeData ) ;
5069 const connections = new Set ( nodeData . connections ) ;
5170 const graphNode : GraphNode = {
52- x : nodeData . x ,
53- y : nodeData . y ,
54- type : nodeData . type ,
55- ip : nodeData . ip ,
56- mask : nodeData . mask ,
71+ ...nodeData ,
5772 connections : connections ,
5873 } ;
5974 dataGraph . addDevice ( nodeData . id , graphNode ) ;
@@ -66,15 +81,20 @@ export class DataGraph {
6681
6782 // Serialize nodes
6883 this . getDevices ( ) . forEach ( ( [ id , info ] ) => {
69- graphData . push ( {
70- id : id ,
84+ const graphNode : GraphDataNode = {
85+ id,
7186 x : info . x ,
7287 y : info . y ,
7388 type : info . type , // Save the device type (Router, Host)
7489 ip : info . ip ,
7590 mask : info . mask ,
7691 connections : Array . from ( info . connections . values ( ) ) ,
77- } ) ;
92+ } ;
93+ if ( isRouter ( info ) ) {
94+ graphData . push ( { ...graphNode , routingTable : info . routingTable } ) ;
95+ } else {
96+ graphData . push ( graphNode ) ;
97+ }
7898 } ) ;
7999 return graphData ;
80100 }
@@ -85,6 +105,7 @@ export class DataGraph {
85105 const graphnode : GraphNode = {
86106 ...deviceInfo ,
87107 connections : new Set < number > ( ) ,
108+ routingTable : [ ] ,
88109 } ;
89110 this . devices . set ( id , graphnode ) ;
90111 console . log ( `Device added with ID ${ id } ` ) ;
@@ -114,23 +135,47 @@ export class DataGraph {
114135 ) ;
115136 return ;
116137 }
117- if ( ! this . devices . has ( n1Id ) ) {
138+ const device1 = this . devices . get ( n1Id ) ;
139+ const device2 = this . devices . get ( n2Id ) ;
140+ if ( ! device1 ) {
118141 console . warn ( `Device with ID ${ n1Id } does not exist in devices.` ) ;
119142 return ;
120143 }
121- if ( ! this . devices . has ( n2Id ) ) {
144+ if ( ! device2 ) {
122145 console . warn ( `Device with ID ${ n2Id } does not exist in devices.` ) ;
123146 return ;
124147 // Check if an edge already exists between these two devices
125148 }
126- if ( this . devices . get ( n1Id ) . connections . has ( n2Id ) ) {
149+ if ( device1 . connections . has ( n2Id ) ) {
127150 console . warn (
128151 `Connection between ID ${ n1Id } and ID ${ n2Id } already exists.` ,
129152 ) ;
130153 return ;
131154 }
132- this . devices . get ( n1Id ) . connections . add ( n2Id ) ;
133- this . devices . get ( n2Id ) . connections . add ( n1Id ) ;
155+ device1 . connections . add ( n2Id ) ;
156+ device2 . connections . add ( n1Id ) ;
157+
158+ if ( isRouter ( device1 ) ) {
159+ if ( ! device1 . routingTable ) {
160+ device1 . routingTable = [ ] ;
161+ }
162+ device1 . routingTable . push ( {
163+ ip : device2 . ip . toString ( ) ,
164+ mask : device2 . mask ,
165+ iface : `eth${ n2Id } ` ,
166+ } ) ;
167+ }
168+
169+ if ( isRouter ( device2 ) ) {
170+ if ( ! device2 . routingTable ) {
171+ device2 . routingTable = [ ] ;
172+ }
173+ device2 . routingTable . push ( {
174+ ip : device1 . ip . toString ( ) ,
175+ mask : device1 . mask ,
176+ iface : `eth${ n1Id } ` ,
177+ } ) ;
178+ }
134179
135180 console . log (
136181 `Connection created between devices ID: ${ n1Id } and ID: ${ n2Id } ` ,
0 commit comments