@@ -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}
0 commit comments