-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReporterObject.js
executable file
·424 lines (318 loc) · 13.8 KB
/
ReporterObject.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Log System
import * as logger from 'loglevel';
let log = logger.getLogger('ReporterObject');
import { divideURL, splitObjectURL } from '../utils/utils';
import Subscription from './Subscription';
import * as cryptoManager from '../cryptoManager/CryptoManager';
class ReporterObject {
constructor(parent, owner, url, childrens, offline) {
let _this = this;
_this._parent = parent;
_this._owner = owner;
_this._url = url;
_this._bus = parent._bus;
_this._domain = divideURL(url).domain;
_this._objSubscriptorURL = _this._url + '/subscription';
_this._subscriptions = {};
_this._childrens = childrens;
_this._childrenListeners = [];
_this._forwards = {};
_this._isToSaveData = false;
_this._allocateListeners();
_this._offline = offline ? offline : false;
}
get offline(){
return this._offline;
}
_allocateListeners() {
let _this = this;
//add subscription listener...
_this._subscriptionListener = _this._bus.addListener(_this._objSubscriptorURL, (msg) => {
log.info('[SyncherManager.ReporterObject received ]', msg);
switch (msg.type) {
case 'subscribe': _this._onRemoteSubscribe(msg); break;
case 'unsubscribe': _this._onRemoteUnSubscribe(msg); break;
case 'response': _this._onRemoteResponse(msg); break;
case 'forward': _this._onForwardedRemoteSubscribe(msg); break;
}
});
let changeURL = _this._url + '/changes';
_this._changeListener = _this._bus.addListener(changeURL, (msg) => {
log.info('[SyncherManager.ReporterObject ] SyncherManager-' + changeURL + '-RCV: ', msg);
//do not save changes to backupRevision to avoid infinite loops
if (this._isToSaveData && msg.body.attribute ) {
let updateRuntimeStatus = msg.body.attribute !== 'backupRevision' ? true : false;
log.log('[SyncherManager.ReporterObject ] SyncherManager - save data: ', msg);
_this._parent._dataObjectsStorage.update(true, _this._url, 'version', msg.body.version, updateRuntimeStatus);
_this._parent._dataObjectsStorage.update(true, _this._url, 'lastModified', msg.body.lastModified, updateRuntimeStatus);
_this._parent._dataObjectsStorage.saveData(true, _this._url, msg.body.attribute, msg.body.value, updateRuntimeStatus);
}
});
}
set isToSaveData(value) {
this._isToSaveData = value;
}
// To handle subscriptions sent while the reporter was offline ie forwarded by a Offline Subscription Manager service
_onForwardedRemoteSubscribe(msg) {
this._onRemoteSubscribe(msg.body);
}
_releaseListeners() {
let _this = this;
_this._subscriptionListener.remove();
_this._changeListener.remove();
_this._childrenListeners.forEach((cl) => {
cl.remove();
});
Object.keys(_this._forwards).forEach((key) => {
_this.forwardUnSubscribe(key);
});
//remove all subscriptions
Object.keys(_this._subscriptions).forEach((key) => {
_this._subscriptions[key]._releaseListeners();
});
}
resumeSubscriptions(subscriptions) {
let _this = this;
if (!subscriptions)
return;
Object.keys(subscriptions).forEach((key) => {
let hypertyURL = subscriptions[key];
log.log('[SyncherManager.ReporterObject] - resume subscriptions', _this, hypertyURL, _this._childrens);
if (!_this._subscriptions[hypertyURL]) {
_this._subscriptions[hypertyURL] = new Subscription(_this._bus, _this._owner, _this._url, true);
}
});
}
/**
* Register a listener in the msg-node and in the local MessageBus, so that messages on this address are forwarded to the reporter object
* @param {string} address - URL to register the listeners
* @return {Promise} Return Promise OK or error
*/
forwardSubscribe(addresses) {
let _this = this;
//FLOW-OUT: message sent to the msg-node SubscriptionManager component
let nodeSubscribeMsg = {
type: 'subscribe', from: _this._parent._url, to: 'domain://msg-node.' + _this._domain + '/sm',
body: { resources: addresses, source: _this._owner }
};
return new Promise((resolve, reject) => {
_this._bus.postMessageWithRetries(nodeSubscribeMsg, 10, (reply) => {
log.log('[SyncherManager.ReporterObject ]forward-subscribe-response(reporter): ', reply);
if (reply.body.code === 200) {
let newForward = _this._bus.addForward(_this._url, _this._owner);
_this._forwards[addresses[0]] = newForward;
resolve();
} else {
reject('Error on msg-node subscription: ' + reply.body.desc);
}
});
});
}
/**
* UnRegister a listener in the msg-node and in the local MessageBus, so that messages on this address are removed from forward
* @param {string} address - URL to un-register the listeners
*/
forwardUnSubscribe(address) {
let _this = this;
_this._forwards[address].remove();
delete _this._forwards[address];
//FLOW-OUT: message sent to the msg-node SubscriptionManager component
let nodeUnSubscribeMsg = {
type: 'unsubscribe', from: _this._parent._url, to: 'domain://msg-node.' + _this._domain + '/sm',
body: { resources: [address], source: _this._owner }
};
_this._bus.postMessage(nodeUnSubscribeMsg);
}
/**
* Register listeners for a list of childrens. Public channels used to transmit messages.
* @param {string[]} childrens - channels to register
* @return {Promise} Return Promise OK or error
*/
addChildrens() {
let _this = this;
return new Promise((resolve, reject) => {
if (_this._childrens.length === 0) {
resolve();
return;
}
let childBaseURL = _this._url + '/children/';
log.log('[SyncherManager.ReporterObject - addChildrens] - childrens: ', childBaseURL);
/* childrens.forEach((child) => {
_this._childrens.push(child);
});*/
/*
_this._childrens.forEach((child) => {
let childId = childBaseURL + child;
let selfForward = _this._bus.addForward(childId, owner);
_this._childrenListeners.push(selfForward);
});*/
let subscriptions = [];
// childrens.forEach((child) => subscriptions.push(childBaseURL + child));
subscriptions.push(childBaseURL );
//_this._storageSubscriptions[_this._objSubscriptorURL] = {url: _this._url, owner: _this._owner, childrens: _this._childrens};
//FLOW-OUT: message sent to the msg-node SubscriptionManager component
let nodeSubscribeMsg = {
type: 'subscribe', from: _this._parent._url, to: 'domain://msg-node.' + _this._domain + '/sm',
body: { resources: subscriptions, source: _this._owner }
};
_this._bus.postMessage(nodeSubscribeMsg, (reply) => {
log.log('[SyncherManager.ReporterObject ]node-subscribe-response(reporter):', reply);
if (reply.body.code === 200) {
//add children listeners on local ...
subscriptions.forEach((childURL) => {
let childListener = _this._bus.addListener(childURL, (msg) => {
//TODO: what todo here? Save childrens?
log.log('[SyncherManager.ReporterObject received]', msg);
if (msg.type === 'create' && msg.to.includes('children') && this._isToSaveData) {
// if the value is not encrypted lets encrypt it
// todo: should be subject to some policy
let splitedReporterURL = splitObjectURL(msg.to);
let url = splitedReporterURL.url;
if (!msg.body.hasOwnProperty('mutual')) msg.body.mutual = true;
//remove false when mutualAuthentication is enabled
if (!(typeof msg.body.value === 'string') && msg.body.mutual) {
log.log('[SyncherManager.ReporterObject] encrypting received data ', msg.body.value);
cryptoManager.default.encryptDataObject(msg.body.value, url).then((encryptedValue)=>{
log.log('[SyncherManager.ReporterObject] encrypted data ', encryptedValue);
_this._storeChildObject(msg, JSON.stringify(encryptedValue));
}).catch((reason) => {
log.warn('[SyncherManager._decryptChildrens] failed : ', reason, ' Storing unencrypted');
_this._storeChildObject(msg, msg.body.value);
});
} else {
_this._storeChildObject(msg, msg.body.value);
}
}
});
_this._childrenListeners.push(childListener);
let selfForward = _this._bus.addForward(childURL, _this._owner);
_this._childrenListeners.push(selfForward);
});
resolve();
} else {
reject('Error on msg-node subscription: ' + reply.body.desc);
}
});
});
}
// store childObject
_storeChildObject(msg, data) {
let _this = this;
let splitedReporterURL = splitObjectURL(msg.to);
let url = splitedReporterURL.url;
let resource = splitedReporterURL.resource;
let value;
/* if (msg.body.identity) {
value.identity = msg.body.identity;
delete value.identity.assertion;
delete value.identity.expires;
}*/
let objectURLResource = msg.body.resource;
let attribute = resource;
if (objectURLResource === 'heartbeat' ) value = data;
else value = {
identity: msg.body.identity,
value: data
};
// if (objectURLResource) attribute += '.' + objectURLResource;
if (objectURLResource) attribute = objectURLResource;
// this identity data is not needed to be stored
console.log('[SyncherManager.ReporterObject._storeChildObject] : ', url, attribute, value);
_this._parent._dataObjectsStorage.saveChildrens(true, url, attribute, value);
}
delete() {
let _this = this;
let domain = divideURL(_this._owner).domain;
//FLOW-OUT: message sent directly to all subscribers of the reporter
_this._bus.postMessage({
type: 'delete', from: _this._objSubscriptorURL, to: _this._url + '/changes'
});
//FLOW-OUT: message sent to the msg-node ObjectAllocationManager component
_this._bus.postMessage({
type: 'delete', from: _this._parent._url, to: 'domain://msg-node.' + domain + '/object-address-allocation',
body: { resource: _this._url, childrenResources: _this._childrens }
});
_this._releaseListeners();
delete _this._parent._reporters[_this._url];
}
_onRemoteResponse(msg) {
let _this = this;
_this._bus.postMessage({
id: msg.id, type: 'response', from: msg.to, to: _this._url,
body: { code: msg.body.code, identity: msg.body.identity, source: msg.from }
});
}
//FLOW-IN: message received from Syncher -> subscribe
_onRemoteSubscribe(msg) {
let _this = this;
let hypertyURL = msg.body.subscriber;
//validate if subscription already exists?
if (_this._subscriptions[hypertyURL]) {
// let errorMsg = {
// id: msg.id, type: 'response', from: msg.to, to: hypertyURL,
// body: { code: 500, desc: 'Subscription for (' + _this._url + ' : ' + hypertyURL + ') already exists!' }
// };
//
// _this._bus.postMessage(errorMsg);
// return;
// new version because of reusage
_this._subscriptions[hypertyURL]._releaseListeners();
}
//ask to subscribe to Syncher? (depends on the operation mode)
//TODO: get mode from object!
let mode = 'sub/pub';
if (mode === 'sub/pub') {
//FLOW-OUT: message sent to local hyperty address Syncher -> _onForward
let forwardMsg = {
type: 'forward', from: _this._url, to: _this._owner,
body: { type: msg.type, from: hypertyURL, to: _this._url, identity: msg.body.identity }
};
//TODO: For Further Study
if (msg.body.hasOwnProperty('mutual')) forwardMsg.body.mutual = msg.body.mutual;
_this._bus.postMessage(forwardMsg, (reply) => {
log.log('[SyncherManager.ReporterObject ]forward-reply: ', reply);
if (reply.body.code === 200) {
if (!_this._subscriptions[hypertyURL]) {
log.log('[SyncherManager.ReporterObject] - _onRemoteSubscribe:', _this._childrens);
_this._subscriptions[hypertyURL] = new Subscription(_this._bus, _this._owner, _this._url, true);
}
}
// Store for each reporter hyperty the dataObject
let userURL;
if (msg.body.identity && msg.body.identity.userProfile.userURL) {
userURL = msg.body.identity.userProfile.userURL;
_this._parent._dataObjectsStorage.update(true, _this._url, 'subscriberUsers', userURL);
}
//TODO: mutual and sessionkeys updates were removed. FFS
/* if (msg.body.hasOwnProperty('mutual')) {
// _this._parent._identityModule.updateIsToEncryptForDataObjectSessionKey(_this._url, msg.body.mutual).then(()=>{
_this._parent._dataObjectsStorage.update(true, _this._url, 'mutual', msg.body.mutual);
// });
}*/
_this._parent._dataObjectsStorage.update(true, _this._url, 'subscriptions', hypertyURL);
reply.body.owner = _this._owner;
//FLOW-OUT: subscription response sent (forward from internal Hyperty)
_this._bus.postMessage({
id: msg.id, type: 'response', from: msg.to, to: msg.from,
body: reply.body
});
});
}
}
//FLOW-IN: message received from remote ObserverObject -> removeSubscription
_onRemoteUnSubscribe(msg) {
let _this = this;
let unsubscriber = msg.body.source;
let subscription = _this._subscriptions[unsubscriber];
if (subscription) {
subscription._releaseListeners();
delete _this._subscriptions[unsubscriber];
let forwardMsg = {
type: 'forward', from: _this._url, to: _this._owner,
body: { type: msg.type, from: unsubscriber, to: _this._url, identity: msg.body.identity }
};
_this._bus.postMessage(forwardMsg);
}
}
}
export default ReporterObject;