-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.js
72 lines (63 loc) · 1.34 KB
/
shell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var util = require("./util");
var udp = require("./udp");
var web = require("./web");
var readline = require("readline");
var i = readline.createInterface( process.stdin, process.stdout, null );
i.on( "line", function( line )
{
var items = line.split( " " );
var action = items[0];
if ( action == "quit" )
{
i.close();
process.stdin.destroy();
}
else
{
if ( items.length >= 2 )
{
web.get( "/boids?pid=" + parseInt( items[1] ), function( data )
{
if ( action == "send" )
{
var boid = JSON.parse( data );
if ( items.length >= 4 )
{
var type = items[2];
var value = util.concat( items, 3 );
udp.send( boid.port, { "type": type.toUpperCase(), "value": value } );
}
else
{
console.log( "usage: 'set <key>=<value>'" );
i.prompt();
}
}
else if ( action == "kill" )
{
var boid = JSON.parse( data );
udp.send( boid.port, { "type": "EXIT" } );
}
else if ( action == "print" )
{
console.log( data );
i.prompt();
}
});
}
else
{
console.log( "expected pid" );
i.prompt();
}
}
i.prompt();
});
i.on( "close", function()
{
console.log( "Bye!" );
process.exit( 0 );
});
console.log( "Welcome to Boid shell" );
console.log( "Available commands: 'print' | 'send' | 'kill' | 'quit'" );
i.prompt();