|
| 1 | +import { ProgramInfo } from "../graphics/renderables/device_info"; |
| 2 | +import { DeviceId } from "../types/graphs/datagraph"; |
| 3 | +import { ViewGraph } from "../types/graphs/viewgraph"; |
| 4 | +import { ProgramBase } from "./program_base"; |
| 5 | +import { ViewNetworkDevice } from "../types/view-devices/vNetworkDevice"; |
| 6 | +import { EthernetFrame } from "../packets/ethernet"; |
| 7 | +import { sendViewPacket } from "../types/packet"; |
| 8 | +import { EmptyPayload, IPv4Packet } from "../packets/ip"; |
| 9 | + |
| 10 | +// Dummy program to test the link layer packets forwarding, like ARP Request/Response |
| 11 | +export class DummyLinkProgram extends ProgramBase { |
| 12 | + static readonly PROGRAM_NAME = "Dummy link program"; |
| 13 | + |
| 14 | + protected dstId: DeviceId; |
| 15 | + |
| 16 | + constructor(viewgraph: ViewGraph, srcId: DeviceId, inputs: string[]) { |
| 17 | + super(viewgraph, srcId, inputs); |
| 18 | + this._parseInputs(inputs); |
| 19 | + } |
| 20 | + |
| 21 | + protected _parseInputs(inputs: string[]): void { |
| 22 | + if (inputs.length !== 1) { |
| 23 | + console.error( |
| 24 | + "DummyLinkProgram requires 1 input. " + inputs.length + " were given.", |
| 25 | + ); |
| 26 | + return; |
| 27 | + } |
| 28 | + this.dstId = parseInt(inputs[0]); |
| 29 | + } |
| 30 | + |
| 31 | + protected _run() { |
| 32 | + this.sendSinglePacket(); |
| 33 | + this.signalStop(); |
| 34 | + } |
| 35 | + |
| 36 | + private sendSinglePacket() { |
| 37 | + const dstDevice = this.viewgraph.getDevice(this.dstId); |
| 38 | + const srcDevice = this.viewgraph.getDevice(this.srcId); |
| 39 | + if (!dstDevice) { |
| 40 | + console.error("Destination device not found"); |
| 41 | + return; |
| 42 | + } |
| 43 | + if ( |
| 44 | + !(srcDevice instanceof ViewNetworkDevice) || |
| 45 | + !(dstDevice instanceof ViewNetworkDevice) |
| 46 | + ) { |
| 47 | + console.log( |
| 48 | + "At least one device between source and destination is not a network device", |
| 49 | + ); |
| 50 | + return; |
| 51 | + } |
| 52 | + const path = this.viewgraph.getPathBetween(this.srcId, this.dstId); |
| 53 | + let dstMac = dstDevice.mac; |
| 54 | + if (!path) return; |
| 55 | + for (const id of path.slice(1)) { |
| 56 | + const device = this.viewgraph.getDevice(id); |
| 57 | + // if there’s a router in the middle, first send frame to router mac |
| 58 | + if (device instanceof ViewNetworkDevice) { |
| 59 | + dstMac = device.mac; |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + const payload = new IPv4Packet( |
| 64 | + srcDevice.ip, |
| 65 | + dstDevice.ip, |
| 66 | + new EmptyPayload(), |
| 67 | + ); |
| 68 | + const ethernetFrame = new EthernetFrame(srcDevice.mac, dstMac, payload); |
| 69 | + sendViewPacket(this.viewgraph, this.srcId, ethernetFrame); |
| 70 | + } |
| 71 | + |
| 72 | + protected _stop() { |
| 73 | + // nothing to do |
| 74 | + } |
| 75 | + |
| 76 | + static getProgramInfo(viewgraph: ViewGraph, srcId: DeviceId): ProgramInfo { |
| 77 | + const programInfo = new ProgramInfo(this.PROGRAM_NAME); |
| 78 | + programInfo.withDestinationDropdown(viewgraph, srcId); |
| 79 | + return programInfo; |
| 80 | + } |
| 81 | + |
| 82 | + static getProgramName(): string { |
| 83 | + return this.PROGRAM_NAME; |
| 84 | + } |
| 85 | + |
| 86 | + static getProgramDescription(): string { |
| 87 | + return "Sends a dummy packet every X ms"; |
| 88 | + } |
| 89 | + |
| 90 | + static getProgramInputs(): string[] { |
| 91 | + return ["Delay in ms"]; |
| 92 | + } |
| 93 | + |
| 94 | + static getProgramOutputs(): string[] { |
| 95 | + return ["Dummy packet"]; |
| 96 | + } |
| 97 | +} |
0 commit comments