-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmist-node-inner.js
More file actions
213 lines (168 loc) · 6.73 KB
/
mist-node-inner.js
File metadata and controls
213 lines (168 loc) · 6.73 KB
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
/**
* Copyright (C) 2020, ControlThings Oy Ab
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* @license Apache-2.0
*/
var EventEmitter = require("events").EventEmitter;
var inherits = require('util').inherits;
function MistNodeInner(addon) {
var self = this;
this.peers = [];
this.requests = {};
this.readCb = {};
this.writeCb = {};
this.invokeCb = {};
this.addon = addon;
addon.on('ready', function(ready, sid) {
self.emit('ready', ready, sid);
if (typeof self.readyCb === 'function') { self.readyCb(ready); }
});
addon.on('online', function(peer) {
self.emit('online', peer);
if (typeof self.onlineCb === 'function') { self.onlineCb(peer); }
self.peers.push(peer);
});
addon.on('offline', function(peer) {
self.emit('offline', peer);
if (typeof self.offlineCb === 'function') { self.offlineCb(peer); }
});
addon.on('frame', function(peer, data) {
if (typeof self.frameCb === 'function') { self.frameCb(peer, data); }
});
addon.on('mistnode', function(msg) {
var id = msg.ack || msg.sig || msg.end || msg.err;
if(typeof self.requests[id] === 'function') {
self.requests[id](msg);
if(!msg.sig) {
delete self.requests[id];
}
} else {
console.log('Request not found for response:', id, self, self.requests);
}
});
this.addon.on('read', function(msg) {
if(typeof self.readCb[msg.read.epid] === 'function') {
self.readCb[msg.read.epid](msg.read.args, msg.peer, (function (id) {
return function(err, data) {
if (err) { return self.addon.request("mistnode", { readError: id, epid: msg.read.epid, code: err.code, msg: err.msg }); }
self.addon.request("mistnode", { read: id, epid: msg.read.epid, data: data });
};
})(msg.read.id));
} else {
console.log("There is no read function registered for", msg.read.epid );
}
});
this.addon.on('write', function(msg) {
if(typeof self.writeCb[msg.write.epid] === 'function') {
self.writeCb[msg.write.epid](msg.write.args, msg.peer, function (err) {
if (err) { return self.addon.request("mistnode", { writeError: msg.write.id, epid: msg.write.epid, code: err.code, msg: err.msg }); }
self.addon.request("mistnode", { write: msg.write.id, epid: msg.write.epid });
});
} else {
console.log("There is no write function registered for", msg.write.epid );
}
});
this.addon.on('invoke', function(msg) {
if(typeof self.invokeCb[msg.invoke.epid] === 'function') {
self.invokeCb[msg.invoke.epid](msg.invoke.args, msg.peer, (function (id) {
return function(err, data) {
if (err) { return self.addon.request("mistnode", { invokeError: id, epid: msg.invoke.epid, code: err.code, msg: err.msg }); }
self.addon.request("mistnode", { invoke: id, epid: msg.invoke.epid, data: data });
};
})(msg.invoke.id));
} else {
console.log("There is no invoke function registered for", msg.invoke.epid );
}
});
}
inherits(MistNodeInner, EventEmitter);
MistNodeInner.prototype.create = function(model, cb) {
var node = this;
var visitor = function (modelFragment, parent) {
for (var epid in modelFragment) {
node.addEndpoint( parent + epid, {
type: modelFragment[epid].type,
read: modelFragment[epid].read,
write: modelFragment[epid].write,
invoke: modelFragment[epid].invoke
});
if (modelFragment[epid]['#']) {
visitor(modelFragment[epid]['#'], parent + epid + '.');
}
}
};
visitor(model, "");
};
MistNodeInner.prototype.addEndpoint = function(fepid, endpoint) {
var path = fepid.split('.');
var parent;
if (path.length === 1) {
parent = null;
} else if (path.length > 1) {
parent = path.slice(0, path.length-1).join('.');
} else {
return console.log('endpoint could not be added, due to invalid arguments');
}
var epid = path.slice(-1)[0];
endpoint.parent = parent;
endpoint.epid = epid;
if (typeof endpoint.read === 'function') { this.read(fepid, endpoint.read); endpoint.read = true; }
if (typeof endpoint.write === 'function') { this.write(fepid, endpoint.write); endpoint.write = true; }
if (typeof endpoint.invoke === 'function') { this.invoke(fepid, endpoint.invoke); endpoint.invoke = true; }
if (endpoint.invoke) { endpoint.type = 'invoke'; }
this.addon.request("mistnode", { endpointAdd: true, ep: endpoint });
};
MistNodeInner.prototype.removeEndpoint = function(epid) {
this.addon.request("mistnode", { endpointRemove: epid });
};
// register read handler for epid
MistNodeInner.prototype.read = function(epid, cb) {
this.readCb[epid] = cb;
};
// register write handler for epid
MistNodeInner.prototype.write = function(epid, cb) {
this.writeCb[epid] = cb;
};
// register invoke handler for epid
MistNodeInner.prototype.invoke = function(epid, cb) {
this.invokeCb[epid] = cb;
};
MistNodeInner.prototype.changed = function(epid) {
var request = { changed: epid };
this.addon.request("mistnode", request);
};
MistNodeInner.prototype.modelChanged = function() {
var request = { modelChanged: true };
this.addon.request("mistnode", request);
}
MistNodeInner.prototype.request = function(peer, op, args, cb) {
return this.requestBare(peer, op, args, function(res) {
//console.log('requestBare cb:', arguments);
if(res.err) { return cb(true, res.data); }
cb(null, res.data);
});
};
MistNodeInner.prototype.requestBare = function(peer, op, args, cb) {
var id = ++this.addon.sharedRequestId;
var request = { peer: peer, op: op, args: args, id: id };
// store callback for response
this.requests[id] = cb;
//console.log("Making request", request, this);
this.addon.request("mistnode", request);
return id;
};
MistNodeInner.prototype.requestCancel = function(id) {
var request = { cancel: id };
this.addon.request("mistnode", request);
};
MistNodeInner.prototype.shutdown = function() {
this.addon.shutdown();
};
module.exports = {
MistNodeInner: MistNodeInner };