forked from hubiquitus/hubiquitus-client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hubiquitus.js
410 lines (339 loc) · 16.5 KB
/
hubiquitus.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
/*
* Copyright (c) Novedia Group 2012.
*
* This file is part of Hubiquitus
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* You should have received a copy of the MIT License along with Hubiquitus.
* If not, see <http://opensource.org/licenses/mit-license.php>.
*/
//Make it compatible with node and web browser
if (typeof define !== 'function') { var define = require('amdefine')(module) }
define(
['./lib/transports/socketio/hsession-socketio',
'./lib/options', './lib/codes'],
function(hSessionSocketIO, createOptions, codes){
var statuses = codes.statuses;
var errors = codes.errors;
var hResultStatus = codes.hResultStatus;
/**
* Creates a new client that manages a connection and connects to the
* gateway.
*/
var HubiquitusClient = function(){
this.onStatus = function(hStatus){};
this.onMessage = function(hMessage){};
this.msgToBeAnswered = {};
this.status = statuses.DISCONNECTED;
};
HubiquitusClient.prototype = {
connect : function(login, password, hOptions, context){
var code = this.status == statuses.CONNECTED ?
errors.ALREADY_CONNECTED : errors.CONN_PROGRESS;
if( this.status == statuses.CONNECTED || this.status == statuses.CONNECTING){
this.onStatus({
status: this.status,
errorCode: code
});
return;
}
this.hOptions = createOptions.hub_options(hOptions || {});
var transportCB = function(type, value){
//'this' is correct because of the bind
switch(type){
case 'hStatus':
if((value.status !== statuses.CONNECTED) || (value.status !== statuses.CONNECTED && this.fullurn !== undefined)){
this.status = value.status;
this.onStatus(value);
}
break;
case 'hMessage':
this.onMessageInternal(value);
break;
case 'attrs':
this.fullurn = value.publisher;
this.resource = this.fullurn.replace(/^.*\//, "")
if(this.status !== statuses.CONNECTED)
{
this.status = statuses.CONNECTED;
this.onStatus({
status:statuses.CONNECTED,
errorCode: errors.NO_ERROR
});
}
//Set Domain and publisher
this.domain = this.splitURN(this.fullurn)[0];
this.publisher = this.bareURN(this.fullurn);
}
};
//Load Balancing
var endpoints = this.hOptions.endpoints;
this.hOptions.endpoint =
endpoints[Math.floor(Math.random()*endpoints.length)];
//Instantiate correct transport
var self = this;
switch(this.hOptions.transport){
default:
this.transport = new hSessionSocketIO.hSessionSocketIO(login, password, context, function(type, value) {transportCB.call(self, type, value)}, this.hOptions);
}
//Establish the connection
this.transport.connect(login);
},
onMessageInternal : function(hMessage) {
var ref;
if (hMessage && hMessage.ref && typeof hMessage.ref === 'string')
ref = hMessage.ref.split("#")[0];
if (ref)
var cb = this.msgToBeAnswered[ref];
if (cb) {
delete this.msgToBeAnswered[ref];
cb(hMessage);
} else
this.onMessage(hMessage);
},
disconnect : function(){
if(this.transport){
this.transport.disconnect();
delete this.transport;
}else{
this.onStatus({
status: codes.statuses.DISCONNECTED,
errorCode: codes.errors.NOT_CONNECTED
});
}
},
subscribe : function(actor, cb){
if(!actor && cb)
return cb(this.buildResult("Unkonwn", "Unknown", hResultStatus.MISSING_ATTR, "Missing actor"));
var hMessage = this.buildCommand(actor, 'hSubscribe');
if(hMessage.timeout === undefined)
hMessage.timeout = this.hOptions.msgTimeout
this.send(hMessage, cb);
},
unsubscribe : function(actor, cb){
if(!actor && cb)
return cb(this.buildResult("Unkonwn", "Unknown", hResultStatus.MISSING_ATTR, "Missing actor"));
var hMessage = this.buildCommand("session", 'hUnsubscribe', {channel:actor});
if(hMessage.timeout === undefined)
hMessage.timeout = this.hOptions.msgTimeout
this.send(hMessage, cb);
},
send : function(hMessage, cb){
if(!(hMessage instanceof Object))
return cb(this.buildResult("Unkonwn", "Unknown", hResultStatus.MISSING_ATTR, "provided hMessage should be an object"));
hMessage.publisher = this.fullurn;
hMessage.msgid = UUID.generate();
hMessage.published = hMessage.published || new Date();
hMessage.sent = new Date();
//Complete hCommand
var errorCode = undefined;
var errorMsg = undefined;
//Verify if well formatted
if(!hMessage.actor){
errorCode = codes.hResultStatus.MISSING_ATTR;
errorMsg = 'the actor attribute is missing';
}else if(this.status != statuses.CONNECTED){
errorCode = codes.hResultStatus.NOT_CONNECTED;
errorMsg = 'client not connected, cannot send command';
}
if(!errorCode){
//if there is a callback and no timeout, timeout is set to default value of 30s
//Add it to the open message to call cb later
if(cb) {
if(hMessage.timeout > 0){
this.msgToBeAnswered[hMessage.msgid] = cb;
var timeout = hMessage.timeout;
var self = this;
//if no response in time we call a timeout
setInterval(function(){
if(self.msgToBeAnswered[hMessage.msgid]) {
delete self.msgToBeAnswered[hMessage.msgid];
if(hMessage.payload && typeof hMessage.payload === 'object')
cmd = hMessage.payload.cmd;
var errCode = codes.hResultStatus.EXEC_TIMEOUT;
var errMsg = 'No response was received within the ' + timeout + ' timeout';
var resultMsg = self.buildResult(hMessage.publisher, hMessage.msgid, errCode, errMsg);
cb(resultMsg);
}
},timeout);
}
else
hMessage.timeout = 0;
}
else
hMessage.timeout = 0;
//Send it to transport
this.transport.sendhMessage(hMessage);
} else if(cb) {
var cmd;
if(hMessage.payload && typeof hMessage.payload === 'string')
cmd = hMessage.payload.cmd;
var actor = hMessage.actor || 'Unknown';
var resultMsg = this.buildResult(actor, hMessage.msgid, errorCode, errorMsg);
cb(resultMsg);
}
},
getSubscriptions: function(cb){
var hMessage = this.buildCommand("session", 'hGetSubscriptions');
if(hMessage.timeout === undefined)
hMessage.timeout = this.hOptions.msgTimeout
this.send(hMessage, cb);
},
setFilter: function(filter, cb){
if(!filter && cb)
return cb(this.buildResult("Unkonwn", "Unknown", hResultStatus.MISSING_ATTR, "Missing filter"));
var hMessage = this.buildCommand("session", 'hSetFilter', filter);
if(hMessage.timeout === undefined)
hMessage.timeout = this.hOptions.msgTimeout
this.filter = filter
this.send(hMessage, cb);
},
buildCommand: function(actor, cmd, params, options){
params = params || {};
options = options || {};
if(!cmd)
throw new Error('missing cmd');
var hCommand = {cmd: cmd, params:params};
return this.buildMessage(actor, 'hCommand', hCommand, options);
},
buildResult: function(actor, ref, status, result, options) {
options = options || {};
if(status === undefined || status === null)
throw new Error('missing status');
if(!ref)
throw new Error('missing ref');
var hResult = {status: status, result: result};
options.ref = ref;
return this.buildMessage(actor, 'hResult', hResult, options);
},
buildMessage: function(actor, type, payload, options){
options = options || {};
if(!actor)
throw new Error('missing actor');
var hMessage = {};
hMessage.actor = actor;
if(options.ref)
hMessage.ref = options.ref;
if(options.convid)
hMessage.convid = options.convid;
if(type)
hMessage.type = type;
if(options.priority)
hMessage.priority = options.priority;
if(options.relevance)
hMessage.relevance = options.relevance;
if(options.relevanceOffset){
var currentDate = new Date();
hMessage.relevance = new Date(currentDate.getTime() + options.relevanceOffset)
}
if(options.persistent !== null || options.persistent !== undefined)
hMessage.persistent = options.persistent;
if(options.location)
hMessage.location = options.location;
if(options.author)
hMessage.author = options.author;
if(options.published)
hMessage.published = options.published;
if(options.headers)
hMessage.headers = options.headers;
if(payload)
hMessage.payload = payload;
if(options.timeout)
hMessage.timeout = options.timeout;
return hMessage;
},
buildMeasure: function(actor, value, unit, options){
if(!value)
throw new Error('missing value');
else if (!unit)
throw new Error('missing unit');
return this.buildMessage(actor, 'hMeasure', {unit: unit, value: value}, options);
},
buildAlert: function(actor, alert, options){
if(!alert)
throw new Error('missing alert');
return this.buildMessage(actor, 'hAlert', {alert: alert}, options);
},
buildAck: function(actor, ref, ack, options){
if(!ack)
throw new Error('missing ack');
if(!ref)
throw new Error('missing ref');
else if(!/recv|read/i.test(ack))
throw new Error('ack does not match "recv" or "read"');
if(typeof options !== 'object')
options = {};
options.ref = ref;
return this.buildMessage(actor, 'hAck', {ack: ack}, options);
},
buildConvState: function(actor, convid, status, options){
if(!convid)
throw new Error('missing convid');
else if(!status)
throw new Error('missing status');
if(!options)
options = {};
options.convid = convid;
return this.buildMessage(actor, 'hConvState', {status: status}, options);
},
validateFullURN: function(urn) {
return /(^urn:[a-zA-Z0-9]{1}[a-zA-Z0-9\-.]+:[a-zA-Z0-9_,=@;!'%/#\(\)\+\-\.\$\*\?]+\/.+$)/.test(urn);
},
splitURN:function(urn) {
var splitted;
if (typeof urn === "string") {
splitted = urn.split(":");
}
if (splitted) {
if (this.validateFullURN(urn)) {
splitted[3] = splitted[2].replace(/(^[^\/]*\/)/, "");
splitted[2] = splitted[2].replace(/\/.*$/g, "");
}
return splitted.splice(1, 3);
} else {
return [undefined, undefined, undefined];
}
},
getBareURN: function(urn) {
var urnParts;
urnParts = this.splitURN(urn);
return "urn:" + urnParts[0] + ":" + urnParts[1];
},
// Deprecated : This function is here for retro compatiblity.
bareURN: function(urn) {
var urnParts;
urnParts = this.splitURN(urn);
return "urn:" + urnParts[0] + ":" + urnParts[1];
},
errors: codes.errors,
statuses: codes.statuses,
hResultStatus: codes.hResultStatus
};
if(typeof module !== 'undefined' && module.exports){
//Entrypoint to hClient in Node mode
exports.hClient = new HubiquitusClient();
exports.HubiquitusClient = HubiquitusClient; //Allow access to constructor (used with stress option)
exports.statuses = codes.statuses;
exports.hResultStatus = codes.hResultStatus;
}else{
//Global entrypoint to hClient in Browser mode
hClient = new HubiquitusClient();
}
}
);
function UUID(){}UUID.generate=function(){var a=UUID._gri,b=UUID._ha;return b(a(32),8)+"-"+b(a(16),4)+"-"+b(16384|a(12),4)+"-"+b(32768|a(14),4)+"-"+b(a(48),12)};UUID._gri=function(a){return 0>a?NaN:30>=a?0|Math.random()*(1<<a):53>=a?(0|1073741824*Math.random())+1073741824*(0|Math.random()*(1<<a-30)):NaN};UUID._ha=function(a,b){for(var c=a.toString(16),d=b-c.length,e="0";0<d;d>>>=1,e+=e)d&1&&(c=e+c);return c};