@rootintf/protocol-2phw 1.0.36-api.1 → 1.1.5-api.1

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.
@@ -3,9 +3,8 @@ import {
3
3
  definitions_2PhW as definitions_2PhW_,
4
4
  schema_MsgWarp as schema_MsgWarp_,
5
5
  schema_MsgEngage as schema_MsgEngage_,
6
- schema_MsgOptions as schema_MsgOptions_,
7
- schema_Options_delay as schema_Options_delay_
8
-
6
+ // schema_MsgOptions as schema_MsgOptions_,
7
+ // schema_Options_delay as schema_Options_delay_,
9
8
  } from './validate-esm.mjs';
10
9
 
11
10
  // import { v4 as uuidv4 } from 'uuid'; // uuid is already a dep of JSON_Msg, so why not
@@ -14,6 +13,20 @@ import { JSON_Msg, JSON_Msg_msg, JSON_MsgAck_msg, l as l_json_msg_ } from '@root
14
13
 
15
14
  import { EventLog, l as l_evtlog_ } from './EventLog-mem.mjs'
16
15
 
16
+ type t_msg = string;
17
+
18
+ interface EventMinimal {
19
+ dt : number;
20
+ data : { msg : t_msg, json_msg : JSON_Msg };
21
+ }
22
+
23
+ // interface Event extends Required<EventMinimal> {
24
+ // type : string;
25
+ // id : string | number;
26
+ // timeout_id : NodeJS.Timeout | null;
27
+ // };
28
+
29
+
17
30
  const k_TTL= 20000;
18
31
 
19
32
  //-------------------------------------------------------------------------------------------------
@@ -28,6 +41,12 @@ const l_twoPhW_ = l_array(['HANDLERS','DROPS', 'PROTOCOL_STATE','RECVER','SENDER
28
41
  const l_ = l_concat(l_evtlog_, l_concat(l_json_msg_, l_twoPhW_) )
29
42
 
30
43
  LOGR_.labels= l_;
44
+ // LOGR_.toggled= {
45
+ // HANDLERS : true,
46
+ // SENDER : true,
47
+ // PROTOCOL_STATE : true,
48
+ // // EVENTS : true
49
+ // }
31
50
 
32
51
  //-------------------------------------------------------------------------------------------------
33
52
 
@@ -46,14 +65,33 @@ function get_ctx(key) {
46
65
  //-------------------------------------------------------------------------------------------------
47
66
 
48
67
  class Config {
68
+ public _callback_msgWarp: (msg : t_msg, json_msg : JSON_Msg) => boolean;
69
+ public _callback_msgEngage: (msg : t_msg, json_msg : JSON_Msg) => boolean;
70
+ public _callback_msgAckWarp: (msg : t_msg, json_msg : JSON_Msg) => boolean;
71
+ public _callback_msgAckEngage: (msg : t_msg, json_msg : JSON_Msg) => boolean;
72
+ public _callback_msg_sent_expired: (msg : t_msg, json_msg : JSON_Msg) => void;
73
+ public _callback_msgOptions: () => void;
74
+
49
75
  constructor() {
50
76
  // Initialize callbacks as no-op functions
51
- this._callback_msgWarp = () => { console.assert(false, 'callback_msgWarp uninitialized') };
52
- this._callback_msgEngage = () => { console.assert(false, 'callback_msgEngage uninitialized') };
53
- this._callback_msgAckWarp = () => { console.assert(false, 'callback_msgAckWarp uninitialized') };
54
- this._callback_msgAckEngage = () => { console.assert(false, 'callback_msgAckEngage uninitialized') };
55
- this._callback_msg_sent_expired = () => { console.assert(false, '_callback_msg_sent_expired uninitialized') };
56
- this._callback_msgOptions = () => { console.assert(false, 'callback_msgOptions uninitialized') };
77
+ this._callback_msgWarp = (_msg : t_msg, _json_msg : JSON_Msg) => {
78
+ console.assert(false, 'callback_msgWarp uninitialized'); return false
79
+ };
80
+ this._callback_msgEngage = (_msg : t_msg, _json_msg : JSON_Msg) => {
81
+ console.assert(false, 'callback_msgEngage uninitialized'); return false
82
+ };
83
+ this._callback_msgAckWarp = (_msg : t_msg, _json_msg : JSON_Msg) => {
84
+ console.assert(false, 'callback_msgAckWarp uninitialized'); return false
85
+ };
86
+ this._callback_msgAckEngage = (_msg : t_msg, _json_msg : JSON_Msg) => {
87
+ console.assert(false, 'callback_msgAckEngage uninitialized'); return false
88
+ };
89
+ this._callback_msg_sent_expired = (_msg : t_msg, _json_msg : JSON_Msg) => {
90
+ console.assert(false, '_callback_msg_sent_expired uninitialized')
91
+ };
92
+ this._callback_msgOptions = () => {
93
+ console.assert(false, 'callback_msgOptions uninitialized')
94
+ };
57
95
  }
58
96
 
59
97
  receiver(callback_msgWarp, callback_msgEngage, callback_msg_sent_expired) {
@@ -90,7 +128,10 @@ class Config {
90
128
 
91
129
  let k_VERSION= "1";
92
130
 
93
- function t_ttl_rtt_(ttl_msec, rtt_msec) {
131
+ // rtt_msec <= ttl_msec *2 => rtt_msec = ttl_msec *2
132
+ // t_ttl_rtt_(TTL, RTT) = t_ttl_rtt_(TTL, TTL *2) = TTL *2 + TTL *2 = TTL *4 MAX
133
+
134
+ function t_ttl_rtt_(ttl_msec: number | undefined, rtt_msec?: number): number | undefined {
94
135
  if (ttl_msec === undefined)
95
136
  return undefined;
96
137
  if (rtt_msec === undefined)
@@ -109,12 +150,12 @@ var JSON_MsgOptions = (function() {
109
150
  });
110
151
  },
111
152
  validate : function(json) {
112
- if (! JSON_Msg_msg.validate(json))
113
- return false;
114
- if (! schema_MsgOptions_(json)) {
115
- log_(l_.VALIDATION, () => ['JSON_MsgOptions: schema_MsgOptions_: ', schema_MsgOptions_.errors])
116
- return false;
117
- }
153
+ // if (! JSON_Msg_msg.validate(json))
154
+ // return false;
155
+ // if (! schema_MsgOptions_(json)) {
156
+ // log_(l_.VALIDATION, () => ['JSON_MsgOptions: schema_MsgOptions_: ', (schema_MsgOptions_ as any).errors])
157
+ // return false;
158
+ // }
118
159
  return true;
119
160
  }
120
161
  }
@@ -133,7 +174,7 @@ var JSON_MsgWarp = (function() {
133
174
  "2PhW": k_VERSION
134
175
  });
135
176
  },
136
- compare : function(json_L, json_R) {
177
+ compare : function(json_L : JSON_Msg, json_R : JSON_Msg) {
137
178
  if (json_L.dt !== json_R.dt)
138
179
  return false;
139
180
  if (json_L.warp.id !== json_R.warp.id)
@@ -144,11 +185,11 @@ var JSON_MsgWarp = (function() {
144
185
  return false;
145
186
  return true;
146
187
  },
147
- validate : function(json) {
148
- if (! JSON_Msg.validate(json))
149
- return false;
150
- if (! schema_MsgWarp_(json)) {
151
- log_(l_.VALIDATION, () => ['JSON_MsgWarp: schema_MsgWarp_: ', schema_MsgWarp_.errors])
188
+ validate : function(json_msg) {
189
+ // if (! JSON_Msg.validate(json))
190
+ // return false;
191
+ if (! schema_MsgWarp_(json_msg)) {
192
+ log_(l_.VALIDATION, () => ['JSON_MsgWarp: schema_MsgWarp_: ', (schema_MsgWarp_ as any).errors])
152
193
  return false;
153
194
  }
154
195
  return true;
@@ -168,13 +209,13 @@ var JSON_MsgEngage = (function() {
168
209
  "2PhW": k_VERSION
169
210
  });
170
211
  },
171
- validate : function(json) {
172
- if (! JSON_Msg_msg.validate(json))
212
+ validate : function(json_msg_msg) {
213
+ // if (! JSON_Msg_msg.validate(json))
214
+ // return false;
215
+ if (! JSON_MsgWarp.validate(json_msg_msg._msg))
173
216
  return false;
174
- if (! JSON_MsgWarp.validate(json._msg))
175
- return false;
176
- if (! schema_MsgEngage_(json)) {
177
- log_(l_.VALIDATION, () => ['JSON_MsgEngage: schema_MsgEngage_: ', schema_MsgEngage_.errors])
217
+ if (! schema_MsgEngage_(json_msg_msg)) {
218
+ log_(l_.VALIDATION, () => ['JSON_MsgEngage: schema_MsgEngage_: ', (schema_MsgEngage_ as any).errors])
178
219
  return false;
179
220
  }
180
221
  return true;
@@ -194,15 +235,15 @@ var JSON_MsgAckWarp = (function() {
194
235
  json_msgAck_msg["2PhW"]= k_VERSION;
195
236
  return json_msgAck_msg;
196
237
  },
197
- create_with_msgExpired : function( json_msgWarp ) {
198
- const json_msgAck_msg= JSON_MsgAck_msg.create_with_msgExpired( json_msgWarp )
199
- json_msgAck_msg["2PhW"]= k_VERSION;
200
- return json_msgAck_msg;
201
- },
202
- validate : function(json) {
203
- if (! JSON_MsgAck_msg.validate(json))
204
- return false;
205
- if (! JSON_MsgWarp.validate(json._msg))
238
+ // create_with_msgExpired : function( json_msgWarp ) {
239
+ // const json_msgAck_msg= JSON_MsgAck_msg.create_with_msgExpired( json_msgWarp )
240
+ // json_msgAck_msg["2PhW"]= k_VERSION;
241
+ // return json_msgAck_msg;
242
+ // },
243
+ validate : function(json_msgAck_msg) {
244
+ // if (! JSON_MsgAck_msg.validate(json))
245
+ // return false;
246
+ if (! JSON_MsgWarp.validate(json_msgAck_msg._msg))
206
247
  return false;
207
248
  return true;
208
249
  }
@@ -221,15 +262,15 @@ var JSON_MsgAckEngage = (function() {
221
262
  json_msgAck_msg["2PhW"]= k_VERSION;
222
263
  return json_msgAck_msg;
223
264
  },
224
- create_with_msgExpired : function( json_msgEngage ) {
225
- const json_msgAck_msg= JSON_MsgAck_msg.create_with_msgExpired( json_msgEngage )
226
- json_msgAck_msg["2PhW"]= k_VERSION;
227
- return json_msgAck_msg;
228
- },
229
- validate : function(json) {
230
- if (! JSON_MsgAck_msg.validate(json))
231
- return false;
232
- if (! JSON_MsgEngage.validate(json._msg))
265
+ // create_with_msgExpired : function( json_msgEngage ) {
266
+ // const json_msgAck_msg= JSON_MsgAck_msg.create_with_msgExpired( json_msgEngage )
267
+ // json_msgAck_msg["2PhW"]= k_VERSION;
268
+ // return json_msgAck_msg;
269
+ // },
270
+ validate : function(json_msgAck_msg) {
271
+ // if (! JSON_MsgAck_msg.validate(json_msgAck_msg))
272
+ // return false;
273
+ if (! JSON_MsgEngage.validate(json_msgAck_msg._msg))
233
274
  return false;
234
275
  return true;
235
276
  }
@@ -240,16 +281,20 @@ var JSON_MsgAckEngage = (function() {
240
281
  // States
241
282
 
242
283
  class StateState {
243
- constructor(idx_event, event) {
244
- this._protocol_parent = null;
284
+ private _idx_event : number;
285
+ private _event : EventMinimal;
286
+ protected _statekeeper : ProtocolStateState;
287
+
288
+ constructor(idx_event : number, event : EventMinimal) {
289
+ this._statekeeper = null;
245
290
  this._idx_event = idx_event;
246
291
  this._event = event;
247
292
  }
248
293
  get protocol() {
249
- return this._protocol_parent;
294
+ return this._statekeeper;
250
295
  }
251
296
  set protocol(parent) {
252
- this._protocol_parent = parent;
297
+ this._statekeeper = parent;
253
298
  }
254
299
  get idx_event() {
255
300
  return this._idx_event;
@@ -258,33 +303,33 @@ class StateState {
258
303
  return this._event;
259
304
  }
260
305
  get json_msg() {
261
- return this._event.data;
306
+ return this._event.data.json_msg;
262
307
  }
263
- changeState(state_new) {
308
+ changeState(state_new : StateState) {
264
309
  if (this.canChangeState(state_new))
265
310
  this.protocol.state = state_new;
266
311
  else
267
312
  this.protocol.state = new StateINVALID(state_new._idx_event, state_new._event);
268
313
  }
269
- canChangeState(_state_new) {
314
+ canChangeState(_state_new : StateState) : boolean {
270
315
  throw new Error("Subclass must implement canChangeState()");
271
316
  }
272
317
  }
273
318
 
274
319
  class StateAckEngage extends StateState {
275
- constructor(idx_event, event) { super(idx_event, event); }
320
+ constructor(idx_event : number, event : EventMinimal) { super(idx_event, event); }
276
321
  get evt_AckEngage() { return this.event }
277
322
  get json_msgAckEngage() { return this.json_msg }
278
- canChangeState(_state_new) {
323
+ canChangeState(_state_new : StateState) {
279
324
  return false;
280
325
  }
281
326
  }
282
327
 
283
328
  class StateEngage extends StateState {
284
- constructor(idx_event, event) { super(idx_event, event); }
329
+ constructor(idx_event : number, event : EventMinimal) { super(idx_event, event); }
285
330
  get evt_Engage() { return this.event }
286
331
  get json_msgEngage() { return this.json_msg }
287
- canChangeState(state_new) {
332
+ canChangeState(state_new : StateState) {
288
333
  if (false === state_new instanceof StateAckEngage)
289
334
  return false;
290
335
 
@@ -300,10 +345,10 @@ class StateEngage extends StateState {
300
345
  }
301
346
 
302
347
  class StateAckWarp extends StateState {
303
- constructor(idx_event, event) { super(idx_event, event); }
348
+ constructor(idx_event : number, event : EventMinimal) { super(idx_event, event); }
304
349
  get evt_AckWarp() { return this.event }
305
350
  get json_msgAckWarp() { return this.json_msg }
306
- canChangeState(state_new) {
351
+ canChangeState(state_new : StateState) {
307
352
  if (false === state_new instanceof StateEngage)
308
353
  return false;
309
354
 
@@ -328,10 +373,10 @@ class StateAckWarp extends StateState {
328
373
  }
329
374
 
330
375
  class StateWarp extends StateState {
331
- constructor(idx_event, event) { super(idx_event, event); }
376
+ constructor(idx_event : number, event : EventMinimal) { super(idx_event, event); }
332
377
  get evt_Warp() { return this.event }
333
378
  get json_msgWarp() { return this.json_msg }
334
- canChangeState(state_new) {
379
+ canChangeState(state_new : StateState) {
335
380
  if (false === state_new instanceof StateAckWarp)
336
381
  return false;
337
382
 
@@ -346,8 +391,8 @@ class StateWarp extends StateState {
346
391
  }
347
392
 
348
393
  class StateINVALID extends StateState {
349
- constructor(idx_event, event) { super(idx_event, event); }
350
- canChangeState(_state_new) {
394
+ constructor(idx_event : number, event : EventMinimal) { super(idx_event, event); }
395
+ canChangeState(_state_new : StateState) {
351
396
  // INVALID can't transition to any state
352
397
  return false;
353
398
  }
@@ -355,7 +400,7 @@ class StateINVALID extends StateState {
355
400
 
356
401
  class StateInitialState extends StateState {
357
402
  constructor() { super(null, null); }
358
- canChangeState(state_new) {
403
+ canChangeState(state_new : StateState) {
359
404
  if (false === state_new instanceof StateWarp)
360
405
  return false;
361
406
 
@@ -372,11 +417,13 @@ class StateInitialState extends StateState {
372
417
  //----
373
418
 
374
419
  class ProtocolStateState {
375
- constructor(state_initial) {
420
+ private _state : StateState;
421
+
422
+ constructor(state_initial? : StateState) {
376
423
  this.state = state_initial || new StateInitialState();
377
424
  }
378
425
 
379
- set state(state_new) {
426
+ set state(state_new : StateState) {
380
427
  if (! (state_new instanceof StateState)) {
381
428
  throw new Error("initState must be an instance of StateState or its subclasses");
382
429
  }
@@ -385,11 +432,11 @@ class ProtocolStateState {
385
432
  state_new.protocol = this;
386
433
  }
387
434
  }
388
- get state() {
435
+ get state() : StateState {
389
436
  return this._state;
390
437
  }
391
438
 
392
- canChangeState(state_new) {
439
+ canChangeState(state_new : StateState) : boolean {
393
440
  return this._state.canChangeState(state_new);
394
441
  }
395
442
  }
@@ -397,7 +444,9 @@ class ProtocolStateState {
397
444
  //-------------------------------------------------------------------------------------------------
398
445
 
399
446
  class TwoPhW_EventLog {
400
- constructor(eventLog) {
447
+ private _eventLog : EventLog;
448
+
449
+ constructor(eventLog : EventLog) {
401
450
  this._eventLog = eventLog;
402
451
  }
403
452
 
@@ -420,9 +469,10 @@ class TwoPhW_EventLog {
420
469
  return { str_type: null, json_msgWarp: null }; // Explicit default for invalid cases
421
470
  }
422
471
 
423
- _addEvent(json_msg, timeout_ms, timeout_callback) {
472
+ // _addEvent(msg: t_msg, json_msg : JSON_Msg, timeout_ms? : number, timeout_callback? : ( args? : {idx_event : number, event : EventMinimal} ) => {}) {
473
+ _addEvent(msg: t_msg, json_msg : JSON_Msg, timeout_ms? : number, timeout_callback? : () => void) {
424
474
  const { str_type, json_msgWarp }= this._warp(json_msg);
425
- const { idx }= this._eventLog.addEvent(str_type, json_msgWarp.warp.id, json_msg, timeout_ms, timeout_callback);
475
+ const { idx }= this._eventLog.addEvent(str_type, json_msgWarp.warp.id, {msg, json_msg}, timeout_ms, timeout_callback);
426
476
  log_(l_.PROTOCOL_STATE, () => [`_addEvent: [${idx}] timeout:${Date.now()}+${timeout_ms} `, json_msg]);
427
477
  }
428
478
 
@@ -438,19 +488,17 @@ class TwoPhW_EventLog {
438
488
  this._eventLog.cancelTimeout(idx_event);
439
489
  }
440
490
 
441
- get_protocolState(json_msgWarp) {
491
+ traverse_State(json_msgWarp) {
442
492
  const protocolState = new ProtocolStateState();
443
493
 
444
- const arr_evts= this._eventLog.getEventById(json_msgWarp.warp.id, k_TTL);
445
- log_(l_.EVENTS, () => ['get_protocolState: arr_evts: ', arr_evts]);
494
+ const arr_evts= this._eventLog.getEventsById(json_msgWarp.warp.id, k_TTL);
495
+ log_(l_.EVENTS, () => ['traverse_State: arr_evts: ', arr_evts]);
496
+ //TODO: what is the right TTL? should it be a constant or calculated
446
497
 
447
498
  for (let i = arr_evts.length - 1; i >= 0; i--) {
448
499
  const { idx, evt } = arr_evts[i];
449
500
 
450
- if (evt.type === '2PhW[DROPPED!]')
451
- continue;
452
-
453
- const json_msg= evt.data;
501
+ const json_msg= evt.data.json_msg;
454
502
  if (! JSON_MsgWarp.compare(json_msgWarp, this._warp(json_msg).json_msgWarp) )
455
503
  protocolState.state.changeState( new StateINVALID(idx, evt) );
456
504
 
@@ -468,10 +516,10 @@ class TwoPhW_EventLog {
468
516
  protocolState.state.changeState( new StateAckEngage(idx, evt) );
469
517
  break;
470
518
  default :
471
- protocolState.state.changeState( new StateINVALID(idx, evt) );
472
519
  break;
473
520
  }
474
- log_(l_.PROTOCOL_STATE, () => ['get_protocolState: type,state: ',
521
+
522
+ log_(l_.PROTOCOL_STATE, () => ['traverse_State: type,state: ',
475
523
  evt.type,
476
524
  (protocolState.state.json_msg.ack?.ok === false) ? '--NACK--' : '',
477
525
  protocolState.state
@@ -483,16 +531,19 @@ class TwoPhW_EventLog {
483
531
  }
484
532
 
485
533
  class TwoPhW {
486
- constructor(eventLog) {
534
+ private _2PhW_eventLog : TwoPhW_EventLog;
535
+ private _config : Config;
536
+
537
+ constructor(eventLog : EventLog) {
487
538
  this._2PhW_eventLog = new TwoPhW_EventLog(eventLog);
488
539
  this._config= new Config();
489
540
  }
490
541
 
491
- get config() {
542
+ get config() : Config {
492
543
  return this._config;
493
544
  }
494
545
 
495
- get eventLog() {
546
+ get eventLog() : TwoPhW_EventLog {
496
547
  return this._2PhW_eventLog;
497
548
  }
498
549
 
@@ -502,6 +553,7 @@ class TwoPhW {
502
553
  {
503
554
  log_(l_.HANDLERS, () => ['handler_msgOptions_: ...']);
504
555
 
556
+ /*
505
557
  if (! validate_Options_delay_(json_msgOptions.options))
506
558
  return false;
507
559
 
@@ -526,6 +578,7 @@ class TwoPhW {
526
578
 
527
579
  return true;
528
580
  }
581
+ */
529
582
 
530
583
  return false;
531
584
  }
@@ -533,14 +586,14 @@ class TwoPhW {
533
586
  //-------------------------------------------------------------------------------------------------
534
587
  // sender Source (src)
535
588
 
536
- register_msgWarp(json_msgWarp)
589
+ register_msgWarp(msg : t_msg, json_msgWarp : JSON_Msg)
537
590
  {
538
591
  log_(l_.SENDER, () => ['register_msgWarp: ', json_msgWarp]);
539
592
 
540
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
593
+ const protocolState= this.eventLog.traverse_State(json_msgWarp);
541
594
  const b_changeState= protocolState.canChangeState( new StateWarp(null, {
542
595
  dt: Date.now(),
543
- data: json_msgWarp
596
+ data: { msg, json_msg: json_msgWarp }
544
597
  }) );
545
598
 
546
599
  log_(l_.HANDLERS | l_.SENDER, () => ['register_msgWarp: protocolState.canChangeState ', b_changeState]);
@@ -550,21 +603,21 @@ class TwoPhW {
550
603
  const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec);
551
604
  const elapsed_ms= Date.now() - Date.parse(json_msgWarp.dt);
552
605
 
553
- this.eventLog._addEvent(json_msgWarp, timeout_ms-elapsed_ms, ({ _idx, _evt}) => {
554
- this.config._callback_msg_sent_expired(json_msgWarp);
606
+ this.eventLog._addEvent(msg, json_msgWarp, timeout_ms-elapsed_ms, () => { // pass { _idx, _evt}?
607
+ this.config._callback_msg_sent_expired(msg, json_msgWarp);
555
608
  });
556
609
  }
557
610
 
558
- _handler_msgAckWarp(msg, json_msgAckWarp)
611
+ _handler_msgAckWarp(msg : t_msg, json_msgAckWarp : JSON_MsgAck_msg)
559
612
  {
560
613
  log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckWarp: ', json_msgAckWarp]);
561
614
 
562
615
  const json_msgWarp= json_msgAckWarp._msg;
563
616
 
564
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
617
+ const protocolState= this.eventLog.traverse_State(json_msgWarp);
565
618
  const b_changeState= protocolState.canChangeState( new StateAckWarp(null, {
566
619
  dt: Date.now(),
567
- data: json_msgAckWarp
620
+ data: { msg, json_msg: json_msgAckWarp }
568
621
  }) );
569
622
 
570
623
  log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckWarp: protocolState.canChangeState ', b_changeState]);
@@ -572,23 +625,23 @@ class TwoPhW {
572
625
  return false;
573
626
 
574
627
  this.eventLog._cancelTimeout(protocolState.state.idx_event);
575
- this.eventLog._addEvent(json_msgAckWarp);
628
+ this.eventLog._addEvent(msg, json_msgAckWarp);
576
629
  if (! this.config._callback_msgAckWarp(msg, json_msgAckWarp))
577
630
  return false;
578
631
 
579
632
  return true;
580
633
  }
581
634
 
582
- register_msgEngage(json_msgEngage)
635
+ register_msgEngage(msg : t_msg, json_msgEngage : JSON_Msg_msg)
583
636
  {
584
637
  log_(l_.SENDER, () => ['register_msgEngage: ', json_msgEngage]);
585
638
 
586
639
  const json_msgWarp= json_msgEngage._msg;
587
640
 
588
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
589
- const b_changeState= protocolState.canChangeState( new StateEngage(null, {
641
+ const protocolState= this.eventLog.traverse_State(json_msgWarp);
642
+ const b_changeState = protocolState.canChangeState( new StateEngage(null, {
590
643
  dt: Date.now(),
591
- data : json_msgEngage
644
+ data : { msg, json_msg: json_msgEngage }
592
645
  }) );
593
646
 
594
647
  log_(l_.HANDLERS | l_.SENDER, () => ['register_msgEngage: protocolState.canChangeState ', b_changeState]);
@@ -598,21 +651,21 @@ class TwoPhW {
598
651
  const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec, json_msgEngage.engage.rtt_msec);
599
652
  const elapsed_ms= Date.now() - Date.parse(json_msgWarp.dt);
600
653
 
601
- this.eventLog._addEvent(json_msgEngage, timeout_ms-elapsed_ms, () => {
602
- this.config._callback_msg_sent_expired(json_msgEngage);
654
+ this.eventLog._addEvent(msg, json_msgEngage, timeout_ms-elapsed_ms, () => {
655
+ this.config._callback_msg_sent_expired(msg, json_msgEngage);
603
656
  });
604
657
  }
605
658
 
606
- _handler_msgAckEngage(msg, json_msgAckEngage)
659
+ _handler_msgAckEngage(msg : t_msg, json_msgAckEngage : JSON_MsgAck_msg)
607
660
  {
608
661
  log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckEngage: ', json_msgAckEngage]);
609
662
 
610
663
  const json_msgWarp= json_msgAckEngage._msg._msg;
611
664
 
612
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
665
+ const protocolState= this.eventLog.traverse_State(json_msgWarp);
613
666
  const b_changeState= protocolState.canChangeState( new StateAckEngage(null, {
614
667
  dt: Date.now(),
615
- data : json_msgAckEngage
668
+ data : { msg, json_msg: json_msgAckEngage }
616
669
  }) );
617
670
 
618
671
  log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckEngage_: protocolState.canChangeState ', b_changeState]);
@@ -620,7 +673,7 @@ class TwoPhW {
620
673
  return false;
621
674
 
622
675
  this.eventLog._cancelTimeout(protocolState.state.idx_event);
623
- this.eventLog._addEvent(json_msgAckEngage);
676
+ this.eventLog._addEvent(msg, json_msgAckEngage);
624
677
  if (! this.config._callback_msgAckEngage(msg, json_msgAckEngage))
625
678
  return false;
626
679
 
@@ -630,37 +683,37 @@ class TwoPhW {
630
683
  //-------------------------------------------------------------------------------------------------
631
684
  // receiver Destination (dst)
632
685
 
633
- _handler_msgWarp(msg, json_msgWarp)
686
+ _handler_msgWarp(msg : t_msg, json_msgWarp : JSON_Msg)
634
687
  {
635
688
  log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgWarp: ', json_msgWarp]);
636
689
 
637
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
690
+ const protocolState= this.eventLog.traverse_State(json_msgWarp);
638
691
  const b_changeState= protocolState.canChangeState( new StateWarp(null, {
639
692
  dt: Date.now(),
640
- data: json_msgWarp
693
+ data: { msg, json_msg: json_msgWarp }
641
694
  }) );
642
695
 
643
696
  log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgWarp: protocolState.canChangeState: ', b_changeState]);
644
697
  if (! b_changeState)
645
698
  return false;
646
699
 
647
- this.eventLog._addEvent(json_msgWarp);
700
+ this.eventLog._addEvent(msg, json_msgWarp);
648
701
  if (! this.config._callback_msgWarp(msg, json_msgWarp))
649
702
  return false;
650
703
 
651
704
  return true;
652
705
  }
653
706
 
654
- register_msgAckWarp(json_msgAckWarp)
707
+ register_msgAckWarp(msg: t_msg, json_msgAckWarp : JSON_MsgAck_msg)
655
708
  {
656
709
  log_(l_.RECVER, () => ['register_msgAckWarp: ', json_msgAckWarp]);
657
710
 
658
711
  const json_msgWarp= json_msgAckWarp._msg;
659
712
 
660
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
713
+ const protocolState= this.eventLog.traverse_State(json_msgWarp);
661
714
  const b_changeState= protocolState.canChangeState( new StateAckWarp(null, {
662
715
  dt: Date.now(),
663
- data: json_msgAckWarp
716
+ data: { msg, json_msg: json_msgAckWarp }
664
717
  }) );
665
718
 
666
719
  log_(l_.HANDLERS | l_.RECVER, () => ['register_msgAckWarp: protocolState.canChangeState: ', b_changeState]);
@@ -670,21 +723,21 @@ class TwoPhW {
670
723
  const elapsed_ms= Date.now() - Date.parse(json_msgAckWarp.dt);
671
724
  const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec) + Date.parse(json_msgAckWarp.dt) - Date.parse(json_msgWarp.dt);
672
725
 
673
- this.eventLog._addEvent(json_msgAckWarp, timeout_ms-elapsed_ms, ({ _idx, _evt}) => {
674
- this.config._callback_msg_sent_expired(json_msgAckWarp);
726
+ this.eventLog._addEvent(msg, json_msgAckWarp, timeout_ms-elapsed_ms, () => { // pass { _idx, _evt} ?
727
+ this.config._callback_msg_sent_expired(msg, json_msgAckWarp);
675
728
  });
676
729
  }
677
730
 
678
- _handler_msgEngage(msg, json_msgEngage)
731
+ _handler_msgEngage(msg : t_msg, json_msgEngage : JSON_Msg_msg)
679
732
  {
680
733
  log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgEngage: ', json_msgEngage]);
681
734
 
682
735
  const json_msgWarp= json_msgEngage["_msg"];
683
736
 
684
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
737
+ const protocolState= this.eventLog.traverse_State(json_msgWarp);
685
738
  const b_changeState= protocolState.canChangeState( new StateEngage(null, {
686
739
  dt: Date.now(),
687
- data: json_msgEngage
740
+ data: { msg, json_msg: json_msgEngage }
688
741
  }) );
689
742
 
690
743
  log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgEngage: protocolState.canChangeState: ', b_changeState]);
@@ -692,54 +745,65 @@ class TwoPhW {
692
745
  return false;
693
746
 
694
747
  this.eventLog._cancelTimeout(protocolState.state.idx_event);
695
- this.eventLog._addEvent(json_msgEngage);
748
+ this.eventLog._addEvent(msg, json_msgEngage);
696
749
  if (! this.config._callback_msgEngage(msg, json_msgEngage))
697
750
  return false;
698
751
 
699
752
  return true;
700
753
  }
701
754
 
702
- register_msgAckEngage(json_msgAckEngage)
755
+ register_msgAckEngage(msg : t_msg, json_msgAckEngage : JSON_MsgAck_msg)
703
756
  {
704
757
  log_(l_.RECVER, () => ['register_msgAckEngage: ', json_msgAckEngage]);
705
758
 
706
759
  const json_msgWarp= json_msgAckEngage._msg._msg;
707
760
 
708
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
761
+ const protocolState= this.eventLog.traverse_State(json_msgWarp);
709
762
  const b_changeState= protocolState.canChangeState( new StateAckEngage(null, {
710
763
  dt: Date.now(),
711
- data: json_msgAckEngage
764
+ data: { msg, json_msg: json_msgAckEngage }
712
765
  }) );
713
766
 
714
767
  log_(l_.HANDLERS | l_.RECVER, () => ['register_msgAckEngage: protocolState.canChangeState: ', b_changeState]);
715
768
  if (! b_changeState)
716
769
  return false;
717
770
 
718
- this.eventLog._addEvent(json_msgAckEngage);
771
+ this.eventLog._addEvent(msg, json_msgAckEngage);
719
772
  }
720
773
 
721
774
  //-------------------------------------------------------------------------------------------------
722
775
 
723
- handler_msg_json(msg, json_msg)
776
+ handler_json_msgAck_msg(msg : t_msg, json_msgAck_msg : JSON_MsgAck_msg)
724
777
  {
725
- log_(l_.HANDLERS, () => ['handler_msg_json: ', json_msg]);
778
+ log_(l_.HANDLERS, () => ['handler_json_msgAck_msg: ', json_msgAck_msg]);
726
779
 
727
- // ADR: I could parse json_Msg_msg first and then MsgAck or MsgOptions, but
728
- // doing so means super classes get parsed multiple times for no reason
729
-
730
- // if (JSON_MsgOptions.validate(json_msg) && this.handler_msgOptions_(this._config, msg, json_msg))
731
- // return true;
732
-
733
- if (JSON_MsgWarp.validate(json_msg) && this._handler_msgWarp(msg, json_msg))
780
+ if (JSON_MsgAckWarp.validate(json_msgAck_msg) && this._handler_msgAckWarp(msg, json_msgAck_msg))
734
781
  return true;
735
782
 
736
- if (JSON_MsgEngage.validate(json_msg) && this._handler_msgEngage(msg, json_msg))
783
+ if (JSON_MsgAckEngage.validate(json_msgAck_msg) && this._handler_msgAckEngage(msg, json_msgAck_msg))
737
784
  return true;
738
785
 
739
- if (JSON_MsgAckWarp.validate(json_msg) && this._handler_msgAckWarp(msg, json_msg))
786
+ return true;
787
+ }
788
+
789
+ handler_json_msg_msg(msg : t_msg, json_msg_msg : JSON_Msg_msg)
790
+ {
791
+ log_(l_.HANDLERS, () => ['handler_json_msg_msg: ', json_msg_msg]);
792
+
793
+ if (JSON_MsgEngage.validate(json_msg_msg) && this._handler_msgEngage(msg, json_msg_msg))
740
794
  return true;
741
795
 
742
- if (JSON_MsgAckEngage.validate(json_msg) && this._handler_msgAckEngage(msg, json_msg))
796
+ // if (JSON_MsgOptions.validate(json_msg) && this.handler_msgOptions_(this._config, msg, json_msg))
797
+ // return true;
798
+
799
+ return false;
800
+ }
801
+
802
+ handler_json_msg(msg : t_msg, json_msg : JSON_Msg)
803
+ {
804
+ log_(l_.HANDLERS, () => ['handler_json_msg: ', json_msg]);
805
+
806
+ if (JSON_MsgWarp.validate(json_msg) && this._handler_msgWarp(msg, json_msg))
743
807
  return true;
744
808
 
745
809
  // https://attacomsian.com/blog/javascript-iterate-objects
@@ -751,39 +815,39 @@ class TwoPhW {
751
815
  // console.log(`${key}: ${json_msg[key]}`);
752
816
  // });
753
817
 
754
- this.eventLog._dropEvent(json_msg);
755
- log_(l_.DROPS, () => ["TwoPhW: handler_msg: DROP! ", msg]);
756
818
  return false;
757
819
  }
758
820
 
759
- handler_msg_str(msg, str_msg)
821
+ handler_json(msg : t_msg, json)
760
822
  {
761
- log_(l_.HANDLERS, () => ['handler_msg_str: ['+ str_msg +']']);
823
+ if (JSON_Msg.validate(json))
824
+ {
825
+ if (JSON_Msg_msg.validate(json))
826
+ {
827
+ if (JSON_MsgAck_msg.validate(json))
828
+ {
829
+ if (JSON_MsgAckWarp.validate(json) && this._handler_msgAckWarp(msg, json))
830
+ return true;
831
+
832
+ if (JSON_MsgAckEngage.validate(json) && this._handler_msgAckEngage(msg, json))
833
+ return true;
834
+
835
+ return false;
836
+ }
837
+
838
+ if (JSON_MsgEngage.validate(json) && this._handler_msgEngage(msg, json))
839
+ return true;
840
+
841
+ return false;
842
+ }
762
843
 
763
- // ... test strings here
844
+ if (JSON_MsgWarp.validate(json) && this._handler_msgWarp(msg, json))
845
+ return true;
764
846
 
765
- let json_msg;
766
- try {
767
- json_msg= JSON.parse(str_msg);
768
- }
769
- catch (err) {
770
847
  return false;
771
848
  }
772
849
 
773
- return this.handler_msg_json(msg, json_msg);
774
- }
775
-
776
- handler_msg(msg)
777
- {
778
- log_(l_.HANDLERS, () => ['handler_msg: ', msg]);
779
-
780
- if (typeof (msg) === 'string' && this.handler_msg_str(msg, msg))
781
- return true;
782
-
783
- if (typeof (msg) === 'object' && this.handler_msg_json(msg, msg))
784
- return true;
785
-
786
- return false
850
+ return false;
787
851
  }
788
852
 
789
853
  };