Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ export class GlobalContext {
load(
datagraph: DataGraph,
layer: Layer = Layer.Link,
speedMultiplier: SpeedMultiplier = new SpeedMultiplier(1),
speedMultiplier?: SpeedMultiplier,
) {
this.setNetwork(datagraph, layer);
this.viewport.restorePosition();
this.setSpeedMultiplier(speedMultiplier);
if (speedMultiplier) this.setSpeedMultiplier(speedMultiplier);
this.setupAutoSave();
this.saveToLocalStorage();
urManager.reset();
Expand Down
16 changes: 8 additions & 8 deletions src/graphics/renderables/device_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import { LabeledProgressBar } from "../components/labeled_progress_bar";
import { ArpTable } from "./arp_table";
import { Layer } from "../../types/layer";
import { DataNetworkDevice, DataSwitch } from "../../types/data-devices";
import { SwitchingTable } from "./switching_table";
import { ForwardingTable } from "./forwarding_table";
import { getRoutingTable } from "../../types/network-modules/tables/routing_table";
import { ToggleInfo } from "../components/toggle_info";
import { getArpTable } from "../../types/network-modules/tables/arp_table";
import { getSwitchingTable } from "../../types/network-modules/tables/switching_table";
import { getForwardingTable } from "../../types/network-modules/tables/forwarding_table";

export class DeviceInfo extends BaseInfo {
readonly device: ViewDevice;
Expand Down Expand Up @@ -218,28 +218,28 @@ export class DeviceInfo extends BaseInfo {
}
}

addSwitchingTable(viewgraph: ViewGraph, deviceId: number): void {
addForwardingTable(viewgraph: ViewGraph, deviceId: number): void {
const dataGraph = viewgraph.getDataGraph();
const entries = getSwitchingTable(dataGraph, deviceId);
const entries = getForwardingTable(dataGraph, deviceId);

const rows = entries.map((entry) => ({
values: [entry.mac, entry.port.toString()],
edited: entry.edited ?? false,
}));

const switchingTable = new SwitchingTable({
const forwardingTable = new ForwardingTable({
rows,
viewgraph,
deviceId,
});

this.inputFields.push(switchingTable.toHTML());
this.inputFields.push(forwardingTable.toHTML());

const dataDevice = viewgraph.getDataGraph().getDevice(deviceId);

if (dataDevice instanceof DataSwitch) {
dataDevice.setSwitchingTableChangeListener(() => {
switchingTable.refreshTable();
dataDevice.setForwardingTableChangeListener(() => {
forwardingTable.refreshTable();
});
} else {
console.warn(`Device with ID ${deviceId} is not a DataNetworkDevice.`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import { DeviceId } from "../../types/graphs/datagraph";
import { ViewGraph } from "../../types/graphs/viewgraph";
import {
clearSwitchingTable,
getSwitchingTable,
removeSwitchingTableEntry,
saveSwitchingTableManualChange,
} from "../../types/network-modules/tables/switching_table";
clearForwardingTable,
getForwardingTable,
removeForwardingTableEntry,
saveForwardingTableManualChange,
} from "../../types/network-modules/tables/forwarding_table";
import { ALERT_MESSAGES } from "../../utils/constants/alert_constants";
import { CSS_CLASSES } from "../../utils/constants/css_constants";
import {
InvalidMacError,
InvalidPortError,
SWITCHING_TABLE_CONSTANTS,
FORWARDING_TABLE_CONSTANTS,
} from "../../utils/constants/table_constants";
import { TOOLTIP_KEYS } from "../../utils/constants/tooltips_constants";
import { Button } from "../basic_components/button";
import { Table, TableRow } from "../basic_components/table";
import { ToggleButton } from "../basic_components/toggle_button";
import { showError, showSuccess } from "./alert_manager";

export interface SwitchingTableProps {
export interface ForwardingTableProps {
rows: TableRow[]; // Rows for the table
viewgraph: ViewGraph; // ViewGraph instance for callbacks
deviceId: DeviceId; // Device ID for callbacks
}

export class SwitchingTable {
export class ForwardingTable {
private container: HTMLElement;
private table: Table;
private toggleButton: ToggleButton;

constructor(private props: SwitchingTableProps) {
constructor(private props: ForwardingTableProps) {
this.container = document.createElement("div");

const { onEdit, onRegenerate, onDelete, onAddRow } =
this.setSwitchingTableCallbacks();
this.setForwardingTableCallbacks();

// Create the regenerate button
const regenerateButton = this.createRegenerateButton(onRegenerate);

const headers = {
[TOOLTIP_KEYS.MAC_ADDRESS]: TOOLTIP_KEYS.MAC_ADDRESS,
[TOOLTIP_KEYS.PORT]: TOOLTIP_KEYS.PORT,
[TOOLTIP_KEYS.INTERFACE]: TOOLTIP_KEYS.INTERFACE,
[TOOLTIP_KEYS.REGENERATE]: regenerateButton, // Add the regenerate button to the header
};

Expand All @@ -57,13 +57,13 @@ export class SwitchingTable {
});

this.toggleButton = new ToggleButton({
text: TOOLTIP_KEYS.SWITCHING_TABLE,
text: TOOLTIP_KEYS.FORWARDING_TABLE,
className: CSS_CLASSES.RIGHT_BAR_TOGGLE_BUTTON,
onToggle: (isToggled) => {
const tableElement = this.table.toHTML();
tableElement.style.display = isToggled ? "block" : "none";
},
tooltip: TOOLTIP_KEYS.SWITCHING_TABLE,
tooltip: TOOLTIP_KEYS.FORWARDING_TABLE,
});

// Initially hide the table
Expand Down Expand Up @@ -101,24 +101,24 @@ export class SwitchingTable {
private OnRegenerate(): void {
const dataGraph = this.props.viewgraph.getDataGraph();

// clear the current switching table
clearSwitchingTable(dataGraph, this.props.deviceId);
// clear the current forwarding table
clearForwardingTable(dataGraph, this.props.deviceId);

this.updateRows([]);

showSuccess(ALERT_MESSAGES.SWITCHING_TABLE_REGENERATED);
showSuccess(ALERT_MESSAGES.FORWARDING_TABLE_REGENERATED);
}

private setSwitchingTableCallbacks() {
private setForwardingTableCallbacks() {
const dataGraph = this.props.viewgraph.getDataGraph();
const onDelete = (mac: string) => {
removeSwitchingTableEntry(dataGraph, this.props.deviceId, mac);
showSuccess(ALERT_MESSAGES.SWITCHING_TABLE_ENTRY_DELETED);
removeForwardingTableEntry(dataGraph, this.props.deviceId, mac);
showSuccess(ALERT_MESSAGES.FORWARDING_TABLE_ENTRY_DELETED);
return true;
};

const onRegenerate = () => {
console.log("Regenerating Switching Table...");
console.log("Regenerating Forwarding Table...");
this.OnRegenerate();
};

Expand All @@ -134,7 +134,7 @@ export class SwitchingTable {
}

try {
const changed = saveSwitchingTableManualChange(
const changed = saveForwardingTableManualChange(
dataGraph,
this.props.deviceId,
mac,
Expand All @@ -145,7 +145,7 @@ export class SwitchingTable {
return false;
}
this.refreshTable();
showSuccess(ALERT_MESSAGES.SWITCHING_TABLE_ENTRY_EDITED);
showSuccess(ALERT_MESSAGES.FORWARDING_TABLE_ENTRY_EDITED);
return true;
} catch (e) {
if (e instanceof InvalidMacError) {
Expand All @@ -163,18 +163,18 @@ export class SwitchingTable {
const [mac, portStr] = values;

try {
const changed = saveSwitchingTableManualChange(
const changed = saveForwardingTableManualChange(
this.props.viewgraph.getDataGraph(),
this.props.deviceId,
mac.trim(),
SWITCHING_TABLE_CONSTANTS.PORT_COL_INDEX,
FORWARDING_TABLE_CONSTANTS.PORT_COL_INDEX,
portStr.trim(),
);
if (!changed) {
return false;
}
this.refreshTable();
showSuccess(ALERT_MESSAGES.SWITCHING_TABLE_ENTRY_ADDED);
showSuccess(ALERT_MESSAGES.FORWARDING_TABLE_ENTRY_ADDED);
return true;
} catch (e) {
if (e instanceof InvalidMacError) {
Expand All @@ -193,7 +193,7 @@ export class SwitchingTable {

refreshTable(): void {
const dataGraph = this.props.viewgraph.getDataGraph();
const updatedEntries = getSwitchingTable(dataGraph, this.props.deviceId);
const updatedEntries = getForwardingTable(dataGraph, this.props.deviceId);

const updatedRows = updatedEntries.map((entry) => ({
values: [entry.mac.toString(), entry.port.toString()],
Expand Down
24 changes: 12 additions & 12 deletions src/types/data-devices/dSwitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { EthernetFrame, MacAddress } from "../../packets/ethernet";
import { DataGraph, DeviceId, SwitchDataNode } from "../graphs/datagraph";
import { EntryData, Table } from "../network-modules/tables/table";

export interface SwitchingEntry extends EntryData {
export interface ForwardingEntry extends EntryData {
mac: string;
port: number;
}

export class DataSwitch extends DataDevice {
switchingTable: Table<SwitchingEntry>;
private switchingTableChangeListener: (() => void) | null = null;
forwardingTable: Table<ForwardingEntry>;
private forwardingTableChangeListener: (() => void) | null = null;

constructor(graphData: SwitchDataNode, datagraph: DataGraph) {
super(graphData, datagraph);
this.switchingTable = new Table<SwitchingEntry>(
this.forwardingTable = new Table<ForwardingEntry>(
"mac",
graphData.switchingTable.map(([mac, port, edited, deleted]) => ({
graphData.forwardingTable.map(([mac, port, edited, deleted]) => ({
mac,
port,
edited,
Expand All @@ -30,24 +30,24 @@ export class DataSwitch extends DataDevice {
return DeviceType.Switch;
}

updateSwitchingTable(mac: MacAddress, iface: number): void {
this.switchingTable.add({
updateForwardingTable(mac: MacAddress, iface: number): void {
this.forwardingTable.add({
mac: mac.toString(),
port: iface,
});
if (this.switchingTableChangeListener) {
this.switchingTableChangeListener();
if (this.forwardingTableChangeListener) {
this.forwardingTableChangeListener();
}
}

setSwitchingTableChangeListener(listener: () => void): void {
this.switchingTableChangeListener = listener;
setForwardingTableChangeListener(listener: () => void): void {
this.forwardingTableChangeListener = listener;
}

getDataNode(): SwitchDataNode {
return {
...super.getDataNode(),
switchingTable: this.switchingTable.serialize(
forwardingTable: this.forwardingTable.serialize(
(entry) =>
[
entry.mac,
Expand Down
4 changes: 2 additions & 2 deletions src/types/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
showTooltip,
} from "../graphics/renderables/canvas_tooltip_manager";
import { updateRoutingTableIface } from "./network-modules/tables/routing_table";
import { updateSwitchingTablePort } from "./network-modules/tables/switching_table";
import { updateForwardingTablePort } from "./network-modules/tables/forwarding_table";

export class Edge extends Graphics {
private _data: DataEdge;
Expand Down Expand Up @@ -268,7 +268,7 @@ export class Edge extends Graphics {
newIface,
);

updateSwitchingTablePort(
updateForwardingTablePort(
this.viewgraph.getDataGraph(),
deviceId,
oldIface,
Expand Down
2 changes: 1 addition & 1 deletion src/types/graphs/datagraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface NetworkInterfaceData {
}

export interface SwitchDataNode extends CommonDataNode {
switchingTable: [string, number, boolean, boolean][]; // [mac, port, edited, deleted]
forwardingTable: [string, number, boolean, boolean][]; // [mac, port, edited, deleted]
type: DeviceType.Switch;
}

Expand Down
Loading