@rootintf/protocol-2phw 0.7.1 → 1.0.28-api-0.8

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/src/twoPhW.mjs CHANGED
@@ -1,582 +1,813 @@
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.7";
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_msgOk : function(json_msgWarp, b_ok= true) {
174
- const json_msgAck= JSON_MsgAck.create_with_msgOk(json_msgWarp, b_ok);
175
- json_msgAck["2PhW"]= k_VERSION;
176
- return json_msgAck;
177
- },
178
- create_with_msgExpired : function( json_msgWarp ) {
179
- const json_msgAck= JSON_MsgAck.create_with_msgExpired( json_msgWarp )
180
- json_msgAck["2PhW"]= k_VERSION;
181
- return json_msgAck;
182
- },
183
- validate : function(json) {
184
- if (! validate_Msg2PhW_(json)) {
185
- LOGR_.log(l_.VALIDATION, 'JSON_MsgAckWarp: validate_Msg2PhW_: ', validate_Msg2PhW_.errors)
186
- return false;
187
- }
188
- if (! JSON_MsgAck.validate(json))
189
- return false;
190
- if (! JSON_MsgWarp.validate(json._msg))
191
- return false;
192
- return true;
193
- }
194
- }
195
- })();
196
-
197
- var JSON_MsgAckEngage = (function() {
198
- return {
199
- // createWith_msgAck_msg : function(json_msgAck, _msg) {
200
- // const json_msgAckEngage= structuredClone(json_msgAck);
201
- // json_msgAckEngage._msg= _msg;
202
- // return json_msgAckEngage;
203
- // }
204
- create_with_msgOk : function(json_msgEngage, b_ok= true) {
205
- const json_msgAck= JSON_MsgAck.create_with_msgOk(json_msgEngage, b_ok);
206
- json_msgAck["2PhW"]= k_VERSION;
207
- return json_msgAck;
208
- },
209
- create_with_msgExpired : function( json_msgEngage ) {
210
- const json_msgAck= JSON_MsgAck.create_with_msgExpired( json_msgEngage )
211
- json_msgAck["2PhW"]= k_VERSION;
212
- return json_msgAck;
213
- },
214
- validate : function(json) {
215
- if (! validate_Msg2PhW_(json)) {
216
- LOGR_.log(l_.VALIDATION, 'JSON_MsgAckEngage: validate_Msg2PhW_: ', validate_Msg2PhW_.errors)
217
- return false;
218
- }
219
- if (! JSON_MsgAck.validate(json))
220
- return false;
221
- if (! JSON_MsgEngage.validate(json._msg))
222
- return false;
223
- return true;
224
- }
225
- }
226
- })();
227
-
228
-
229
- //-------------------------------------------------------------------------------------------------
230
- // State
231
-
232
- class StateState {
233
- constructor(initState) {
234
- this.state_current_ = initState;
235
- this.dt_start_= null;
236
- this.nr_timeout_id_= -1;
237
- this.nr_remaining_msec_= 0;
238
- this.f_callback_= undefined;
239
- }
240
- start() {
241
- this.dt_start_= Date.now();
242
- console.log(`setTimeout: start ${this.dt_start_}, remaining ${this.nr_remaining_msec_}`);
243
- this.nr_timeout_id_= setTimeout(this.f_callback_, this.nr_remaining_msec_);
244
- }
245
- changeState(newState, nr_ttl_msec, f_callback) {
246
- if (! this.state_current_.changeState(newState))
247
- return false;
248
- this.state_current_ = newState;
249
-
250
- clearTimeout(this.nr_timeout_id_);
251
- if (nr_ttl_msec != undefined && f_callback != undefined) {
252
- this.f_callback_= f_callback;
253
- this.nr_remaining_msec_= nr_ttl_msec;
254
- this.start();
255
- }
256
- return true;
257
- }
258
- delayTimeout(nr_delay_msec) {
259
- clearTimeout(this.nr_timeout_id_);
260
- const nr_elapsed_ms= Date.now() - this.dt_start_;
261
- this.nr_remaining_msec_= Math.max(this.nr_remaining_msec_ - nr_elapsed_ms, 0) + nr_delay_msec;
262
- this.start();
263
- }
264
- inState(stateClass) {
265
- return this.state_current_ instanceof stateClass;
266
- }
267
- }
268
-
269
- class StateInitialState {
270
- changeState(newState) {
271
- return true;
272
- }
273
- }
274
-
275
- // class State<Name> {
276
- // changeState= function(newState) {
277
- // return false;
278
- // }
279
- // }
280
-
281
- // if (newState instanceof State<Name>)
282
- // return true;
283
-
284
- // if (! <Class>.inState( State<Name> ))
285
- // if (! <Class>.changeState( new State<Name>() ))
286
-
287
- //-------------------------------------------------------------------------------------------------
288
-
289
- class ProtocolStateState extends StateState {
290
- constructor(initState= new StateInitialState() ) {
291
- super(initState);
292
- }
293
- }
294
-
295
- class StateWarp {
296
- changeState(newState) {
297
- if (newState instanceof StateAckWarp)
298
- return true;
299
- if (newState instanceof StateComplete)
300
- return true;
301
- return false;
302
- }
303
- }
304
-
305
- class StateAckWarp {
306
- changeState(newState) {
307
- if (newState instanceof StateEngage)
308
- return true;
309
- if (newState instanceof StateComplete)
310
- return true;
311
- return false;
312
- }
313
- }
314
-
315
- class StateEngage {
316
- changeState(newState) {
317
- if (newState instanceof StateComplete)
318
- return true;
319
- return false;
320
- }
321
- }
322
-
323
- class StateComplete {
324
- changeState(newState) {
325
- return false;
326
- }
327
- }
328
-
329
- //-------------------------------------------------------------------------------------------------
330
-
331
- function handler_msgOptions_(cfg, msg, json_msgOptions)
332
- {
333
- LOGR_.log(l_.HANDLERS, 'handler_msgOptions_: ...');
334
-
335
- if (! validate_Options_delay_(json_msgOptions.options))
336
- return false;
337
-
338
- if (JSON_MsgWarp.validate(json_msgOptions._msg)) {
339
- const json_msgWarp= json_msgOptions._msg;
340
-
341
- const ctx= get_ctx(json_msgWarp.id);
342
- if (!ctx )
343
- return false;
344
-
345
- const ttl_msec= json_msgOptions.options.ttl_msec_incr[0];
346
- const ttl_incr= json_msgOptions.options.ttl_msec_incr[1];
347
-
348
- //TODO: double check ttl_msec is the same as current accummulated ttl for idempotence.
349
-
350
- LOGR_.log(l_.HANDLERS, 'handler_msgOptions_: ', json_msgOptions);
351
-
352
- ctx.protocol.delayTimeout(ttl_incr);
353
-
354
- if (cfg._callback_msgOptions)
355
- return cfg._callback_msgOptions(msg, json_msgOptions);
356
-
357
- return true;
358
- }
359
-
360
- return false;
361
- }
362
-
363
- //-------------------------------------------------------------------------------------------------
364
- // Sender
365
-
366
- function handler_msgAckWarp_(cfg, msg, json_msgAckWarp)
367
- {
368
- // NOTE: json_msgAck here has a string as _msg, instead of JSON.
369
-
370
- LOGR_.log(l_.HANDLERS, 'handler_msgAckWarp: ...');
371
-
372
- const json_msgWarp= json_msgAckWarp._msg;
373
-
374
- const ctx= get_ctx(json_msgWarp.id);
375
- if (!ctx || !ctx.protocol.changeState( new StateAckWarp() ))
376
- return false;
377
-
378
- //TODO: check if multiple warps have been sent out with the same destination
379
-
380
- LOGR_.log(l_.HANDLERS, 'handler_msgAckWarp: ', json_msgAckWarp);
381
-
382
- // const json_msgAckWarp= JSON_MsgAckWarp.createWith_msgAck_msg(json_msgAck, json_msgWarp);
383
-
384
- if (! cfg._callback_msgAckWarp(msg, json_msgAckWarp))
385
- return false;
386
-
387
- if (json_msgWarp.warp.instantaneous) {
388
- const b_res= ctx.protocol.changeState( new StateComplete() );
389
- console.assert(b_res);
390
-
391
- return true;
392
- }
393
-
394
- const b_res= ctx.protocol.changeState( new StateEngage() );
395
- console.assert(b_res);
396
-
397
- return true;
398
- }
399
-
400
- function handler_msgAckEngage_(cfg, msg, json_msgAckEngage)
401
- {
402
- // NOTE: json_msgAck here has a string as _msg, instead of JSON.
403
-
404
- LOGR_.log(l_.HANDLERS, 'handler_msgAckEngage: ...');
405
-
406
- const json_msgEngage= json_msgAckEngage._msg;
407
-
408
- const ctx= get_ctx(json_msgEngage.id);
409
- if (!ctx || !ctx.protocol.changeState( new StateAckEngage() ))
410
- return false;
411
-
412
- LOGR_.log(l_.HANDLERS, 'handler_msgAckEngage: ', json_msgAckEngage);
413
-
414
- // const json_msgAckEngage= JSON_MsgAckEngage.createWith_msgAck_msg(json_msgAck, json_msgEngage);
415
-
416
- if (! cfg._callback_msgAckEngage(msg, json_msgAckEngage))
417
- return false;
418
-
419
- return true;
420
- }
421
-
422
- //-------------------------------------------------------------------------------------------------
423
- // Receiver
424
-
425
- function handler_msgWarp_(cfg, msg, json_msgWarp)
426
- {
427
- LOGR_.log(l_.HANDLERS, 'handler_msgWarp: ...');
428
-
429
- let ctx= get_ctx(json_msgWarp.id);
430
- if (ctx === undefined)
431
- ctx= set_ctx(json_msgWarp.id, { protocol: new ProtocolStateState() });
432
-
433
- if (!ctx || !ctx.protocol.changeState( new StateWarp() ))
434
- return false;
435
-
436
- LOGR_.log(l_.HANDLERS, 'handler_msgWarp: ', json_msgWarp);
437
-
438
- if (! cfg._callback_msgWarp(msg, json_msgWarp))
439
- return false;
440
-
441
- if (json_msgWarp.warp.instantaneous) {
442
- const b_res= ctx.protocol.changeState( new StateComplete() );
443
- console.assert(b_res);
444
-
445
- return true;
446
- }
447
-
448
- const b_res= ctx.protocol.changeState( new StateAckWarp() );
449
- console.assert(b_res);
450
-
451
- return true;
452
- }
453
-
454
- function handler_msgEngage_(cfg, msg, json_msgEngage)
455
- {
456
- LOGR_.log(l_.HANDLERS, 'handler_msgEngage: ...');
457
-
458
- const ctx= get_ctx(json_msgEngage.id);
459
- if (!ctx || !ctx.protocol.changeState( new StateEngage() ))
460
- return false;
461
-
462
- LOGR_.log(l_.HANDLERS, 'handler_msgEngage: ', json_msgEngage);
463
-
464
- if (! cfg._callback_msgEngage(msg, json_msgEngage))
465
- return false;
466
-
467
- const b_res= ctx.protocol.changeState( new StateComplete() );
468
- console.assert(b_res);
469
-
470
- return true;
471
- }
472
-
473
- //-------------------------------------------------------------------------------------------------
474
-
475
- class TwoPhW {
476
- constructor() {
477
- this._config= new Config();
478
- }
479
-
480
- get config() {
481
- return this._config;
482
- }
483
-
484
- // this function need to be removed when a microservice db is used instead of a state machine
485
- create_MsgWarp_out(str_id, json_user, json_hyperport, nr_ttl_msec, b_instantaneous)
486
- {
487
- if (nr_ttl_msec === undefined)
488
- nr_ttl_msec= k_TTL;
489
-
490
- const json_msgWarp= JSON_MsgWarp.create_with_user_hyperport(json_user, json_hyperport, nr_ttl_msec);
491
- json_msgWarp.id= str_id;
492
-
493
- if (b_instantaneous !== undefined)
494
- json_msgWarp.warp.instantaneous= b_instantaneous;
495
-
496
- const ctx= set_ctx(json_msgWarp.id, { protocol: new ProtocolStateState() });
497
- const b_res= ctx.protocol.changeState( new StateWarp(), nr_ttl_msec, function() {
498
- const json_Nack= JSON_MsgAckWarp.create_with_msgExpired( json_msgWarp );
499
- //TODO: the referer is NULL here !
500
- this._config._callback_msgAckWarp(json_Nack, json_Nack);
501
- }.bind(this) );
502
-
503
- console.assert(b_res);
504
- return json_msgWarp;
505
- }
506
-
507
- handler_msg_json(msg, json_msg)
508
- {
509
- LOGR_.log(l_.HANDLERS, 'handler_msg_json: ', json_msg);
510
-
511
- // ADR: I could parse json_Msg_msg first and then MsgAck or MsgOptions, but
512
- // doing so means super classes get parsed multiple times for no reason
513
-
514
- if (JSON_MsgOptions.validate(json_msg) && handler_msgOptions_(this._config, msg, json_msg))
515
- return true;
516
-
517
- if (JSON_MsgWarp.validate(json_msg) && handler_msgWarp_(this._config, msg, json_msg))
518
- return true;
519
-
520
- if (JSON_MsgEngage.validate(json_msg) && handler_msgEngage_(this._config, msg, json_msg))
521
- return true;
522
-
523
- if (JSON_MsgAckWarp.validate(json_msg) && handler_msgAckWarp_(this._config, msg, json_msg))
524
- return true;
525
-
526
- if (JSON_MsgAckEngage.validate(json_msg) && handler_msgAckEngage_(this._config, msg, json_msg))
527
- return true;
528
-
529
- // https://attacomsian.com/blog/javascript-iterate-objects
530
- // Object.keys(json_msg).forEach((key, index) => {
531
- // if (key == 'dt')
532
- // return;
533
- // if (key == 'referer')
534
- // return;
535
- // console.log(`${key}: ${json_msg[key]}`);
536
- // });
537
-
538
- return false;
539
- }
540
-
541
- handler_msg_str(msg, str_msg)
542
- {
543
- LOGR_.log(l_.HANDLERS, 'handler_msg_str: ['+ str_msg +']');
544
-
545
- // ... test strings here
546
-
547
- let json_msg;
548
- try {
549
- json_msg= JSON.parse(str_msg);
550
- }
551
- catch (err) {
552
- return false;
553
- }
554
-
555
- return this.handler_msg_json(msg, json_msg);
556
- }
557
-
558
- handler_msg(msg)
559
- {
560
- LOGR_.log(l_.HANDLERS, 'handler_msg: ', msg);
561
-
562
- if (typeof (msg) === 'string' && this.handler_msg_str(msg, msg))
563
- return true;
564
-
565
- if (typeof (msg) === 'object' && this.handler_msg_json(msg, msg))
566
- return true;
567
-
568
- LOGR_.log(l_.DROPS, "handler_msg: DROP! ", msg);
569
- return false
570
- }
571
-
572
- };
573
-
574
- export {
575
- TwoPhW,
576
- JSON_MsgOptions,
577
- JSON_MsgWarp,
578
- JSON_MsgEngage,
579
- JSON_MsgAckWarp,
580
- JSON_MsgAckEngage,
581
- l_ as l
582
- };
1
+
2
+ import {
3
+ definitions_2PhW as definitions_2PhW_,
4
+ schema_MsgWarp as schema_MsgWarp_,
5
+ schema_MsgEngage as schema_MsgEngage_,
6
+ schema_MsgOptions as schema_MsgOptions_,
7
+ schema_Options_delay as schema_Options_delay_
8
+
9
+ } from './validate-esm.mjs';
10
+
11
+ // import { v4 as uuidv4 } from 'uuid'; // uuid is already a dep of JSON_Msg, so why not
12
+ import { LOGR, l_array, l_concat } from '@knev/bitlogr';
13
+ import { JSON_Msg, JSON_Msg_msg, JSON_MsgAck_msg } from '@rootintf/json-msg'
14
+
15
+ import { EventLog, l as l_evtlog_ } from './EventLog-mem.mjs'
16
+
17
+ const k_TTL= 20000;
18
+
19
+ //-------------------------------------------------------------------------------------------------
20
+
21
+ // LOGR
22
+ // - l_LL and l_RR should accept 0b1 << 0 style shift
23
+ // - function to debug output bit values of labels instead of binary
24
+ // - rollup example in READMNE
25
+
26
+ const LOGR_= LOGR.get_instance();
27
+ const log_= LOGR_.log;
28
+
29
+ const l_msgs_ = l_array(['PROTOCOL_STATE','RECVER','SENDER']); // b0, b1, b2
30
+
31
+ Object.assign(l_evtlog_, l_array(['EVENTS','TIMEOUTS'], 0b1 << 3)); // b3, b4
32
+
33
+ const l_= l_concat( l_msgs_, l_concat(l_evtlog_, l_array(['HANDLERS','DROPS','VALIDATION'])) )
34
+ LOGR_.labels= l_;
35
+
36
+ //-------------------------------------------------------------------------------------------------
37
+
38
+ let map_Context_= new Map();
39
+
40
+ function set_ctx(key, ctx) {
41
+ map_Context_.set(key, ctx);
42
+ // console.log(map_Context_);
43
+ return ctx;
44
+ }
45
+
46
+ function get_ctx(key) {
47
+ return map_Context_.get(key);
48
+ }
49
+
50
+ //-------------------------------------------------------------------------------------------------
51
+
52
+ class Config {
53
+ constructor() {
54
+ // Initialize callbacks as no-op functions
55
+ this._callback_msgWarp = () => { console.assert(false, 'callback_msgWarp uninitialized') };
56
+ this._callback_msgEngage = () => { console.assert(false, 'callback_msgEngage uninitialized') };
57
+ this._callback_msgAckWarp = () => { console.assert(false, 'callback_msgAckWarp uninitialized') };
58
+ this._callback_msgAckEngage = () => { console.assert(false, 'callback_msgAckEngage uninitialized') };
59
+ this._callback_msg_sent_expired = () => { console.assert(false, '_callback_msg_sent_expired uninitialized') };
60
+ this._callback_msgOptions = () => { console.assert(false, 'callback_msgOptions uninitialized') };
61
+ }
62
+
63
+ receiver(callback_msgWarp, callback_msgEngage, callback_msg_sent_expired) {
64
+ this._callback_msgWarp= callback_msgWarp;
65
+ this._callback_msgEngage= callback_msgEngage;
66
+ this._callback_msg_sent_expired= callback_msg_sent_expired;
67
+ }
68
+ sender(callback_msgAckWarp, callback_msgAckEngage, callback_msg_sent_expired ) {
69
+ this._callback_msgAckWarp= callback_msgAckWarp;
70
+ this._callback_msgAckEngage= callback_msgAckEngage;
71
+ this._callback_msg_sent_expired= callback_msg_sent_expired;
72
+ }
73
+
74
+ set callback_msgOptions(callback) {
75
+ this._callback_msgOptions= callback;
76
+ }
77
+ }
78
+
79
+ // var config = (function() {
80
+ // let _options;
81
+
82
+ // return {
83
+ // get callback_Abort() { return _options.callback_Abort; },
84
+
85
+ // set options(obj) {
86
+ // _options= obj;
87
+ // if (_options.logr) // && options_.logr[ __name(l_) ] )
88
+ // LOGR_.toggled= _options.logr
89
+ // }
90
+ // }
91
+ // })();
92
+
93
+ //-------------------------------------------------------------------------------------------------
94
+
95
+ let k_VERSION= "0.8";
96
+
97
+ function t_ttl_rtt_(ttl_msec, rtt_msec) {
98
+ if (ttl_msec === undefined)
99
+ return undefined;
100
+ if (rtt_msec === undefined)
101
+ return ttl_msec *2;
102
+
103
+ console.assert(rtt_msec <= ttl_msec *2, `t_ttl_rtt_: INVALID rtt_msec ttl_msec[${ttl_msec}] rtt_msec[${rtt_msec}]`)
104
+ return (ttl_msec *2) + rtt_msec;
105
+ }
106
+
107
+ var JSON_MsgOptions = (function() {
108
+ return {
109
+ create_with_msg_options : function(_msg, options) {
110
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
111
+ options: options,
112
+ "2PhW": k_VERSION
113
+ });
114
+ },
115
+ validate : function(json) {
116
+ if (! JSON_Msg_msg.validate(json))
117
+ return false;
118
+ if (! schema_MsgOptions_(json)) {
119
+ log_(l_.VALIDATION, () => ['JSON_MsgOptions: schema_MsgOptions_: ', schema_MsgOptions_.errors])
120
+ return false;
121
+ }
122
+ return true;
123
+ }
124
+ }
125
+ })();
126
+
127
+ var JSON_MsgWarp = (function() {
128
+ return {
129
+ create_with_id_user_hyperport : function(json_warp_id, json_user, json_hyperport, nr_ttl_msec) {
130
+ return JSON_Msg.merge({
131
+ warp : {
132
+ id : json_warp_id,
133
+ user : json_user,
134
+ hyperport : json_hyperport,
135
+ ttl_msec : nr_ttl_msec
136
+ },
137
+ "2PhW": k_VERSION
138
+ });
139
+ },
140
+ compare : function(json_L, json_R) {
141
+ if (json_L.dt !== json_R.dt)
142
+ return false;
143
+ if (json_L.warp.id !== json_R.warp.id)
144
+ return false;
145
+ if (json_L.referer[0] !== json_R.referer[0])
146
+ return false;
147
+ if (json_L.referer[1] !== json_R.referer[1])
148
+ return false;
149
+ return true;
150
+ },
151
+ validate : function(json) {
152
+ if (! JSON_Msg.validate(json))
153
+ return false;
154
+ if (! schema_MsgWarp_(json)) {
155
+ log_(l_.VALIDATION, () => ['JSON_MsgWarp: schema_MsgWarp_: ', schema_MsgWarp_.errors])
156
+ return false;
157
+ }
158
+ return true;
159
+ }
160
+ }
161
+ })();
162
+
163
+ var JSON_MsgEngage = (function() {
164
+ return {
165
+ create_with_MsgWarp : function(json_msgWarp) {
166
+ console.assert(json_msgWarp.ack === undefined, "Engage with a Warp, not an Ack");
167
+ return JSON_Msg_msg.create_with_msg_merge(json_msgWarp, {
168
+ engage : {
169
+ "ok" : true,
170
+ "rtt_msec" : Date.now() - Date.parse(json_msgWarp.dt)
171
+ },
172
+ "2PhW": k_VERSION
173
+ });
174
+ },
175
+ validate : function(json) {
176
+ if (! JSON_Msg_msg.validate(json))
177
+ return false;
178
+ if (! JSON_MsgWarp.validate(json._msg))
179
+ return false;
180
+ if (! schema_MsgEngage_(json)) {
181
+ log_(l_.VALIDATION, () => ['JSON_MsgEngage: schema_MsgEngage_: ', schema_MsgEngage_.errors])
182
+ return false;
183
+ }
184
+ return true;
185
+ }
186
+ };
187
+ })();
188
+
189
+ var JSON_MsgAckWarp = (function() {
190
+ return {
191
+ // createWith_msgAck_msg : function(json_msgAck, _msg) {
192
+ // const json_msgAckWarp= structuredClone(json_msgAck);
193
+ // json_msgAckWarp._msg= _msg;
194
+ // return json_msgAckWarp;
195
+ // }
196
+ create_with_msgOk : function(json_msgWarp, b_ok= true) {
197
+ const json_msgAck_msg= JSON_MsgAck_msg.create_with_msgOk(json_msgWarp, b_ok);
198
+ json_msgAck_msg["2PhW"]= k_VERSION;
199
+ return json_msgAck_msg;
200
+ },
201
+ create_with_msgExpired : function( json_msgWarp ) {
202
+ const json_msgAck_msg= JSON_MsgAck_msg.create_with_msgExpired( json_msgWarp )
203
+ json_msgAck_msg["2PhW"]= k_VERSION;
204
+ return json_msgAck_msg;
205
+ },
206
+ validate : function(json) {
207
+ if (! JSON_MsgAck_msg.validate(json))
208
+ return false;
209
+ if (! JSON_MsgWarp.validate(json._msg))
210
+ return false;
211
+ return true;
212
+ }
213
+ }
214
+ })();
215
+
216
+ var JSON_MsgAckEngage = (function() {
217
+ return {
218
+ // createWith_msgAck_msg : function(json_msgAck, _msg) {
219
+ // const json_msgAckEngage= structuredClone(json_msgAck);
220
+ // json_msgAckEngage._msg= _msg;
221
+ // return json_msgAckEngage;
222
+ // }
223
+ create_with_msgOk : function(json_msgEngage, b_ok= true) {
224
+ const json_msgAck_msg= JSON_MsgAck_msg.create_with_msgOk(json_msgEngage, b_ok);
225
+ json_msgAck_msg["2PhW"]= k_VERSION;
226
+ return json_msgAck_msg;
227
+ },
228
+ create_with_msgExpired : function( json_msgEngage ) {
229
+ const json_msgAck_msg= JSON_MsgAck_msg.create_with_msgExpired( json_msgEngage )
230
+ json_msgAck_msg["2PhW"]= k_VERSION;
231
+ return json_msgAck_msg;
232
+ },
233
+ validate : function(json) {
234
+ if (! JSON_MsgAck_msg.validate(json))
235
+ return false;
236
+ if (! JSON_MsgEngage.validate(json._msg))
237
+ return false;
238
+ return true;
239
+ }
240
+ }
241
+ })();
242
+
243
+ //-------------------------------------------------------------------------------------------------
244
+ // States
245
+
246
+ class StateState {
247
+ constructor(idx_event, event) {
248
+ this._protocol_parent = null;
249
+ this._idx_event = idx_event;
250
+ this._event = event;
251
+ }
252
+ get protocol() {
253
+ return this._protocol_parent;
254
+ }
255
+ set protocol(parent) {
256
+ this._protocol_parent = parent;
257
+ }
258
+ get idx_event() {
259
+ return this._idx_event;
260
+ }
261
+ get event() {
262
+ return this._event;
263
+ }
264
+ get json_msg() {
265
+ return this._event.data;
266
+ }
267
+ changeState(state_new) {
268
+ if (this.canChangeState(state_new))
269
+ this.protocol.state = state_new;
270
+ else
271
+ this.protocol.state = new StateINVALID(state_new._idx_event, state_new._event);
272
+ }
273
+ canChangeState(_state_new) {
274
+ throw new Error("Subclass must implement canChangeState()");
275
+ }
276
+ }
277
+
278
+ class StateAckEngage extends StateState {
279
+ constructor(idx_event, event) { super(idx_event, event); }
280
+ get evt_AckEngage() { return this.event }
281
+ get json_msgAckEngage() { return this.json_msg }
282
+ canChangeState(_state_new) {
283
+ return false;
284
+ }
285
+ }
286
+
287
+ class StateEngage extends StateState {
288
+ constructor(idx_event, event) { super(idx_event, event); }
289
+ get evt_Engage() { return this.event }
290
+ get json_msgEngage() { return this.json_msg }
291
+ canChangeState(state_new) {
292
+ if (false === state_new instanceof StateAckEngage)
293
+ return false;
294
+
295
+ // The Engage gets to live for THE WARP'S creation time + TTL*2 + RTT
296
+ const json_msgWarp= this.json_msgEngage._msg;
297
+ const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec, this.json_msgEngage.engage.rtt_msec);
298
+ log_(l_.PROTOCOL_STATE, () => [`StateEngage: evt_AckEngage.dt@${state_new.evt_AckEngage.dt}, msgWarp.dt@${Date.parse(json_msgWarp.dt)} + ${timeout_ms}`])
299
+ if (state_new.evt_AckEngage.dt >= Date.parse(json_msgWarp.dt) + timeout_ms)
300
+ return false;
301
+
302
+ return true;
303
+ }
304
+ }
305
+
306
+ class StateAckWarp extends StateState {
307
+ constructor(idx_event, event) { super(idx_event, event); }
308
+ get evt_AckWarp() { return this.event }
309
+ get json_msgAckWarp() { return this.json_msg }
310
+ canChangeState(state_new) {
311
+ if (false === state_new instanceof StateEngage)
312
+ return false;
313
+
314
+ if (! this.json_msgAckWarp.ack.ok)
315
+ return false;
316
+
317
+ if (this.json_msgAckWarp._msg.warp.instantaneous)
318
+ return false;
319
+
320
+ // The Ack gets to live for THE ACK'S creation time + TTL*2 + STT
321
+ // +STT, so lines up with the Engage timeout
322
+ // - Engage: T0 +2TTL +RTT; RTT= STT+STT
323
+ // - AckWarp: TAckWarp +2TLL +STT; TAckWarp= T0 +STT;
324
+ // - STT: Date of the Ack minus date of the Warp.
325
+ const timeout_ms= t_ttl_rtt_(this.json_msgAckWarp._msg.warp.ttl_msec) + Date.parse(this.json_msgAckWarp.dt) - Date.parse(this.json_msgAckWarp._msg.dt);
326
+ log_(l_.PROTOCOL_STATE, () => [`StateAckWarp: evt_Engage.dt@${state_new.evt_Engage.dt}, msgAckWarp.dt@${Date.parse(this.json_msgAckWarp.dt)} + ${timeout_ms}`])
327
+ if (state_new.evt_Engage.dt >= Date.parse(this.json_msgAckWarp.dt) + timeout_ms)
328
+ return false;
329
+
330
+ return true;
331
+ }
332
+ }
333
+
334
+ class StateWarp extends StateState {
335
+ constructor(idx_event, event) { super(idx_event, event); }
336
+ get evt_Warp() { return this.event }
337
+ get json_msgWarp() { return this.json_msg }
338
+ canChangeState(state_new) {
339
+ if (false === state_new instanceof StateAckWarp)
340
+ return false;
341
+
342
+ // The Warp gets to live for THE WARP'S creation time + TTL*2
343
+ const timeout_ms= t_ttl_rtt_(this.json_msgWarp.warp.ttl_msec);
344
+ log_(l_.PROTOCOL_STATE, () => [`StateWarp: evt_AckWarp.dt@${state_new.evt_AckWarp.dt}, msgWarp.dt@${Date.parse(this.json_msgWarp.dt)} + ${timeout_ms}`])
345
+ if (state_new.evt_AckWarp.dt >= Date.parse(this.json_msgWarp.dt) + timeout_ms)
346
+ return false;
347
+
348
+ return true;
349
+ }
350
+ }
351
+
352
+ class StateINVALID extends StateState {
353
+ constructor(idx_event, event) { super(idx_event, event); }
354
+ canChangeState(_state_new) {
355
+ // INVALID can't transition to any state
356
+ return false;
357
+ }
358
+ }
359
+
360
+ class StateInitialState extends StateState {
361
+ constructor() { super(null, null); }
362
+ canChangeState(state_new) {
363
+ if (false === state_new instanceof StateWarp)
364
+ return false;
365
+
366
+ // The NEWLY created Warp only gets to live for its creation time + TTL
367
+ const timeout_ms= state_new.json_msgWarp.warp.ttl_msec;
368
+ log_(l_.PROTOCOL_STATE, () => [`StateInitialState: evt_Warp.dt@${state_new.evt_Warp.dt}, msgWarp.dt@${Date.parse(state_new.json_msgWarp.dt)} + ${timeout_ms}`])
369
+ if (state_new.evt_Warp.dt >= Date.parse(state_new.json_msgWarp.dt) + timeout_ms)
370
+ return false;
371
+
372
+ return true;
373
+ }
374
+ }
375
+
376
+ //----
377
+
378
+ class ProtocolStateState {
379
+ constructor(state_initial) {
380
+ this.state = state_initial || new StateInitialState();
381
+ }
382
+
383
+ set state(state_new) {
384
+ if (! (state_new instanceof StateState)) {
385
+ throw new Error("initState must be an instance of StateState or its subclasses");
386
+ }
387
+ this._state = state_new;
388
+ if (state_new && state_new.protocol !== this) {
389
+ state_new.protocol = this;
390
+ }
391
+ }
392
+ get state() {
393
+ return this._state;
394
+ }
395
+
396
+ canChangeState(state_new) {
397
+ return this._state.canChangeState(state_new);
398
+ }
399
+ }
400
+
401
+ //-------------------------------------------------------------------------------------------------
402
+
403
+ class TwoPhW_EventLog {
404
+ constructor(eventLog) {
405
+ this._eventLog = eventLog;
406
+ }
407
+
408
+ _warp(json_msg) {
409
+ const b_Ack = json_msg.ack !== undefined;
410
+ const json = b_Ack ? json_msg["_msg"] : json_msg;
411
+
412
+ if (json.warp) {
413
+ return {
414
+ str_type: `2PhW[JSON_Msg${b_Ack ? 'Ack' : ''}Warp]`,
415
+ json_msgWarp: json
416
+ };
417
+ }
418
+ if (json.engage) {
419
+ return {
420
+ str_type: `2PhW[JSON_Msg${b_Ack ? 'Ack' : ''}Engage]`,
421
+ json_msgWarp: json["_msg"]
422
+ };
423
+ }
424
+ return null; // Explicit default for invalid cases
425
+ }
426
+
427
+ _addEvent(json_msg, timeout_ms, timeout_callback) {
428
+ const { str_type, json_msgWarp }= this._warp(json_msg);
429
+ const { idx }= this._eventLog.addEvent(str_type, json_msgWarp.warp.id, json_msg, timeout_ms, timeout_callback);
430
+ log_(l_.PROTOCOL_STATE, () => [`_addEvent: [${idx}] timeout:${Date.now()}+${timeout_ms} `, json_msg]);
431
+ }
432
+
433
+ _dropEvent(json_msg) {
434
+ const { json_msgWarp }= this._warp(json_msg);
435
+ this._eventLog.addEvent('2PhW[DROPPED!]', json_msgWarp.warp.id, json_msg);
436
+ }
437
+
438
+ _cancelTimeout(idx_event) {
439
+ log_(l_.PROTOCOL_STATE, () => [`_cancelTimeout: [${idx_event}] timeout cancelled`]);
440
+ this._eventLog.cancelTimeout(idx_event);
441
+ }
442
+
443
+ get_protocolState(json_msgWarp) {
444
+ const protocolState = new ProtocolStateState();
445
+
446
+ const arr_evts= this._eventLog.getEventById(json_msgWarp.warp.id, k_TTL);
447
+ log_(l_.EVENTS, () => ['get_protocolState: arr_evts: ', arr_evts]);
448
+
449
+ for (let i = arr_evts.length - 1; i >= 0; i--) {
450
+ const { idx, evt } = arr_evts[i];
451
+
452
+ if (evt.type === '2PhW[DROPPED!]')
453
+ continue;
454
+
455
+ const json_msg= evt.data;
456
+ if (! JSON_MsgWarp.compare(json_msgWarp, this._warp(json_msg).json_msgWarp) )
457
+ protocolState.state.changeState( new StateINVALID(idx, evt) );
458
+
459
+ switch(evt.type) {
460
+ case '2PhW[JSON_MsgWarp]' :
461
+ protocolState.state.changeState( new StateWarp(idx, evt) );
462
+ break;
463
+ case '2PhW[JSON_MsgAckWarp]' :
464
+ protocolState.state.changeState( new StateAckWarp(idx, evt) );
465
+ break;
466
+ case '2PhW[JSON_MsgEngage]' :
467
+ protocolState.state.changeState( new StateEngage(idx, evt) );
468
+ break;
469
+ case '2PhW[JSON_MsgAckEngage]' :
470
+ protocolState.state.changeState( new StateAckEngage(idx, evt) );
471
+ break;
472
+ default :
473
+ protocolState.state.changeState( new StateINVALID(idx, evt) );
474
+ break;
475
+ }
476
+ log_(l_.PROTOCOL_STATE, () => ['get_protocolState: type,state: ',
477
+ evt.type,
478
+ (protocolState.state.json_msg.ack?.ok === false) ? '--NACK--' : '',
479
+ protocolState.state
480
+ ]);
481
+ }
482
+ return protocolState;
483
+ }
484
+
485
+ }
486
+
487
+ class TwoPhW {
488
+ constructor(eventLog) {
489
+ this._2PhW_eventLog = new TwoPhW_EventLog(eventLog);
490
+ this._config= new Config();
491
+ }
492
+
493
+ get config() {
494
+ return this._config;
495
+ }
496
+
497
+ get eventLog() {
498
+ return this._2PhW_eventLog;
499
+ }
500
+
501
+ //-------------------------------------------------------------------------------------------------
502
+
503
+ _handler_msgOptions(cfg, msg, json_msgOptions)
504
+ {
505
+ log_(l_.HANDLERS, () => ['handler_msgOptions_: ...']);
506
+
507
+ if (! validate_Options_delay_(json_msgOptions.options))
508
+ return false;
509
+
510
+ if (JSON_MsgWarp.validate(json_msgOptions._msg)) {
511
+ const json_msgWarp= json_msgOptions._msg;
512
+
513
+ const ctx= get_ctx(json_msgWarp.id);
514
+ if (!ctx )
515
+ return false;
516
+
517
+ const ttl_msec= json_msgOptions.options.ttl_msec_incr[0];
518
+ const ttl_incr= json_msgOptions.options.ttl_msec_incr[1];
519
+
520
+ //TODO: double check ttl_msec is the same as current accummulated ttl for idempotence.
521
+
522
+ log_(l_.HANDLERS, () => ['handler_msgOptions_: ', json_msgOptions]);
523
+
524
+ ctx.protocol.delayTimeout(ttl_incr);
525
+
526
+ if (cfg._callback_msgOptions)
527
+ return cfg._callback_msgOptions(msg, json_msgOptions);
528
+
529
+ return true;
530
+ }
531
+
532
+ return false;
533
+ }
534
+
535
+ //-------------------------------------------------------------------------------------------------
536
+ // sender Source (src)
537
+
538
+ register_msgWarp(json_msgWarp)
539
+ {
540
+ log_(l_.SENDER, () => ['register_msgWarp: ', json_msgWarp]);
541
+
542
+ const protocolState= this.eventLog.get_protocolState(json_msgWarp);
543
+ const b_changeState= protocolState.canChangeState( new StateWarp(null, {
544
+ dt: Date.now(),
545
+ data: json_msgWarp
546
+ }) );
547
+
548
+ log_(l_.HANDLERS | l_.SENDER, () => ['register_msgWarp: protocolState.canChangeState ', b_changeState]);
549
+ if (! b_changeState)
550
+ return false;
551
+
552
+ const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec);
553
+ const elapsed_ms= Date.now() - Date.parse(json_msgWarp.dt);
554
+
555
+ this.eventLog._addEvent(json_msgWarp, timeout_ms-elapsed_ms, () => {
556
+ this.config._callback_msg_sent_expired(json_msgWarp);
557
+ });
558
+ }
559
+
560
+ _handler_msgAckWarp(msg, json_msgAckWarp)
561
+ {
562
+ log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckWarp: ', json_msgAckWarp]);
563
+
564
+ const json_msgWarp= json_msgAckWarp._msg;
565
+
566
+ const protocolState= this.eventLog.get_protocolState(json_msgWarp);
567
+ const b_changeState= protocolState.canChangeState( new StateAckWarp(null, {
568
+ dt: Date.now(),
569
+ data: json_msgAckWarp
570
+ }) );
571
+
572
+ log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckWarp: protocolState.canChangeState ', b_changeState]);
573
+ if (! b_changeState)
574
+ return false;
575
+
576
+ this.eventLog._cancelTimeout(protocolState.state.idx_event);
577
+ this.eventLog._addEvent(json_msgAckWarp);
578
+ if (! this.config._callback_msgAckWarp(msg, json_msgAckWarp))
579
+ return false;
580
+
581
+ return true;
582
+ }
583
+
584
+ register_msgEngage(json_msgEngage)
585
+ {
586
+ log_(l_.SENDER, () => ['register_msgEngage: ', json_msgEngage]);
587
+
588
+ const json_msgWarp= json_msgEngage._msg;
589
+
590
+ const protocolState= this.eventLog.get_protocolState(json_msgWarp);
591
+ const b_changeState= protocolState.canChangeState( new StateEngage(null, {
592
+ dt: Date.now(),
593
+ data : json_msgEngage
594
+ }) );
595
+
596
+ log_(l_.HANDLERS | l_.SENDER, () => ['register_msgEngage: protocolState.canChangeState ', b_changeState]);
597
+ if (! b_changeState)
598
+ return false;
599
+
600
+ const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec, json_msgEngage.engage.rtt_msec);
601
+ const elapsed_ms= Date.now() - Date.parse(json_msgWarp.dt);
602
+
603
+ this.eventLog._addEvent(json_msgEngage, timeout_ms-elapsed_ms, () => {
604
+ this.config._callback_msg_sent_expired(json_msgEngage);
605
+ });
606
+ }
607
+
608
+ _handler_msgAckEngage(msg, json_msgAckEngage)
609
+ {
610
+ log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckEngage: ', json_msgAckEngage]);
611
+
612
+ const json_msgWarp= json_msgAckEngage._msg._msg;
613
+
614
+ const protocolState= this.eventLog.get_protocolState(json_msgWarp);
615
+ const b_changeState= protocolState.canChangeState( new StateAckEngage(null, {
616
+ dt: Date.now(),
617
+ data : json_msgAckEngage
618
+ }) );
619
+
620
+ log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckEngage_: protocolState.canChangeState ', b_changeState]);
621
+ if (! b_changeState)
622
+ return false;
623
+
624
+ this.eventLog._cancelTimeout(protocolState.state.idx_event);
625
+ this.eventLog._addEvent(json_msgAckEngage);
626
+ if (! this.config._callback_msgAckEngage(msg, json_msgAckEngage))
627
+ return false;
628
+
629
+ return true;
630
+ }
631
+
632
+ //-------------------------------------------------------------------------------------------------
633
+ // receiver Destination (dst)
634
+
635
+ _handler_msgWarp(msg, json_msgWarp)
636
+ {
637
+ log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgWarp: ', json_msgWarp]);
638
+
639
+ const protocolState= this.eventLog.get_protocolState(json_msgWarp);
640
+ const b_changeState= protocolState.canChangeState( new StateWarp(null, {
641
+ dt: Date.now(),
642
+ data: json_msgWarp
643
+ }) );
644
+
645
+ log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgWarp: protocolState.canChangeState: ', b_changeState]);
646
+ if (! b_changeState)
647
+ return false;
648
+
649
+ this.eventLog._addEvent(json_msgWarp);
650
+ if (! this.config._callback_msgWarp(msg, json_msgWarp))
651
+ return false;
652
+
653
+ return true;
654
+ }
655
+
656
+ register_msgAckWarp(json_msgAckWarp)
657
+ {
658
+ log_(l_.RECVER, () => ['register_msgAckWarp: ', json_msgAckWarp]);
659
+
660
+ const json_msgWarp= json_msgAckWarp._msg;
661
+
662
+ const protocolState= this.eventLog.get_protocolState(json_msgWarp);
663
+ const b_changeState= protocolState.canChangeState( new StateAckWarp(null, {
664
+ dt: Date.now(),
665
+ data: json_msgAckWarp
666
+ }) );
667
+
668
+ log_(l_.HANDLERS | l_.RECVER, () => ['register_msgAckWarp: protocolState.canChangeState: ', b_changeState]);
669
+ if (! b_changeState)
670
+ return false;
671
+
672
+ const elapsed_ms= Date.now() - Date.parse(json_msgAckWarp.dt);
673
+ const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec) + Date.parse(json_msgAckWarp.dt) - Date.parse(json_msgWarp.dt);
674
+
675
+ this.eventLog._addEvent(json_msgAckWarp, timeout_ms-elapsed_ms, () => {
676
+ this.config._callback_msg_sent_expired(json_msgAckWarp);
677
+ });
678
+ }
679
+
680
+ _handler_msgEngage(msg, json_msgEngage)
681
+ {
682
+ log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgEngage: ', json_msgEngage]);
683
+
684
+ const json_msgWarp= json_msgEngage["_msg"];
685
+
686
+ const protocolState= this.eventLog.get_protocolState(json_msgWarp);
687
+ const b_changeState= protocolState.canChangeState( new StateEngage(null, {
688
+ dt: Date.now(),
689
+ data: json_msgEngage
690
+ }) );
691
+
692
+ log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgEngage: protocolState.canChangeState: ', b_changeState]);
693
+ if (! b_changeState)
694
+ return false;
695
+
696
+ this.eventLog._cancelTimeout(protocolState.state.idx_event);
697
+ this.eventLog._addEvent(json_msgEngage);
698
+ if (! this.config._callback_msgEngage(msg, json_msgEngage))
699
+ return false;
700
+
701
+ return true;
702
+ }
703
+
704
+ register_msgAckEngage(json_msgAckEngage)
705
+ {
706
+ log_(l_.RECVER, () => ['register_msgAckEngage: ', json_msgAckEngage]);
707
+
708
+ const json_msgWarp= json_msgAckEngage._msg._msg;
709
+
710
+ const protocolState= this.eventLog.get_protocolState(json_msgWarp);
711
+ const b_changeState= protocolState.canChangeState( new StateAckEngage(null, {
712
+ dt: Date.now(),
713
+ data: json_msgAckEngage
714
+ }) );
715
+
716
+ log_(l_.HANDLERS | l_.RECVER, () => ['register_msgAckEngage: protocolState.canChangeState: ', b_changeState]);
717
+ if (! b_changeState)
718
+ return false;
719
+
720
+ this.eventLog._addEvent(json_msgAckEngage);
721
+ }
722
+
723
+ //-------------------------------------------------------------------------------------------------
724
+
725
+ handler_msg_json(msg, json_msg)
726
+ {
727
+ log_(l_.HANDLERS, () => ['handler_msg_json: ', json_msg]);
728
+
729
+ // ADR: I could parse json_Msg_msg first and then MsgAck or MsgOptions, but
730
+ // doing so means super classes get parsed multiple times for no reason
731
+
732
+ // if (JSON_MsgOptions.validate(json_msg) && this.handler_msgOptions_(this._config, msg, json_msg))
733
+ // return true;
734
+
735
+ if (JSON_MsgWarp.validate(json_msg) && this._handler_msgWarp(msg, json_msg))
736
+ return true;
737
+
738
+ if (JSON_MsgEngage.validate(json_msg) && this._handler_msgEngage(msg, json_msg))
739
+ return true;
740
+
741
+ if (JSON_MsgAckWarp.validate(json_msg) && this._handler_msgAckWarp(msg, json_msg))
742
+ return true;
743
+
744
+ if (JSON_MsgAckEngage.validate(json_msg) && this._handler_msgAckEngage(msg, json_msg))
745
+ return true;
746
+
747
+ // https://attacomsian.com/blog/javascript-iterate-objects
748
+ // Object.keys(json_msg).forEach((key, index) => {
749
+ // if (key == 'dt')
750
+ // return;
751
+ // if (key == 'referer')
752
+ // return;
753
+ // console.log(`${key}: ${json_msg[key]}`);
754
+ // });
755
+
756
+ this.eventLog._dropEvent(json_msg);
757
+ log_(l_.DROPS, () => ["TwoPhW: handler_msg: DROP! ", msg]);
758
+ return false;
759
+ }
760
+
761
+ handler_msg_str(msg, str_msg)
762
+ {
763
+ log_(l_.HANDLERS, () => ['handler_msg_str: ['+ str_msg +']']);
764
+
765
+ // ... test strings here
766
+
767
+ let json_msg;
768
+ try {
769
+ json_msg= JSON.parse(str_msg);
770
+ }
771
+ catch (err) {
772
+ return false;
773
+ }
774
+
775
+ return this.handler_msg_json(msg, json_msg);
776
+ }
777
+
778
+ handler_msg(msg)
779
+ {
780
+ log_(l_.HANDLERS, () => ['handler_msg: ', msg]);
781
+
782
+ if (typeof (msg) === 'string' && this.handler_msg_str(msg, msg))
783
+ return true;
784
+
785
+ if (typeof (msg) === 'object' && this.handler_msg_json(msg, msg))
786
+ return true;
787
+
788
+ return false
789
+ }
790
+
791
+ };
792
+
793
+ //-------------------------------------------------------------------------------------------------
794
+
795
+ export {
796
+ EventLog,
797
+ t_ttl_rtt_ as t_ttl_rtt,
798
+ TwoPhW,
799
+ JSON_MsgOptions,
800
+ JSON_MsgWarp,
801
+ JSON_MsgEngage,
802
+ JSON_MsgAckWarp,
803
+ JSON_MsgAckEngage,
804
+ l_ as l,
805
+ // -----
806
+ StateAckEngage,
807
+ StateEngage,
808
+ StateAckWarp,
809
+ StateWarp,
810
+ StateINVALID,
811
+ StateInitialState
812
+ };
813
+