/
machregistry.js
269 lines (215 loc) · 7.37 KB
/
machregistry.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// = Create a machine registry. We can make it a local storage so it works with
// mininet. TODO: Make it work with Xiru's remote registry as well.
// Creation/addition:
// = Create machine registry - with a name - filename reflects the machine name
// (machine name and app name are the same)
// = Only one cloud node for now. Fail if multiple cloud nodes are added to the
// registry. This could be a problem with the UUID4 generation.
// = Add as many fog and device nodes. If the fog/device is already there, don't add.
// Updates:
// = Update the IP address of the node. If the node is not the cloud, it should
// have a parent. The parent should be active (any node that is not updated for the
// last K seconds is considered inactive. If the parent is inactive, select another
// parent.
// Query:
// = find the fog:
// Used by the device to find the fog
// = find the cloud:
// Used by the fog to find the cloud
// = find the cloud for the device:
// Used by the device that is connected to a given fog to find the cloud
// TODO: There could be an issue with multiple nodes writing to the local storage at the
// same time
var os = require('os'),
Random = require('random-js'),
rgen = new Random();
var localStorage;
// Some global values that are exported by the machRegistry.
var deviceId = undefined,
deviceAddr = undefined,
port = undefined,
type = undefined;
module.exports = new function () {
var that = this;
this.deviceId = deviceId;
this.deviceAddr = deviceAddr;
this.port = port;
this.type = type;
this.init = function (co, dp) {
var registry = co.registry,
app = co.app;
if (registry) {
// Use remote registry.. at a predefined global URL
// TODO: This is not yet implemented.
console.log("ERROR! Globale registry not yet implemented..\nQuiting");
process.exit(1);
}
else {
// put the 'app' as a hidden directory in user's home
appdir = os.homedir() + '/.' + app
// Set deviceId
deviceId = dp.getItem('deviceId');
// Open local storage
if (typeof localStorage === "undefined" || localStorage === null) {
var LocalStorage = require('node-localstorage').LocalStorage;
localStorage = new LocalStorage(appdir);
}
// We need to setup the device
setDevice(that, co);
}
}
// insert cloud..
//
this.setCloud = function (devId) {
if (devId === undefined)
devId = deviceId;
// get existing clouds
clouds = localStorage.getItem('cloud');
if (clouds !== null) {
if (clouds !== devId)
console.log("WARNING! Another cloud is already set.. resetting");
}
localStorage.setItem('cloud', devId);
// Also store the record for the current node
nodestr = localStorage.getItem(devId);
if (nodestr === null)
node = {"id": devId};
else
node = JSON.parse(nodestr);
node.type = 'CLOUD';
node.utime = Date.now();
localStorage.setItem(devId, JSON.stringify(node));
}
// insert fog
// WARNING: There could be a Race Condition here
//
this.setFog = function (devId) {
if (devId === undefined)
devId = deviceId;
// get existing fogs
fogstr = localStorage.getItem('fogs');
if (fogstr !== null) {
fogs = JSON.parse(fogstr);
for (f in fogs) {
if (fogs[f] === devId)
return;
}
} else
fogs = [];
fogs.push(devId);
localStorage.setItem('fogs', JSON.stringify(fogs));
// Also store the record for the current node
nodestr = localStorage.getItem(devId);
if (nodestr === null)
node = {"id": devId};
else
node = JSON.parse(nodestr);
node.type = 'FOG';
node.utime = Date.now();
localStorage.setItem(devId, JSON.stringify(node));
}
// insert device
this.setDevice = function (devId) {
if (devId === undefined)
devId = deviceId;
// Also store the record for the current node
nodestr = localStorage.getItem(devId);
if (nodestr === null)
node = {"id": devId};
else
node = JSON.parse(nodestr);
node.type = "DEVICE";
node.utime = Date.now();
localStorage.setItem(devId, JSON.stringify(node));
}
this.setPort = function (devId, port) {
if (devId === undefined)
devId = deviceId;
nodestr = localStorage.getItem(devId);
// if not found in local storage, then exit without any changes
if (nodestr === null)
return;
node = JSON.parse(nodestr);
node.port = port;
localStorage.setItem(devId, JSON.stringify(node));
}
this.getAFog = function () {
fogstr = localStorage.getItem('fogs');
if (fogstr === null) {
console.log("WARNING! Cannot find a fog to connect.. ");
return undefined;
}
fogs = JSON.parse(fogstr);
return pickANewerNode(fogs);
}
// update the address of the device
this.update = function (devId) {
if (devId === undefined)
devId = deviceId;
nodestr = localStorage.getItem(devId);
// if not found in local storage, then exit without any changes
if (nodestr === null)
return;
node = JSON.parse(nodestr);
node.addr = getIPv4Address();
node.utime = Date.now();
localStorage.setItem(devId, JSON.stringify(node));
}
this.getURL = function (devId) {
if (devId === undefined)
devId = deviceId;
nodestr = localStorage.getItem(devId);
if (nodestr === null)
return undefined;
node = JSON.parse(nodestr);
return "tcp://" + node.addr + ":" + node.port;
}
this.getTheCloud = function() {
cloud = localStorage.getItem('cloud');
if (cloud === null)
return undefined;
return cloud;
}
}
function setDevice(mr, co) {
if (co.cloud !== undefined) {
mr.setCloud(mr.deviceId);
mr.type = 'CLOUD';
}
else if (co.fog !== undefined) {
mr.setFog(mr.deviceId);
mr.type = 'FOG';
}
else {
mr.setDevice(mr.deviceId);
mr.type = 'DEVICE';
}
mr.setPort(mr.deviceId, co.port);
mr.port = co.port;
}
function getIPv4Address() {
var niaddrs = os.networkInterfaces();
for (var ni in niaddrs) {
nielm = niaddrs[ni];
for (n in nielm) {
if (nielm[n].family === 'IPv4' && nielm[n].internal === false)
return nielm[n].address
}
}
return "127.0.0.1";
}
function pickANewerNode(nds) {
var devs = [];
for (n in nds) {
nodestr = localStorage.getItem(nds[n]);
if (nodestr !== null) {
node = JSON.parse(nodestr);
if ((Date.now() - node.utime) < 120000)
devs.push(nds[n]);
}
}
if (devs.length == 0)
return undefined;
else
return rgen.pick(devs);
}