Skip to content

Commit

Permalink
minor changes v4
Browse files Browse the repository at this point in the history
  • Loading branch information
NybyDK committed May 14, 2024
1 parent bbf5d80 commit 9e2230a
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 33 deletions.
2 changes: 2 additions & 0 deletions src/lib/classes/MPLS/CE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default class CE extends Router {
};

updateAddress = (oldAddress: string, newAddress: string) => {
if (oldAddress === newAddress) return;

const oldValue = this.firstHop.get(oldAddress);
if (oldValue === undefined) {
alert(`Unable to update old first hop Address '${oldAddress}' to '${newAddress}'.`);
Expand Down
7 changes: 7 additions & 0 deletions src/lib/classes/MPLS/Packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export default class Packet {
);
}

validateFirstHop() {
return (
network.doesLinkExist({ source: this.source, target: this.nextHop }) ||
network.doesLinkExist({ source: this.nextHop, target: this.source })
);
}

setFallbackNextHop() {
const nextHop = this.fallbackRoute?.shift();

Expand Down
8 changes: 0 additions & 8 deletions src/lib/classes/NetworkStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,6 @@ export class NetworkStore implements Writable<NetworkState> {
return this.CEMap.get(address);
}

getSureCERouter(address: string): CE {
const router = this.getCERouter(address);

if (!router) throw new Error(`Router with address ${address} not found`);

return router;
}

addPacket(source: CE, destination: CE) {
if (this._packets.length >= this.maxPackets) return;

Expand Down
9 changes: 8 additions & 1 deletion src/lib/components/ControlPanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
callback: () => {
for (const source of CERouters) {
for (const [destination] of source.firstHop) {
network.addPacket(source, network.getSureCERouter(destination));
const destinationRouter = network.getCERouter(destination);
if (destinationRouter) network.addPacket(source, destinationRouter);
}
}
},
Expand Down Expand Up @@ -76,6 +77,7 @@
</div>
<div id="control-panel" style={`width: ${panelWidth}px; transition: ${transition};`}>
<div id="control-panel-content">
<h1 id="simulation-panel-title">Simulation Panel</h1>
<p id="packet-controls-title">Packet Controls</p>
<p id="packet-controls-count">Count: {$network.packets.length}</p>
<div id="control-panel-buttons">
Expand Down Expand Up @@ -116,6 +118,11 @@
background-color: black;
}
#simulation-panel-title {
text-align: center;
padding-top: 20px;
}
#control-panel {
display: flex;
flex-direction: column;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/HelpButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<br />
<p>Pressing Space will pause/resume the packet animations.</p>
<br />
<p>Pressing Tab will open the packet control panel.</p>
<p>Pressing Tab will open the simulation panel.</p>
<br />
<p>Hold Shift, then click and drag between routers to create links</p>
<br />
Expand Down
22 changes: 12 additions & 10 deletions src/lib/components/LSPElement.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@
import ToolboxButton from "$lib/components/ToolboxButton.svelte";
export let source: CE;
export let destination: CE;
export let destination: CE | undefined;
function createPacket() {
network.addPacket(source, destination);
if (destination) network.addPacket(source, destination);
}
function deleteDestination() {
if (!confirm("Are you sure you want to delete this LSP?")) return;
if (!confirm("Are you sure you want to delete this LSP?") || !destination) return;
source.deleteEntry(destination.address);
network.notify();
}
</script>

<div>
<p>{source.address}</p>
<p>→</p>
<p>{destination.address}</p>
<ToolboxButton text={"Play"} callback={createPacket} />
<ToolboxButton text={"Delete"} callback={deleteDestination} />
</div>
{#if destination}
<div>
<p>{source.address}</p>
<p>→</p>
<p>{destination.address}</p>
<ToolboxButton text={"Play"} callback={createPacket} />
<ToolboxButton text={"Delete"} callback={deleteDestination} />
</div>
{/if}

<style>
div {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/LSPList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div>
{#each CERouters as source}
{#each [...source.firstHop] as [destination]}
<LSPElement {source} destination={network.getSureCERouter(destination)} />
<LSPElement {source} destination={network.getCERouter(destination)} />
{/each}
{/each}
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/lib/components/Packet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@
animateToNextHop();
}
requestAnimationFrame(animateToNextHop);
if (packet.validateFirstHop()) {
requestAnimationFrame(animateToNextHop);
} else {
packet.drop("Invalid first hop");
}
</script>

<circle class:labeled={packet.label !== -1} bind:this={packetElement} r="5" />
Expand Down
6 changes: 0 additions & 6 deletions src/lib/components/RouterSettings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@
<Dialog
bind:dialog
on:close={() => {
// TODO: Temporary fix to not crash app (ISSUE #125) - remove when fixed - it's because LSPList.svelte using getSureCERouter
if (router instanceof CE) {
for (const key of router.firstHop.keys()) {
if (key === "") router.firstHop.delete(key);
}
}
network.notify();
}}
>
Expand Down
1 change: 1 addition & 0 deletions src/lib/components/RouterTables/Destination.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
if (!destinationRouter) {
alert("Destination router not found.");
event.target.value = destination;
return;
}
Expand Down
10 changes: 6 additions & 4 deletions src/lib/components/ViewBox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
}
function handleKeyDown(event: KeyboardEvent) {
for (const dialog of document.querySelectorAll("dialog")) {
if (dialog.hasAttribute("open")) return;
}
switch (event.code) {
case "Escape":
$editorState.placing = null;
Expand Down Expand Up @@ -127,9 +131,7 @@
}
function buttonPreviewRouter(type: "CE" | "LSR" | "LER") {
for (const dialog of document.querySelectorAll("dialog")) {
if (dialog.hasAttribute("open")) return;
}
if ($locked) return;
$editorState.placing = type;
}
Expand All @@ -138,7 +140,7 @@
event.preventDefault();
if (interactionState === InteractionState.ADDING_ROUTERS) {
if (interactionState === InteractionState.ADDING_ROUTERS && !$locked) {
switch ($editorState.placing) {
case "CE":
network.createCE(scaledX(event.clientX), scaledY(event.clientY));
Expand Down
3 changes: 2 additions & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
background-color: #222222;
}
:global(p) {
:global(p),
:global(h1) {
color: white;
}
Expand Down

0 comments on commit 9e2230a

Please sign in to comment.