@rootintf/protocol-2phw 3.0.13-api.2 → 3.1.2-api.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -35
- package/dist/twoPhW.cjs.js +233 -517
- package/dist/twoPhW.d.ts +62 -43
- package/dist/twoPhW.es.mjs +233 -511
- package/package.json +5 -6
- package/src/twoPhW.ts +283 -188
- package/dist/generated/iface-TwoPhW/index.d.mts +0 -25
- package/dist/generated/iface-TwoPhW/validate.d.cts +0 -76
- package/dist/generated/iface-TwoPhW/validate.d.mts +0 -75
- package/schema/schema_MsgOptions.json +0 -12
- package/schema/schema_Options_delay.json +0 -17
- package/src/generated/.gitattributes +0 -4
- package/src/generated/iface-TwoPhW/index.cjs +0 -26
- package/src/generated/iface-TwoPhW/index.d.cts +0 -25
- package/src/generated/iface-TwoPhW/index.d.mts +0 -25
- package/src/generated/iface-TwoPhW/index.d.ts +0 -25
- package/src/generated/iface-TwoPhW/index.mjs +0 -23
- package/src/generated/iface-TwoPhW/package.json +0 -36
- package/src/generated/iface-TwoPhW/schema/schema_TwoPhW.json +0 -131
- package/src/generated/iface-TwoPhW/validate.cjs +0 -1
- package/src/generated/iface-TwoPhW/validate.mjs +0 -1
- package/src/generated/iface-introspection/index.cjs +0 -13
- package/src/generated/iface-introspection/index.d.cts +0 -6
- package/src/generated/iface-introspection/index.d.mts +0 -6
- package/src/generated/iface-introspection/index.d.ts +0 -6
- package/src/generated/iface-introspection/index.mjs +0 -10
- package/src/generated/iface-introspection/package.json +0 -30
- package/src/schema-cache/.gitattributes +0 -4
- package/src/schema-cache/5fdbd096ad99962b7e3d77cde8bd355fe180c859f35a61cfd07ee36b96c6700a.json +0 -131
package/src/twoPhW.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
|
|
2
|
-
// The wire interface (validators, constructors, VERSION) is GENERATED by iface-gen from
|
|
3
|
-
//
|
|
4
|
-
|
|
2
|
+
// The wire interface (validators, constructors, VERSION) is GENERATED by iface-gen from schema_TwoPhW.json
|
|
3
|
+
// -- in the HOST's project, never here. This package has no interface and no schema of its own: the host
|
|
4
|
+
// hands its generated interface to protocol_TwoPhW() (at the bottom of this file), which is what binds
|
|
5
|
+
// the two. So the package and the host cannot disagree about the wire, the version included.
|
|
5
6
|
|
|
6
7
|
// import { v4 as uuidv4 } from 'uuid'; // uuid is already a dep of JSON_Msg, so why not
|
|
7
8
|
import { LOGR, lRef, l_merge, l_array, l_concat, l_LL } from '@knev/bitlogr';
|
|
8
|
-
import { JSON_Msg, JSON_Eff, JSON_Ack, logr as logr_json_msg_ } from '@rootintf/json-msg'
|
|
9
|
+
import { JSON_Msg, JSON_Eff, JSON_Ack, JSON_Hold, logr as logr_json_msg_ } from '@rootintf/json-msg'
|
|
9
10
|
|
|
10
11
|
import { EventLog, logr as logr_evtlog_ } from '@rootintf/eventlog'
|
|
11
12
|
|
|
@@ -51,11 +52,13 @@ class Config {
|
|
|
51
52
|
public _callback_effEngage : t_callback | null = null;
|
|
52
53
|
public _callback_ackWarp : t_callback | null = null;
|
|
53
54
|
public _callback_ackEngage : t_callback | null = null;
|
|
55
|
+
// Optional, and not what makes a hold ours (that is the sender role, _callback_ackWarp): a hold
|
|
56
|
+
// moves the Warp's expiry whether or not anybody wants to be told about it.
|
|
57
|
+
public _callback_holdWarp : t_callback | null = null;
|
|
54
58
|
// "a step I sent was never answered inside its window", one per role: the sender's fires for its
|
|
55
59
|
// Warp / Engage, the receiver's for its AckWarp. A participant playing both roles keeps both.
|
|
56
60
|
public _callback_sender_sent_expired : t_callback_void
|
|
57
61
|
public _callback_receiver_sent_expired : t_callback_void
|
|
58
|
-
public _callback_msgOptions: () => void;
|
|
59
62
|
|
|
60
63
|
constructor() {
|
|
61
64
|
this._callback_sender_sent_expired = (_msg : t_msg, _json_msg) => {
|
|
@@ -64,9 +67,6 @@ class Config {
|
|
|
64
67
|
this._callback_receiver_sent_expired = (_msg : t_msg, _json_msg) => {
|
|
65
68
|
console.assert(false, '_callback_receiver_sent_expired uninitialized');
|
|
66
69
|
};
|
|
67
|
-
this._callback_msgOptions = () => {
|
|
68
|
-
console.assert(false, 'callback_msgOptions uninitialized');
|
|
69
|
-
};
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
receiver(callback_msgWarp : t_callback, callback_effEngage : t_callback, callback_msg_sent_expired : t_callback_void) {
|
|
@@ -74,14 +74,12 @@ class Config {
|
|
|
74
74
|
this._callback_effEngage= callback_effEngage;
|
|
75
75
|
this._callback_receiver_sent_expired= callback_msg_sent_expired;
|
|
76
76
|
}
|
|
77
|
-
|
|
77
|
+
// callback_holdWarp: the receiver says it is working on our Warp (what it waits for, how far it is)
|
|
78
|
+
sender(callback_ackWarp : t_callback, callback_ackEngage : t_callback, callback_msg_sent_expired : t_callback_void, callback_holdWarp : t_callback | null = null) {
|
|
78
79
|
this._callback_ackWarp= callback_ackWarp;
|
|
79
80
|
this._callback_ackEngage= callback_ackEngage;
|
|
80
81
|
this._callback_sender_sent_expired= callback_msg_sent_expired;
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
set callback_msgOptions(callback) {
|
|
84
|
-
this._callback_msgOptions= callback;
|
|
82
|
+
this._callback_holdWarp= callback_holdWarp;
|
|
85
83
|
}
|
|
86
84
|
}
|
|
87
85
|
|
|
@@ -101,107 +99,33 @@ class Config {
|
|
|
101
99
|
|
|
102
100
|
//-------------------------------------------------------------------------------------------------
|
|
103
101
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
//
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
return
|
|
102
|
+
// Timing. Every step is timed from the message it answers, by that message's own dt; no step looks further
|
|
103
|
+
// back than the message before it. Two durations:
|
|
104
|
+
// - TTL (warp.ttl_msec, set by the sender): the longest a message may take to travel ONE way. Network only.
|
|
105
|
+
// - the lease (hold.ttl_msec of a JSON_HoldWarp, set by the receiver): "my next word comes within this long".
|
|
106
|
+
// Work only.
|
|
107
|
+
// A message that hands the turn over (Warp, AckWarp, Engage) is answered within its dt + TTL*2, there and
|
|
108
|
+
// back. After a hold the next word is due within the hold's dt + its lease + TTL. Of several holds the one
|
|
109
|
+
// that ends latest counts; nothing is added up. The exchange as a whole has no limit: it stays open for as
|
|
110
|
+
// long as leases are renewed.
|
|
111
|
+
|
|
112
|
+
// the window in which a message that hands the turn over is answered: TTL there, TTL back
|
|
113
|
+
function window_msec_(nr_ttl_msec: number): number {
|
|
114
|
+
return nr_ttl_msec *2;
|
|
117
115
|
}
|
|
118
116
|
|
|
119
|
-
//
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
// if (! schema_MsgOptions_(json)) {
|
|
132
|
-
// logr_.log(l_.VALIDATION, () => ['JSON_MsgOptions: schema_MsgOptions_: ', (schema_MsgOptions_ as any).errors])
|
|
133
|
-
// return false;
|
|
134
|
-
// }
|
|
135
|
-
return true;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
})();
|
|
139
|
-
|
|
140
|
-
// Each JSON_* below is the generated namespace (validate + merge / create_with_cause_merge; the
|
|
141
|
-
// validators are delta-only: the base is validated upstream, in the responder chain) extended with
|
|
142
|
-
// what the schema cannot express: the protocol's own constructors.
|
|
143
|
-
|
|
144
|
-
var JSON_MsgWarp = (function() {
|
|
145
|
-
return {
|
|
146
|
-
...iface_TwoPhW_.JSON_MsgWarp,
|
|
147
|
-
create_with_id_user_hyperport : function(json_warp_id, json_user, json_hyperport, nr_ttl_msec) {
|
|
148
|
-
return iface_TwoPhW_.JSON_MsgWarp.merge({
|
|
149
|
-
id : json_warp_id,
|
|
150
|
-
user : json_user,
|
|
151
|
-
hyperport : json_hyperport,
|
|
152
|
-
ttl_msec : nr_ttl_msec
|
|
153
|
-
});
|
|
154
|
-
},
|
|
155
|
-
compare : function(json_L, json_R) {
|
|
156
|
-
if (json_L.dt !== json_R.dt)
|
|
157
|
-
return false;
|
|
158
|
-
if (json_L.warp.id !== json_R.warp.id)
|
|
159
|
-
return false;
|
|
160
|
-
if (json_L.referer[0] !== json_R.referer[0])
|
|
161
|
-
return false;
|
|
162
|
-
if (json_L.referer[1] !== json_R.referer[1])
|
|
163
|
-
return false;
|
|
164
|
-
return true;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
})();
|
|
168
|
-
|
|
169
|
-
var JSON_EffEngage = (function() {
|
|
170
|
-
return {
|
|
171
|
-
...iface_TwoPhW_.JSON_EffEngage,
|
|
172
|
-
create_with_Warp : function(json_msgWarp) {
|
|
173
|
-
console.assert(json_msgWarp.ack === undefined, "Engage with a Warp, not an Ack");
|
|
174
|
-
return iface_TwoPhW_.JSON_EffEngage.create_with_cause_merge(json_msgWarp, {
|
|
175
|
-
"ok" : true,
|
|
176
|
-
"rtt_msec" : Date.now() - Date.parse(json_msgWarp.dt)
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
})();
|
|
181
|
-
|
|
182
|
-
// iface-gen emits no constructor for a JSON_Ack: build the base ack, stamp the protocol marker
|
|
183
|
-
|
|
184
|
-
var JSON_AckWarp = (function() {
|
|
185
|
-
return {
|
|
186
|
-
...iface_TwoPhW_.JSON_AckWarp,
|
|
187
|
-
create_with_Warp_Ok : function(json_msgWarp, b_ok= true) {
|
|
188
|
-
const json_ack= JSON_Ack.create_with_cause_Ok(json_msgWarp, b_ok);
|
|
189
|
-
json_ack["2PhW"]= k_VERSION;
|
|
190
|
-
return json_ack;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
})();
|
|
194
|
-
|
|
195
|
-
var JSON_AckEngage = (function() {
|
|
196
|
-
return {
|
|
197
|
-
...iface_TwoPhW_.JSON_AckEngage,
|
|
198
|
-
create_with_Engage_Ok : function(json_effEngage, b_ok= true) {
|
|
199
|
-
const json_ack= JSON_Ack.create_with_cause_Ok(json_effEngage, b_ok);
|
|
200
|
-
json_ack["2PhW"]= k_VERSION;
|
|
201
|
-
return json_ack;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
})();
|
|
117
|
+
// Are these the same Warp? The identity of an exchange: the Warp's own dt, its warp.id and its sender.
|
|
118
|
+
function compare_msgWarp_(json_L, json_R) : boolean {
|
|
119
|
+
if (json_L.dt !== json_R.dt)
|
|
120
|
+
return false;
|
|
121
|
+
if (json_L.warp.id !== json_R.warp.id)
|
|
122
|
+
return false;
|
|
123
|
+
if (json_L.referer[0] !== json_R.referer[0])
|
|
124
|
+
return false;
|
|
125
|
+
if (json_L.referer[1] !== json_R.referer[1])
|
|
126
|
+
return false;
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
205
129
|
|
|
206
130
|
//-------------------------------------------------------------------------------------------------
|
|
207
131
|
// States
|
|
@@ -259,11 +183,10 @@ class StateEngage extends StateState {
|
|
|
259
183
|
if (false === state_new instanceof StateAckEngage)
|
|
260
184
|
return false;
|
|
261
185
|
|
|
262
|
-
// The
|
|
263
|
-
const
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (state_new.evt_AckEngage.dt >= Date.parse(json_msgWarp.dt) + timeout_ms)
|
|
186
|
+
// The AckEngage is accepted until THE ENGAGE'S creation time + TTL*2
|
|
187
|
+
const timeout_ms= window_msec_(this.json_effEngage._cause.warp.ttl_msec);
|
|
188
|
+
logr_.log(l_.PROTOCOL_STATE, () => [`StateEngage: evt_AckEngage.dt@${state_new.evt_AckEngage.dt}, effEngage.dt@${Date.parse(this.json_effEngage.dt)} + ${timeout_ms}`])
|
|
189
|
+
if (state_new.evt_AckEngage.dt >= Date.parse(this.json_effEngage.dt) + timeout_ms)
|
|
267
190
|
return false;
|
|
268
191
|
|
|
269
192
|
return true;
|
|
@@ -287,16 +210,9 @@ class StateAckWarp extends StateState {
|
|
|
287
210
|
if (! isEngageAwaited_(this.json_ackWarp))
|
|
288
211
|
return false;
|
|
289
212
|
|
|
290
|
-
//
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
// The Ack gets to live for THE ACK'S creation time + TTL*2 + STT
|
|
295
|
-
// +STT, so lines up with the Engage timeout
|
|
296
|
-
// - Engage: T0 +2TTL +RTT; RTT= STT+STT
|
|
297
|
-
// - AckWarp: TAckWarp +2TLL +STT; TAckWarp= T0 +STT;
|
|
298
|
-
// - STT: Date of the Ack minus date of the Warp.
|
|
299
|
-
const timeout_ms= time_ttl_rtt_(this.json_ackWarp._cause.warp.ttl_msec) + Date.parse(this.json_ackWarp.dt) - Date.parse(this.json_ackWarp._cause.dt);
|
|
213
|
+
// The Engage is accepted until THE ACK'S creation time + TTL*2. Nothing of phase 1 enters it: how
|
|
214
|
+
// long the receiver took to acknowledge the Warp is not this phase's business.
|
|
215
|
+
const timeout_ms= window_msec_(this.json_ackWarp._cause.warp.ttl_msec);
|
|
300
216
|
logr_.log(l_.PROTOCOL_STATE, () => [`StateAckWarp: evt_Engage.dt@${state_new.evt_Engage.dt}, msgAckWarp.dt@${Date.parse(this.json_ackWarp.dt)} + ${timeout_ms}`])
|
|
301
217
|
if (state_new.evt_Engage.dt >= Date.parse(this.json_ackWarp.dt) + timeout_ms)
|
|
302
218
|
return false;
|
|
@@ -306,17 +222,52 @@ class StateAckWarp extends StateState {
|
|
|
306
222
|
}
|
|
307
223
|
|
|
308
224
|
class StateWarp extends StateState {
|
|
225
|
+
// where the latest lease of a JSON_HoldWarp moved the end of the window to (epoch msec), and the event
|
|
226
|
+
// whose timer now guards it. null: no hold moved it.
|
|
227
|
+
private _nr_deadline_lease : number | null = null;
|
|
228
|
+
private _idx_event_timer : number | null = null;
|
|
229
|
+
|
|
309
230
|
constructor(idx_event : number, event : EventMinimal) { super(idx_event, event); }
|
|
310
231
|
get evt_Warp() { return this.event }
|
|
311
232
|
get json_msgWarp() { return this.json_msg }
|
|
233
|
+
|
|
234
|
+
// Where a JSON_HoldWarp puts the end of the window: its OWN dt + its lease (hold.ttl_msec) + the
|
|
235
|
+
// warp's TTL (one for the next message to travel). NaN when its dt does not read: a lease with no anchor.
|
|
236
|
+
static deadline_holdWarp(json_holdWarp) : number {
|
|
237
|
+
const nr_lease= JSON_Hold.lease_epoch_msec(json_holdWarp);
|
|
238
|
+
return (nr_lease === null) ? NaN : nr_lease + json_holdWarp._cause.warp.ttl_msec;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// The last instant the JSON_AckWarp is accepted: THE WARP'S creation time + TTL*2, or where a hold
|
|
242
|
+
// moved it, whichever is later. The latest counts; nothing is added up.
|
|
243
|
+
get deadline_msec() : number {
|
|
244
|
+
const nr_deadline= Date.parse(this.json_msgWarp.dt) + window_msec_(this.json_msgWarp.warp.ttl_msec);
|
|
245
|
+
return (this._nr_deadline_lease !== null && this._nr_deadline_lease > nr_deadline) ? this._nr_deadline_lease : nr_deadline;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// the event that holds this Warp's expiry timer: the Warp's own, or the hold that last moved the window
|
|
249
|
+
get idx_event_timer() : number {
|
|
250
|
+
return (this._idx_event_timer !== null) ? this._idx_event_timer : this.idx_event;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// A hold on this Warp, logged as event idx_event. Returns whether it moved the end of the window:
|
|
254
|
+
// not when its lease ends no later than the window in force, nor when its dt does not read.
|
|
255
|
+
lease(idx_event : number, json_holdWarp) : boolean {
|
|
256
|
+
const nr_deadline= StateWarp.deadline_holdWarp(json_holdWarp);
|
|
257
|
+
if (! (nr_deadline > this.deadline_msec))
|
|
258
|
+
return false;
|
|
259
|
+
|
|
260
|
+
this._nr_deadline_lease= nr_deadline;
|
|
261
|
+
this._idx_event_timer= idx_event;
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
|
|
312
265
|
canChangeState(state_new : StateState) {
|
|
313
266
|
if (false === state_new instanceof StateAckWarp)
|
|
314
267
|
return false;
|
|
315
268
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
logr_.log(l_.PROTOCOL_STATE, () => [`StateWarp: evt_AckWarp.dt@${state_new.evt_AckWarp.dt}, msgWarp.dt@${Date.parse(this.json_msgWarp.dt)} + ${timeout_ms}`])
|
|
319
|
-
if (state_new.evt_AckWarp.dt >= Date.parse(this.json_msgWarp.dt) + timeout_ms)
|
|
269
|
+
logr_.log(l_.PROTOCOL_STATE, () => [`StateWarp: evt_AckWarp.dt@${state_new.evt_AckWarp.dt}, deadline@${this.deadline_msec}`])
|
|
270
|
+
if (state_new.evt_AckWarp.dt >= this.deadline_msec)
|
|
320
271
|
return false;
|
|
321
272
|
|
|
322
273
|
return true;
|
|
@@ -370,6 +321,13 @@ class ProtocolStateState {
|
|
|
370
321
|
}
|
|
371
322
|
|
|
372
323
|
canChangeState(state_new : StateState) : boolean {
|
|
324
|
+
// Every window is measured from a message's own dt, and the schema only asks dt to be a string.
|
|
325
|
+
// A step whose dt does not read as a time has no window to be inside of: never legal. (Unchecked,
|
|
326
|
+
// it parses to NaN, every "is it past the deadline?" answers no, and it escapes all the windows.)
|
|
327
|
+
// Every logged step passed here, so the states can rely on the dt of the messages they wrap.
|
|
328
|
+
if (! Number.isFinite(Date.parse(state_new.json_msg.dt)))
|
|
329
|
+
return false;
|
|
330
|
+
|
|
373
331
|
// An exchange is a dialogue: a step we RECEIVE is only legal after a step we SENT, and vice
|
|
374
332
|
// versa. Every participant on the ME hears every exchange, so without this a warp we merely
|
|
375
333
|
// heard would be "answered" by the ack its real receiver sent, and we could answer our own step.
|
|
@@ -383,6 +341,7 @@ class ProtocolStateState {
|
|
|
383
341
|
//-------------------------------------------------------------------------------------------------
|
|
384
342
|
|
|
385
343
|
const kstr_EVT_DROPPED_= '2PhW[DROPPED!]';
|
|
344
|
+
const kstr_EVT_HOLDWARP_= '2PhW[JSON_HoldWarp]';
|
|
386
345
|
|
|
387
346
|
class TwoPhW_EventLog {
|
|
388
347
|
private _eventLog : EventLog;
|
|
@@ -393,11 +352,12 @@ class TwoPhW_EventLog {
|
|
|
393
352
|
|
|
394
353
|
_warp(json_msg) {
|
|
395
354
|
const b_Ack = json_msg.ack !== undefined;
|
|
396
|
-
const
|
|
355
|
+
const b_Hold = ! b_Ack && json_msg.hold !== undefined; // a message that carries both is an ack
|
|
356
|
+
const json = (b_Ack || b_Hold) ? json_msg["_cause"] : json_msg;
|
|
397
357
|
|
|
398
358
|
if (json.warp) {
|
|
399
359
|
return {
|
|
400
|
-
str_type: b_Ack ? '2PhW[JSON_AckWarp]' : '2PhW[JSON_MsgWarp]',
|
|
360
|
+
str_type: b_Ack ? '2PhW[JSON_AckWarp]' : (b_Hold ? kstr_EVT_HOLDWARP_ : '2PhW[JSON_MsgWarp]'),
|
|
401
361
|
json_msgWarp: json
|
|
402
362
|
};
|
|
403
363
|
}
|
|
@@ -434,11 +394,14 @@ class TwoPhW_EventLog {
|
|
|
434
394
|
traverse_State(json_msgWarp) {
|
|
435
395
|
const protocolState = new ProtocolStateState();
|
|
436
396
|
|
|
437
|
-
// Look back over the WHOLE exchange
|
|
438
|
-
//
|
|
439
|
-
//
|
|
440
|
-
|
|
397
|
+
// Look back over the WHOLE exchange: to the Warp's own dt (+ TTL*2: a participant logs by its own
|
|
398
|
+
// clock). An exchange has no limit as a whole (holds keep it open for as long as they are renewed), so
|
|
399
|
+
// its age says nothing; whether it is still open is decided by its windows. A Warp whose dt does not
|
|
400
|
+
// read has no exchange to look back over, and is refused by every caller.
|
|
401
|
+
const nr_age_msec= Date.now() - Date.parse(json_msgWarp.dt);
|
|
402
|
+
const lookback_ms= window_msec_(json_msgWarp.warp.ttl_msec) + (Number.isFinite(nr_age_msec) ? Math.max(nr_age_msec, 0) : 0);
|
|
441
403
|
const arr_evts= this._eventLog.get_Events_by_id(json_msgWarp.warp.id, lookback_ms);
|
|
404
|
+
|
|
442
405
|
logr_.log(l_.EVENTS, () => ['traverse_State: arr_evts: ', arr_evts]);
|
|
443
406
|
|
|
444
407
|
for (let i = arr_evts.length - 1; i >= 0; i--) {
|
|
@@ -450,13 +413,18 @@ class TwoPhW_EventLog {
|
|
|
450
413
|
continue;
|
|
451
414
|
|
|
452
415
|
const json_msg= evt.data.json_msg;
|
|
453
|
-
if (!
|
|
416
|
+
if (! compare_msgWarp_(json_msgWarp, this._warp(json_msg).json_msgWarp) )
|
|
454
417
|
protocolState.state.changeState( new StateINVALID(idx, evt) );
|
|
455
418
|
|
|
456
419
|
switch(evt.type) {
|
|
457
420
|
case '2PhW[JSON_MsgWarp]' :
|
|
458
421
|
protocolState.state.changeState( new StateWarp(idx, evt) );
|
|
459
422
|
break;
|
|
423
|
+
case kstr_EVT_HOLDWARP_ :
|
|
424
|
+
// not a step: it changes no state, it moves the window of the Warp it is about
|
|
425
|
+
if (protocolState.state instanceof StateWarp)
|
|
426
|
+
protocolState.state.lease(idx, json_msg);
|
|
427
|
+
break;
|
|
460
428
|
case '2PhW[JSON_AckWarp]' :
|
|
461
429
|
protocolState.state.changeState( new StateAckWarp(idx, evt) );
|
|
462
430
|
break;
|
|
@@ -498,42 +466,6 @@ class TwoPhW {
|
|
|
498
466
|
return this._2PhW_eventLog;
|
|
499
467
|
}
|
|
500
468
|
|
|
501
|
-
//-------------------------------------------------------------------------------------------------
|
|
502
|
-
|
|
503
|
-
_handler_msgOptions(cfg, msg, json_msgOptions) : boolean
|
|
504
|
-
{
|
|
505
|
-
logr_.log(l_.HANDLERS, () => ['handler_msgOptions_: ...']);
|
|
506
|
-
|
|
507
|
-
/*
|
|
508
|
-
if (! validate_Options_delay_(json_msgOptions.options))
|
|
509
|
-
return false;
|
|
510
|
-
|
|
511
|
-
if (JSON_MsgWarp.validate(json_msgOptions._cause)) {
|
|
512
|
-
const json_msgWarp= json_msgOptions._cause;
|
|
513
|
-
|
|
514
|
-
const ctx= get_ctx(json_msgWarp.id);
|
|
515
|
-
if (!ctx )
|
|
516
|
-
return false;
|
|
517
|
-
|
|
518
|
-
const ttl_msec= json_msgOptions.options.ttl_msec_incr[0];
|
|
519
|
-
const ttl_incr= json_msgOptions.options.ttl_msec_incr[1];
|
|
520
|
-
|
|
521
|
-
//TODO: double check ttl_msec is the same as current accummulated ttl for idempotence.
|
|
522
|
-
|
|
523
|
-
logr_.log(l_.HANDLERS, () => ['handler_msgOptions_: ', json_msgOptions]);
|
|
524
|
-
|
|
525
|
-
ctx.protocol.delayTimeout(ttl_incr);
|
|
526
|
-
|
|
527
|
-
if (cfg._callback_msgOptions)
|
|
528
|
-
return cfg._callback_msgOptions(msg, json_msgOptions);
|
|
529
|
-
|
|
530
|
-
return true;
|
|
531
|
-
}
|
|
532
|
-
*/
|
|
533
|
-
|
|
534
|
-
return false;
|
|
535
|
-
}
|
|
536
|
-
|
|
537
469
|
//-------------------------------------------------------------------------------------------------
|
|
538
470
|
// The typed handlers (handler_json_<message>) are what the responder's base dispatch calls AFTER it
|
|
539
471
|
// validated the message as that type (generated: `JSON_AckWarp.validate(json_ack) &&
|
|
@@ -556,7 +488,8 @@ class TwoPhW {
|
|
|
556
488
|
if (! b_changeState)
|
|
557
489
|
return false;
|
|
558
490
|
|
|
559
|
-
|
|
491
|
+
// the first answer, a hold or the ack, is due within the Warp's dt + TTL*2
|
|
492
|
+
const timeout_ms= window_msec_(json_msgWarp.warp.ttl_msec);
|
|
560
493
|
const elapsed_ms= Date.now() - Date.parse(json_msgWarp.dt);
|
|
561
494
|
|
|
562
495
|
this.eventLog._addEvent(msg, json_msgWarp, true, timeout_ms-elapsed_ms, () => { // pass { _idx, _evt}?
|
|
@@ -585,11 +518,54 @@ class TwoPhW {
|
|
|
585
518
|
if (! b_changeState)
|
|
586
519
|
return false;
|
|
587
520
|
|
|
588
|
-
|
|
521
|
+
// the Warp's own timer, or the one of the hold that last moved its window
|
|
522
|
+
this.eventLog._cancelTimeout((protocolState.state as StateWarp).idx_event_timer);
|
|
589
523
|
this.eventLog._addEvent(msg, json_ackWarp, false);
|
|
590
524
|
return this.config._callback_ackWarp(msg, json_ackWarp);
|
|
591
525
|
}
|
|
592
526
|
|
|
527
|
+
// The receiver says it is working on a Warp we sent and has not decided yet (JSON_HoldWarp). Not a step:
|
|
528
|
+
// the exchange stays where it is, waiting for the JSON_AckWarp. What changes is how long we wait for it:
|
|
529
|
+
// the expiry of the Warp moves to the end of the hold's lease.
|
|
530
|
+
handler_json_holdWarp(msg : t_msg, json_holdWarp) : boolean
|
|
531
|
+
{
|
|
532
|
+
logr_.log(l_.HANDLERS | l_.SENDER, () => ['handler_json_holdWarp: ', json_holdWarp]);
|
|
533
|
+
|
|
534
|
+
// the sender role is not played here: not ours -- before anything is logged
|
|
535
|
+
if (! this.config._callback_ackWarp)
|
|
536
|
+
return false;
|
|
537
|
+
|
|
538
|
+
const json_msgWarp= json_holdWarp._cause;
|
|
539
|
+
|
|
540
|
+
// ours iff it is about a Warp WE sent that is still unanswered and still inside its window. Anything
|
|
541
|
+
// else (a Warp we only heard, one already acknowledged or expired) is somebody else's, or too late.
|
|
542
|
+
const state= this.eventLog.traverse_State(json_msgWarp).state;
|
|
543
|
+
if (! (state instanceof StateWarp) || ! state.event.data.b_sent)
|
|
544
|
+
return false;
|
|
545
|
+
|
|
546
|
+
const nr_now= Date.now();
|
|
547
|
+
if (nr_now >= state.deadline_msec)
|
|
548
|
+
return false;
|
|
549
|
+
|
|
550
|
+
const nr_deadline= StateWarp.deadline_holdWarp(json_holdWarp);
|
|
551
|
+
if (! Number.isFinite(nr_deadline))
|
|
552
|
+
return false; // a dt that does not read: a lease with no anchor
|
|
553
|
+
|
|
554
|
+
if (nr_deadline > state.deadline_msec) {
|
|
555
|
+
// the timer follows the window: the one in force is cancelled, this hold carries the new one.
|
|
556
|
+
// What expires is still the Warp: "the step I sent was never answered".
|
|
557
|
+
const { msg: msg_Warp, json_msg: json_msgWarp_sent }= state.event.data;
|
|
558
|
+
this.eventLog._cancelTimeout(state.idx_event_timer);
|
|
559
|
+
this.eventLog._addEvent(msg, json_holdWarp, false, nr_deadline - nr_now, () => {
|
|
560
|
+
this.config._callback_sender_sent_expired(msg_Warp, json_msgWarp_sent);
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
else
|
|
564
|
+
this.eventLog._addEvent(msg, json_holdWarp, false); // ends inside the window in force: logged, nothing moves
|
|
565
|
+
|
|
566
|
+
return this.config._callback_holdWarp ? this.config._callback_holdWarp(msg, json_holdWarp) : true;
|
|
567
|
+
}
|
|
568
|
+
|
|
593
569
|
register_effEngage(msg : t_msg, json_effEngage) : boolean
|
|
594
570
|
{
|
|
595
571
|
logr_.log(l_.SENDER, () => ['register_effEngage: ', json_effEngage]);
|
|
@@ -606,8 +582,9 @@ class TwoPhW {
|
|
|
606
582
|
if (! b_changeState)
|
|
607
583
|
return false;
|
|
608
584
|
|
|
609
|
-
|
|
610
|
-
const
|
|
585
|
+
// the AckEngage is due within the Engage's own dt + TTL*2
|
|
586
|
+
const timeout_ms= window_msec_(json_msgWarp.warp.ttl_msec);
|
|
587
|
+
const elapsed_ms= Date.now() - Date.parse(json_effEngage.dt);
|
|
611
588
|
|
|
612
589
|
this.eventLog._addEvent(msg, json_effEngage, true, timeout_ms-elapsed_ms, () => {
|
|
613
590
|
this.config._callback_sender_sent_expired(msg, json_effEngage);
|
|
@@ -665,6 +642,30 @@ class TwoPhW {
|
|
|
665
642
|
return this.config._callback_msgWarp(msg, json_msgWarp)
|
|
666
643
|
}
|
|
667
644
|
|
|
645
|
+
// Register a JSON_HoldWarp we are about to publish: "this Warp is being worked on, wait for the ack until
|
|
646
|
+
// my lease ends". Legal only for a Warp we RECEIVED, have not answered, and whose window is still open
|
|
647
|
+
// (a window that has closed cannot be reopened). It is what lets register_ackWarp succeed later than the
|
|
648
|
+
// Warp's own dt + TTL*2. Returns true, or false when it is not legal now: then do not publish it.
|
|
649
|
+
register_holdWarp(msg : t_msg, json_holdWarp) : boolean
|
|
650
|
+
{
|
|
651
|
+
logr_.log(l_.RECVER, () => ['register_holdWarp: ', json_holdWarp]);
|
|
652
|
+
|
|
653
|
+
const json_msgWarp= json_holdWarp._cause;
|
|
654
|
+
|
|
655
|
+
const state= this.eventLog.traverse_State(json_msgWarp).state;
|
|
656
|
+
if (! (state instanceof StateWarp) || state.event.data.b_sent)
|
|
657
|
+
return false;
|
|
658
|
+
|
|
659
|
+
if (Date.now() >= state.deadline_msec)
|
|
660
|
+
return false;
|
|
661
|
+
|
|
662
|
+
if (! Number.isFinite(StateWarp.deadline_holdWarp(json_holdWarp)))
|
|
663
|
+
return false;
|
|
664
|
+
|
|
665
|
+
this.eventLog._addEvent(msg, json_holdWarp, true);
|
|
666
|
+
return true;
|
|
667
|
+
}
|
|
668
|
+
|
|
668
669
|
register_ackWarp(msg: t_msg, json_ackWarp) : boolean
|
|
669
670
|
{
|
|
670
671
|
logr_.log(l_.RECVER, () => ['register_ackWarp: ', json_ackWarp]);
|
|
@@ -688,8 +689,9 @@ class TwoPhW {
|
|
|
688
689
|
return true;
|
|
689
690
|
}
|
|
690
691
|
|
|
692
|
+
// the Engage is due within the ack's own dt + TTL*2
|
|
691
693
|
const elapsed_ms= Date.now() - Date.parse(json_ackWarp.dt);
|
|
692
|
-
const timeout_ms=
|
|
694
|
+
const timeout_ms= window_msec_(json_msgWarp.warp.ttl_msec);
|
|
693
695
|
|
|
694
696
|
this.eventLog._addEvent(msg, json_ackWarp, true, timeout_ms-elapsed_ms, () => { // pass { _idx, _evt} ?
|
|
695
697
|
this.config._callback_receiver_sent_expired(msg, json_ackWarp);
|
|
@@ -744,21 +746,114 @@ class TwoPhW {
|
|
|
744
746
|
|
|
745
747
|
};
|
|
746
748
|
|
|
749
|
+
|
|
750
|
+
//-------------------------------------------------------------------------------------------------
|
|
751
|
+
// The seam between this package and the interface
|
|
752
|
+
|
|
753
|
+
// What the host's generated 2PhW interface must offer (iface-gen, from schema_TwoPhW.json): the version
|
|
754
|
+
// and, per message, its validator and its constructor. Structural: this package names no generated type.
|
|
755
|
+
interface Iface_TwoPhW {
|
|
756
|
+
VERSION : string;
|
|
757
|
+
JSON_MsgWarp : { validate(msg) : boolean; merge(body) };
|
|
758
|
+
JSON_AckWarp : { validate(msg) : boolean };
|
|
759
|
+
JSON_HoldWarp : { validate(msg) : boolean; create_with_cause_ttl(cause, ttl_msec : number, hold?) };
|
|
760
|
+
JSON_EffEngage : { validate(msg) : boolean; create_with_cause_merge(cause, body) };
|
|
761
|
+
JSON_AckEngage : { validate(msg) : boolean };
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// Bind the protocol to the HOST's generated interface. Called once, where the responder is set up:
|
|
765
|
+
//
|
|
766
|
+
// import * as iface_TwoPhW from '../generated/iface-TwoPhW';
|
|
767
|
+
// const { TwoPhW, JSON_MsgWarp, JSON_EffEngage, JSON_AckWarp, JSON_HoldWarp, JSON_AckEngage } = protocol_TwoPhW(iface_TwoPhW);
|
|
768
|
+
//
|
|
769
|
+
// Returns the state machine and, per message, the generated namespace (validate + merge /
|
|
770
|
+
// create_with_cause_merge; the validators are delta-only: the base is validated upstream, in the
|
|
771
|
+
// responder chain) extended with what the schema cannot express: the protocol's own constructors.
|
|
772
|
+
// Whatever is built through them carries the interface's own VERSION, so the package cannot speak
|
|
773
|
+
// another version of 2PhW than its host validates.
|
|
774
|
+
function protocol_TwoPhW<T extends Iface_TwoPhW>(iface_TwoPhW : T) {
|
|
775
|
+
|
|
776
|
+
// What was handed in is the host's to get right: a wrong import (the module's default export, another
|
|
777
|
+
// interface) must fail here, where it is made, not as an undefined deep inside an exchange.
|
|
778
|
+
const arr_str_missing= [
|
|
779
|
+
[ 'VERSION', typeof iface_TwoPhW?.VERSION === 'string' ],
|
|
780
|
+
[ 'JSON_MsgWarp.validate', typeof iface_TwoPhW?.JSON_MsgWarp?.validate === 'function' ],
|
|
781
|
+
[ 'JSON_MsgWarp.merge', typeof iface_TwoPhW?.JSON_MsgWarp?.merge === 'function' ],
|
|
782
|
+
[ 'JSON_AckWarp.validate', typeof iface_TwoPhW?.JSON_AckWarp?.validate === 'function' ],
|
|
783
|
+
[ 'JSON_HoldWarp.validate', typeof iface_TwoPhW?.JSON_HoldWarp?.validate === 'function' ],
|
|
784
|
+
[ 'JSON_HoldWarp.create_with_cause_ttl', typeof iface_TwoPhW?.JSON_HoldWarp?.create_with_cause_ttl === 'function' ],
|
|
785
|
+
[ 'JSON_EffEngage.validate', typeof iface_TwoPhW?.JSON_EffEngage?.validate === 'function' ],
|
|
786
|
+
[ 'JSON_EffEngage.create_with_cause_merge', typeof iface_TwoPhW?.JSON_EffEngage?.create_with_cause_merge === 'function' ],
|
|
787
|
+
[ 'JSON_AckEngage.validate', typeof iface_TwoPhW?.JSON_AckEngage?.validate === 'function' ]
|
|
788
|
+
].filter(([ _str_member, b_present ]) => ! b_present).map(([ str_member ]) => str_member);
|
|
789
|
+
if (arr_str_missing.length)
|
|
790
|
+
throw new Error(`protocol_TwoPhW: not a generated 2PhW interface, it lacks: ${arr_str_missing.join(', ')}`);
|
|
791
|
+
|
|
792
|
+
const k_VERSION= iface_TwoPhW.VERSION;
|
|
793
|
+
|
|
794
|
+
const JSON_MsgWarp = {
|
|
795
|
+
...iface_TwoPhW.JSON_MsgWarp,
|
|
796
|
+
create_with_id_user_hyperport : function(json_warp_id, json_user, json_hyperport, nr_ttl_msec) {
|
|
797
|
+
return iface_TwoPhW.JSON_MsgWarp.merge({
|
|
798
|
+
id : json_warp_id,
|
|
799
|
+
user : json_user,
|
|
800
|
+
hyperport : json_hyperport,
|
|
801
|
+
ttl_msec : nr_ttl_msec
|
|
802
|
+
});
|
|
803
|
+
},
|
|
804
|
+
compare : compare_msgWarp_
|
|
805
|
+
};
|
|
806
|
+
|
|
807
|
+
const JSON_EffEngage = {
|
|
808
|
+
...iface_TwoPhW.JSON_EffEngage,
|
|
809
|
+
create_with_Warp : function(json_msgWarp) {
|
|
810
|
+
console.assert(json_msgWarp.ack === undefined, "Engage with a Warp, not an Ack");
|
|
811
|
+
return iface_TwoPhW.JSON_EffEngage.create_with_cause_merge(json_msgWarp, {
|
|
812
|
+
"ok" : true
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
|
|
817
|
+
// iface-gen emits no constructor for a JSON_Ack: build the base ack, stamp the protocol marker
|
|
818
|
+
|
|
819
|
+
const JSON_AckWarp = {
|
|
820
|
+
...iface_TwoPhW.JSON_AckWarp,
|
|
821
|
+
create_with_Warp_Ok : function(json_msgWarp, b_ok= true) {
|
|
822
|
+
const json_ack= JSON_Ack.create_with_cause_Ok(json_msgWarp, b_ok);
|
|
823
|
+
json_ack["2PhW"]= k_VERSION;
|
|
824
|
+
return json_ack;
|
|
825
|
+
}
|
|
826
|
+
};
|
|
827
|
+
|
|
828
|
+
const JSON_AckEngage = {
|
|
829
|
+
...iface_TwoPhW.JSON_AckEngage,
|
|
830
|
+
create_with_Engage_Ok : function(json_effEngage, b_ok= true) {
|
|
831
|
+
const json_ack= JSON_Ack.create_with_cause_Ok(json_effEngage, b_ok);
|
|
832
|
+
json_ack["2PhW"]= k_VERSION;
|
|
833
|
+
return json_ack;
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
|
|
837
|
+
return {
|
|
838
|
+
TwoPhW,
|
|
839
|
+
VERSION : k_VERSION,
|
|
840
|
+
JSON_MsgWarp,
|
|
841
|
+
JSON_EffEngage,
|
|
842
|
+
JSON_AckWarp,
|
|
843
|
+
JSON_HoldWarp : iface_TwoPhW.JSON_HoldWarp, // as generated: its constructor is built from the lease already
|
|
844
|
+
JSON_AckEngage
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
|
|
747
848
|
//-------------------------------------------------------------------------------------------------
|
|
748
849
|
|
|
749
850
|
export {
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
JSON_MsgOptions,
|
|
753
|
-
JSON_MsgWarp,
|
|
754
|
-
JSON_EffEngage,
|
|
755
|
-
JSON_AckWarp,
|
|
756
|
-
JSON_AckEngage,
|
|
851
|
+
protocol_TwoPhW,
|
|
852
|
+
window_msec_ as window_msec,
|
|
757
853
|
logr_ as logr,
|
|
758
854
|
JSON_Msg,
|
|
759
855
|
JSON_Eff,
|
|
760
856
|
JSON_Ack,
|
|
761
|
-
k_VERSION as VERSION,
|
|
762
857
|
// -----
|
|
763
858
|
StateAckEngage,
|
|
764
859
|
StateEngage,
|
|
@@ -767,4 +862,4 @@ export {
|
|
|
767
862
|
StateINVALID,
|
|
768
863
|
StateInitialState
|
|
769
864
|
};
|
|
770
|
-
|
|
865
|
+
export type { Iface_TwoPhW, TwoPhW };
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
// GENERATED by iface-gen from schema_TwoPhW.json — DO NOT EDIT
|
|
2
|
-
import type { JSON_Msg, JSON_Ack, JSON_Eff } from "@rootintf/json-msg";
|
|
3
|
-
export declare const VERSION: "2";
|
|
4
|
-
|
|
5
|
-
export type JSON_MsgWarp = JSON_Msg & { "warp": { "id": string; "ttl_msec": number; "instantaneous"?: boolean; "user": unknown; "hyperport": unknown }; "2PhW": "2" };
|
|
6
|
-
export declare const JSON_MsgWarp: {
|
|
7
|
-
validate(msg: JSON_Msg): msg is JSON_MsgWarp;
|
|
8
|
-
merge(body: { "id": string; "ttl_msec": number; "instantaneous"?: boolean; "user": unknown; "hyperport": unknown }): JSON_MsgWarp;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export type JSON_AckWarp = JSON_Ack & { "_cause"?: unknown; "ack": Record<string, unknown>; "2PhW": "2" };
|
|
12
|
-
export declare const JSON_AckWarp: {
|
|
13
|
-
validate(msg: JSON_Ack): msg is JSON_AckWarp;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export type JSON_EffEngage = JSON_Eff & { "_cause"?: unknown; "engage": { "ok": boolean; "rtt_msec": number }; "2PhW": "2" };
|
|
17
|
-
export declare const JSON_EffEngage: {
|
|
18
|
-
validate(msg: JSON_Eff): msg is JSON_EffEngage;
|
|
19
|
-
create_with_cause_merge(cause: JSON_Msg, body: { "ok": boolean; "rtt_msec": number }): JSON_EffEngage;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export type JSON_AckEngage = JSON_Ack & { "_cause"?: unknown; "ack": Record<string, unknown>; "2PhW": "2" };
|
|
23
|
-
export declare const JSON_AckEngage: {
|
|
24
|
-
validate(msg: JSON_Ack): msg is JSON_AckEngage;
|
|
25
|
-
};
|