Skip to content

Commit bff0ca5

Browse files
Manuel-Polpgallino
andauthored
Routing tables use interfaces as ifaces and connections are limited to one connection per interface (#194)
Co-authored-by: pgallino <[email protected]>
1 parent dac01de commit bff0ca5

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/types/graphs/datagraph.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
});

src/types/graphs/viewgraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export class ViewGraph {
154154
const edgeData = this.datagraph.addNewEdge(device1Id, device2Id);
155155

156156
if (!edgeData) {
157-
console.error("Failed to create new edge");
157+
console.warn("Failed to create new edge");
158158
return false;
159159
}
160160

src/types/view-devices/vDevice.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export abstract class ViewDevice extends Container {
166166
}
167167
// If the stored device is this, ignore
168168
if (ViewDevice.connectionTarget === this) {
169+
ViewDevice.connectionTarget = null;
169170
return;
170171
}
171172
// Connect both devices
@@ -174,8 +175,8 @@ export abstract class ViewDevice extends Container {
174175
const move = new AddEdgeMove(this.viewgraph.getLayer(), n1, n2);
175176
if (urManager.push(this.viewgraph, move)) {
176177
refreshElement();
177-
ViewDevice.connectionTarget = null;
178178
}
179+
ViewDevice.connectionTarget = null;
179180
}
180181

181182
selectToConnect() {

0 commit comments

Comments
 (0)