Skip to content

Commit 2b8aaa7

Browse files
authored
feat: decrease TTL on each hop (#269)
This PR makes the TTL on each packet decrease on each hop, to simulate the behavior on real networks.
1 parent de6179f commit 2b8aaa7

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/types/view-devices/vRouter.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,28 @@ export class ViewRouter extends ViewNetworkDevice {
176176

177177
addPacketToQueue(datagram: IPv4Packet) {
178178
const wasEmpty = this.packetQueue.isEmpty();
179+
datagram.timeToLive -= 1;
180+
if (datagram.timeToLive <= 0) {
181+
console.debug(`Device ${this.id} dropped packet with TTL 0`);
182+
this.dropPacket(datagram);
183+
}
179184
if (!this.packetQueue.enqueue(datagram)) {
180185
console.debug("Packet queue full, dropping packet");
181-
// dummy values
182-
const dummyMac = this.interfaces[0].mac;
183-
const frame = new EthernetFrame(dummyMac, dummyMac, datagram);
184-
dropPacket(this.viewgraph, this.id, frame);
186+
this.dropPacket(datagram);
185187
return;
186188
}
187189
if (wasEmpty) {
188190
this.startPacketProcessor();
189191
}
190192
}
191193

194+
dropPacket(datagram: IPv4Packet) {
195+
// dummy values
196+
const dummyMac = this.interfaces[0].mac;
197+
const frame = new EthernetFrame(dummyMac, dummyMac, datagram);
198+
dropPacket(this.viewgraph, this.id, frame);
199+
}
200+
192201
processPacket(ticker: Ticker) {
193202
const elapsedTime = ticker.deltaMS * this.viewgraph.getSpeed();
194203
const datagram = this.getPacketsToProcess(elapsedTime);
@@ -239,7 +248,7 @@ export class ViewRouter extends ViewNetworkDevice {
239248

240249
routePacket(datagram: IPv4Packet): number {
241250
console.debug(
242-
`Device ${this.id} va a rutear el datagram con origen ${datagram.sourceAddress.toString()} y destino ${datagram.destinationAddress.toString()}`,
251+
`Device ${this.id} will route datagram of origin ${datagram.sourceAddress.toString()} and destination ${datagram.destinationAddress.toString()}`,
243252
);
244253
const device = this.viewgraph.getDataGraph().getDevice(this.id);
245254
if (!device || !(device instanceof DataRouter)) {

0 commit comments

Comments
 (0)