@rootintf/protocol-2phw 0.7.5-api-0.7 → 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,34 +1,36 @@
1
1
 
2
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_
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
+
8
9
  } from './validate-esm.mjs';
9
10
 
10
- import { BitLogr, l_LL } from '@knev/bitlogr';
11
- import { JSON_Msg, JSON_Msg_msg, JSON_MsgAck } from '@rootintf/json-msg'
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'
12
16
 
13
17
  const k_TTL= 20000;
14
18
 
15
19
  //-------------------------------------------------------------------------------------------------
16
20
 
17
- let LOGR_= new BitLogr();
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
18
25
 
19
- const l_msgs_ = {
20
- MSG : 0b1 << 0,
21
- WARP : 0b1 << 1,
22
- ENGAGE : 0b1 << 2,
23
- ACK : 0b1 << 3,
24
- }
26
+ const LOGR_= LOGR.get_instance();
27
+ const log_= LOGR_.log;
25
28
 
26
- const l_= {
27
- HANDLERS : 0b1 << 0,
28
- DROPS : 0b1 << 1,
29
- VALIDATION : 0b1 << 2,
30
- ... l_LL(l_msgs_, 8)
31
- }
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'])) )
32
34
  LOGR_.labels= l_;
33
35
 
34
36
  //-------------------------------------------------------------------------------------------------
@@ -48,20 +50,30 @@ function get_ctx(key) {
48
50
  //-------------------------------------------------------------------------------------------------
49
51
 
50
52
  class Config {
51
- receiver(callback_msgWarp,callback_msgEngage) {
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) {
52
64
  this._callback_msgWarp= callback_msgWarp;
53
65
  this._callback_msgEngage= callback_msgEngage;
66
+ this._callback_msg_sent_expired= callback_msg_sent_expired;
54
67
  }
55
- sender(callback_msgAckWarp,callback_msgAckEngage ) {
68
+ sender(callback_msgAckWarp, callback_msgAckEngage, callback_msg_sent_expired ) {
56
69
  this._callback_msgAckWarp= callback_msgAckWarp;
57
70
  this._callback_msgAckEngage= callback_msgAckEngage;
71
+ this._callback_msg_sent_expired= callback_msg_sent_expired;
58
72
  }
73
+
59
74
  set callback_msgOptions(callback) {
60
75
  this._callback_msgOptions= callback;
61
76
  }
62
- set logr(obj_logr) {
63
- LOGR_.toggled= obj_logr;
64
- }
65
77
  }
66
78
 
67
79
  // var config = (function() {
@@ -80,11 +92,21 @@ class Config {
80
92
 
81
93
  //-------------------------------------------------------------------------------------------------
82
94
 
83
- let k_VERSION= "0.7";
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
+ }
84
106
 
85
107
  var JSON_MsgOptions = (function() {
86
108
  return {
87
- create_with_msg_options : function(_msg, options) {
109
+ create_with_msg_options : function(_msg, options) {
88
110
  return JSON_Msg_msg.create_with_msg_merge(_msg, {
89
111
  options: options,
90
112
  "2PhW": k_VERSION
@@ -93,12 +115,8 @@ var JSON_MsgOptions = (function() {
93
115
  validate : function(json) {
94
116
  if (! JSON_Msg_msg.validate(json))
95
117
  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)
118
+ if (! schema_MsgOptions_(json)) {
119
+ log_(l_.VALIDATION, () => ['JSON_MsgOptions: schema_MsgOptions_: ', schema_MsgOptions_.errors])
102
120
  return false;
103
121
  }
104
122
  return true;
@@ -108,9 +126,10 @@ var JSON_MsgOptions = (function() {
108
126
 
109
127
  var JSON_MsgWarp = (function() {
110
128
  return {
111
- create_with_user_hyperport : function(json_user, json_hyperport, nr_ttl_msec) {
129
+ create_with_id_user_hyperport : function(json_warp_id, json_user, json_hyperport, nr_ttl_msec) {
112
130
  return JSON_Msg.merge({
113
131
  warp : {
132
+ id : json_warp_id,
114
133
  user : json_user,
115
134
  hyperport : json_hyperport,
116
135
  ttl_msec : nr_ttl_msec
@@ -118,15 +137,22 @@ var JSON_MsgWarp = (function() {
118
137
  "2PhW": k_VERSION
119
138
  });
120
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
+ },
121
151
  validate : function(json) {
122
152
  if (! JSON_Msg.validate(json))
123
153
  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)
154
+ if (! schema_MsgWarp_(json)) {
155
+ log_(l_.VALIDATION, () => ['JSON_MsgWarp: schema_MsgWarp_: ', schema_MsgWarp_.errors])
130
156
  return false;
131
157
  }
132
158
  return true;
@@ -135,32 +161,29 @@ var JSON_MsgWarp = (function() {
135
161
  })();
136
162
 
137
163
  var JSON_MsgEngage = (function() {
138
- return {
164
+ return {
139
165
  create_with_MsgWarp : function(json_msgWarp) {
140
- let json_msgEngage= JSON_Msg.merge({
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, {
141
168
  engage : {
142
- user : json_msgWarp.warp.user,
143
- hyperport : json_msgWarp.warp.hyperport,
169
+ "ok" : true,
170
+ "rtt_msec" : Date.now() - Date.parse(json_msgWarp.dt)
144
171
  },
145
172
  "2PhW": k_VERSION
146
173
  });
147
- json_msgEngage["id"]= json_msgWarp.id;
148
- return json_msgEngage;
149
174
  },
150
175
  validate : function(json) {
151
- if (! JSON_Msg.validate(json))
176
+ if (! JSON_Msg_msg.validate(json))
152
177
  return false;
153
- if (! validate_Msg2PhW_(json)) {
154
- LOGR_.log(l_.VALIDATION, 'JSON_MsgWarp: validate_Msg2PhW_: ', validate_Msg2PhW_.errors)
178
+ if (! JSON_MsgWarp.validate(json._msg))
155
179
  return false;
156
- }
157
- if (! validate_MsgEngage_(json)) {
158
- LOGR_.log(l_.VALIDATION, 'JSON_MsgEngage: validate_MsgEngage_: ', validate_MsgEngage_.errors)
180
+ if (! schema_MsgEngage_(json)) {
181
+ log_(l_.VALIDATION, () => ['JSON_MsgEngage: schema_MsgEngage_: ', schema_MsgEngage_.errors])
159
182
  return false;
160
183
  }
161
184
  return true;
162
185
  }
163
- }
186
+ };
164
187
  })();
165
188
 
166
189
  var JSON_MsgAckWarp = (function() {
@@ -171,21 +194,17 @@ var JSON_MsgAckWarp = (function() {
171
194
  // return json_msgAckWarp;
172
195
  // }
173
196
  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;
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;
177
200
  },
178
201
  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;
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;
182
205
  },
183
206
  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))
207
+ if (! JSON_MsgAck_msg.validate(json))
189
208
  return false;
190
209
  if (! JSON_MsgWarp.validate(json._msg))
191
210
  return false;
@@ -202,21 +221,17 @@ var JSON_MsgAckEngage = (function() {
202
221
  // return json_msgAckEngage;
203
222
  // }
204
223
  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;
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;
208
227
  },
209
228
  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;
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;
213
232
  },
214
233
  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))
234
+ if (! JSON_MsgAck_msg.validate(json))
220
235
  return false;
221
236
  if (! JSON_MsgEngage.validate(json._msg))
222
237
  return false;
@@ -225,305 +240,508 @@ var JSON_MsgAckEngage = (function() {
225
240
  }
226
241
  })();
227
242
 
228
-
229
243
  //-------------------------------------------------------------------------------------------------
230
- // State
244
+ // States
231
245
 
232
246
  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;
247
+ constructor(idx_event, event) {
248
+ this._protocol_parent = null;
249
+ this._idx_event = idx_event;
250
+ this._event = event;
257
251
  }
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();
252
+ get protocol() {
253
+ return this._protocol_parent;
263
254
  }
264
- inState(stateClass) {
265
- return this.state_current_ instanceof stateClass;
255
+ set protocol(parent) {
256
+ this._protocol_parent = parent;
266
257
  }
267
- }
268
-
269
- class StateInitialState {
270
- changeState(newState) {
271
- return true;
258
+ get idx_event() {
259
+ return this._idx_event;
272
260
  }
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);
261
+ get event() {
262
+ return this._event;
292
263
  }
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;
264
+ get json_msg() {
265
+ return this._event.data;
302
266
  }
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;
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()");
312
275
  }
313
276
  }
314
277
 
315
- class StateEngage {
316
- changeState(newState) {
317
- if (newState instanceof StateComplete)
318
- return true;
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) {
319
283
  return false;
320
284
  }
321
285
  }
322
286
 
323
- class StateComplete {
324
- changeState(newState) {
325
- return false;
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;
326
303
  }
327
304
  }
328
305
 
329
- //-------------------------------------------------------------------------------------------------
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;
330
316
 
331
- function handler_msgOptions_(cfg, msg, json_msgOptions)
332
- {
333
- LOGR_.log(l_.HANDLERS, 'handler_msgOptions_: ...');
317
+ if (this.json_msgAckWarp._msg.warp.instantaneous)
318
+ return false;
334
319
 
335
- if (! validate_Options_delay_(json_msgOptions.options))
336
- return false;
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;
337
329
 
338
- if (JSON_MsgWarp.validate(json_msgOptions._msg)) {
339
- const json_msgWarp= json_msgOptions._msg;
330
+ return true;
331
+ }
332
+ }
340
333
 
341
- const ctx= get_ctx(json_msgWarp.id);
342
- if (!ctx )
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)
343
340
  return false;
344
341
 
345
- const ttl_msec= json_msgOptions.options.ttl_msec_incr[0];
346
- const ttl_incr= json_msgOptions.options.ttl_msec_incr[1];
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
347
 
348
- //TODO: double check ttl_msec is the same as current accummulated ttl for idempotence.
348
+ return true;
349
+ }
350
+ }
349
351
 
350
- LOGR_.log(l_.HANDLERS, 'handler_msgOptions_: ', json_msgOptions);
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
+ }
351
359
 
352
- ctx.protocol.delayTimeout(ttl_incr);
360
+ class StateInitialState extends StateState {
361
+ constructor() { super(null, null); }
362
+ canChangeState(state_new) {
363
+ if (false === state_new instanceof StateWarp)
364
+ return false;
353
365
 
354
- if (cfg._callback_msgOptions)
355
- return cfg._callback_msgOptions(msg, json_msgOptions);
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;
356
371
 
357
372
  return true;
358
373
  }
359
-
360
- return false;
361
374
  }
362
375
 
363
- //-------------------------------------------------------------------------------------------------
364
- // Sender
376
+ //----
365
377
 
366
- function handler_msgAckWarp_(cfg, msg, json_msgAckWarp)
367
- {
368
- // NOTE: json_msgAck here has a string as _msg, instead of JSON.
378
+ class ProtocolStateState {
379
+ constructor(state_initial) {
380
+ this.state = state_initial || new StateInitialState();
381
+ }
369
382
 
370
- LOGR_.log(l_.HANDLERS, 'handler_msgAckWarp: ...');
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
+ }
371
395
 
372
- const json_msgWarp= json_msgAckWarp._msg;
396
+ canChangeState(state_new) {
397
+ return this._state.canChangeState(state_new);
398
+ }
399
+ }
373
400
 
374
- const ctx= get_ctx(json_msgWarp.id);
375
- if (!ctx || !ctx.protocol.changeState( new StateAckWarp() ))
376
- return false;
401
+ //-------------------------------------------------------------------------------------------------
377
402
 
378
- //TODO: check if multiple warps have been sent out with the same destination
403
+ class TwoPhW_EventLog {
404
+ constructor(eventLog) {
405
+ this._eventLog = eventLog;
406
+ }
379
407
 
380
- LOGR_.log(l_.HANDLERS, 'handler_msgAckWarp: ', json_msgAckWarp);
408
+ _warp(json_msg) {
409
+ const b_Ack = json_msg.ack !== undefined;
410
+ const json = b_Ack ? json_msg["_msg"] : json_msg;
381
411
 
382
- // const json_msgAckWarp= JSON_MsgAckWarp.createWith_msgAck_msg(json_msgAck, json_msgWarp);
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
+ }
383
426
 
384
- if (! cfg._callback_msgAckWarp(msg, json_msgAckWarp))
385
- return false;
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
+ }
386
432
 
387
- if (json_msgWarp.warp.instantaneous) {
388
- const b_res= ctx.protocol.changeState( new StateComplete() );
389
- console.assert(b_res);
390
-
391
- return true;
433
+ _dropEvent(json_msg) {
434
+ const { json_msgWarp }= this._warp(json_msg);
435
+ this._eventLog.addEvent('2PhW[DROPPED!]', json_msgWarp.warp.id, json_msg);
392
436
  }
393
437
 
394
- const b_res= ctx.protocol.changeState( new StateEngage() );
395
- console.assert(b_res);
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
+ }
396
484
 
397
- return true;
398
485
  }
399
486
 
400
- function handler_msgAckEngage_(cfg, msg, json_msgAckEngage)
401
- {
402
- // NOTE: json_msgAck here has a string as _msg, instead of JSON.
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
+ }
403
496
 
404
- LOGR_.log(l_.HANDLERS, 'handler_msgAckEngage: ...');
497
+ get eventLog() {
498
+ return this._2PhW_eventLog;
499
+ }
405
500
 
406
- const json_msgEngage= json_msgAckEngage._msg;
501
+ //-------------------------------------------------------------------------------------------------
407
502
 
408
- const ctx= get_ctx(json_msgEngage.id);
409
- if (!ctx || !ctx.protocol.changeState( new StateAckEngage() ))
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
+
410
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]);
411
541
 
412
- LOGR_.log(l_.HANDLERS, 'handler_msgAckEngage: ', json_msgAckEngage);
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
+ }) );
413
547
 
414
- // const json_msgAckEngage= JSON_MsgAckEngage.createWith_msgAck_msg(json_msgAck, json_msgEngage);
548
+ log_(l_.HANDLERS | l_.SENDER, () => ['register_msgWarp: protocolState.canChangeState ', b_changeState]);
549
+ if (! b_changeState)
550
+ return false;
415
551
 
416
- if (! cfg._callback_msgAckEngage(msg, json_msgAckEngage))
417
- return false;
552
+ const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec);
553
+ const elapsed_ms= Date.now() - Date.parse(json_msgWarp.dt);
418
554
 
419
- return true;
420
- }
555
+ this.eventLog._addEvent(json_msgWarp, timeout_ms-elapsed_ms, () => {
556
+ this.config._callback_msg_sent_expired(json_msgWarp);
557
+ });
558
+ }
421
559
 
422
- //-------------------------------------------------------------------------------------------------
423
- // Receiver
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;
424
575
 
425
- function handler_msgWarp_(cfg, msg, json_msgWarp)
426
- {
427
- LOGR_.log(l_.HANDLERS, 'handler_msgWarp: ...');
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
+ }
428
583
 
429
- let ctx= get_ctx(json_msgWarp.id);
430
- if (ctx === undefined)
431
- ctx= set_ctx(json_msgWarp.id, { protocol: new ProtocolStateState() });
584
+ register_msgEngage(json_msgEngage)
585
+ {
586
+ log_(l_.SENDER, () => ['register_msgEngage: ', json_msgEngage]);
432
587
 
433
- if (!ctx || !ctx.protocol.changeState( new StateWarp() ))
434
- return false;
588
+ const json_msgWarp= json_msgEngage._msg;
435
589
 
436
- LOGR_.log(l_.HANDLERS, 'handler_msgWarp: ', json_msgWarp);
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
+ }) );
437
595
 
438
- if (! cfg._callback_msgWarp(msg, json_msgWarp))
439
- return false;
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);
440
602
 
441
- if (json_msgWarp.warp.instantaneous) {
442
- const b_res= ctx.protocol.changeState( new StateComplete() );
443
- console.assert(b_res);
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;
444
628
 
445
629
  return true;
446
630
  }
631
+
632
+ //-------------------------------------------------------------------------------------------------
633
+ // receiver Destination (dst)
447
634
 
448
- const b_res= ctx.protocol.changeState( new StateAckWarp() );
449
- console.assert(b_res);
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;
450
648
 
451
- return true;
452
- }
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]);
453
659
 
454
- function handler_msgEngage_(cfg, msg, json_msgEngage)
455
- {
456
- LOGR_.log(l_.HANDLERS, 'handler_msgEngage: ...');
660
+ const json_msgWarp= json_msgAckWarp._msg;
457
661
 
458
- const ctx= get_ctx(json_msgEngage.id);
459
- if (!ctx || !ctx.protocol.changeState( new StateEngage() ))
460
- return false;
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
+ }) );
461
667
 
462
- LOGR_.log(l_.HANDLERS, 'handler_msgEngage: ', json_msgEngage);
668
+ log_(l_.HANDLERS | l_.RECVER, () => ['register_msgAckWarp: protocolState.canChangeState: ', b_changeState]);
669
+ if (! b_changeState)
670
+ return false;
463
671
 
464
- if (! cfg._callback_msgEngage(msg, json_msgEngage))
465
- return false;
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);
466
674
 
467
- const b_res= ctx.protocol.changeState( new StateComplete() );
468
- console.assert(b_res);
675
+ this.eventLog._addEvent(json_msgAckWarp, timeout_ms-elapsed_ms, () => {
676
+ this.config._callback_msg_sent_expired(json_msgAckWarp);
677
+ });
678
+ }
469
679
 
470
- return true;
471
- }
680
+ _handler_msgEngage(msg, json_msgEngage)
681
+ {
682
+ log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgEngage: ', json_msgEngage]);
472
683
 
473
- //-------------------------------------------------------------------------------------------------
684
+ const json_msgWarp= json_msgEngage["_msg"];
474
685
 
475
- class TwoPhW {
476
- constructor() {
477
- this._config= new Config();
478
- }
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
+ }) );
479
691
 
480
- get config() {
481
- return this._config;
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;
482
702
  }
483
703
 
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)
704
+ register_msgAckEngage(json_msgAckEngage)
486
705
  {
487
- if (nr_ttl_msec === undefined)
488
- nr_ttl_msec= k_TTL;
706
+ log_(l_.RECVER, () => ['register_msgAckEngage: ', json_msgAckEngage]);
489
707
 
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;
708
+ const json_msgWarp= json_msgAckEngage._msg._msg;
495
709
 
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) );
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
+ }) );
502
715
 
503
- console.assert(b_res);
504
- return json_msgWarp;
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);
505
721
  }
506
722
 
723
+ //-------------------------------------------------------------------------------------------------
724
+
507
725
  handler_msg_json(msg, json_msg)
508
726
  {
509
- LOGR_.log(l_.HANDLERS, 'handler_msg_json: ', json_msg);
727
+ log_(l_.HANDLERS, () => ['handler_msg_json: ', json_msg]);
510
728
 
511
729
  // ADR: I could parse json_Msg_msg first and then MsgAck or MsgOptions, but
512
730
  // doing so means super classes get parsed multiple times for no reason
513
731
 
514
- if (JSON_MsgOptions.validate(json_msg) && handler_msgOptions_(this._config, msg, json_msg))
515
- return true;
732
+ // if (JSON_MsgOptions.validate(json_msg) && this.handler_msgOptions_(this._config, msg, json_msg))
733
+ // return true;
516
734
 
517
- if (JSON_MsgWarp.validate(json_msg) && handler_msgWarp_(this._config, msg, json_msg))
735
+ if (JSON_MsgWarp.validate(json_msg) && this._handler_msgWarp(msg, json_msg))
518
736
  return true;
519
737
 
520
- if (JSON_MsgEngage.validate(json_msg) && handler_msgEngage_(this._config, msg, json_msg))
738
+ if (JSON_MsgEngage.validate(json_msg) && this._handler_msgEngage(msg, json_msg))
521
739
  return true;
522
740
 
523
- if (JSON_MsgAckWarp.validate(json_msg) && handler_msgAckWarp_(this._config, msg, json_msg))
741
+ if (JSON_MsgAckWarp.validate(json_msg) && this._handler_msgAckWarp(msg, json_msg))
524
742
  return true;
525
743
 
526
- if (JSON_MsgAckEngage.validate(json_msg) && handler_msgAckEngage_(this._config, msg, json_msg))
744
+ if (JSON_MsgAckEngage.validate(json_msg) && this._handler_msgAckEngage(msg, json_msg))
527
745
  return true;
528
746
 
529
747
  // https://attacomsian.com/blog/javascript-iterate-objects
@@ -535,12 +753,14 @@ class TwoPhW {
535
753
  // console.log(`${key}: ${json_msg[key]}`);
536
754
  // });
537
755
 
756
+ this.eventLog._dropEvent(json_msg);
757
+ log_(l_.DROPS, () => ["TwoPhW: handler_msg: DROP! ", msg]);
538
758
  return false;
539
759
  }
540
760
 
541
761
  handler_msg_str(msg, str_msg)
542
762
  {
543
- LOGR_.log(l_.HANDLERS, 'handler_msg_str: ['+ str_msg +']');
763
+ log_(l_.HANDLERS, () => ['handler_msg_str: ['+ str_msg +']']);
544
764
 
545
765
  // ... test strings here
546
766
 
@@ -557,26 +777,37 @@ class TwoPhW {
557
777
 
558
778
  handler_msg(msg)
559
779
  {
560
- LOGR_.log(l_.HANDLERS, 'handler_msg: ', msg);
780
+ log_(l_.HANDLERS, () => ['handler_msg: ', msg]);
561
781
 
562
782
  if (typeof (msg) === 'string' && this.handler_msg_str(msg, msg))
563
783
  return true;
564
784
 
565
785
  if (typeof (msg) === 'object' && this.handler_msg_json(msg, msg))
566
786
  return true;
567
-
568
- LOGR_.log(l_.DROPS, "handler_msg: DROP! ", msg);
787
+
569
788
  return false
570
789
  }
571
790
 
572
791
  };
573
792
 
793
+ //-------------------------------------------------------------------------------------------------
794
+
574
795
  export {
796
+ EventLog,
797
+ t_ttl_rtt_ as t_ttl_rtt,
575
798
  TwoPhW,
576
799
  JSON_MsgOptions,
577
800
  JSON_MsgWarp,
578
801
  JSON_MsgEngage,
579
802
  JSON_MsgAckWarp,
580
803
  JSON_MsgAckEngage,
581
- l_ as l
804
+ l_ as l,
805
+ // -----
806
+ StateAckEngage,
807
+ StateEngage,
808
+ StateAckWarp,
809
+ StateWarp,
810
+ StateINVALID,
811
+ StateInitialState
582
812
  };
813
+