Skip to content

Commit a968190

Browse files
authored
fix: take into account used IPs (#75)
1 parent fdc53dc commit a968190

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

src/context.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
saveToLocalStorage,
77
} from "./types/viewportManager";
88
import { Layer } from "./types/devices/device";
9-
import { IpAddressGenerator } from "./packets/ip";
9+
import { IpAddress, IpAddressGenerator } from "./packets/ip";
1010
import { layerFromName } from "./types/devices/utils";
1111

1212
export 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
}

src/types/graphs/datagraph.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -155,28 +155,6 @@ export class DataGraph {
155155
device1.connections.add(n2Id);
156156
device2.connections.add(n1Id);
157157

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: 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: n1Id,
177-
});
178-
}
179-
180158
console.log(
181159
`Connection created between devices ID: ${n1Id} and ID: ${n2Id}`,
182160
);

0 commit comments

Comments
 (0)