-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_helper.js
107 lines (93 loc) · 2.09 KB
/
server_helper.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var helper = {
request:require('request'),
clients:{},
dashboardSocket:null,
initializeSocket:function(socket){
socket.on('start process', function (process) {
console.log('start process called...');
this.startProcess(process, function(e, processId){
if (!e)
{
this.emitMessage('Client start succeeded for process with id: ' + process.id + ", step name " + process.initialStep.name, "success");
}
else
{
this.emitMessage('Process start failed: ' + e, 'error');
}
}.bind(this));
}.bind(this));
dashboardSocket = socket;
},
registerClient:function(client, done)
{
try
{
console.log('registering client');
console.log(client);
var clientKey = client.name + client.ip.replace(/\./g,'') + client.port.toString();
this.clients[clientKey] = client;
client.key = clientKey;
console.log('emitting registration');
dashboardSocket.emit('client register result', { status: 'OK', 'client':client});
done(null);
}
catch(e)
{
console.log('error happened registering client');
console.log(e);
done(e);
}
},
startProcess:function(process, done)
{
try
{
//We generate a random id for the new process
var uuid = require('node-uuid').v4();
process.id = uuid;
shared_helper.callStep(process.initialStep, process, function(e){
if (!e)
done(null, process.id);
else
done(e);
}.bind(this));
}
catch(e)
{
done(e);
}
},
emitMessage: function(message, type, done)
{
try
{
dashboardSocket.emit('message', { 'message': message, 'type':type });
if (done)
done();
}
catch(e)
{
if (done)
done(e);
else
console.log(e);
}
},
emitStepMessage: function(data, message, done)
{
try
{
dashboardSocket.emit('step message', {'data':data, 'message': message });
if (done)
done();
}
catch(e)
{
if (done)
done(e);
else
console.log(e);
}
}
}
module.exports = helper;