-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_helper.js
148 lines (124 loc) · 2.99 KB
/
client_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var helper = {
client:null,
request:require('request'),
register:function(client, done)
{
try
{
this.client = client;
console.log('posting registration');
console.log(client);
this.request.post({url:'http://' + client.serverIP + ':' + client.serverPort + '/register', json:client}, function(e, response, responseBody){
if (e)
{
done(e);
}
else
{
done(null, responseBody);
}
});
}
catch(e)
{
done(e);
}
},
getLocalAddress:function(done)
{
var os=require('os');
var ifaces=os.networkInterfaces();
var address = null;
var addressFound = false;
for (var dev in ifaces) {
var alias=0;
ifaces[dev].forEach(function(details){
if (details.family=='IPv4' && details.address != '127.0.0.1' && !addressFound) {
address = details.address;
addressFound = true;
}
++alias;
});
}
done(address);
},
processStep:function(process, key, done)
{
console.log('processing step with key ' + key);
console.log(process);
var step = process.steps[key];
var plugin = require('./plugins/' + step.plugin);
var currentStep = null;
console.log(step);
plugin.execute(step, process, function(e, process_out){
if (!e)
{
console.log('plugin processed OK');
console.log(step);
this.notifyHub(step, 'Step ran OK', function(e){
if (e)
console.log('Couldnt notify the hub: ' + e);
else
{
if (step['steps'] != null && step.steps.length > 0)
{
var nextStep = process.steps[step.steps[0]];
console.log('nextStep');
console.log(nextStep);
console.log(step.steps);
if (nextStep.key == key)
{
//bad - we cannot run with circular references
this.notifyHub(step, 'Step run Failed: Circular reference with step: ' + nextStep.key, function(e){
if (e)
console.log(e);
});
}
else
shared_helper.callStep(nextStep, process_out, done);
}
else
{
//means the process has come to an end
this.notifyHub(process_out, 'Process complete', function(e){
if (e)
console.log(e);
});
}
}
}.bind(this));
}
else
{
console.log('plugin processed failure');
console.log('Error executing step plugin ' + step.plugin + ':' + e);
//bad - we cannot run with circular references
this.notifyHub(step, 'Step run Failed: ' + e, function(e){
if (e)
console.log(e);
});
}
}.bind(this));
},
notifyHub:function(data, message, done)
{
try
{
this.request.post({url:'http://' + this.client.serverIP + ':' + this.client.serverPort + '/step_notify', json:{data:data, message:message}}, function(e, response, responseBody){
if (e)
{
done(e);
}
else
{
done(null, responseBody);
}
});
}
catch(e)
{
done(e);
}
}
}
module.exports = helper;