@@ -237,15 +237,25 @@ export class DataGraph {
237237 ) ;
238238 return null ;
239239 }
240- const n1Iface = this . getNextInterfaceNumber ( device1 ) ;
241- const n2Iface = this . getNextInterfaceNumber ( device2 ) ;
240+ const n1Iface = this . getNextFreeInterfaceNumber ( device1 ) ;
241+ const n2Iface = this . getNextFreeInterfaceNumber ( device2 ) ;
242+
243+ if ( n1Iface === null || n2Iface === null ) {
244+ const unavailableDevices =
245+ n1Iface === null && n2Iface === null
246+ ? `devices ${ n1Id } and ${ n2Id } `
247+ : `device ${ n1Id === null ? n1Id : n2Id } ` ;
248+ alert ( `No free interfaces available for ${ unavailableDevices } .` ) ;
249+ return null ;
250+ }
242251 const edge = {
243252 from : { id : n1Id , iface : n1Iface } ,
244253 to : { id : n2Id , iface : n2Iface } ,
245254 } ;
246255 return this . reAddEdge ( edge ) ;
247256 }
248257
258+ // NOTE: May be used in future
249259 private getNextInterfaceNumber ( device : DataDevice ) : number {
250260 const numberOfInterfaces = getNumberOfInterfaces ( device . getType ( ) ) ;
251261 const ifaceUses = new Array ( numberOfInterfaces )
@@ -256,6 +266,17 @@ export class DataGraph {
256266 return ifaceUses [ 0 ] [ 0 ] ;
257267 }
258268
269+ private getNextFreeInterfaceNumber ( device : DataDevice ) : number | null {
270+ const numberOfInterfaces = getNumberOfInterfaces ( device . getType ( ) ) ;
271+ for ( let i = 0 ; i < numberOfInterfaces ; i ++ ) {
272+ const connections = this . getConnectionsInInterface ( device . id , i ) ;
273+ if ( ! connections || connections . length === 0 ) {
274+ return i ; // Return the first free interface
275+ }
276+ }
277+ return null ; // Return null if no free interface is found
278+ }
279+
259280 updateDevicePosition ( id : DeviceId , newValues : { x ?: number ; y ?: number } ) {
260281 const deviceDataNode = this . deviceGraph . getVertex ( id ) ;
261282 if ( ! deviceDataNode ) {
@@ -445,7 +466,6 @@ export class DataGraph {
445466 const parents = new Map < DeviceId , DeviceId > ( ) ;
446467 parents . set ( id , id ) ;
447468 const queue = [ id ] ;
448-
449469 while ( queue . length > 0 ) {
450470 const currentId = queue . shift ( ) ;
451471 const current = this . deviceGraph . getVertex ( currentId ) ;
@@ -475,10 +495,21 @@ export class DataGraph {
475495 const dst = this . deviceGraph . getVertex ( dstId ) ;
476496
477497 if ( dst instanceof DataNetworkDevice ) {
498+ const dataEdge = this . deviceGraph . getEdge ( currentId , childId ) ;
499+ if ( ! dataEdge ) {
500+ console . warn (
501+ `Edge between devices ${ currentId } and ${ childId } not found!` ,
502+ ) ;
503+ return ;
504+ }
505+ const iface =
506+ dataEdge . from . id === currentId
507+ ? dataEdge . from . iface
508+ : dataEdge . to . iface ;
478509 newTable . push ( {
479510 ip : dst . ip . toString ( ) ,
480511 mask : dst . ipMask . toString ( ) ,
481- iface : childId ,
512+ iface,
482513 } ) ;
483514 }
484515 } ) ;
0 commit comments