Skip to content

Commit ba1bcac

Browse files
committedMay 3, 2016
Fixes prompting issue and queue processing.
1 parent 0577cc9 commit ba1bcac

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed
 

‎agent.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const protocol = require('./protocol');
1313
const readline = require('readline');
1414

1515
// Standard timeout for a response to a request.
16-
const msg_timeout = 5000;
16+
const msg_timeout = 1000;
1717

1818
const args = process.argv.slice(2);
1919
assert(args.length == 2);
@@ -51,12 +51,12 @@ var last_msg_timeout = null;
5151
var seq_num = 0;
5252
var last_msg_sent = -1;
5353

54+
var shouldPrompt = false;
5455
const rl = readline.createInterface({
5556
input: process.stdin,
5657
output: process.stdout,
5758
terminal: false
5859
});
59-
var shouldPrompt = true;
6060
rl.setPrompt('Enter r(egister), u(nregister), f(etch), p(robe), or q(uit): ');
6161
rl.pause();
6262

@@ -71,6 +71,12 @@ function processQueue(messageAction){
7171
if (nextAction != undefined) {
7272
nextAction();
7373
}
74+
} else {
75+
if (shouldPrompt) {
76+
rl.prompt();
77+
shouldPrompt = false;
78+
}
79+
rl.resume();
7480
}
7581
}
7682

@@ -83,10 +89,15 @@ function protocolError(location){
8389
}
8490

8591
function msgTimeout(errMsg, verbose){
92+
if (last_msg_sent == -1) {
93+
return;
94+
}
95+
8696
if (typeof verbose == "undefined") {verbose = true;}
8797
if (verbose) {
8898
console.log(errMsg);
8999
}
100+
90101
last_msg_timeout = null;
91102
if (last_register_msg) {
92103
port = last_register_msg['service_port'];
@@ -99,8 +110,6 @@ function msgTimeout(errMsg, verbose){
99110
}
100111
last_msg_sent = -1;
101112
processQueue();
102-
rl.prompt();
103-
rl.resume();
104113
}
105114

106115
// Message Handlers //
@@ -118,7 +127,6 @@ function process_registered(msg, rinfo){
118127

119128
if (last_register_msg['explicit_call']) {
120129
console.log("Register successful.");
121-
rl.prompt();
122130
}
123131
else if ('timeout' in port_map[port] && port_map[port].timeout != null){
124132
clearTimeout(port_map[port].timeout);
@@ -133,9 +141,10 @@ function process_registered(msg, rinfo){
133141
//called in re-register timeout, on user input and on-message-response
134142
processQueue(function(){
135143
send_register(port, service_data, service_name)
136-
});
144+
}, false);
137145
}, reregister_time);
138146
last_register_msg = {};
147+
processQueue();
139148
}
140149

141150
function process_fetchresponse(msg, rinfo){
@@ -146,7 +155,7 @@ function process_fetchresponse(msg, rinfo){
146155
data = protocol.unpackFetchResponse(msg);
147156
if (data == null) {protocolError("null data in process_fetchresponse");}
148157
console.log(data.entries);
149-
rl.prompt();
158+
processQueue();
150159
}
151160

152161
function process_probe(msg, rinfo){
@@ -165,7 +174,7 @@ function process_ack(msg, rinfo){
165174
}else{
166175
protocolError("process_ack");
167176
}
168-
rl.prompt();
177+
processQueue();
169178
}
170179

171180
function send(msg, socket, callback){
@@ -246,13 +255,15 @@ function send_ack(socket){
246255
// IO and IO EVENT BINDINGS
247256
// -------------------------------------------------------------------------- //
248257
rl.on('line', (line) => {
258+
shouldPrompt = true;
249259
rl.pause();
250260
var arguments = line.split(" ");
251261
switch (arguments[0]) {
252262
case "r":
253263
if (arguments.length != 4 || parseInt(arguments[1]) == NaN) {
254264
console.log("Register command format is: r port service_data service_name");
255265
rl.prompt();
266+
shouldPrompt = false;
256267
rl.resume();
257268
break;
258269
}
@@ -267,6 +278,7 @@ rl.on('line', (line) => {
267278
if (arguments.length != 2 || parseInt(arguments[1]) == NaN) {
268279
console.log("Unregister command format is: u service_port");
269280
rl.prompt();
281+
shouldPrompt = false;
270282
rl.resume();
271283
break;
272284
}
@@ -279,6 +291,7 @@ rl.on('line', (line) => {
279291
if (arguments.length != 2) {
280292
console.log("Fetch command format is: f service_name");
281293
rl.prompt();
294+
shouldPrompt = false;
282295
rl.resume();
283296
break;
284297
}
@@ -299,6 +312,7 @@ rl.on('line', (line) => {
299312
default:
300313
console.log("Unrecognized Command");
301314
rl.prompt();
315+
shouldPrompt = false;
302316
rl.resume();
303317
break;
304318
}
@@ -322,6 +336,7 @@ socket_out.on('listening', () => {
322336
num_listening++;
323337
if (num_listening == 2) {
324338
rl.prompt();
339+
shouldPrompt = false;
325340
rl.resume();
326341
}
327342
});
@@ -330,6 +345,7 @@ socket_in.on('listening', () => {
330345
num_listening++;
331346
if (num_listening == 2) {
332347
rl.prompt();
348+
shouldPrompt = false;
333349
rl.resume();
334350
}
335351
});
@@ -362,8 +378,8 @@ socket_out.on('message', (buf, rinfo) => {
362378
if (command_ok(header.command) && sequence_num_ok(header.seq_num)){
363379
// valid packet
364380
MSG_HANDLER[header.command](buf, rinfo);
381+
last_msg_sent = -1;
365382
processQueue();
366-
rl.resume();
367383
}
368384
}
369385
});

0 commit comments

Comments
 (0)
Please sign in to comment.