Skip to content

Commit 056c37a

Browse files
authored
feat: add basic Switch implementation (#116)
Added the initial implementation of the Switch device with basic functionality. Further development is required to implement switch-specific features.
1 parent e6f533f commit 056c37a

File tree

8 files changed

+95
-3
lines changed

8 files changed

+95
-3
lines changed

src/assets/switch.svg

Lines changed: 12 additions & 0 deletions
Loading

src/graphics/left_bar.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import RouterSvg from "../assets/router.svg";
22
import ComputerSvg from "../assets/pc.svg";
3+
import SwitchSvg from "../assets/switch.svg";
34
import { addDevice } from "../types/viewportManager";
45
import { GlobalContext } from "../context";
56
import { DeviceType } from "../types/devices/device";
@@ -46,6 +47,11 @@ export class LeftBar {
4647
this.addButton(ComputerSvg, addHost, "Add Host");
4748
}
4849

50+
private addSwitchButton() {
51+
const addSwitch = () => addDevice(this.ctx, DeviceType.Switch);
52+
this.addButton(SwitchSvg, addSwitch, "Add Switch");
53+
}
54+
4955
setButtonsByLayer(layerName: string) {
5056
this.clear();
5157

@@ -56,5 +62,9 @@ export class LeftBar {
5662
if (layer <= Layer.Network) {
5763
this.addRouterButton();
5864
}
65+
66+
if (layer <= Layer.Link) {
67+
this.addSwitchButton();
68+
}
5969
}
6070
}

src/graphics/renderables/device_info.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,7 @@ function getTypeName(device: Device): string {
116116
return "Router";
117117
case DeviceType.Host:
118118
return "Host";
119+
case DeviceType.Switch:
120+
return "Switch";
119121
}
120122
}

src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { GlobalContext } from "./context";
1818
// Doing this includes the file in the build
1919
import "./styles";
2020
import RouterSvg from "./assets/router.svg";
21+
import SwitchSvg from "./assets/switch.svg";
2122
import ComputerSvg from "./assets/pc.svg";
2223
import PlaySvg from "./assets/play-icon.svg";
2324
import PauseSvg from "./assets/pause-icon.svg";
@@ -26,7 +27,15 @@ import RedoSvg from "./assets/right-curve-arrow.svg";
2627
import { layerToName } from "./types/devices/layer";
2728
import { SpeedMultiplier } from "./types/devices/speedMultiplier";
2829

29-
const assets = [RouterSvg, ComputerSvg, PlaySvg, PauseSvg, UndoSvg, RedoSvg];
30+
const assets = [
31+
RouterSvg,
32+
ComputerSvg,
33+
PlaySvg,
34+
PauseSvg,
35+
UndoSvg,
36+
RedoSvg,
37+
SwitchSvg,
38+
];
3039

3140
async function loadAssets(otherPromises: Promise<void>[]) {
3241
await Promise.all([...otherPromises, ...assets.map((as) => Assets.load(as))]);

src/types/devices/device.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const DEVICE_SIZE = 20;
3232
export enum DeviceType {
3333
Router = 0,
3434
Host = 1,
35+
Switch = 2,
3536
}
3637

3738
export function layerFromType(type: DeviceType) {
@@ -40,6 +41,8 @@ export function layerFromType(type: DeviceType) {
4041
return Layer.Network;
4142
case DeviceType.Host:
4243
return Layer.App;
44+
case DeviceType.Switch:
45+
return Layer.Link;
4346
}
4447
}
4548

src/types/devices/switch.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Device, DeviceType, Layer } from "./device";
2+
import { ViewGraph } from "../graphs/viewgraph";
3+
import SwitchImage from "../../assets/switch.svg";
4+
import { Position } from "../common";
5+
import { DeviceInfo, RightBar } from "../../graphics/right_bar";
6+
import { DeviceId } from "../graphs/datagraph";
7+
import { Packet } from "../packet";
8+
9+
export class Switch extends Device {
10+
constructor(id: DeviceId, viewgraph: ViewGraph, position: Position) {
11+
super(id, SwitchImage, viewgraph, position, null, null);
12+
}
13+
14+
showInfo(): void {
15+
const info = new DeviceInfo(this);
16+
RightBar.getInstance().renderInfo(info);
17+
}
18+
19+
getLayer(): Layer {
20+
return Layer.Link;
21+
}
22+
23+
getType(): DeviceType {
24+
return DeviceType.Switch;
25+
}
26+
27+
receivePacket(packet: Packet): DeviceId | null {
28+
console.log(packet); // lint
29+
throw new Error("Method not implemented.");
30+
}
31+
}

src/types/devices/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ViewGraph } from "../graphs/viewgraph";
55
import { Device, DeviceType } from "./device";
66
import { Host } from "./host";
77
import { Router } from "./router";
8+
import { Switch } from "./switch";
89

910
export interface CreateDevice {
1011
id: DeviceId;
@@ -24,5 +25,7 @@ export function createDevice(
2425
return new Router(deviceInfo.id, viewgraph, position, ip, mask);
2526
case DeviceType.Host:
2627
return new Host(deviceInfo.id, viewgraph, position, ip, mask);
28+
case DeviceType.Switch:
29+
return new Switch(deviceInfo.id, viewgraph, position);
2730
}
2831
}

src/types/graphs/datagraph.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ interface HostGraphNode extends CommonGraphNode {
3030
runningPrograms: RunningProgram[];
3131
}
3232

33+
interface SwitchGraphNode extends CommonGraphNode {
34+
type: DeviceType.Switch;
35+
}
36+
3337
// Typescript type guard
3438
export function isRouter(node: GraphNode): node is RouterGraphNode {
3539
return node.type === DeviceType.Router;
@@ -39,9 +43,21 @@ export function isHost(node: GraphNode): node is HostGraphNode {
3943
return node.type === DeviceType.Host;
4044
}
4145

42-
export type GraphNode = CommonGraphNode | RouterGraphNode | HostGraphNode;
46+
export function isSwitch(node: GraphNode): node is SwitchGraphNode {
47+
return node.type === DeviceType.Switch;
48+
}
49+
50+
export type GraphNode =
51+
| CommonGraphNode
52+
| RouterGraphNode
53+
| HostGraphNode
54+
| SwitchGraphNode;
4355

44-
export type GraphDataNode = CommonDataNode | RouterDataNode | HostDataNode;
56+
export type GraphDataNode =
57+
| CommonDataNode
58+
| RouterDataNode
59+
| HostDataNode
60+
| SwitchDataNode;
4561

4662
interface CommonDataNode {
4763
id: DeviceId;
@@ -63,6 +79,10 @@ interface HostDataNode extends CommonDataNode {
6379
runningPrograms: RunningProgram[];
6480
}
6581

82+
interface SwitchDataNode extends CommonDataNode {
83+
type: DeviceType.Switch;
84+
}
85+
6686
export type GraphData = GraphDataNode[];
6787

6888
export interface NewDevice {
@@ -129,6 +149,8 @@ export class DataGraph {
129149
graphData.push({ ...graphNode, routingTable: info.routingTable });
130150
} else if (isHost(info)) {
131151
graphData.push({ ...graphNode, runningPrograms: info.runningPrograms });
152+
} else if (isSwitch(info)) {
153+
graphData.push({ ...graphNode });
132154
}
133155
});
134156
return graphData;

0 commit comments

Comments
 (0)