Skip to content

Commit 09982ce

Browse files
authored
refactor: use new Graph in ViewGraph (#155)
~Depends on #154~ This PR refactors `ViewGraph` to use the `Graph` ADT internally
1 parent 69eb4d5 commit 09982ce

File tree

11 files changed

+109
-144
lines changed

11 files changed

+109
-144
lines changed

src/graphics/renderables/device_info.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ export class DeviceInfo extends StyledInfo {
2323
}
2424

2525
private addCommonInfoFields() {
26-
const { id, connections, mac } = this.device;
26+
const { id, mac } = this.device;
27+
const connections = this.device.viewgraph
28+
.getConnections(id)
29+
.map((edge) => edge.otherEnd(id));
2730
super.addField("ID", id.toString());
2831
super.addField("MacAddress", mac.toString());
29-
super.addListField("Connected Devices", Array.from(connections.values()));
32+
super.addListField("Connected Devices", connections);
3033
}
3134

3235
private addCommonButtons() {

src/programs/echo_sender.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { EthernetFrame } from "../packets/ethernet";
1111

1212
function adjacentDevices(viewgraph: ViewGraph, srcId: DeviceId) {
1313
const adjacentDevices = viewgraph
14-
.getAdjacentDeviceIds(srcId)
14+
.getDeviceIds()
15+
.filter((id) => id !== srcId)
1516
.map((id) => ({ value: id.toString(), text: `Device ${id}` }));
1617

1718
return adjacentDevices;
@@ -63,7 +64,6 @@ export class SingleEcho extends ProgramBase {
6364
const path = this.viewgraph.getPathBetween(this.srcId, this.dstId);
6465
let dstMac = dstDevice.mac;
6566
if (!path) return;
66-
console.log(path);
6767
for (const id of path.slice(1)) {
6868
const device = this.viewgraph.getDevice(id);
6969
// if there’s a router in the middle, first send frame to router mac

src/types/devices/device.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export abstract class Device extends Container {
5252
readonly id: DeviceId;
5353
readonly viewgraph: ViewGraph;
5454
ctx: GlobalContext;
55-
connections = new Set<DeviceId>();
5655

5756
mac: MacAddress;
5857
arpTable: Map<IpAddress, MacAddress> = new Map<IpAddress, MacAddress>();
@@ -132,25 +131,13 @@ export abstract class Device extends Container {
132131
this.addChild(idText); // Add the ID text as a child of the device
133132
}
134133

135-
getConnections(): DeviceId[] {
136-
return Array.from(this.connections.values());
137-
}
138-
139134
/// Returns the data needed to create the device
140135
getCreateDevice(): CreateDevice {
141136
const node = this.viewgraph.getDataGraph().getDevice(this.id);
142137
const connections = this.viewgraph.getDataGraph().getConnections(this.id);
143138
return { id: this.id, node, connections };
144139
}
145140

146-
addConnection(adjacentId: DeviceId) {
147-
this.connections.add(adjacentId);
148-
}
149-
150-
removeConnection(id: DeviceId) {
151-
this.connections.delete(id);
152-
}
153-
154141
delete(): void {
155142
this.viewgraph.removeDevice(this.id);
156143
console.log(`Device ${this.id} deleted`);
@@ -172,16 +159,11 @@ export abstract class Device extends Container {
172159
this.parent.on("pointerup", onPointerUp);
173160
}
174161

162+
// TODO: why is this even here??
175163
connectTo(adjacentId: DeviceId): boolean {
176164
// Connects both devices with an edge.
177-
// console.log("Entered connectTo");
178-
179165
const edgeId = this.viewgraph.addEdge(this.id, adjacentId);
180166
if (edgeId) {
181-
const adjacentDevice = this.viewgraph.getDevice(adjacentId);
182-
this.addConnection(adjacentId);
183-
adjacentDevice.addConnection(this.id);
184-
185167
// Register move
186168
const move = new AddEdgeMove(this.viewgraph.getLayer(), {
187169
n1: this.id,
@@ -277,7 +259,6 @@ export abstract class Device extends Container {
277259

278260
destroy() {
279261
// Clear connections
280-
this.connections.clear();
281262
deselectElement();
282263
super.destroy();
283264
}

src/types/devices/router.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ export class Router extends NetworkDevice {
9494
}
9595
// a router changed forward datagram to destination, have to change current destination mac
9696
const dstDevice = this.viewgraph.getDeviceByIP(datagram.destinationAddress);
97+
if (!dstDevice) {
98+
console.error("Destination device not found");
99+
return null;
100+
}
97101
const path = this.viewgraph.getPathBetween(this.id, dstDevice.id);
98102
let dstMac = dstDevice.mac;
99103
if (!path) return null;

src/types/devices/switch.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export class Switch extends Device {
5050
const dstDevice = this.viewgraph.getDeviceByIP(
5151
datagram.destinationAddress,
5252
);
53+
if (!dstDevice) {
54+
console.error("Destination device not found");
55+
return null;
56+
}
5357
const path = this.viewgraph.getPathBetween(this.id, dstDevice.id);
5458
return Promise.resolve(path.length > 1 ? path[1] : null);
5559
}

src/types/edge.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ export class Edge extends Graphics {
2020
viewgraph: ViewGraph;
2121
rightbar: RightBar;
2222

23-
static generateConnectionKey(connectedNodes: EdgeEdges): string {
24-
const { n1, n2 } = connectedNodes;
25-
return [n1, n2].sort().join(",");
26-
}
27-
2823
constructor(
2924
connectedNodes: EdgeEdges,
3025
device1: Device,
@@ -101,7 +96,6 @@ export class Edge extends Graphics {
10196
// Method to show the Edge information
10297
showInfo() {
10398
const info = new StyledInfo("Edge Information");
104-
info.addField("Edge ID", Edge.generateConnectionKey(this.connectedNodes));
10599
info.addField(
106100
"Connected Devices",
107101
`${this.connectedNodes.n1} <=> ${this.connectedNodes.n2}`,
@@ -149,9 +143,9 @@ export class Edge extends Graphics {
149143
// Method to delete the edge
150144
delete() {
151145
// Remove the edge from the viewgraph and datagraph
152-
const id = Edge.generateConnectionKey(this.connectedNodes);
153-
this.viewgraph.removeEdge(id);
154-
console.log(`Edge ${id} deleted.`);
146+
const { n1, n2 } = this.connectedNodes;
147+
this.viewgraph.removeEdge(n1, n2);
148+
console.log(`Edge ${n1},${n2} deleted.`);
155149
this.destroy();
156150
}
157151

src/types/graphs/datagraph.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class DataGraph {
151151
const graphData: GraphData = [];
152152

153153
// Serialize nodes
154-
for (const [id, info] of this.deviceGraph.getVertices()) {
154+
for (const [id, info] of this.deviceGraph.getAllVertices()) {
155155
const graphNode: GraphDataNode = {
156156
...info,
157157
id,
@@ -256,7 +256,7 @@ export class DataGraph {
256256

257257
// Get all devices in the graph
258258
getDevices(): IterableIterator<[DeviceId, GraphNode]> {
259-
return this.deviceGraph.getVertices();
259+
return this.deviceGraph.getAllVertices();
260260
}
261261

262262
// Get the number of devices in the graph
@@ -302,7 +302,7 @@ export class DataGraph {
302302

303303
regenerateAllRoutingTables() {
304304
console.log("Regenerating all routing tables");
305-
for (const [id] of this.deviceGraph.getVertices()) {
305+
for (const [id] of this.deviceGraph.getAllVertices()) {
306306
this.regenerateRoutingTable(id);
307307
}
308308
}

src/types/graphs/graph.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,21 @@ export class Graph<Vertex, Edge> {
2727
return this.edges.get(id)?.get(otherId);
2828
}
2929

30+
/**
31+
* Returns the edges connected to a given vertex.
32+
*/
33+
getEdges(id: VertexId): IterableIterator<[VertexId, Edge]> | undefined {
34+
return this.edges.get(id)?.entries();
35+
}
36+
3037
getNeighbors(id: VertexId): VertexId[] | undefined {
3138
if (!this.hasVertex(id)) {
3239
return undefined;
3340
}
3441
return Array.from(this.edges.get(id).keys());
3542
}
3643

37-
getVertices(): IterableIterator<[VertexId, Vertex]> {
44+
getAllVertices(): IterableIterator<[VertexId, Vertex]> {
3845
return this.vertices.entries();
3946
}
4047

@@ -46,7 +53,7 @@ export class Graph<Vertex, Edge> {
4653
* Returns an iterator over all edges in the graph.
4754
* @returns an iterator over all edges in the graph, with no duplicates.
4855
*/
49-
getEdges(): IterableIterator<[VertexId, VertexId, Edge]> {
56+
getAllEdges(): IterableIterator<[VertexId, VertexId, Edge]> {
5057
return edgeIterator(this.edges);
5158
}
5259

@@ -84,6 +91,11 @@ export class Graph<Vertex, Edge> {
8491
this.edges.get(id).delete(otherId);
8592
this.edges.get(otherId).delete(id);
8693
}
94+
95+
clear() {
96+
this.vertices.clear();
97+
this.edges.clear();
98+
}
8799
}
88100

89101
function* edgeIterator<Edge>(

0 commit comments

Comments
 (0)