Skip to content

Commit

Permalink
feat: add basic Switch implementation (#116)
Browse files Browse the repository at this point in the history
Added the initial implementation of the Switch device with basic
functionality. Further development is required to implement
switch-specific features.
  • Loading branch information
pgallino authored Feb 9, 2025
1 parent e6f533f commit 056c37a
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/assets/switch.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/graphics/left_bar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import RouterSvg from "../assets/router.svg";
import ComputerSvg from "../assets/pc.svg";
import SwitchSvg from "../assets/switch.svg";
import { addDevice } from "../types/viewportManager";
import { GlobalContext } from "../context";
import { DeviceType } from "../types/devices/device";
Expand Down Expand Up @@ -46,6 +47,11 @@ export class LeftBar {
this.addButton(ComputerSvg, addHost, "Add Host");
}

private addSwitchButton() {
const addSwitch = () => addDevice(this.ctx, DeviceType.Switch);
this.addButton(SwitchSvg, addSwitch, "Add Switch");
}

setButtonsByLayer(layerName: string) {
this.clear();

Expand All @@ -56,5 +62,9 @@ export class LeftBar {
if (layer <= Layer.Network) {
this.addRouterButton();
}

if (layer <= Layer.Link) {
this.addSwitchButton();
}
}
}
2 changes: 2 additions & 0 deletions src/graphics/renderables/device_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,7 @@ function getTypeName(device: Device): string {
return "Router";
case DeviceType.Host:
return "Host";
case DeviceType.Switch:
return "Switch";
}
}
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { GlobalContext } from "./context";
// Doing this includes the file in the build
import "./styles";
import RouterSvg from "./assets/router.svg";
import SwitchSvg from "./assets/switch.svg";
import ComputerSvg from "./assets/pc.svg";
import PlaySvg from "./assets/play-icon.svg";
import PauseSvg from "./assets/pause-icon.svg";
Expand All @@ -26,7 +27,15 @@ import RedoSvg from "./assets/right-curve-arrow.svg";
import { layerToName } from "./types/devices/layer";
import { SpeedMultiplier } from "./types/devices/speedMultiplier";

const assets = [RouterSvg, ComputerSvg, PlaySvg, PauseSvg, UndoSvg, RedoSvg];
const assets = [
RouterSvg,
ComputerSvg,
PlaySvg,
PauseSvg,
UndoSvg,
RedoSvg,
SwitchSvg,
];

async function loadAssets(otherPromises: Promise<void>[]) {
await Promise.all([...otherPromises, ...assets.map((as) => Assets.load(as))]);
Expand Down
3 changes: 3 additions & 0 deletions src/types/devices/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const DEVICE_SIZE = 20;
export enum DeviceType {
Router = 0,
Host = 1,
Switch = 2,
}

export function layerFromType(type: DeviceType) {
Expand All @@ -40,6 +41,8 @@ export function layerFromType(type: DeviceType) {
return Layer.Network;
case DeviceType.Host:
return Layer.App;
case DeviceType.Switch:
return Layer.Link;
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/types/devices/switch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Device, DeviceType, Layer } from "./device";
import { ViewGraph } from "../graphs/viewgraph";
import SwitchImage from "../../assets/switch.svg";
import { Position } from "../common";
import { DeviceInfo, RightBar } from "../../graphics/right_bar";
import { DeviceId } from "../graphs/datagraph";
import { Packet } from "../packet";

export class Switch extends Device {
constructor(id: DeviceId, viewgraph: ViewGraph, position: Position) {
super(id, SwitchImage, viewgraph, position, null, null);
}

showInfo(): void {
const info = new DeviceInfo(this);
RightBar.getInstance().renderInfo(info);
}

getLayer(): Layer {
return Layer.Link;
}

getType(): DeviceType {
return DeviceType.Switch;
}

receivePacket(packet: Packet): DeviceId | null {
console.log(packet); // lint
throw new Error("Method not implemented.");
}
}
3 changes: 3 additions & 0 deletions src/types/devices/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ViewGraph } from "../graphs/viewgraph";
import { Device, DeviceType } from "./device";
import { Host } from "./host";
import { Router } from "./router";
import { Switch } from "./switch";

export interface CreateDevice {
id: DeviceId;
Expand All @@ -24,5 +25,7 @@ export function createDevice(
return new Router(deviceInfo.id, viewgraph, position, ip, mask);
case DeviceType.Host:
return new Host(deviceInfo.id, viewgraph, position, ip, mask);
case DeviceType.Switch:
return new Switch(deviceInfo.id, viewgraph, position);
}
}
26 changes: 24 additions & 2 deletions src/types/graphs/datagraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ interface HostGraphNode extends CommonGraphNode {
runningPrograms: RunningProgram[];
}

interface SwitchGraphNode extends CommonGraphNode {
type: DeviceType.Switch;
}

// Typescript type guard
export function isRouter(node: GraphNode): node is RouterGraphNode {
return node.type === DeviceType.Router;
Expand All @@ -39,9 +43,21 @@ export function isHost(node: GraphNode): node is HostGraphNode {
return node.type === DeviceType.Host;
}

export type GraphNode = CommonGraphNode | RouterGraphNode | HostGraphNode;
export function isSwitch(node: GraphNode): node is SwitchGraphNode {
return node.type === DeviceType.Switch;
}

export type GraphNode =
| CommonGraphNode
| RouterGraphNode
| HostGraphNode
| SwitchGraphNode;

export type GraphDataNode = CommonDataNode | RouterDataNode | HostDataNode;
export type GraphDataNode =
| CommonDataNode
| RouterDataNode
| HostDataNode
| SwitchDataNode;

interface CommonDataNode {
id: DeviceId;
Expand All @@ -63,6 +79,10 @@ interface HostDataNode extends CommonDataNode {
runningPrograms: RunningProgram[];
}

interface SwitchDataNode extends CommonDataNode {
type: DeviceType.Switch;
}

export type GraphData = GraphDataNode[];

export interface NewDevice {
Expand Down Expand Up @@ -129,6 +149,8 @@ export class DataGraph {
graphData.push({ ...graphNode, routingTable: info.routingTable });
} else if (isHost(info)) {
graphData.push({ ...graphNode, runningPrograms: info.runningPrograms });
} else if (isSwitch(info)) {
graphData.push({ ...graphNode });
}
});
return graphData;
Expand Down

0 comments on commit 056c37a

Please sign in to comment.