Skip to content

Commit 2176e22

Browse files
authored
refactor: add edge object to datagraph (#157)
Related to #119 This PR uses the new `Graph` to specify the interface used on each connection, instead of it being implicitly the ID of the other device.
1 parent de9519d commit 2176e22

File tree

16 files changed

+337
-282
lines changed

16 files changed

+337
-282
lines changed

src/graphics/renderables/device_info.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ export class DeviceInfo extends StyledInfo {
4343
createRightBarButton(
4444
"Delete device",
4545
() => {
46-
const deviceData = this.device.getCreateDevice();
4746
const currLayer = this.device.viewgraph.getLayer();
48-
const move = new RemoveDeviceMove(currLayer, deviceData);
49-
this.device.delete();
50-
urManager.push(move);
47+
const move = new RemoveDeviceMove(currLayer, this.device.id);
48+
urManager.push(this.device.viewgraph, move);
5149
},
5250
"right-bar-delete-button",
5351
),

src/handlers/undoRedoHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class UndoRedoHandler {
2929

3030
if (this.undoButton && this.redoButton) {
3131
this.setupButtons();
32-
urManager.suscribe(() => this.updateButtons());
32+
urManager.subscribe(() => this.updateButtons());
3333
}
3434
this.setupShortcuts();
3535
}

src/types/devices/device.ts

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ import { Colors, ZIndexLevels } from "../../utils";
1919
import { Position } from "../common";
2020
import { DeviceInfo } from "../../graphics/renderables/device_info";
2121
import { IpAddress } from "../../packets/ip";
22-
import { DeviceId } from "../graphs/datagraph";
22+
import { DeviceId, RemovedNodeData } from "../graphs/datagraph";
2323
import { DragDeviceMove, AddEdgeMove } from "../undo-redo";
2424
import { Layer } from "./layer";
2525
import { Packet } from "../packet";
26-
import { CreateDevice } from "./utils";
2726
import { MacAddress } from "../../packets/ethernet";
2827
import { GlobalContext } from "../../context";
2928

@@ -131,17 +130,11 @@ export abstract class Device extends Container {
131130
this.addChild(idText); // Add the ID text as a child of the device
132131
}
133132

134-
/// Returns the data needed to create the device
135-
getCreateDevice(): CreateDevice {
136-
const node = this.viewgraph.getDataGraph().getDevice(this.id);
137-
const connections = this.viewgraph.getDataGraph().getConnections(this.id);
138-
return { id: this.id, node, connections };
139-
}
140-
141-
delete(): void {
142-
this.viewgraph.removeDevice(this.id);
133+
delete(): RemovedNodeData {
134+
const deviceData = this.viewgraph.removeDevice(this.id);
143135
console.log(`Device ${this.id} deleted`);
144136
this.destroy();
137+
return deviceData;
145138
}
146139

147140
onPointerDown(event: FederatedPointerEvent): void {
@@ -159,36 +152,22 @@ export abstract class Device extends Container {
159152
this.parent.on("pointerup", onPointerUp);
160153
}
161154

162-
// TODO: why is this even here??
163-
connectTo(adjacentId: DeviceId): boolean {
164-
// Connects both devices with an edge.
165-
const edgeId = this.viewgraph.addEdge(this.id, adjacentId);
166-
if (edgeId) {
167-
// Register move
168-
const move = new AddEdgeMove(this.viewgraph.getLayer(), {
169-
n1: this.id,
170-
n2: adjacentId,
171-
});
172-
urManager.push(move);
173-
174-
return true;
175-
}
176-
return false;
177-
}
178-
179155
onClick(e: FederatedPointerEvent) {
180156
e.stopPropagation();
181157

182158
if (!Device.connectionTarget) {
183159
selectElement(this);
184160
return;
185161
}
186-
// If the stored device is this, reset it
162+
// If the stored device is this, ignore
187163
if (Device.connectionTarget === this) {
188164
return;
189165
}
190-
// The "LineStart" device ends up as the end of the drawing but it's the same
191-
if (this.connectTo(Device.connectionTarget.id)) {
166+
// Connect both devices
167+
const n1 = Device.connectionTarget.id;
168+
const n2 = this.id;
169+
const move = new AddEdgeMove(this.viewgraph.getLayer(), { n1, n2 });
170+
if (urManager.push(this.viewgraph, move)) {
192171
refreshElement();
193172
Device.connectionTarget = null;
194173
}
@@ -258,7 +237,6 @@ export abstract class Device extends Container {
258237
abstract getLayer(): Layer;
259238

260239
destroy() {
261-
// Clear connections
262240
deselectElement();
263241
super.destroy();
264242
}
@@ -278,13 +256,14 @@ function onPointerMove(event: FederatedPointerEvent): void {
278256
}
279257

280258
function onPointerUp(): void {
281-
if (Device.dragTarget && Device.startPosition) {
259+
const target = Device.dragTarget;
260+
if (target && Device.startPosition) {
282261
const endPosition: Position = {
283-
x: Device.dragTarget.x,
284-
y: Device.dragTarget.y,
262+
x: target.x,
263+
y: target.y,
285264
};
286265
console.log("Finalizing move for device:", {
287-
id: Device.dragTarget.id,
266+
id: target.id,
288267
startPosition: Device.startPosition,
289268
endPosition,
290269
});
@@ -294,24 +273,24 @@ function onPointerUp(): void {
294273
Device.startPosition.y === endPosition.y
295274
) {
296275
console.log(
297-
`No movement detected for device ID ${Device.dragTarget.id}. Move not registered.`,
276+
`No movement detected for device ID ${target.id}. Move not registered.`,
298277
);
299278
} else {
300279
const move = new DragDeviceMove(
301-
Device.dragTarget.viewgraph.getLayer(),
302-
Device.dragTarget.id,
280+
target.viewgraph.getLayer(),
281+
target.id,
303282
Device.startPosition,
304283
endPosition,
305284
);
306-
urManager.push(move);
285+
urManager.push(target.viewgraph, move);
307286
}
308287

309-
// Resetear variables estáticas
288+
// Reset static variables
310289
Device.startPosition = null;
311290

312291
// Remove global pointermove and pointerup events
313-
Device.dragTarget.parent.off("pointermove", onPointerMove);
314-
Device.dragTarget.parent.off("pointerup", onPointerUp);
292+
target.parent.off("pointermove", onPointerMove);
293+
target.parent.off("pointerup", onPointerUp);
315294
Device.dragTarget = null;
316295
}
317296
}

src/types/devices/host.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export class Host extends NetworkDevice {
140140
}
141141

142142
destroy() {
143+
super.destroy();
143144
this.runningPrograms.forEach((program) => program.stop());
144145
this.runningPrograms.clear();
145146
}

src/types/devices/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ export { Device } from "./device";
44
export { NetworkDevice } from "./networkDevice";
55
export { Router } from "./router";
66
export { Host } from "./host";
7-
export { createDevice } from "./utils";

src/types/devices/utils.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,34 @@ import { Host } from "./host";
99
import { Router } from "./router";
1010
import { Switch } from "./switch";
1111

12-
export interface CreateDevice {
13-
id: DeviceId;
14-
node: GraphNode;
15-
connections: DeviceId[];
16-
}
17-
1812
export function createDevice(
19-
deviceInfo: CreateDevice,
13+
id: DeviceId,
14+
node: GraphNode,
2015
viewgraph: ViewGraph,
2116
ctx: GlobalContext,
2217
): Device {
23-
const position: { x: number; y: number } = deviceInfo.node;
18+
const position: { x: number; y: number } = node;
2419
let mac: MacAddress;
2520

26-
if (isLinkNode(deviceInfo.node)) {
27-
mac = MacAddress.parse(deviceInfo.node.mac);
21+
if (isLinkNode(node)) {
22+
mac = MacAddress.parse(node.mac);
2823
}
2924

3025
let ip: IpAddress;
3126
let mask: IpAddress;
3227

33-
if (isNetworkNode(deviceInfo.node)) {
34-
ip = IpAddress.parse(deviceInfo.node.ip);
35-
mask = IpAddress.parse(deviceInfo.node.mask);
36-
mac = MacAddress.parse(deviceInfo.node.mac);
28+
if (isNetworkNode(node)) {
29+
ip = IpAddress.parse(node.ip);
30+
mask = IpAddress.parse(node.mask);
31+
mac = MacAddress.parse(node.mac);
3732
}
3833

39-
switch (deviceInfo.node.type) {
34+
switch (node.type) {
4035
case DeviceType.Router:
41-
return new Router(deviceInfo.id, viewgraph, ctx, position, mac, ip, mask);
36+
return new Router(id, viewgraph, ctx, position, mac, ip, mask);
4237
case DeviceType.Host:
43-
return new Host(deviceInfo.id, viewgraph, ctx, position, mac, ip, mask);
38+
return new Host(id, viewgraph, ctx, position, mac, ip, mask);
4439
case DeviceType.Switch:
45-
return new Switch(deviceInfo.id, viewgraph, ctx, position, mac);
40+
return new Switch(id, viewgraph, ctx, position, mac);
4641
}
4742
}

src/types/edge.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,23 @@ export class Edge extends Graphics {
115115
this.rightbar.addButton(
116116
"Delete Edge",
117117
() => {
118+
const viewgraph = this.viewgraph;
118119
// Obtener las tablas de enrutamiento antes de eliminar la conexión
119-
const routingTable1 = this.viewgraph.getRoutingTable(
120-
this.connectedNodes.n1,
121-
);
122-
const routingTable2 = this.viewgraph.getRoutingTable(
123-
this.connectedNodes.n2,
124-
);
120+
const routingTable1 = viewgraph.getRoutingTable(this.connectedNodes.n1);
121+
const routingTable2 = viewgraph.getRoutingTable(this.connectedNodes.n2);
125122

126123
// Crear el movimiento de eliminación de la arista con la información adicional
124+
const routingTables = new Map([
125+
[this.connectedNodes.n1, routingTable1],
126+
[this.connectedNodes.n2, routingTable2],
127+
]);
127128
const move = new RemoveEdgeMove(
128-
this.viewgraph.getLayer(),
129+
viewgraph.getLayer(),
129130
this.connectedNodes,
130-
new Map([
131-
[this.connectedNodes.n1, routingTable1],
132-
[this.connectedNodes.n2, routingTable2],
133-
]),
131+
routingTables,
134132
);
135133

136-
this.delete();
137-
urManager.push(move);
134+
urManager.push(viewgraph, move);
138135
},
139136
"right-bar-delete-button",
140137
);

0 commit comments

Comments
 (0)