@rootintf/protocol-2phw 0.6.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/twoPhW.cjs.js +852 -0
- package/dist/twoPhW.es.mjs +842 -0
- package/jasmine.json +7 -0
- package/package.json +42 -0
- package/rollup.config.mjs +24 -0
- package/schema/schema_Msg2PhW.json +12 -0
- package/schema/schema_MsgEngage.json +12 -0
- package/schema/schema_MsgOptions.json +11 -0
- package/schema/schema_MsgWarp.json +16 -0
- package/schema/schema_Options_delay.json +11 -0
- package/src/ajv-gen-validate.cjs +24 -0
- package/src/twoPhW.mjs +551 -0
- package/src/validate-esm.mjs +1 -0
- package/tests/twoPhW-new.spec.mjs +159 -0
- package/tests/twoPhW.test.mjs +175 -0
package/src/twoPhW.mjs
ADDED
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
validate_Msg2PhW as validate_Msg2PhW_,
|
|
4
|
+
validate_MsgOptions as validate_MsgOptions_,
|
|
5
|
+
validate_Options_delay as validate_Options_delay_,
|
|
6
|
+
validate_MsgWarp as validate_MsgWarp_,
|
|
7
|
+
validate_MsgEngage as validate_MsgEngage_
|
|
8
|
+
} from './validate-esm.mjs';
|
|
9
|
+
|
|
10
|
+
import { BitLogr, l_LL } from '@knev/bitlogr';
|
|
11
|
+
import { JSON_Msg, JSON_Msg_msg, JSON_MsgAck } from '@rootintf/json-msg'
|
|
12
|
+
|
|
13
|
+
const k_TTL= 20000;
|
|
14
|
+
|
|
15
|
+
//-------------------------------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
let LOGR_= new BitLogr();
|
|
18
|
+
|
|
19
|
+
const l_msgs_ = {
|
|
20
|
+
MSG : 0b1 << 0,
|
|
21
|
+
WARP : 0b1 << 1,
|
|
22
|
+
ENGAGE : 0b1 << 2,
|
|
23
|
+
ACK : 0b1 << 3,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const l_= {
|
|
27
|
+
HANDLERS : 0b1 << 0,
|
|
28
|
+
DROPS : 0b1 << 1,
|
|
29
|
+
VALIDATION : 0b1 << 2,
|
|
30
|
+
... l_LL(l_msgs_, 8)
|
|
31
|
+
}
|
|
32
|
+
LOGR_.labels= l_;
|
|
33
|
+
|
|
34
|
+
//-------------------------------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
let map_Context_= new Map();
|
|
37
|
+
|
|
38
|
+
function set_ctx(key, ctx) {
|
|
39
|
+
map_Context_.set(key, ctx);
|
|
40
|
+
// console.log(map_Context_);
|
|
41
|
+
return ctx;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function get_ctx(key) {
|
|
45
|
+
return map_Context_.get(key);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//-------------------------------------------------------------------------------------------------
|
|
49
|
+
|
|
50
|
+
class Config {
|
|
51
|
+
receiver(callback_msgWarp,callback_msgEngage) {
|
|
52
|
+
this._callback_msgWarp= callback_msgWarp;
|
|
53
|
+
this._callback_msgEngage= callback_msgEngage;
|
|
54
|
+
}
|
|
55
|
+
sender(callback_msgAckWarp,callback_msgAckEngage ) {
|
|
56
|
+
this._callback_msgAckWarp= callback_msgAckWarp;
|
|
57
|
+
this._callback_msgAckEngage= callback_msgAckEngage;
|
|
58
|
+
}
|
|
59
|
+
set callback_msgOptions(callback) {
|
|
60
|
+
this._callback_msgOptions= callback;
|
|
61
|
+
}
|
|
62
|
+
set logr(obj_logr) {
|
|
63
|
+
LOGR_.toggled= obj_logr;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// var config = (function() {
|
|
68
|
+
// let _options;
|
|
69
|
+
|
|
70
|
+
// return {
|
|
71
|
+
// get callback_Abort() { return _options.callback_Abort; },
|
|
72
|
+
|
|
73
|
+
// set options(obj) {
|
|
74
|
+
// _options= obj;
|
|
75
|
+
// if (_options.logr) // && options_.logr[ __name(l_) ] )
|
|
76
|
+
// LOGR_.toggled= _options.logr
|
|
77
|
+
// }
|
|
78
|
+
// }
|
|
79
|
+
// })();
|
|
80
|
+
|
|
81
|
+
//-------------------------------------------------------------------------------------------------
|
|
82
|
+
|
|
83
|
+
let k_VERSION= "0.6";
|
|
84
|
+
|
|
85
|
+
var JSON_MsgOptions = (function() {
|
|
86
|
+
return {
|
|
87
|
+
create_with_msg_options : function(_msg, options) {
|
|
88
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
89
|
+
options: options,
|
|
90
|
+
"2PhW": k_VERSION
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
validate : function(json) {
|
|
94
|
+
if (! JSON_Msg_msg.validate(json))
|
|
95
|
+
return false;
|
|
96
|
+
if (! validate_Msg2PhW_(json)) {
|
|
97
|
+
LOGR_.log(l_.VALIDATION, 'JSON_MsgWarp: validate_Msg2PhW_: ', validate_Msg2PhW_.errors)
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (! validate_MsgOptions_(json)) {
|
|
101
|
+
LOGR_.log(l_.VALIDATION, 'JSON_MsgOptions: validate_MsgOptions_: ', validate_MsgOptions_.errors)
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
})();
|
|
108
|
+
|
|
109
|
+
var JSON_MsgWarp = (function() {
|
|
110
|
+
return {
|
|
111
|
+
create_with_user_hyperport : function(json_user, json_hyperport, nr_ttl_msec) {
|
|
112
|
+
return JSON_Msg.merge({
|
|
113
|
+
warp : {
|
|
114
|
+
user : json_user,
|
|
115
|
+
hyperport : json_hyperport,
|
|
116
|
+
ttl_msec : nr_ttl_msec
|
|
117
|
+
},
|
|
118
|
+
"2PhW": k_VERSION
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
validate : function(json) {
|
|
122
|
+
if (! JSON_Msg.validate(json))
|
|
123
|
+
return false;
|
|
124
|
+
if (! validate_Msg2PhW_(json)) {
|
|
125
|
+
LOGR_.log(l_.VALIDATION, 'JSON_MsgWarp: validate_Msg2PhW_: ', validate_Msg2PhW_.errors)
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
if (! validate_MsgWarp_(json)) {
|
|
129
|
+
LOGR_.log(l_.VALIDATION, 'JSON_MsgWarp: validate_MsgWarp_: ', validate_MsgWarp_.errors)
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
})();
|
|
136
|
+
|
|
137
|
+
var JSON_MsgEngage = (function() {
|
|
138
|
+
return {
|
|
139
|
+
create_with_MsgWarp : function(json_msgWarp) {
|
|
140
|
+
let json_msgEngage= JSON_Msg.merge({
|
|
141
|
+
engage : {
|
|
142
|
+
user : json_msgWarp.warp.user,
|
|
143
|
+
hyperport : json_msgWarp.warp.hyperport,
|
|
144
|
+
},
|
|
145
|
+
"2PhW": k_VERSION
|
|
146
|
+
});
|
|
147
|
+
json_msgEngage["id"]= json_msgWarp.id;
|
|
148
|
+
return json_msgEngage;
|
|
149
|
+
},
|
|
150
|
+
validate : function(json) {
|
|
151
|
+
if (! JSON_Msg.validate(json))
|
|
152
|
+
return false;
|
|
153
|
+
if (! validate_Msg2PhW_(json)) {
|
|
154
|
+
LOGR_.log(l_.VALIDATION, 'JSON_MsgWarp: validate_Msg2PhW_: ', validate_Msg2PhW_.errors)
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
if (! validate_MsgEngage_(json)) {
|
|
158
|
+
LOGR_.log(l_.VALIDATION, 'JSON_MsgEngage: validate_MsgEngage_: ', validate_MsgEngage_.errors)
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
})();
|
|
165
|
+
|
|
166
|
+
var JSON_MsgAckWarp = (function() {
|
|
167
|
+
return {
|
|
168
|
+
// createWith_msgAck_msg : function(json_msgAck, _msg) {
|
|
169
|
+
// const json_msgAckWarp= structuredClone(json_msgAck);
|
|
170
|
+
// json_msgAckWarp._msg= _msg;
|
|
171
|
+
// return json_msgAckWarp;
|
|
172
|
+
// }
|
|
173
|
+
create_with_msgExpired : function( json_msgWarp ) {
|
|
174
|
+
const json_msgAck= JSON_MsgAck.create_with_msgExpired( json_msgWarp )
|
|
175
|
+
json_msgAck["2PhW"]= k_VERSION;
|
|
176
|
+
return json_msgAck;
|
|
177
|
+
},
|
|
178
|
+
validate : function(json) {
|
|
179
|
+
if (! validate_Msg2PhW_(json)) {
|
|
180
|
+
LOGR_.log(l_.VALIDATION, 'JSON_MsgAckWarp: validate_Msg2PhW_: ', validate_Msg2PhW_.errors)
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
if (! JSON_MsgAck.validate(json))
|
|
184
|
+
return false;
|
|
185
|
+
if (! JSON_MsgWarp.validate(json._msg))
|
|
186
|
+
return false;
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
})();
|
|
191
|
+
|
|
192
|
+
var JSON_MsgAckEngage = (function() {
|
|
193
|
+
return {
|
|
194
|
+
// createWith_msgAck_msg : function(json_msgAck, _msg) {
|
|
195
|
+
// const json_msgAckEngage= structuredClone(json_msgAck);
|
|
196
|
+
// json_msgAckEngage._msg= _msg;
|
|
197
|
+
// return json_msgAckEngage;
|
|
198
|
+
// }
|
|
199
|
+
validate : function(json) {
|
|
200
|
+
if (! validate_Msg2PhW_(json)) {
|
|
201
|
+
LOGR_.log(l_.VALIDATION, 'JSON_MsgAckEngage: validate_Msg2PhW_: ', validate_Msg2PhW_.errors)
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
if (! JSON_MsgAck.validate(json))
|
|
205
|
+
return false;
|
|
206
|
+
if (! JSON_MsgEngage.validate(json._msg))
|
|
207
|
+
return false;
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
})();
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
//-------------------------------------------------------------------------------------------------
|
|
215
|
+
// State
|
|
216
|
+
|
|
217
|
+
class StateState {
|
|
218
|
+
constructor(initState) {
|
|
219
|
+
this.state_current_ = initState;
|
|
220
|
+
this.dt_start_= null;
|
|
221
|
+
this.nr_timeout_id_= -1;
|
|
222
|
+
this.nr_remaining_msec_= 0;
|
|
223
|
+
this.f_callback_= undefined;
|
|
224
|
+
}
|
|
225
|
+
start() {
|
|
226
|
+
this.dt_start_= Date.now();
|
|
227
|
+
console.log(`setTimeout: start ${this.dt_start_}, remaining ${this.nr_remaining_msec_}`);
|
|
228
|
+
this.nr_timeout_id_= setTimeout(this.f_callback_, this.nr_remaining_msec_);
|
|
229
|
+
}
|
|
230
|
+
changeState(newState, nr_ttl_msec, f_callback) {
|
|
231
|
+
if (! this.state_current_.changeState(newState))
|
|
232
|
+
return false;
|
|
233
|
+
this.state_current_ = newState;
|
|
234
|
+
|
|
235
|
+
clearTimeout(this.nr_timeout_id_);
|
|
236
|
+
if (nr_ttl_msec != undefined && f_callback != undefined) {
|
|
237
|
+
this.f_callback_= f_callback;
|
|
238
|
+
this.nr_remaining_msec_= nr_ttl_msec;
|
|
239
|
+
this.start();
|
|
240
|
+
}
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
delayTimeout(nr_delay_msec) {
|
|
244
|
+
clearTimeout(this.nr_timeout_id_);
|
|
245
|
+
const nr_elapsed_ms= Date.now() - this.dt_start_;
|
|
246
|
+
this.nr_remaining_msec_= Math.max(this.nr_remaining_msec_ - nr_elapsed_ms, 0) + nr_delay_msec;
|
|
247
|
+
this.start();
|
|
248
|
+
}
|
|
249
|
+
inState(stateClass) {
|
|
250
|
+
return this.state_current_ instanceof stateClass;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
class StateInitialState {
|
|
255
|
+
changeState(newState) {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// class State<Name> {
|
|
261
|
+
// changeState= function(newState) {
|
|
262
|
+
// return false;
|
|
263
|
+
// }
|
|
264
|
+
// }
|
|
265
|
+
|
|
266
|
+
// if (newState instanceof State<Name>)
|
|
267
|
+
// return true;
|
|
268
|
+
|
|
269
|
+
// if (! <Class>.inState( State<Name> ))
|
|
270
|
+
// if (! <Class>.changeState( new State<Name>() ))
|
|
271
|
+
|
|
272
|
+
//-------------------------------------------------------------------------------------------------
|
|
273
|
+
|
|
274
|
+
class ProtocolStateState extends StateState {
|
|
275
|
+
constructor(initState= new StateInitialState() ) {
|
|
276
|
+
super(initState);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
class StateWarp {
|
|
281
|
+
changeState(newState) {
|
|
282
|
+
if (newState instanceof StateAckWarp)
|
|
283
|
+
return true;
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
class StateAckWarp {
|
|
289
|
+
changeState(newState) {
|
|
290
|
+
if (newState instanceof StateEngage)
|
|
291
|
+
return true;
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
class StateEngage {
|
|
297
|
+
changeState(newState) {
|
|
298
|
+
if (newState instanceof StateAckEngage)
|
|
299
|
+
return true;
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
class StateAckEngage {
|
|
305
|
+
changeState(newState) {
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
class StateComplete {
|
|
311
|
+
changeState(newState) {
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
//-------------------------------------------------------------------------------------------------
|
|
317
|
+
|
|
318
|
+
function handler_msgOptions_(cfg, msg, json_msgOptions)
|
|
319
|
+
{
|
|
320
|
+
LOGR_.log(l_.HANDLERS, 'handler_msgOptions_: ...');
|
|
321
|
+
|
|
322
|
+
if (! validate_Options_delay_(json_msgOptions.options))
|
|
323
|
+
return false;
|
|
324
|
+
|
|
325
|
+
if (JSON_MsgWarp.validate(json_msgOptions._msg)) {
|
|
326
|
+
const json_msgWarp= json_msgOptions._msg;
|
|
327
|
+
|
|
328
|
+
const ctx= get_ctx(json_msgWarp.id);
|
|
329
|
+
if (!ctx )
|
|
330
|
+
return false;
|
|
331
|
+
|
|
332
|
+
const ttl_msec= json_msgOptions.options.ttl_msec_incr[0];
|
|
333
|
+
const ttl_incr= json_msgOptions.options.ttl_msec_incr[1];
|
|
334
|
+
|
|
335
|
+
//TODO: double check ttl_msec is the same as current accummulated ttl for idempotence.
|
|
336
|
+
|
|
337
|
+
LOGR_.log(l_.HANDLERS, 'handler_msgOptions_: ', json_msgOptions);
|
|
338
|
+
|
|
339
|
+
ctx.protocol.delayTimeout(ttl_incr);
|
|
340
|
+
|
|
341
|
+
if (cfg._callback_msgOptions)
|
|
342
|
+
return cfg._callback_msgOptions(msg, json_msgOptions);
|
|
343
|
+
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
//-------------------------------------------------------------------------------------------------
|
|
351
|
+
// Sender
|
|
352
|
+
|
|
353
|
+
function handler_msgAckWarp_(cfg, msg, json_msgAckWarp)
|
|
354
|
+
{
|
|
355
|
+
// NOTE: json_msgAck here has a string as _msg, instead of JSON.
|
|
356
|
+
|
|
357
|
+
LOGR_.log(l_.HANDLERS, 'handler_msgAckWarp: ...');
|
|
358
|
+
|
|
359
|
+
const json_msgWarp= json_msgAckWarp._msg;
|
|
360
|
+
|
|
361
|
+
const ctx= get_ctx(json_msgWarp.id);
|
|
362
|
+
if (!ctx || !ctx.protocol.changeState( new StateAckWarp() ))
|
|
363
|
+
return false;
|
|
364
|
+
|
|
365
|
+
//TODO: check if multiple warps have been sent out with the same destination
|
|
366
|
+
|
|
367
|
+
LOGR_.log(l_.HANDLERS, 'handler_msgAckWarp: ', json_msgAckWarp);
|
|
368
|
+
|
|
369
|
+
// const json_msgAckWarp= JSON_MsgAckWarp.createWith_msgAck_msg(json_msgAck, json_msgWarp);
|
|
370
|
+
|
|
371
|
+
if (! cfg._callback_msgAckWarp(msg, json_msgAckWarp))
|
|
372
|
+
return false;
|
|
373
|
+
|
|
374
|
+
const b_res= ctx.protocol.changeState( new StateEngage() );
|
|
375
|
+
console.assert(b_res);
|
|
376
|
+
|
|
377
|
+
return true;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function handler_msgAckEngage_(cfg, msg, json_msgAckEngage)
|
|
381
|
+
{
|
|
382
|
+
// NOTE: json_msgAck here has a string as _msg, instead of JSON.
|
|
383
|
+
|
|
384
|
+
LOGR_.log(l_.HANDLERS, 'handler_msgAckEngage: ...');
|
|
385
|
+
|
|
386
|
+
const json_msgEngage= json_msgAckEngage._msg;
|
|
387
|
+
|
|
388
|
+
const ctx= get_ctx(json_msgEngage.id);
|
|
389
|
+
if (!ctx || !ctx.protocol.changeState( new StateAckEngage() ))
|
|
390
|
+
return false;
|
|
391
|
+
|
|
392
|
+
LOGR_.log(l_.HANDLERS, 'handler_msgAckEngage: ', json_msgAckEngage);
|
|
393
|
+
|
|
394
|
+
// const json_msgAckEngage= JSON_MsgAckEngage.createWith_msgAck_msg(json_msgAck, json_msgEngage);
|
|
395
|
+
|
|
396
|
+
if (! cfg._callback_msgAckEngage(msg, json_msgAckEngage))
|
|
397
|
+
return false;
|
|
398
|
+
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
//-------------------------------------------------------------------------------------------------
|
|
403
|
+
// Receiver
|
|
404
|
+
|
|
405
|
+
function handler_msgWarp_(cfg, msg, json_msgWarp)
|
|
406
|
+
{
|
|
407
|
+
LOGR_.log(l_.HANDLERS, 'handler_msgWarp: ...');
|
|
408
|
+
|
|
409
|
+
let ctx= get_ctx(json_msgWarp.id);
|
|
410
|
+
if (ctx === undefined)
|
|
411
|
+
ctx= set_ctx(json_msgWarp.id, { protocol: new ProtocolStateState() });
|
|
412
|
+
|
|
413
|
+
if (!ctx || !ctx.protocol.changeState( new StateWarp() ))
|
|
414
|
+
return false;
|
|
415
|
+
|
|
416
|
+
LOGR_.log(l_.HANDLERS, 'handler_msgWarp: ', json_msgWarp);
|
|
417
|
+
|
|
418
|
+
if (! cfg._callback_msgWarp(msg, json_msgWarp))
|
|
419
|
+
return false;
|
|
420
|
+
|
|
421
|
+
const b_res= ctx.protocol.changeState( new StateAckWarp() );
|
|
422
|
+
console.assert(b_res);
|
|
423
|
+
|
|
424
|
+
return true;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function handler_msgEngage_(cfg, msg, json_msgEngage)
|
|
428
|
+
{
|
|
429
|
+
LOGR_.log(l_.HANDLERS, 'handler_msgEngage: ...');
|
|
430
|
+
|
|
431
|
+
const ctx= get_ctx(json_msgEngage.id);
|
|
432
|
+
if (!ctx || !ctx.protocol.changeState( new StateEngage() ))
|
|
433
|
+
return false;
|
|
434
|
+
|
|
435
|
+
LOGR_.log(l_.HANDLERS, 'handler_msgEngage: ', json_msgEngage);
|
|
436
|
+
|
|
437
|
+
if (! cfg._callback_msgEngage(msg, json_msgEngage))
|
|
438
|
+
return false;
|
|
439
|
+
|
|
440
|
+
const b_res= ctx.protocol.changeState( new StateAckEngage() );
|
|
441
|
+
console.assert(b_res);
|
|
442
|
+
|
|
443
|
+
return true;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
//-------------------------------------------------------------------------------------------------
|
|
447
|
+
|
|
448
|
+
class TwoPhW {
|
|
449
|
+
constructor() {
|
|
450
|
+
this._config= new Config();
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
get config() {
|
|
454
|
+
return this._config;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// this function need to be removed when a microservice db is used instead of a state machine
|
|
458
|
+
create_MsgWarp_out(str_id, json_user, json_hyperport, nr_ttl_msec)
|
|
459
|
+
{
|
|
460
|
+
if (nr_ttl_msec === undefined)
|
|
461
|
+
nr_ttl_msec= k_TTL;
|
|
462
|
+
|
|
463
|
+
const json_msgWarp= JSON_MsgWarp.create_with_user_hyperport(json_user, json_hyperport, nr_ttl_msec);
|
|
464
|
+
json_msgWarp.id= str_id;
|
|
465
|
+
|
|
466
|
+
const ctx= set_ctx(json_msgWarp.id, { protocol: new ProtocolStateState() });
|
|
467
|
+
const b_res= ctx.protocol.changeState( new StateWarp(), nr_ttl_msec, function() {
|
|
468
|
+
const json_Nack= JSON_MsgAckWarp.create_with_msgExpired( json_msgWarp );
|
|
469
|
+
this._config._callback_msgAckWarp(json_Nack, json_Nack);
|
|
470
|
+
}.bind(this) );
|
|
471
|
+
|
|
472
|
+
console.assert(b_res);
|
|
473
|
+
return json_msgWarp;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
handler_msg_json(msg, json_msg)
|
|
477
|
+
{
|
|
478
|
+
LOGR_.log(l_.HANDLERS, 'handler_msg_json: ', json_msg);
|
|
479
|
+
|
|
480
|
+
// ADR: I could parse json_Msg_msg first and then MsgAck or MsgOptions, but
|
|
481
|
+
// doing so means super classes get parsed multiple times for no reason
|
|
482
|
+
|
|
483
|
+
if (JSON_MsgOptions.validate(json_msg) && handler_msgOptions_(this._config, msg, json_msg))
|
|
484
|
+
return true;
|
|
485
|
+
|
|
486
|
+
if (JSON_MsgWarp.validate(json_msg) && handler_msgWarp_(this._config, msg, json_msg))
|
|
487
|
+
return true;
|
|
488
|
+
|
|
489
|
+
if (JSON_MsgEngage.validate(json_msg) && handler_msgEngage_(this._config, msg, json_msg))
|
|
490
|
+
return true;
|
|
491
|
+
|
|
492
|
+
if (JSON_MsgAckWarp.validate(json_msg) && handler_msgAckWarp_(this._config, msg, json_msg))
|
|
493
|
+
return true;
|
|
494
|
+
|
|
495
|
+
if (JSON_MsgAckEngage.validate(json_msg) && handler_msgAckEngage_(this._config, msg, json_msg))
|
|
496
|
+
return true;
|
|
497
|
+
|
|
498
|
+
// https://attacomsian.com/blog/javascript-iterate-objects
|
|
499
|
+
// Object.keys(json_msg).forEach((key, index) => {
|
|
500
|
+
// if (key == 'dt')
|
|
501
|
+
// return;
|
|
502
|
+
// if (key == 'referer')
|
|
503
|
+
// return;
|
|
504
|
+
// console.log(`${key}: ${json_msg[key]}`);
|
|
505
|
+
// });
|
|
506
|
+
|
|
507
|
+
return false;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
handler_msg_str(msg, str_msg)
|
|
511
|
+
{
|
|
512
|
+
LOGR_.log(l_.HANDLERS, 'handler_msg_str: ['+ str_msg +']');
|
|
513
|
+
|
|
514
|
+
// ... test strings here
|
|
515
|
+
|
|
516
|
+
let json_msg;
|
|
517
|
+
try {
|
|
518
|
+
json_msg= JSON.parse(str_msg);
|
|
519
|
+
}
|
|
520
|
+
catch (err) {
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
return this.handler_msg_json(msg, json_msg);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
handler_msg(msg)
|
|
528
|
+
{
|
|
529
|
+
LOGR_.log(l_.HANDLERS, 'handler_msg: ', msg);
|
|
530
|
+
|
|
531
|
+
if (typeof (msg) === 'string' && this.handler_msg_str(msg, msg))
|
|
532
|
+
return true;
|
|
533
|
+
|
|
534
|
+
if (typeof (msg) === 'object' && this.handler_msg_json(msg, msg))
|
|
535
|
+
return true;
|
|
536
|
+
|
|
537
|
+
LOGR_.log(l_.DROPS, "handler_msg: DROP! ", msg);
|
|
538
|
+
return false
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
export {
|
|
544
|
+
TwoPhW,
|
|
545
|
+
JSON_MsgOptions,
|
|
546
|
+
JSON_MsgWarp,
|
|
547
|
+
JSON_MsgEngage,
|
|
548
|
+
JSON_MsgAckWarp,
|
|
549
|
+
JSON_MsgAckEngage,
|
|
550
|
+
l_ as l
|
|
551
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";export const validate_Msg2PhW = validate10;const schema11 = {"$id":"validate_Msg2PhW","$schema":"http://json-schema.org/draft-07/schema#","type":"object","required":["2PhW"],"properties":{"2PhW":{"type":"string","pattern":"0\\.6"}}};const pattern0 = new RegExp("0\\.6", "u");function validate10(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){/*# sourceURL="validate_Msg2PhW" */;let vErrors = null;let errors = 0;if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data["2PhW"] === undefined) && (missing0 = "2PhW")){validate10.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data["2PhW"] !== undefined){let data0 = data["2PhW"];const _errs1 = errors;if(errors === _errs1){if(typeof data0 === "string"){if(!pattern0.test(data0)){validate10.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"#/properties/2PhW/pattern",keyword:"pattern",params:{pattern: "0\\.6"},message:"must match pattern \""+"0\\.6"+"\""}];return false;}}else {validate10.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"#/properties/2PhW/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}}}}}else {validate10.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate10.errors = vErrors;return errors === 0;}export const validate_MsgOptions = validate11;const schema12 = {"$id":"validate_MsgOptions","$schema":"http://json-schema.org/draft-07/schema#","type":"object","required":["options"],"properties":{"options":{"type":"object"}}};function validate11(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){/*# sourceURL="validate_MsgOptions" */;let vErrors = null;let errors = 0;if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.options === undefined) && (missing0 = "options")){validate11.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.options !== undefined){let data0 = data.options;if(!(data0 && typeof data0 == "object" && !Array.isArray(data0))){validate11.errors = [{instancePath:instancePath+"/options",schemaPath:"#/properties/options/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}else {validate11.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate11.errors = vErrors;return errors === 0;}export const validate_Options_delay = validate12;const schema13 = {"$id":"validate_Options_delay","$schema":"http://json-schema.org/draft-07/schema#","type":"object","required":["ttl_msec_incr"],"properties":{"ttl_msec":{"type":"array"}}};function validate12(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){/*# sourceURL="validate_Options_delay" */;let vErrors = null;let errors = 0;if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ttl_msec_incr === undefined) && (missing0 = "ttl_msec_incr")){validate12.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ttl_msec !== undefined){if(!(Array.isArray(data.ttl_msec))){validate12.errors = [{instancePath:instancePath+"/ttl_msec",schemaPath:"#/properties/ttl_msec/type",keyword:"type",params:{type: "array"},message:"must be array"}];return false;}}}}else {validate12.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate12.errors = vErrors;return errors === 0;}export const validate_MsgWarp = validate13;const schema14 = {"$id":"validate_MsgWarp","$schema":"http://json-schema.org/draft-07/schema#","type":"object","required":["warp"],"properties":{"warp":{"type":"object","required":["user","hyperport"],"properties":{"ttl_msec":{"type":"integer"}}}}};function validate13(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){/*# sourceURL="validate_MsgWarp" */;let vErrors = null;let errors = 0;if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.warp === undefined) && (missing0 = "warp")){validate13.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.warp !== undefined){let data0 = data.warp;const _errs1 = errors;if(errors === _errs1){if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if(((data0.user === undefined) && (missing1 = "user")) || ((data0.hyperport === undefined) && (missing1 = "hyperport"))){validate13.errors = [{instancePath:instancePath+"/warp",schemaPath:"#/properties/warp/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ttl_msec !== undefined){let data1 = data0.ttl_msec;if(!(((typeof data1 == "number") && (!(data1 % 1) && !isNaN(data1))) && (isFinite(data1)))){validate13.errors = [{instancePath:instancePath+"/warp/ttl_msec",schemaPath:"#/properties/warp/properties/ttl_msec/type",keyword:"type",params:{type: "integer"},message:"must be integer"}];return false;}}}}else {validate13.errors = [{instancePath:instancePath+"/warp",schemaPath:"#/properties/warp/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate13.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate13.errors = vErrors;return errors === 0;}export const validate_MsgEngage = validate14;const schema15 = {"$id":"validate_MsgEngage","$schema":"http://json-schema.org/draft-07/schema#","type":"object","required":["engage"],"properties":{"engage":{"type":"object","required":["user","hyperport"]}}};function validate14(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){/*# sourceURL="validate_MsgEngage" */;let vErrors = null;let errors = 0;if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.engage === undefined) && (missing0 = "engage")){validate14.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.engage !== undefined){let data0 = data.engage;const _errs1 = errors;if(errors === _errs1){if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if(((data0.user === undefined) && (missing1 = "user")) || ((data0.hyperport === undefined) && (missing1 = "hyperport"))){validate14.errors = [{instancePath:instancePath+"/engage",schemaPath:"#/properties/engage/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}}else {validate14.errors = [{instancePath:instancePath+"/engage",schemaPath:"#/properties/engage/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate14.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate14.errors = vErrors;return errors === 0;}
|