66 saveToLocalStorage ,
77} from "./types/viewportManager" ;
88import { Layer } from "./types/devices/device" ;
9- import { IpAddressGenerator } from "./packets/ip" ;
9+ import { IpAddress , IpAddressGenerator } from "./packets/ip" ;
1010import { layerFromName } from "./types/devices/utils" ;
1111
1212export class GlobalContext {
@@ -19,10 +19,10 @@ export class GlobalContext {
1919 initialize ( viewport : Viewport ) {
2020 this . viewport = viewport ;
2121
22- const baseIp = "10.0.0.0" ;
23- const mask = "255.255.255.255" ;
24- this . ipGenerator = new IpAddressGenerator ( baseIp , mask ) ;
22+ // Sets the initial datagraph and viewgraph
2523 loadFromLocalStorage ( this ) ;
24+
25+ this . setIpGenerator ( ) ;
2626 }
2727
2828 getNextIp ( ) : { ip : string ; mask : string } {
@@ -33,6 +33,7 @@ export class GlobalContext {
3333 this . datagraph = datagraph ;
3434 this . viewport . clear ( ) ;
3535 this . viewgraph = new ViewGraph ( this . datagraph , this . viewport , layer ) ;
36+ this . setIpGenerator ( ) ;
3637 }
3738
3839 load ( datagraph : DataGraph , layer : Layer = Layer . Link ) {
@@ -79,4 +80,18 @@ export class GlobalContext {
7980 this . saveIntervalId = null ;
8081 }
8182 }
83+
84+ private setIpGenerator ( ) {
85+ let maxIp = IpAddress . parse ( "10.0.0.0" ) ;
86+ this . datagraph . getDevices ( ) . forEach ( ( device ) => {
87+ const ip = IpAddress . parse ( device . ip ) ;
88+ if ( maxIp . octets < ip . octets ) {
89+ maxIp = ip ;
90+ }
91+ } ) ;
92+ // TODO: we should use IpAddress instead of string here and in Datagraph
93+ const baseIp = maxIp . toString ( ) ;
94+ const mask = "255.255.255.255" ;
95+ this . ipGenerator = new IpAddressGenerator ( baseIp , mask ) ;
96+ }
8297}
0 commit comments