@@ -13,7 +13,7 @@ const protocol = require('./protocol');
13
13
const readline = require ( 'readline' ) ;
14
14
15
15
// Standard timeout for a response to a request.
16
- const msg_timeout = 5000 ;
16
+ const msg_timeout = 1000 ;
17
17
18
18
const args = process . argv . slice ( 2 ) ;
19
19
assert ( args . length == 2 ) ;
@@ -51,12 +51,12 @@ var last_msg_timeout = null;
51
51
var seq_num = 0 ;
52
52
var last_msg_sent = - 1 ;
53
53
54
+ var shouldPrompt = false ;
54
55
const rl = readline . createInterface ( {
55
56
input : process . stdin ,
56
57
output : process . stdout ,
57
58
terminal : false
58
59
} ) ;
59
- var shouldPrompt = true ;
60
60
rl . setPrompt ( 'Enter r(egister), u(nregister), f(etch), p(robe), or q(uit): ' ) ;
61
61
rl . pause ( ) ;
62
62
@@ -71,6 +71,12 @@ function processQueue(messageAction){
71
71
if ( nextAction != undefined ) {
72
72
nextAction ( ) ;
73
73
}
74
+ } else {
75
+ if ( shouldPrompt ) {
76
+ rl . prompt ( ) ;
77
+ shouldPrompt = false ;
78
+ }
79
+ rl . resume ( ) ;
74
80
}
75
81
}
76
82
@@ -83,10 +89,15 @@ function protocolError(location){
83
89
}
84
90
85
91
function msgTimeout ( errMsg , verbose ) {
92
+ if ( last_msg_sent == - 1 ) {
93
+ return ;
94
+ }
95
+
86
96
if ( typeof verbose == "undefined" ) { verbose = true ; }
87
97
if ( verbose ) {
88
98
console . log ( errMsg ) ;
89
99
}
100
+
90
101
last_msg_timeout = null ;
91
102
if ( last_register_msg ) {
92
103
port = last_register_msg [ 'service_port' ] ;
@@ -99,8 +110,6 @@ function msgTimeout(errMsg, verbose){
99
110
}
100
111
last_msg_sent = - 1 ;
101
112
processQueue ( ) ;
102
- rl . prompt ( ) ;
103
- rl . resume ( ) ;
104
113
}
105
114
106
115
// Message Handlers //
@@ -118,7 +127,6 @@ function process_registered(msg, rinfo){
118
127
119
128
if ( last_register_msg [ 'explicit_call' ] ) {
120
129
console . log ( "Register successful." ) ;
121
- rl . prompt ( ) ;
122
130
}
123
131
else if ( 'timeout' in port_map [ port ] && port_map [ port ] . timeout != null ) {
124
132
clearTimeout ( port_map [ port ] . timeout ) ;
@@ -133,9 +141,10 @@ function process_registered(msg, rinfo){
133
141
//called in re-register timeout, on user input and on-message-response
134
142
processQueue ( function ( ) {
135
143
send_register ( port , service_data , service_name )
136
- } ) ;
144
+ } , false ) ;
137
145
} , reregister_time ) ;
138
146
last_register_msg = { } ;
147
+ processQueue ( ) ;
139
148
}
140
149
141
150
function process_fetchresponse ( msg , rinfo ) {
@@ -146,7 +155,7 @@ function process_fetchresponse(msg, rinfo){
146
155
data = protocol . unpackFetchResponse ( msg ) ;
147
156
if ( data == null ) { protocolError ( "null data in process_fetchresponse" ) ; }
148
157
console . log ( data . entries ) ;
149
- rl . prompt ( ) ;
158
+ processQueue ( ) ;
150
159
}
151
160
152
161
function process_probe ( msg , rinfo ) {
@@ -165,7 +174,7 @@ function process_ack(msg, rinfo){
165
174
} else {
166
175
protocolError ( "process_ack" ) ;
167
176
}
168
- rl . prompt ( ) ;
177
+ processQueue ( ) ;
169
178
}
170
179
171
180
function send ( msg , socket , callback ) {
@@ -246,13 +255,15 @@ function send_ack(socket){
246
255
// IO and IO EVENT BINDINGS
247
256
// -------------------------------------------------------------------------- //
248
257
rl . on ( 'line' , ( line ) => {
258
+ shouldPrompt = true ;
249
259
rl . pause ( ) ;
250
260
var arguments = line . split ( " " ) ;
251
261
switch ( arguments [ 0 ] ) {
252
262
case "r" :
253
263
if ( arguments . length != 4 || parseInt ( arguments [ 1 ] ) == NaN ) {
254
264
console . log ( "Register command format is: r port service_data service_name" ) ;
255
265
rl . prompt ( ) ;
266
+ shouldPrompt = false ;
256
267
rl . resume ( ) ;
257
268
break ;
258
269
}
@@ -267,6 +278,7 @@ rl.on('line', (line) => {
267
278
if ( arguments . length != 2 || parseInt ( arguments [ 1 ] ) == NaN ) {
268
279
console . log ( "Unregister command format is: u service_port" ) ;
269
280
rl . prompt ( ) ;
281
+ shouldPrompt = false ;
270
282
rl . resume ( ) ;
271
283
break ;
272
284
}
@@ -279,6 +291,7 @@ rl.on('line', (line) => {
279
291
if ( arguments . length != 2 ) {
280
292
console . log ( "Fetch command format is: f service_name" ) ;
281
293
rl . prompt ( ) ;
294
+ shouldPrompt = false ;
282
295
rl . resume ( ) ;
283
296
break ;
284
297
}
@@ -299,6 +312,7 @@ rl.on('line', (line) => {
299
312
default :
300
313
console . log ( "Unrecognized Command" ) ;
301
314
rl . prompt ( ) ;
315
+ shouldPrompt = false ;
302
316
rl . resume ( ) ;
303
317
break ;
304
318
}
@@ -322,6 +336,7 @@ socket_out.on('listening', () => {
322
336
num_listening ++ ;
323
337
if ( num_listening == 2 ) {
324
338
rl . prompt ( ) ;
339
+ shouldPrompt = false ;
325
340
rl . resume ( ) ;
326
341
}
327
342
} ) ;
@@ -330,6 +345,7 @@ socket_in.on('listening', () => {
330
345
num_listening ++ ;
331
346
if ( num_listening == 2 ) {
332
347
rl . prompt ( ) ;
348
+ shouldPrompt = false ;
333
349
rl . resume ( ) ;
334
350
}
335
351
} ) ;
@@ -362,8 +378,8 @@ socket_out.on('message', (buf, rinfo) => {
362
378
if ( command_ok ( header . command ) && sequence_num_ok ( header . seq_num ) ) {
363
379
// valid packet
364
380
MSG_HANDLER [ header . command ] ( buf , rinfo ) ;
381
+ last_msg_sent = - 1 ;
365
382
processQueue ( ) ;
366
- rl . resume ( ) ;
367
383
}
368
384
}
369
385
} ) ;
0 commit comments