Skip to content

Commit cf7c2ab

Browse files
feat: configurable ping delay for EchoServer (#123)
Closes #90 How it looks now: ![image](https://github.com/user-attachments/assets/86cf779b-bef5-4260-94c1-ce7902a38d35) The input is a dropdown, but we could move to a slider or text input in the future. --------- Co-authored-by: Pedro Gallino <[email protected]>
1 parent 8e1de32 commit cf7c2ab

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

src/programs/echo_sender.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,31 @@ function adjacentDevices(viewgraph: ViewGraph, srcId: DeviceId) {
1515
return adjacentDevices;
1616
}
1717

18-
export abstract class EchoSender extends ProgramBase {
18+
export class SingleEcho extends ProgramBase {
19+
static readonly PROGRAM_NAME = "Send ICMP echo";
20+
1921
protected dstId: DeviceId;
2022

2123
protected _parseInputs(inputs: string[]): void {
2224
if (inputs.length !== 1) {
2325
console.error(
24-
"Program requires 1 input. " + inputs.length + " were given.",
26+
"SingleEcho requires 1 input. " + inputs.length + " were given.",
2527
);
2628
return;
2729
}
2830
this.dstId = parseInt(inputs[0]);
2931
}
3032

31-
protected sendSingleEcho() {
33+
protected _run() {
34+
this.sendSingleEcho();
35+
this.signalStop();
36+
}
37+
38+
protected _stop() {
39+
// Nothing to do
40+
}
41+
42+
private sendSingleEcho() {
3243
const dstDevice = this.viewgraph.getDevice(this.dstId);
3344
const srcDevice = this.viewgraph.getDevice(this.srcId);
3445
if (!dstDevice) {
@@ -39,19 +50,6 @@ export abstract class EchoSender extends ProgramBase {
3950
const ipPacket = new IPv4Packet(srcDevice.ip, dstDevice.ip, echoRequest);
4051
sendRawPacket(this.viewgraph, this.srcId, ipPacket);
4152
}
42-
}
43-
44-
export class SingleEcho extends EchoSender {
45-
static readonly PROGRAM_NAME = "Send ICMP echo";
46-
47-
protected _run() {
48-
this.sendSingleEcho();
49-
this.signalStop();
50-
}
51-
52-
protected _stop() {
53-
// Nothing to do
54-
}
5553

5654
static getProgramInfo(viewgraph: ViewGraph, srcId: DeviceId): ProgramInfo {
5755
const programInfo = new ProgramInfo(this.PROGRAM_NAME);
@@ -60,24 +58,38 @@ export class SingleEcho extends EchoSender {
6058
}
6159
}
6260

63-
const DEFAULT_ECHO_DELAY_MS = 250;
64-
65-
export class EchoServer extends EchoSender {
61+
export class EchoServer extends ProgramBase {
6662
static readonly PROGRAM_NAME = "Echo server";
6763

68-
progress = 0;
64+
private echoProgram: SingleEcho;
65+
private progress = 0;
66+
67+
private delay: number;
68+
69+
protected _parseInputs(inputs: string[]): void {
70+
if (inputs.length !== 2) {
71+
console.error(
72+
"EchoServer requires 2 inputs. " + inputs.length + " were given.",
73+
);
74+
return;
75+
}
76+
this.echoProgram = new SingleEcho(this.viewgraph, this.srcId, [inputs[0]]);
77+
this.delay = parseInt(inputs[1]);
78+
}
6979

7080
protected _run() {
7181
Ticker.shared.add(this.tick, this);
7282
}
7383

7484
private tick(ticker: Ticker) {
75-
const delay = DEFAULT_ECHO_DELAY_MS;
85+
const delay = this.delay;
7686
this.progress += ticker.deltaMS * this.viewgraph.getSpeed().value;
7787
if (this.progress < delay) {
7888
return;
7989
}
80-
this.sendSingleEcho();
90+
this.echoProgram.run(() => {
91+
// do nothing
92+
});
8193
this.progress -= delay;
8294
}
8395

@@ -86,8 +98,18 @@ export class EchoServer extends EchoSender {
8698
}
8799

88100
static getProgramInfo(viewgraph: ViewGraph, srcId: DeviceId): ProgramInfo {
101+
// TODO: make this a slider or text field
102+
const delayOptions = [
103+
{ value: "250", text: "250ms" },
104+
{ value: "500", text: "500ms" },
105+
{ value: "1000", text: "1s" },
106+
{ value: "5000", text: "5s" },
107+
{ value: "15000", text: "15s" },
108+
];
109+
89110
const programInfo = new ProgramInfo(this.PROGRAM_NAME);
90111
programInfo.withDropdown("Destination", adjacentDevices(viewgraph, srcId));
112+
programInfo.withDropdown("Time between pings", delayOptions);
91113
return programInfo;
92114
}
93115
}

src/programs/program_base.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export abstract class ProgramBase implements Program {
1010
protected viewgraph: ViewGraph;
1111
protected srcId: DeviceId;
1212

13-
protected signalStop: () => void;
13+
private _signalStop?: () => void;
1414

1515
constructor(viewgraph: ViewGraph, srcId: DeviceId, inputs: string[]) {
1616
this.viewgraph = viewgraph;
@@ -20,11 +20,11 @@ export abstract class ProgramBase implements Program {
2020
}
2121

2222
run(signalStop: () => void) {
23-
if (this.signalStop) {
23+
if (this._signalStop) {
2424
console.error("Program already running");
2525
return;
2626
}
27-
this.signalStop = signalStop;
27+
this._signalStop = signalStop;
2828

2929
this._run();
3030
}
@@ -35,6 +35,11 @@ export abstract class ProgramBase implements Program {
3535
this._stop();
3636
}
3737

38+
protected signalStop() {
39+
this._signalStop?.();
40+
delete this._signalStop;
41+
}
42+
3843
// Functions to be implemented by subclasses
3944

4045
/**

0 commit comments

Comments
 (0)