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