-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
66 lines (50 loc) · 1.55 KB
/
server.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
/* server.js: Run this with node to start your server.
* Copyright © Hexanome H4101 INSA IF.
*/
// Import modules
var net = require ('net'),
camp = require ('./camp/camp'),
protocol = require ('./protocol');
// Connect to remote monitoring system
var socket = new net.Socket();
// What to do when connected
socket.on('connect', function() {
console.log('socket connected');
// Listen to incoming data
socket.on('data', function(data) {
var message = protocol.readmessage(data);
console.log('DATA: %s%s',
message.type === 'e'? '[error] ': '', message.message);
camp.Server.emit('gotlog', message);
});
// All camp actions are defined here.
camp.add('log', function() {}, function gotlog(log) {return log;});
camp.add('init', function(data) {
console.log('INIT nbpart1:' + data.nbpart1, 'nbpart2:' + data.nbpart2);
var nbpart1 = data.nbpart1 || 20,
nbpart2 = data.nbpart2 || 20;
socket.write(protocol.craftinit([nbpart1, nbpart2]));
});
camp.add('stop', function(data) {
console.log('STOP');
socket.write(protocol.craftanswer('s'));
});
camp.add('go', function(data) {
console.log('GO');
socket.write(protocol.craftanswer('c'));
});
camp.add('comm', function(data) {
console.log('COMM');
socket.write(protocol.craftcommand(data.nbpart1, data.nbpart2));
});
});
// Connect to remote system
socket.connect(1337);
// Web server options
var options = {
port: +process.argv[2],
secure: process.argv[3],
debug: +process.argv[4]
}
// Start web server
camp.start(options);