@rootintf/protocol-2phw 0.7.1 → 1.0.28-api-0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dev.txt +7 -0
- package/dist/twoPhW.cjs.js +1225 -799
- package/dist/twoPhW.es.mjs +1218 -798
- package/jasmine.json +6 -6
- package/package.json +43 -42
- package/rollup.config.mjs +23 -23
- package/schema/definitions_2PhW.json +12 -0
- package/schema/schema_MsgEngage.json +20 -11
- package/schema/schema_MsgOptions.json +11 -10
- package/schema/schema_MsgWarp.json +21 -19
- package/schema/schema_Options_delay.json +16 -16
- package/src/EventLog-mem.mjs +136 -0
- package/src/ajv-gen-validate.cjs +39 -23
- package/src/twoPhW.mjs +813 -582
- package/src/validate-esm.mjs +1 -1
- package/tests/event-log-mem.spec.mjs +234 -0
- package/tests/twoPhW-new.spec.mjs +824 -173
- package/tests/twoPhW.test.mjs +175 -175
- package/schema/schema_Msg2PhW.json +0 -12
package/dist/twoPhW.cjs.js
CHANGED
|
@@ -1,172 +1,84 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const validate_Msg2PhW = validate10$1;const pattern0 = new RegExp("0\\.7", "u");function validate10$1(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data["2PhW"] === undefined) && (missing0 = "2PhW")){validate10$1.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data["2PhW"] !== undefined){let data0 = data["2PhW"];{if(typeof data0 === "string"){if(!pattern0.test(data0)){validate10$1.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"#/properties/2PhW/pattern",keyword:"pattern",params:{pattern: "0\\.7"},message:"must match pattern \""+"0\\.7"+"\""}];return false;}}else {validate10$1.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"#/properties/2PhW/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}}}}}else {validate10$1.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate10$1.errors = vErrors;return errors === 0;}const validate_MsgOptions = validate11$1;function validate11$1(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.options === undefined) && (missing0 = "options")){validate11$1.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.options !== undefined){let data0 = data.options;if(!(data0 && typeof data0 == "object" && !Array.isArray(data0))){validate11$1.errors = [{instancePath:instancePath+"/options",schemaPath:"#/properties/options/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}else {validate11$1.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate11$1.errors = vErrors;return errors === 0;}const validate_Options_delay = validate12$1;function validate12$1(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ttl_msec_incr === undefined) && (missing0 = "ttl_msec_incr")){validate12$1.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ttl_msec_incr !== undefined){let data0 = data.ttl_msec_incr;const _errs1 = errors;{if(Array.isArray(data0)){var valid1 = true;const len0 = data0.length;for(let i0=0; i0<len0; i0++){let data1 = data0[i0];const _errs3 = errors;if(!(((typeof data1 == "number") && (!(data1 % 1) && !isNaN(data1))) && (isFinite(data1)))){validate12$1.errors = [{instancePath:instancePath+"/ttl_msec_incr/" + i0,schemaPath:"#/properties/ttl_msec_incr/items/type",keyword:"type",params:{type: "integer"},message:"must be integer"}];return false;}var valid1 = _errs3 === errors;if(!valid1){break;}}}else {validate12$1.errors = [{instancePath:instancePath+"/ttl_msec_incr",schemaPath:"#/properties/ttl_msec_incr/type",keyword:"type",params:{type: "array"},message:"must be array"}];return false;}}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.reason !== undefined){const _errs5 = errors;if(typeof data.reason !== "string"){validate12$1.errors = [{instancePath:instancePath+"/reason",schemaPath:"#/properties/reason/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs5 === errors;}else {var valid0 = true;}}}}else {validate12$1.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate12$1.errors = vErrors;return errors === 0;}const validate_MsgWarp = validate13;function validate13(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.warp === undefined) && (missing0 = "warp")){validate13.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.warp !== undefined){let data0 = data.warp;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if(((data0.user === undefined) && (missing1 = "user")) || ((data0.hyperport === undefined) && (missing1 = "hyperport"))){validate13.errors = [{instancePath:instancePath+"/warp",schemaPath:"#/properties/warp/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ttl_msec !== undefined){let data1 = data0.ttl_msec;const _errs3 = errors;if(!(((typeof data1 == "number") && (!(data1 % 1) && !isNaN(data1))) && (isFinite(data1)))){validate13.errors = [{instancePath:instancePath+"/warp/ttl_msec",schemaPath:"#/properties/warp/properties/ttl_msec/type",keyword:"type",params:{type: "integer"},message:"must be integer"}];return false;}var valid1 = _errs3 === errors;}else {var valid1 = true;}if(valid1){if(data0.instantaneous !== undefined){const _errs5 = errors;if(typeof data0.instantaneous !== "boolean"){validate13.errors = [{instancePath:instancePath+"/warp/instantaneous",schemaPath:"#/properties/warp/properties/instantaneous/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}var valid1 = _errs5 === errors;}else {var valid1 = true;}}}}else {validate13.errors = [{instancePath:instancePath+"/warp",schemaPath:"#/properties/warp/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate13.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate13.errors = vErrors;return errors === 0;}const validate_MsgEngage = validate14;function validate14(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.engage === undefined) && (missing0 = "engage")){validate14.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.engage !== undefined){let data0 = data.engage;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if(((data0.user === undefined) && (missing1 = "user")) || ((data0.hyperport === undefined) && (missing1 = "hyperport"))){validate14.errors = [{instancePath:instancePath+"/engage",schemaPath:"#/properties/engage/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}}else {validate14.errors = [{instancePath:instancePath+"/engage",schemaPath:"#/properties/engage/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate14.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate14.errors = vErrors;return errors === 0;}
|
|
3
|
+
const schema_MsgWarp = validate19;function validate19(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate19.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.warp === undefined) && (missing0 = "warp")){validate19.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.warp !== undefined){let data0 = data.warp;const _errs1 = errors;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if(((((data0.id === undefined) && (missing1 = "id")) || ((data0.user === undefined) && (missing1 = "user"))) || ((data0.hyperport === undefined) && (missing1 = "hyperport"))) || ((data0.ttl_msec === undefined) && (missing1 = "ttl_msec"))){validate19.errors = [{instancePath:instancePath+"/warp",schemaPath:"#/properties/warp/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.id !== undefined){const _errs3 = errors;if(typeof data0.id !== "string"){validate19.errors = [{instancePath:instancePath+"/warp/id",schemaPath:"definitions_2PhW#/definitions/warp_id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid1 = _errs3 === errors;}else {var valid1 = true;}if(valid1){if(data0.ttl_msec !== undefined){let data2 = data0.ttl_msec;const _errs6 = errors;if(!(((typeof data2 == "number") && (!(data2 % 1) && !isNaN(data2))) && (isFinite(data2)))){validate19.errors = [{instancePath:instancePath+"/warp/ttl_msec",schemaPath:"#/properties/warp/properties/ttl_msec/type",keyword:"type",params:{type: "integer"},message:"must be integer"}];return false;}var valid1 = _errs6 === errors;}else {var valid1 = true;}if(valid1){if(data0.instantaneous !== undefined){const _errs8 = errors;if(typeof data0.instantaneous !== "boolean"){validate19.errors = [{instancePath:instancePath+"/warp/instantaneous",schemaPath:"#/properties/warp/properties/instantaneous/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}var valid1 = _errs8 === errors;}else {var valid1 = true;}}}}}else {validate19.errors = [{instancePath:instancePath+"/warp",schemaPath:"#/properties/warp/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data["2PhW"] !== undefined){const _errs10 = errors;if("0.8" !== data["2PhW"]){validate19.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"definitions_2PhW#/definitions/2PhW/const",keyword:"const",params:{allowedValue: "0.8"},message:"must be equal to constant"}];return false;}var valid0 = _errs10 === errors;}else {var valid0 = true;}}}}else {validate19.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate19.errors = vErrors;return errors === 0;}validate19.evaluated = {"props":{"warp":true,"2PhW":true},"dynamicProps":false,"dynamicItems":false};const schema_MsgEngage = validate20;function validate20(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate20.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.engage === undefined) && (missing0 = "engage")){validate20.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.engage !== undefined){let data0 = data.engage;const _errs1 = errors;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if(((data0.ok === undefined) && (missing1 = "ok")) || ((data0.rtt_msec === undefined) && (missing1 = "rtt_msec"))){validate20.errors = [{instancePath:instancePath+"/engage",schemaPath:"#/properties/engage/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){const _errs3 = errors;if(typeof data0.ok !== "boolean"){validate20.errors = [{instancePath:instancePath+"/engage/ok",schemaPath:"#/properties/engage/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}var valid1 = _errs3 === errors;}else {var valid1 = true;}if(valid1){if(data0.rtt_msec !== undefined){let data2 = data0.rtt_msec;const _errs5 = errors;if(!(((typeof data2 == "number") && (!(data2 % 1) && !isNaN(data2))) && (isFinite(data2)))){validate20.errors = [{instancePath:instancePath+"/engage/rtt_msec",schemaPath:"#/properties/engage/properties/rtt_msec/type",keyword:"type",params:{type: "integer"},message:"must be integer"}];return false;}var valid1 = _errs5 === errors;}else {var valid1 = true;}}}}else {validate20.errors = [{instancePath:instancePath+"/engage",schemaPath:"#/properties/engage/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data["2PhW"] !== undefined){const _errs7 = errors;if("0.8" !== data["2PhW"]){validate20.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"definitions_2PhW#/definitions/2PhW/const",keyword:"const",params:{allowedValue: "0.8"},message:"must be equal to constant"}];return false;}var valid0 = _errs7 === errors;}else {var valid0 = true;}}}}else {validate20.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate20.errors = vErrors;return errors === 0;}validate20.evaluated = {"props":{"engage":true,"2PhW":true},"dynamicProps":false,"dynamicItems":false};const schema_MsgOptions = validate21;function validate21(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate21.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.options === undefined) && (missing0 = "options")){validate21.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.options !== undefined){let data0 = data.options;const _errs1 = errors;if(!(data0 && typeof data0 == "object" && !Array.isArray(data0))){validate21.errors = [{instancePath:instancePath+"/options",schemaPath:"#/properties/options/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data["2PhW"] !== undefined){const _errs3 = errors;if("0.8" !== data["2PhW"]){validate21.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"definitions_2PhW#/definitions/2PhW/const",keyword:"const",params:{allowedValue: "0.8"},message:"must be equal to constant"}];return false;}var valid0 = _errs3 === errors;}else {var valid0 = true;}}}}else {validate21.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate21.errors = vErrors;return errors === 0;}validate21.evaluated = {"props":{"options":true,"2PhW":true},"dynamicProps":false,"dynamicItems":false};
|
|
6
4
|
|
|
7
5
|
// console.log('OUT', __name({variableName}) );
|
|
8
6
|
|
|
9
|
-
function l_toBigInt_$1(ref, obj, ignore= false) {
|
|
10
|
-
console.assert(ref !== BigInt(0), 'no labels initialized');
|
|
11
|
-
let bigInt = BigInt(0);
|
|
12
|
-
for (const [k,v] of Object.entries(obj)) {
|
|
13
|
-
if ( ( ignore || v ) && ref[k])
|
|
14
|
-
bigInt|= BigInt( ref[k] );
|
|
15
|
-
// console.log('0b'+ bigInt.toString(2) );
|
|
16
|
-
}
|
|
17
|
-
return bigInt;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function l_LL_(obj, x) {
|
|
21
|
-
let obj_new= {};
|
|
22
|
-
for (var [k,v] of Object.entries(obj))
|
|
23
|
-
obj_new[k]= v<<x;
|
|
24
|
-
return obj_new;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
7
|
//-------------------------------------------------------------------------------------------------
|
|
28
8
|
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
9
|
+
function l_length_(obj_labels) {
|
|
10
|
+
if (! obj_labels || typeof obj_labels !== 'object')
|
|
11
|
+
throw new Error('obj_labels must be an object');
|
|
12
|
+
const obj_1label = Object.values(obj_labels);
|
|
13
|
+
if (! obj_1label.every(v => typeof v === 'number' && Number.isFinite(v)))
|
|
14
|
+
throw new Error('All values must be finite numbers');
|
|
34
15
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
constructor() {
|
|
39
|
-
this._handler_log= handler_default_$1;
|
|
40
|
-
this._Bint_labels= BigInt(0);
|
|
41
|
-
this._Bint_toggled= BigInt(0);
|
|
42
|
-
|
|
43
|
-
BitLogr$1.prototype['log']= function (nr_logged, /* ... */ ) {
|
|
44
|
-
// console.log('NOP')
|
|
45
|
-
};
|
|
46
|
-
}
|
|
16
|
+
const labels = Object.values(obj_labels);
|
|
17
|
+
if (labels.length === 0)
|
|
18
|
+
return 1; // Empty object case, start at 1
|
|
47
19
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
20
|
+
const value_max = Math.max(...labels);
|
|
21
|
+
if (value_max <= 0) return 1
|
|
51
22
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
this._Bint_labels= obj;
|
|
55
|
-
this._Bint_toggled= BigInt(0);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// put= function(label, abbrv) {
|
|
59
|
-
// let name= __name(label);
|
|
60
|
-
// _labels[name]= label[name];
|
|
61
|
-
// console.log(_labels);
|
|
62
|
-
// }
|
|
63
|
-
|
|
64
|
-
get toggled() { return this._Bint_toggled; }
|
|
65
|
-
set toggled(obj) {
|
|
66
|
-
this._Bint_toggled= l_toBigInt_$1(this._Bint_labels, obj);
|
|
67
|
-
|
|
68
|
-
if (this._Bint_toggled === BigInt(0)) {
|
|
69
|
-
console.log('adlkjasdlfk');
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
BitLogr$1.prototype['log']= function (nr_logged, /* ... */ ) {
|
|
74
|
-
if ( (BigInt(nr_logged) & this._Bint_toggled) === BigInt(0))
|
|
75
|
-
return false;
|
|
76
|
-
|
|
77
|
-
var args = Array.prototype.slice.call(arguments);
|
|
78
|
-
args.shift(); // remove first arg: nr_logged
|
|
79
|
-
this._handler_log.apply(this, args);
|
|
80
|
-
|
|
81
|
-
return true;
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// log= function (nr_logged, /* ... */ ) {}
|
|
23
|
+
const bit_highest = Math.floor(Math.log2(value_max));
|
|
24
|
+
return 1 << (bit_highest + 1);
|
|
86
25
|
}
|
|
87
26
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
// Unique ID creation requires a high quality random # generator. In the browser we therefore
|
|
95
|
-
// require the crypto API and do not support built-in fallback to lower quality random number
|
|
96
|
-
// generators (like Math.random()).
|
|
97
|
-
let getRandomValues;
|
|
98
|
-
const rnds8 = new Uint8Array(16);
|
|
99
|
-
function rng() {
|
|
100
|
-
// lazy load so that environments that need to polyfill have a chance to do so
|
|
101
|
-
if (!getRandomValues) {
|
|
102
|
-
// getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
|
|
103
|
-
getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
|
|
104
|
-
|
|
105
|
-
if (!getRandomValues) {
|
|
106
|
-
throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
|
|
107
|
-
}
|
|
108
|
-
}
|
|
27
|
+
function l_array_(arr_labels, start = 1) {
|
|
28
|
+
if (! Array.isArray(arr_labels))
|
|
29
|
+
throw new Error('arr_labels must be an array');
|
|
30
|
+
if (! Number.isSafeInteger(start) || start < 0)
|
|
31
|
+
throw new Error('start must be a safe, non-negative integer');
|
|
109
32
|
|
|
110
|
-
|
|
33
|
+
return Object.freeze(arr_labels.reduce((acc, key, index) => {
|
|
34
|
+
acc[key] = start << index;
|
|
35
|
+
return acc;
|
|
36
|
+
}, {}));
|
|
111
37
|
}
|
|
112
38
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
131
|
-
var native = {
|
|
132
|
-
randomUUID
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
function v4(options, buf, offset) {
|
|
136
|
-
if (native.randomUUID && !buf && !options) {
|
|
137
|
-
return native.randomUUID();
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
options = options || {};
|
|
141
|
-
const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
|
|
142
|
-
|
|
143
|
-
rnds[6] = rnds[6] & 0x0f | 0x40;
|
|
144
|
-
rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
|
|
145
|
-
|
|
146
|
-
if (buf) {
|
|
147
|
-
offset = offset || 0;
|
|
148
|
-
|
|
149
|
-
for (let i = 0; i < 16; ++i) {
|
|
150
|
-
buf[offset + i] = rnds[i];
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return buf;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return unsafeStringify(rnds);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// console.log('OUT', __name({variableName}) );
|
|
39
|
+
function l_concat_(obj_labels, arg) {
|
|
40
|
+
if (! obj_labels || typeof obj_labels !== 'object')
|
|
41
|
+
throw new Error('obj_labels must be an object');
|
|
42
|
+
const obj_1label = Object.values(obj_labels);
|
|
43
|
+
if (! obj_1label.every(v => typeof v === 'number' && Number.isFinite(v)))
|
|
44
|
+
throw new Error('All values must be finite numbers');
|
|
45
|
+
if (! arg || (typeof arg !== 'object' && ! Array.isArray(arg)))
|
|
46
|
+
throw new Error('arg must be an object or array');
|
|
47
|
+
|
|
48
|
+
if (Array.isArray(arg)) {
|
|
49
|
+
const len = l_length_(obj_labels);
|
|
50
|
+
const arr_labels_new = l_array_(arg, len);
|
|
51
|
+
return l_concat_(obj_labels, arr_labels_new);
|
|
52
|
+
}
|
|
160
53
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
54
|
+
const next_pos = l_length_(obj_labels);
|
|
55
|
+
const arg_entries = Object.entries(arg);
|
|
56
|
+
const result = Object.create(null);
|
|
57
|
+
Object.entries(obj_labels).forEach(([k, v]) => {
|
|
58
|
+
if (k !== '__proto__' && k !== 'constructor')
|
|
59
|
+
result[k] = v;
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
let min_arg = Infinity;
|
|
63
|
+
for (const [, value] of arg_entries) {
|
|
64
|
+
if (typeof value !== 'number' || ! Number.isFinite(value))
|
|
65
|
+
continue; // Skip non-numeric
|
|
66
|
+
|
|
67
|
+
if (value > 0 && value < min_arg)
|
|
68
|
+
min_arg = value;
|
|
168
69
|
}
|
|
169
|
-
|
|
70
|
+
// Shift only if min_arg is less than next_pos
|
|
71
|
+
const shift = min_arg === Infinity || min_arg >= next_pos
|
|
72
|
+
? 0
|
|
73
|
+
: Math.floor(Math.log2(next_pos / min_arg));
|
|
74
|
+
|
|
75
|
+
for (const [key, value] of arg_entries) {
|
|
76
|
+
if (!(key in result)) {
|
|
77
|
+
result[key] = value === 0 ? 0 : value << shift;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return Object.freeze(result);
|
|
170
82
|
}
|
|
171
83
|
|
|
172
84
|
//-------------------------------------------------------------------------------------------------
|
|
@@ -179,711 +91,1225 @@ function handler_default_( /* ... */ ) {
|
|
|
179
91
|
|
|
180
92
|
//-------------------------------------------------------------------------------------------------
|
|
181
93
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
BitLogr.prototype['log']= function (nr_logged, /* ... */ ) {
|
|
189
|
-
// console.log('NOP')
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
set handler(fx) {
|
|
194
|
-
this._handler_log= fx;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
get labels() { return this._Bint_labels; }
|
|
198
|
-
set labels(obj) {
|
|
199
|
-
this._Bint_labels= obj;
|
|
200
|
-
this._Bint_toggled= BigInt(0);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// put= function(label, abbrv) {
|
|
204
|
-
// let name= __name(label);
|
|
205
|
-
// _labels[name]= label[name];
|
|
206
|
-
// console.log(_labels);
|
|
207
|
-
// }
|
|
208
|
-
|
|
209
|
-
get toggled() { return this._Bint_toggled; }
|
|
210
|
-
set toggled(obj) {
|
|
211
|
-
this._Bint_toggled= l_toBigInt_(this._Bint_labels, obj);
|
|
94
|
+
function l_toBigInt_(obj_labels, obj, ignore= false) {
|
|
95
|
+
if (! obj_labels || typeof obj_labels !== 'object')
|
|
96
|
+
throw new Error('obj_labels must be an object');
|
|
97
|
+
if (! obj || typeof obj !== 'object')
|
|
98
|
+
throw new Error('obj must be an object');
|
|
212
99
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
BitLogr.prototype['log']= function (nr_logged, /* ... */ ) {
|
|
219
|
-
if ( (BigInt(nr_logged) & this._Bint_toggled) === BigInt(0))
|
|
220
|
-
return false;
|
|
221
|
-
|
|
222
|
-
var args = Array.prototype.slice.call(arguments);
|
|
223
|
-
args.shift(); // remove first arg: nr_logged
|
|
224
|
-
this._handler_log.apply(this, args);
|
|
225
|
-
|
|
226
|
-
return true;
|
|
227
|
-
};
|
|
100
|
+
let bigInt = BigInt(0);
|
|
101
|
+
for (const [k,v] of Object.entries(obj)) {
|
|
102
|
+
if ( ( ignore || v ) && obj_labels[k] !== undefined && typeof obj_labels[k] === 'number')
|
|
103
|
+
bigInt|= BigInt( obj_labels[k] );
|
|
104
|
+
// console.log('0b'+ bigInt.toString(2) );
|
|
228
105
|
}
|
|
229
|
-
|
|
230
|
-
// log= function (nr_logged, /* ... */ ) {}
|
|
106
|
+
return bigInt;
|
|
231
107
|
}
|
|
232
108
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
const l_$1= {
|
|
236
|
-
VALIDATION : 0b1 << 0,
|
|
237
|
-
};
|
|
109
|
+
// console.log(l_toBigInt_({},{}))
|
|
238
110
|
|
|
239
|
-
|
|
111
|
+
const LOGR = (function () {
|
|
112
|
+
let _instance; // Private variable to hold the single instance
|
|
240
113
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
let json_msg= {
|
|
247
|
-
id : v4(),
|
|
248
|
-
referer : null,
|
|
249
|
-
dt : dt_Msg.toISOString(),
|
|
250
|
-
};
|
|
251
|
-
// https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object
|
|
252
|
-
for (var key of Object.keys(json)) {
|
|
253
|
-
json_msg[key]= json[key];
|
|
254
|
-
}
|
|
255
|
-
return json_msg;
|
|
256
|
-
},
|
|
257
|
-
validate : function(json) {
|
|
258
|
-
const b_valid= validate_Msg(json);
|
|
259
|
-
if (!b_valid)
|
|
260
|
-
LOGR_$1.log(l_$1.VALIDATION, 'JSON_Msg: validate: ', validate_Msg.errors);
|
|
261
|
-
return b_valid;
|
|
262
|
-
}
|
|
263
|
-
// TODO: requires a separate ajv module require, and JTD specific schema
|
|
264
|
-
// https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
|
|
265
|
-
// static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
|
|
266
|
-
// static parse(json) {
|
|
267
|
-
// return this.#ajv_parse_(json);
|
|
268
|
-
// }
|
|
269
|
-
}
|
|
270
|
-
})();
|
|
271
|
-
|
|
272
|
-
var JSON_Msg_msg = (function() {
|
|
273
|
-
return {
|
|
274
|
-
create_with_msg_merge : function(_msg, json) {
|
|
275
|
-
let json_msg= JSON_Msg.merge(json);
|
|
276
|
-
json_msg._msg= _msg;
|
|
277
|
-
return json_msg;
|
|
278
|
-
},
|
|
279
|
-
validate : function(json) {
|
|
280
|
-
if (! JSON_Msg.validate(json))
|
|
281
|
-
return false;
|
|
282
|
-
const b_valid= validate_Msg_msg(json);
|
|
283
|
-
if (!b_valid)
|
|
284
|
-
LOGR_$1.log(l_$1.VALIDATION, 'JSON_Msg_msg: validate: ', validate_Msg_msg.errors);
|
|
285
|
-
return b_valid;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
})();
|
|
114
|
+
function _create_instance() {
|
|
115
|
+
// Private state (replacing constructor properties)
|
|
116
|
+
let _handler_log = handler_default_;
|
|
117
|
+
let _obj_labels = undefined;
|
|
118
|
+
let _Bint_toggled = BigInt(0);
|
|
289
119
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
create_with_msgOk : function(_msg, b_ok= true) {
|
|
293
|
-
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
294
|
-
ack : {
|
|
295
|
-
"ok" : b_ok,
|
|
296
|
-
},
|
|
297
|
-
});
|
|
298
|
-
},
|
|
299
|
-
create_with_msgExpired : function(_msg) {
|
|
300
|
-
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
301
|
-
ack : {
|
|
302
|
-
"ok" : false,
|
|
303
|
-
"expired" : true
|
|
304
|
-
},
|
|
305
|
-
});
|
|
306
|
-
},
|
|
307
|
-
validate : function(json) {
|
|
308
|
-
if (! JSON_Msg_msg.validate(json))
|
|
120
|
+
function _log_fxn(nr_logged, argsFn /* args */) {
|
|
121
|
+
if ((BigInt(nr_logged) & _Bint_toggled) === BigInt(0))
|
|
309
122
|
return false;
|
|
310
|
-
const b_valid= validate_MsgAck(json);
|
|
311
|
-
if (!b_valid)
|
|
312
|
-
LOGR_$1.log(l_$1.VALIDATION, 'JSON_MsgAck: validate: ', validate_MsgAck.errors);
|
|
313
|
-
return b_valid;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
})();
|
|
317
|
-
|
|
318
|
-
var JSON_Msg_1 = jsonMsg_cjs.JSON_Msg = JSON_Msg;
|
|
319
|
-
var JSON_MsgAck_1 = jsonMsg_cjs.JSON_MsgAck = JSON_MsgAck;
|
|
320
|
-
var JSON_Msg_msg_1 = jsonMsg_cjs.JSON_Msg_msg = JSON_Msg_msg;
|
|
321
|
-
|
|
322
|
-
const k_TTL= 20000;
|
|
323
|
-
|
|
324
|
-
//-------------------------------------------------------------------------------------------------
|
|
325
|
-
|
|
326
|
-
let LOGR_= new BitLogr$1();
|
|
327
|
-
|
|
328
|
-
const l_msgs_ = {
|
|
329
|
-
MSG : 0b1 << 0,
|
|
330
|
-
WARP : 0b1 << 1,
|
|
331
|
-
ENGAGE : 0b1 << 2,
|
|
332
|
-
ACK : 0b1 << 3,
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
const l_= {
|
|
336
|
-
HANDLERS : 0b1 << 0,
|
|
337
|
-
DROPS : 0b1 << 1,
|
|
338
|
-
VALIDATION : 0b1 << 2,
|
|
339
|
-
... l_LL_(l_msgs_, 8)
|
|
340
|
-
};
|
|
341
|
-
LOGR_.labels= l_;
|
|
342
|
-
|
|
343
|
-
//-------------------------------------------------------------------------------------------------
|
|
344
|
-
|
|
345
|
-
let map_Context_= new Map();
|
|
346
123
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
return ctx;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
function get_ctx(key) {
|
|
354
|
-
return map_Context_.get(key);
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
//-------------------------------------------------------------------------------------------------
|
|
358
|
-
|
|
359
|
-
class Config {
|
|
360
|
-
receiver(callback_msgWarp,callback_msgEngage) {
|
|
361
|
-
this._callback_msgWarp= callback_msgWarp;
|
|
362
|
-
this._callback_msgEngage= callback_msgEngage;
|
|
363
|
-
}
|
|
364
|
-
sender(callback_msgAckWarp,callback_msgAckEngage ) {
|
|
365
|
-
this._callback_msgAckWarp= callback_msgAckWarp;
|
|
366
|
-
this._callback_msgAckEngage= callback_msgAckEngage;
|
|
367
|
-
}
|
|
368
|
-
set callback_msgOptions(callback) {
|
|
369
|
-
this._callback_msgOptions= callback;
|
|
370
|
-
}
|
|
371
|
-
set logr(obj_logr) {
|
|
372
|
-
LOGR_.toggled= obj_logr;
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
// var config = (function() {
|
|
377
|
-
// let _options;
|
|
378
|
-
|
|
379
|
-
// return {
|
|
380
|
-
// get callback_Abort() { return _options.callback_Abort; },
|
|
381
|
-
|
|
382
|
-
// set options(obj) {
|
|
383
|
-
// _options= obj;
|
|
384
|
-
// if (_options.logr) // && options_.logr[ __name(l_) ] )
|
|
385
|
-
// LOGR_.toggled= _options.logr
|
|
386
|
-
// }
|
|
387
|
-
// }
|
|
388
|
-
// })();
|
|
389
|
-
|
|
390
|
-
//-------------------------------------------------------------------------------------------------
|
|
391
|
-
|
|
392
|
-
let k_VERSION= "0.7";
|
|
393
|
-
|
|
394
|
-
var JSON_MsgOptions = (function() {
|
|
395
|
-
return {
|
|
396
|
-
create_with_msg_options : function(_msg, options) {
|
|
397
|
-
return JSON_Msg_msg_1.create_with_msg_merge(_msg, {
|
|
398
|
-
options: options,
|
|
399
|
-
"2PhW": k_VERSION
|
|
400
|
-
});
|
|
401
|
-
},
|
|
402
|
-
validate : function(json) {
|
|
403
|
-
if (! JSON_Msg_msg_1.validate(json))
|
|
404
|
-
return false;
|
|
405
|
-
if (! validate_Msg2PhW(json)) {
|
|
406
|
-
LOGR_.log(l_.VALIDATION, 'JSON_MsgWarp: validate_Msg2PhW_: ', validate_Msg2PhW.errors);
|
|
407
|
-
return false;
|
|
408
|
-
}
|
|
409
|
-
if (! validate_MsgOptions(json)) {
|
|
410
|
-
LOGR_.log(l_.VALIDATION, 'JSON_MsgOptions: validate_MsgOptions_: ', validate_MsgOptions.errors);
|
|
411
|
-
return false;
|
|
412
|
-
}
|
|
124
|
+
const args = argsFn();
|
|
125
|
+
_handler_log.apply(this, args);
|
|
126
|
+
// _handler_log.apply(this, args);
|
|
413
127
|
return true;
|
|
414
128
|
}
|
|
415
|
-
}
|
|
416
|
-
})();
|
|
417
129
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
create_with_MsgWarp : function(json_msgWarp) {
|
|
449
|
-
let json_msgEngage= JSON_Msg_1.merge({
|
|
450
|
-
engage : {
|
|
451
|
-
user : json_msgWarp.warp.user,
|
|
452
|
-
hyperport : json_msgWarp.warp.hyperport,
|
|
453
|
-
},
|
|
454
|
-
"2PhW": k_VERSION
|
|
455
|
-
});
|
|
456
|
-
json_msgEngage["id"]= json_msgWarp.id;
|
|
457
|
-
return json_msgEngage;
|
|
458
|
-
},
|
|
459
|
-
validate : function(json) {
|
|
460
|
-
if (! JSON_Msg_1.validate(json))
|
|
461
|
-
return false;
|
|
462
|
-
if (! validate_Msg2PhW(json)) {
|
|
463
|
-
LOGR_.log(l_.VALIDATION, 'JSON_MsgWarp: validate_Msg2PhW_: ', validate_Msg2PhW.errors);
|
|
464
|
-
return false;
|
|
465
|
-
}
|
|
466
|
-
if (! validate_MsgEngage(json)) {
|
|
467
|
-
LOGR_.log(l_.VALIDATION, 'JSON_MsgEngage: validate_MsgEngage_: ', validate_MsgEngage.errors);
|
|
468
|
-
return false;
|
|
469
|
-
}
|
|
470
|
-
return true;
|
|
471
|
-
}
|
|
130
|
+
return {
|
|
131
|
+
set handler(fx) {
|
|
132
|
+
_handler_log = fx;
|
|
133
|
+
},
|
|
134
|
+
get handler() {
|
|
135
|
+
return _handler_log;
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
get labels() { return _obj_labels; },
|
|
139
|
+
set labels(obj) {
|
|
140
|
+
_obj_labels = obj;
|
|
141
|
+
_Bint_toggled = BigInt(0);
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
// put= function(label, abbrv) {
|
|
145
|
+
// let name= __name(label);
|
|
146
|
+
// _labels[name]= label[name];
|
|
147
|
+
// console.log(_labels);
|
|
148
|
+
// }
|
|
149
|
+
|
|
150
|
+
get toggled() { return _Bint_toggled; },
|
|
151
|
+
set toggled(obj_toggled) {
|
|
152
|
+
_Bint_toggled= l_toBigInt_(_obj_labels, obj_toggled);
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
log(nr_logged, argsFn) {
|
|
156
|
+
|
|
157
|
+
return _log_fxn.call(this, nr_logged, argsFn); // Pass the thunk
|
|
158
|
+
}
|
|
159
|
+
};
|
|
472
160
|
}
|
|
473
|
-
})();
|
|
474
161
|
|
|
475
|
-
|
|
162
|
+
// Public interface
|
|
476
163
|
return {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
// return json_msgAckWarp;
|
|
481
|
-
// }
|
|
482
|
-
create_with_msgOk : function(json_msgWarp, b_ok= true) {
|
|
483
|
-
const json_msgAck= JSON_MsgAck_1.create_with_msgOk(json_msgWarp, b_ok);
|
|
484
|
-
json_msgAck["2PhW"]= k_VERSION;
|
|
485
|
-
return json_msgAck;
|
|
486
|
-
},
|
|
487
|
-
create_with_msgExpired : function( json_msgWarp ) {
|
|
488
|
-
const json_msgAck= JSON_MsgAck_1.create_with_msgExpired( json_msgWarp );
|
|
489
|
-
json_msgAck["2PhW"]= k_VERSION;
|
|
490
|
-
return json_msgAck;
|
|
491
|
-
},
|
|
492
|
-
validate : function(json) {
|
|
493
|
-
if (! validate_Msg2PhW(json)) {
|
|
494
|
-
LOGR_.log(l_.VALIDATION, 'JSON_MsgAckWarp: validate_Msg2PhW_: ', validate_Msg2PhW.errors);
|
|
495
|
-
return false;
|
|
164
|
+
get_instance() {
|
|
165
|
+
if (!_instance) {
|
|
166
|
+
_instance = _create_instance(); // Lazy initialization
|
|
496
167
|
}
|
|
497
|
-
|
|
498
|
-
return false;
|
|
499
|
-
if (! JSON_MsgWarp.validate(json._msg))
|
|
500
|
-
return false;
|
|
501
|
-
return true;
|
|
168
|
+
return _instance;
|
|
502
169
|
}
|
|
503
|
-
}
|
|
504
|
-
})();
|
|
170
|
+
};
|
|
505
171
|
|
|
506
|
-
var JSON_MsgAckEngage = (function() {
|
|
507
|
-
return {
|
|
508
|
-
// createWith_msgAck_msg : function(json_msgAck, _msg) {
|
|
509
|
-
// const json_msgAckEngage= structuredClone(json_msgAck);
|
|
510
|
-
// json_msgAckEngage._msg= _msg;
|
|
511
|
-
// return json_msgAckEngage;
|
|
512
|
-
// }
|
|
513
|
-
create_with_msgOk : function(json_msgEngage, b_ok= true) {
|
|
514
|
-
const json_msgAck= JSON_MsgAck_1.create_with_msgOk(json_msgEngage, b_ok);
|
|
515
|
-
json_msgAck["2PhW"]= k_VERSION;
|
|
516
|
-
return json_msgAck;
|
|
517
|
-
},
|
|
518
|
-
create_with_msgExpired : function( json_msgEngage ) {
|
|
519
|
-
const json_msgAck= JSON_MsgAck_1.create_with_msgExpired( json_msgEngage );
|
|
520
|
-
json_msgAck["2PhW"]= k_VERSION;
|
|
521
|
-
return json_msgAck;
|
|
522
|
-
},
|
|
523
|
-
validate : function(json) {
|
|
524
|
-
if (! validate_Msg2PhW(json)) {
|
|
525
|
-
LOGR_.log(l_.VALIDATION, 'JSON_MsgAckEngage: validate_Msg2PhW_: ', validate_Msg2PhW.errors);
|
|
526
|
-
return false;
|
|
527
|
-
}
|
|
528
|
-
if (! JSON_MsgAck_1.validate(json))
|
|
529
|
-
return false;
|
|
530
|
-
if (! JSON_MsgEngage.validate(json._msg))
|
|
531
|
-
return false;
|
|
532
|
-
return true;
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
172
|
})();
|
|
536
173
|
|
|
174
|
+
var jsonMsg_cjs = {};
|
|
537
175
|
|
|
538
|
-
|
|
539
|
-
// State
|
|
540
|
-
|
|
541
|
-
class StateState {
|
|
542
|
-
constructor(initState) {
|
|
543
|
-
this.state_current_ = initState;
|
|
544
|
-
this.dt_start_= null;
|
|
545
|
-
this.nr_timeout_id_= -1;
|
|
546
|
-
this.nr_remaining_msec_= 0;
|
|
547
|
-
this.f_callback_= undefined;
|
|
548
|
-
}
|
|
549
|
-
start() {
|
|
550
|
-
this.dt_start_= Date.now();
|
|
551
|
-
console.log(`setTimeout: start ${this.dt_start_}, remaining ${this.nr_remaining_msec_}`);
|
|
552
|
-
this.nr_timeout_id_= setTimeout(this.f_callback_, this.nr_remaining_msec_);
|
|
553
|
-
}
|
|
554
|
-
changeState(newState, nr_ttl_msec, f_callback) {
|
|
555
|
-
if (! this.state_current_.changeState(newState))
|
|
556
|
-
return false;
|
|
557
|
-
this.state_current_ = newState;
|
|
558
|
-
|
|
559
|
-
clearTimeout(this.nr_timeout_id_);
|
|
560
|
-
if (nr_ttl_msec != undefined && f_callback != undefined) {
|
|
561
|
-
this.f_callback_= f_callback;
|
|
562
|
-
this.nr_remaining_msec_= nr_ttl_msec;
|
|
563
|
-
this.start();
|
|
564
|
-
}
|
|
565
|
-
return true;
|
|
566
|
-
}
|
|
567
|
-
delayTimeout(nr_delay_msec) {
|
|
568
|
-
clearTimeout(this.nr_timeout_id_);
|
|
569
|
-
const nr_elapsed_ms= Date.now() - this.dt_start_;
|
|
570
|
-
this.nr_remaining_msec_= Math.max(this.nr_remaining_msec_ - nr_elapsed_ms, 0) + nr_delay_msec;
|
|
571
|
-
this.start();
|
|
572
|
-
}
|
|
573
|
-
inState(stateClass) {
|
|
574
|
-
return this.state_current_ instanceof stateClass;
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
class StateInitialState {
|
|
579
|
-
changeState(newState) {
|
|
580
|
-
return true;
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
// class State<Name> {
|
|
585
|
-
// changeState= function(newState) {
|
|
586
|
-
// return false;
|
|
587
|
-
// }
|
|
588
|
-
// }
|
|
589
|
-
|
|
590
|
-
// if (newState instanceof State<Name>)
|
|
591
|
-
// return true;
|
|
592
|
-
|
|
593
|
-
// if (! <Class>.inState( State<Name> ))
|
|
594
|
-
// if (! <Class>.changeState( new State<Name>() ))
|
|
595
|
-
|
|
596
|
-
//-------------------------------------------------------------------------------------------------
|
|
597
|
-
|
|
598
|
-
class ProtocolStateState extends StateState {
|
|
599
|
-
constructor(initState= new StateInitialState() ) {
|
|
600
|
-
super(initState);
|
|
601
|
-
}
|
|
602
|
-
}
|
|
176
|
+
var hasRequiredJsonMsg_cjs;
|
|
603
177
|
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
return true;
|
|
608
|
-
if (newState instanceof StateComplete)
|
|
609
|
-
return true;
|
|
610
|
-
return false;
|
|
611
|
-
}
|
|
612
|
-
}
|
|
178
|
+
function requireJsonMsg_cjs () {
|
|
179
|
+
if (hasRequiredJsonMsg_cjs) return jsonMsg_cjs;
|
|
180
|
+
hasRequiredJsonMsg_cjs = 1;
|
|
613
181
|
|
|
614
|
-
|
|
615
|
-
changeState(newState) {
|
|
616
|
-
if (newState instanceof StateEngage)
|
|
617
|
-
return true;
|
|
618
|
-
if (newState instanceof StateComplete)
|
|
619
|
-
return true;
|
|
620
|
-
return false;
|
|
621
|
-
}
|
|
622
|
-
}
|
|
182
|
+
const schema_Msg = validate18;function validate18(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate18.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate18.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate18.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs3 = errors;if(typeof data.dt !== "string"){validate18.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs3 === errors;}else {var valid0 = true;}}}}else {validate18.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate18.errors = vErrors;return errors === 0;}validate18.evaluated = {"props":{"id":true,"dt":true},"dynamicProps":false,"dynamicItems":false};const schema_Msg_msg = validate19;function validate19(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate19.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate19.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate19.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate19.errors = vErrors;return errors === 0;}validate19.evaluated = {"dynamicProps":false,"dynamicItems":false};const schema_MsgAck_msg = validate20;function validate20(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate20.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate20.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate20.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate20.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate20.errors = vErrors;return errors === 0;}validate20.evaluated = {"props":{"ack":true},"dynamicProps":false,"dynamicItems":false};
|
|
623
183
|
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
return true;
|
|
628
|
-
return false;
|
|
184
|
+
const byteToHex = [];
|
|
185
|
+
for (let i = 0; i < 256; ++i) {
|
|
186
|
+
byteToHex.push((i + 0x100).toString(16).slice(1));
|
|
629
187
|
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
188
|
+
function unsafeStringify(arr, offset = 0) {
|
|
189
|
+
return (byteToHex[arr[offset + 0]] +
|
|
190
|
+
byteToHex[arr[offset + 1]] +
|
|
191
|
+
byteToHex[arr[offset + 2]] +
|
|
192
|
+
byteToHex[arr[offset + 3]] +
|
|
193
|
+
'-' +
|
|
194
|
+
byteToHex[arr[offset + 4]] +
|
|
195
|
+
byteToHex[arr[offset + 5]] +
|
|
196
|
+
'-' +
|
|
197
|
+
byteToHex[arr[offset + 6]] +
|
|
198
|
+
byteToHex[arr[offset + 7]] +
|
|
199
|
+
'-' +
|
|
200
|
+
byteToHex[arr[offset + 8]] +
|
|
201
|
+
byteToHex[arr[offset + 9]] +
|
|
202
|
+
'-' +
|
|
203
|
+
byteToHex[arr[offset + 10]] +
|
|
204
|
+
byteToHex[arr[offset + 11]] +
|
|
205
|
+
byteToHex[arr[offset + 12]] +
|
|
206
|
+
byteToHex[arr[offset + 13]] +
|
|
207
|
+
byteToHex[arr[offset + 14]] +
|
|
208
|
+
byteToHex[arr[offset + 15]]).toLowerCase();
|
|
635
209
|
}
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
//-------------------------------------------------------------------------------------------------
|
|
639
|
-
|
|
640
|
-
function handler_msgOptions_(cfg, msg, json_msgOptions)
|
|
641
|
-
{
|
|
642
|
-
LOGR_.log(l_.HANDLERS, 'handler_msgOptions_: ...');
|
|
643
|
-
|
|
644
|
-
if (! validate_Options_delay(json_msgOptions.options))
|
|
645
|
-
return false;
|
|
646
|
-
|
|
647
|
-
if (JSON_MsgWarp.validate(json_msgOptions._msg)) {
|
|
648
|
-
const json_msgWarp= json_msgOptions._msg;
|
|
649
|
-
|
|
650
|
-
const ctx= get_ctx(json_msgWarp.id);
|
|
651
|
-
if (!ctx )
|
|
652
|
-
return false;
|
|
653
|
-
|
|
654
|
-
json_msgOptions.options.ttl_msec_incr[0];
|
|
655
|
-
const ttl_incr= json_msgOptions.options.ttl_msec_incr[1];
|
|
656
|
-
|
|
657
|
-
//TODO: double check ttl_msec is the same as current accummulated ttl for idempotence.
|
|
658
210
|
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
211
|
+
let getRandomValues;
|
|
212
|
+
const rnds8 = new Uint8Array(16);
|
|
213
|
+
function rng() {
|
|
214
|
+
if (!getRandomValues) {
|
|
215
|
+
if (typeof crypto === 'undefined' || !crypto.getRandomValues) {
|
|
216
|
+
throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
|
|
217
|
+
}
|
|
218
|
+
getRandomValues = crypto.getRandomValues.bind(crypto);
|
|
219
|
+
}
|
|
220
|
+
return getRandomValues(rnds8);
|
|
667
221
|
}
|
|
668
222
|
|
|
669
|
-
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
{
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
if (!ctx || !ctx.protocol.changeState( new StateAckWarp() ))
|
|
685
|
-
return false;
|
|
686
|
-
|
|
687
|
-
//TODO: check if multiple warps have been sent out with the same destination
|
|
688
|
-
|
|
689
|
-
LOGR_.log(l_.HANDLERS, 'handler_msgAckWarp: ', json_msgAckWarp);
|
|
690
|
-
|
|
691
|
-
// const json_msgAckWarp= JSON_MsgAckWarp.createWith_msgAck_msg(json_msgAck, json_msgWarp);
|
|
692
|
-
|
|
693
|
-
if (! cfg._callback_msgAckWarp(msg, json_msgAckWarp))
|
|
694
|
-
return false;
|
|
695
|
-
|
|
696
|
-
if (json_msgWarp.warp.instantaneous) {
|
|
697
|
-
const b_res= ctx.protocol.changeState( new StateComplete() );
|
|
698
|
-
console.assert(b_res);
|
|
699
|
-
|
|
700
|
-
return true;
|
|
223
|
+
const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
224
|
+
var native = { randomUUID };
|
|
225
|
+
|
|
226
|
+
function v4(options, buf, offset) {
|
|
227
|
+
if (native.randomUUID && true && !options) {
|
|
228
|
+
return native.randomUUID();
|
|
229
|
+
}
|
|
230
|
+
options = options || {};
|
|
231
|
+
const rnds = options.random ?? options.rng?.() ?? rng();
|
|
232
|
+
if (rnds.length < 16) {
|
|
233
|
+
throw new Error('Random bytes length must be >= 16');
|
|
234
|
+
}
|
|
235
|
+
rnds[6] = (rnds[6] & 0x0f) | 0x40;
|
|
236
|
+
rnds[8] = (rnds[8] & 0x3f) | 0x80;
|
|
237
|
+
return unsafeStringify(rnds);
|
|
701
238
|
}
|
|
702
239
|
|
|
703
|
-
|
|
704
|
-
console.assert(b_res);
|
|
705
|
-
|
|
706
|
-
return true;
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
function handler_msgAckEngage_(cfg, msg, json_msgAckEngage)
|
|
710
|
-
{
|
|
711
|
-
// NOTE: json_msgAck here has a string as _msg, instead of JSON.
|
|
712
|
-
|
|
713
|
-
LOGR_.log(l_.HANDLERS, 'handler_msgAckEngage: ...');
|
|
714
|
-
|
|
715
|
-
const json_msgEngage= json_msgAckEngage._msg;
|
|
716
|
-
|
|
717
|
-
const ctx= get_ctx(json_msgEngage.id);
|
|
718
|
-
if (!ctx || !ctx.protocol.changeState( new StateAckEngage() ))
|
|
719
|
-
return false;
|
|
720
|
-
|
|
721
|
-
LOGR_.log(l_.HANDLERS, 'handler_msgAckEngage: ', json_msgAckEngage);
|
|
722
|
-
|
|
723
|
-
// const json_msgAckEngage= JSON_MsgAckEngage.createWith_msgAck_msg(json_msgAck, json_msgEngage);
|
|
724
|
-
|
|
725
|
-
if (! cfg._callback_msgAckEngage(msg, json_msgAckEngage))
|
|
726
|
-
return false;
|
|
727
|
-
|
|
728
|
-
return true;
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
//-------------------------------------------------------------------------------------------------
|
|
732
|
-
// Receiver
|
|
733
|
-
|
|
734
|
-
function handler_msgWarp_(cfg, msg, json_msgWarp)
|
|
735
|
-
{
|
|
736
|
-
LOGR_.log(l_.HANDLERS, 'handler_msgWarp: ...');
|
|
737
|
-
|
|
738
|
-
let ctx= get_ctx(json_msgWarp.id);
|
|
739
|
-
if (ctx === undefined)
|
|
740
|
-
ctx= set_ctx(json_msgWarp.id, { protocol: new ProtocolStateState() });
|
|
741
|
-
|
|
742
|
-
if (!ctx || !ctx.protocol.changeState( new StateWarp() ))
|
|
743
|
-
return false;
|
|
240
|
+
// console.log('OUT', __name({variableName}) );
|
|
744
241
|
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
return true;
|
|
242
|
+
function l_toBigInt_(ref, obj, ignore= false) {
|
|
243
|
+
console.assert(ref !== BigInt(0), 'no labels initialized');
|
|
244
|
+
let bigInt = BigInt(0);
|
|
245
|
+
for (const [k,v] of Object.entries(obj)) {
|
|
246
|
+
if ( ( ignore || v ) && ref[k])
|
|
247
|
+
bigInt|= BigInt( ref[k] );
|
|
248
|
+
// console.log('0b'+ bigInt.toString(2) );
|
|
249
|
+
}
|
|
250
|
+
return bigInt;
|
|
755
251
|
}
|
|
756
252
|
|
|
757
|
-
|
|
758
|
-
console.assert(b_res);
|
|
759
|
-
|
|
760
|
-
return true;
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
function handler_msgEngage_(cfg, msg, json_msgEngage)
|
|
764
|
-
{
|
|
765
|
-
LOGR_.log(l_.HANDLERS, 'handler_msgEngage: ...');
|
|
766
|
-
|
|
767
|
-
const ctx= get_ctx(json_msgEngage.id);
|
|
768
|
-
if (!ctx || !ctx.protocol.changeState( new StateEngage() ))
|
|
769
|
-
return false;
|
|
770
|
-
|
|
771
|
-
LOGR_.log(l_.HANDLERS, 'handler_msgEngage: ', json_msgEngage);
|
|
772
|
-
|
|
773
|
-
if (! cfg._callback_msgEngage(msg, json_msgEngage))
|
|
774
|
-
return false;
|
|
775
|
-
|
|
776
|
-
const b_res= ctx.protocol.changeState( new StateComplete() );
|
|
777
|
-
console.assert(b_res);
|
|
778
|
-
|
|
779
|
-
return true;
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
//-------------------------------------------------------------------------------------------------
|
|
253
|
+
//-------------------------------------------------------------------------------------------------
|
|
783
254
|
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
255
|
+
function handler_default_( /* ... */ ) {
|
|
256
|
+
// https://stackoverflow.com/questions/18746440/passing-multiple-arguments-to-console-log
|
|
257
|
+
var args = Array.prototype.slice.call(arguments);
|
|
258
|
+
console.log.apply(console, args);
|
|
787
259
|
}
|
|
788
260
|
|
|
789
|
-
|
|
790
|
-
return this._config;
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
// this function need to be removed when a microservice db is used instead of a state machine
|
|
794
|
-
create_MsgWarp_out(str_id, json_user, json_hyperport, nr_ttl_msec, b_instantaneous)
|
|
795
|
-
{
|
|
796
|
-
if (nr_ttl_msec === undefined)
|
|
797
|
-
nr_ttl_msec= k_TTL;
|
|
798
|
-
|
|
799
|
-
const json_msgWarp= JSON_MsgWarp.create_with_user_hyperport(json_user, json_hyperport, nr_ttl_msec);
|
|
800
|
-
json_msgWarp.id= str_id;
|
|
261
|
+
//-------------------------------------------------------------------------------------------------
|
|
801
262
|
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
console.assert(b_res);
|
|
813
|
-
return json_msgWarp;
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
handler_msg_json(msg, json_msg)
|
|
817
|
-
{
|
|
818
|
-
LOGR_.log(l_.HANDLERS, 'handler_msg_json: ', json_msg);
|
|
819
|
-
|
|
820
|
-
// ADR: I could parse json_Msg_msg first and then MsgAck or MsgOptions, but
|
|
821
|
-
// doing so means super classes get parsed multiple times for no reason
|
|
263
|
+
class BitLogr {
|
|
264
|
+
constructor() {
|
|
265
|
+
this._handler_log= handler_default_;
|
|
266
|
+
this._Bint_labels= BigInt(0);
|
|
267
|
+
this._Bint_toggled= BigInt(0);
|
|
268
|
+
|
|
269
|
+
BitLogr.prototype['log']= function (nr_logged, /* ... */ ) {
|
|
270
|
+
// console.log('NOP')
|
|
271
|
+
};
|
|
272
|
+
}
|
|
822
273
|
|
|
823
|
-
|
|
824
|
-
|
|
274
|
+
set handler(fx) {
|
|
275
|
+
this._handler_log= fx;
|
|
276
|
+
}
|
|
825
277
|
|
|
826
|
-
|
|
827
|
-
|
|
278
|
+
get labels() { return this._Bint_labels; }
|
|
279
|
+
set labels(obj) {
|
|
280
|
+
this._Bint_labels= obj;
|
|
281
|
+
this._Bint_toggled= BigInt(0);
|
|
282
|
+
}
|
|
828
283
|
|
|
829
|
-
|
|
830
|
-
|
|
284
|
+
// put= function(label, abbrv) {
|
|
285
|
+
// let name= __name(label);
|
|
286
|
+
// _labels[name]= label[name];
|
|
287
|
+
// console.log(_labels);
|
|
288
|
+
// }
|
|
831
289
|
|
|
832
|
-
|
|
833
|
-
|
|
290
|
+
get toggled() { return this._Bint_toggled; }
|
|
291
|
+
set toggled(obj) {
|
|
292
|
+
this._Bint_toggled= l_toBigInt_(this._Bint_labels, obj);
|
|
834
293
|
|
|
835
|
-
|
|
836
|
-
|
|
294
|
+
if (this._Bint_toggled === BigInt(0)) {
|
|
295
|
+
console.log('adlkjasdlfk');
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
837
298
|
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
299
|
+
BitLogr.prototype['log']= function (nr_logged, /* ... */ ) {
|
|
300
|
+
if ( (BigInt(nr_logged) & this._Bint_toggled) === BigInt(0))
|
|
301
|
+
return false;
|
|
302
|
+
|
|
303
|
+
var args = Array.prototype.slice.call(arguments);
|
|
304
|
+
args.shift(); // remove first arg: nr_logged
|
|
305
|
+
this._handler_log.apply(this, args);
|
|
306
|
+
|
|
307
|
+
return true;
|
|
308
|
+
};
|
|
309
|
+
}
|
|
846
310
|
|
|
847
|
-
|
|
311
|
+
// log= function (nr_logged, /* ... */ ) {}
|
|
848
312
|
}
|
|
849
313
|
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
314
|
+
let LOGR_= new BitLogr();
|
|
315
|
+
|
|
316
|
+
const l_= {
|
|
317
|
+
VALIDATION : 0b1 << 0,
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
// TODO: use RFC3339 "timestamp"? https://ajv.js.org/json-type-definition.html
|
|
321
|
+
|
|
322
|
+
// "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
|
|
323
|
+
var JSON_Msg = (function() {
|
|
324
|
+
return {
|
|
325
|
+
merge: function(json) {
|
|
326
|
+
return Object.assign({
|
|
327
|
+
id: v4(),
|
|
328
|
+
referer: null,
|
|
329
|
+
dt: new Date().toISOString()
|
|
330
|
+
}, json || {});
|
|
331
|
+
},
|
|
332
|
+
validate : function (json) {
|
|
333
|
+
const b_valid = schema_Msg(json);
|
|
334
|
+
if (!b_valid)
|
|
335
|
+
LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
|
|
336
|
+
return b_valid;
|
|
337
|
+
},
|
|
338
|
+
// TODO: requires a separate ajv module require, and JTD specific schema
|
|
339
|
+
// https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
|
|
340
|
+
// static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
|
|
341
|
+
// static parse(json) {
|
|
342
|
+
// return this.#ajv_parse_(json);
|
|
343
|
+
// }
|
|
344
|
+
}
|
|
345
|
+
})();
|
|
346
|
+
|
|
347
|
+
var JSON_Msg_msg = (function() {
|
|
348
|
+
return {
|
|
349
|
+
create_with_msg_merge: function(_msg, json) {
|
|
350
|
+
return Object.assign(JSON_Msg.merge(json), { _msg });
|
|
351
|
+
},
|
|
352
|
+
validate : function(json) {
|
|
353
|
+
if (! JSON_Msg.validate(json))
|
|
354
|
+
return false;
|
|
355
|
+
const b_valid= schema_Msg_msg(json);
|
|
356
|
+
if (!b_valid)
|
|
357
|
+
LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
|
|
358
|
+
return b_valid;
|
|
359
|
+
}
|
|
859
360
|
}
|
|
860
|
-
|
|
861
|
-
|
|
361
|
+
})();
|
|
362
|
+
|
|
363
|
+
var JSON_MsgAck_msg = (function() {
|
|
364
|
+
return {
|
|
365
|
+
create_with_msgOk : function(_msg, b_ok= true) {
|
|
366
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
367
|
+
ack : {
|
|
368
|
+
"ok" : b_ok,
|
|
369
|
+
},
|
|
370
|
+
});
|
|
371
|
+
},
|
|
372
|
+
create_with_msgExpired : function(_msg) {
|
|
373
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
374
|
+
ack : {
|
|
375
|
+
"ok" : false,
|
|
376
|
+
"expired" : true
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
},
|
|
380
|
+
validate : function(json) {
|
|
381
|
+
if (! JSON_Msg_msg.validate(json))
|
|
382
|
+
return false;
|
|
383
|
+
const b_valid= schema_MsgAck_msg(json);
|
|
384
|
+
if (!b_valid)
|
|
385
|
+
LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
|
|
386
|
+
return b_valid;
|
|
387
|
+
}
|
|
862
388
|
}
|
|
389
|
+
})();
|
|
863
390
|
|
|
864
|
-
|
|
865
|
-
|
|
391
|
+
jsonMsg_cjs.JSON_Msg = JSON_Msg;
|
|
392
|
+
jsonMsg_cjs.JSON_MsgAck_msg = JSON_MsgAck_msg;
|
|
393
|
+
jsonMsg_cjs.JSON_Msg_msg = JSON_Msg_msg;
|
|
394
|
+
return jsonMsg_cjs;
|
|
395
|
+
}
|
|
866
396
|
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
397
|
+
var jsonMsg_cjsExports = requireJsonMsg_cjs();
|
|
398
|
+
|
|
399
|
+
//-------------------------------------------------------------------------------------------------
|
|
400
|
+
|
|
401
|
+
let LOGR_$1= LOGR.get_instance();
|
|
402
|
+
const log_$1= LOGR_$1.log;
|
|
403
|
+
|
|
404
|
+
const l_$1= {
|
|
405
|
+
EVENTS : 0b1 << 0,
|
|
406
|
+
TIMEOUTS : 0b1 << 1,
|
|
407
|
+
};
|
|
408
|
+
LOGR_$1.labels= l_$1;
|
|
409
|
+
// LOGR_.toggled= {
|
|
410
|
+
// EVENTS : 1
|
|
411
|
+
// }
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
//-------------------------------------------------------------------------------------------------
|
|
415
|
+
|
|
416
|
+
class EventLog {
|
|
417
|
+
constructor() {
|
|
418
|
+
this.arr_events = []; // In-memory event store
|
|
419
|
+
this.map_index_id = new Map(); // Map of id -> array of indices into arr_events; arr_events[idx]
|
|
420
|
+
this.map_timeouts = new Map(); // Store timeout references: idx -> timeout_id
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
addEvent(type, id, data, timeout_ms = null, timeout_callback = null) {
|
|
424
|
+
const evt = {
|
|
425
|
+
type,
|
|
426
|
+
dt: Date.now(),
|
|
427
|
+
id,
|
|
428
|
+
data,
|
|
429
|
+
timeout_id: null // Will store timeout reference if set
|
|
430
|
+
};
|
|
431
|
+
const idx = this.arr_events.push(evt) - 1; // More efficient than .length before push
|
|
432
|
+
|
|
433
|
+
let arr_indices = this.map_index_id.get(id);
|
|
434
|
+
if (! arr_indices) {
|
|
435
|
+
arr_indices = [];
|
|
436
|
+
this.map_index_id.set(id, arr_indices);
|
|
437
|
+
}
|
|
438
|
+
arr_indices.push(idx);
|
|
439
|
+
|
|
440
|
+
// Set timeout if specified
|
|
441
|
+
if (timeout_ms && timeout_callback) {
|
|
442
|
+
evt.timeout_id = setTimeout(() => {
|
|
443
|
+
if (this.map_timeouts.delete(idx)) {
|
|
444
|
+
evt.timeout_id = null;
|
|
445
|
+
timeout_callback(evt);
|
|
446
|
+
log_$1(l_$1.EVENTS, `Event DELETED[${idx}]: ${type}`, evt);
|
|
447
|
+
}
|
|
448
|
+
}, timeout_ms);
|
|
449
|
+
|
|
450
|
+
this.map_timeouts.set(idx, evt.timeout_id);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
log_$1(l_$1.EVENTS, `Event added[${idx}] @${Date(evt.dt).toISOString()}: ${type}`, evt);
|
|
454
|
+
return { idx, evt };
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
cancelTimeout(idx) {
|
|
458
|
+
const timeout_id = this.map_timeouts.get(idx);
|
|
459
|
+
if (timeout_id) {
|
|
460
|
+
clearTimeout(timeout_id);
|
|
461
|
+
this.map_timeouts.delete(idx);
|
|
462
|
+
if (this.arr_events[idx])
|
|
463
|
+
this.arr_events[idx].timeout_id = null;
|
|
464
|
+
return true;
|
|
465
|
+
}
|
|
466
|
+
return false;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
cancelAllTimeouts() {
|
|
470
|
+
for (const [idx, timeout_id] of this.map_timeouts) {
|
|
471
|
+
clearTimeout(timeout_id);
|
|
472
|
+
if (this.arr_events[idx])
|
|
473
|
+
this.arr_events[idx].timeout_id = null;
|
|
474
|
+
}
|
|
475
|
+
this.map_timeouts.clear();
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
getEventById(id, lookback_ms = null) {
|
|
479
|
+
const arr_indices = this.map_index_id.get(id);
|
|
480
|
+
if (!arr_indices?.length) return [];
|
|
481
|
+
|
|
482
|
+
const result = [];
|
|
483
|
+
const cutoff = lookback_ms !== null ? Date.now() - lookback_ms : -Infinity;
|
|
484
|
+
|
|
485
|
+
// Single loop with early cutoff
|
|
486
|
+
for (let i = arr_indices.length - 1; i >= 0; i--) {
|
|
487
|
+
const idx = arr_indices[i];
|
|
488
|
+
const evt = this.arr_events[idx];
|
|
489
|
+
if (evt.dt < cutoff)
|
|
490
|
+
break;
|
|
491
|
+
result.push({ idx, evt });
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return result; // Newest to oldest order
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// getRecentEvents(id, lookbackMs) {
|
|
498
|
+
// const cutoff = Date.now() - lookbackMs;
|
|
499
|
+
// return this.events.filter(e => e.id === id && e.dt >= cutoff);
|
|
500
|
+
// }
|
|
501
|
+
|
|
502
|
+
getEvents(lookback_ms, predicate) {
|
|
503
|
+
const cutoff = Date.now() - lookback_ms;
|
|
504
|
+
const result = [];
|
|
505
|
+
|
|
506
|
+
// Iterate backwards from the end
|
|
507
|
+
for (let idx = this.arr_events.length - 1; idx >= 0; idx--) {
|
|
508
|
+
const evt = this.arr_events[idx];
|
|
509
|
+
if (evt.dt < cutoff) {
|
|
510
|
+
break; // Stop once we pass the cutoff
|
|
511
|
+
}
|
|
512
|
+
if (predicate(evt)) {
|
|
513
|
+
result.push({ idx, evt });
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
return result; // Newest to oldest order
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// setLogr(obj_logr) {
|
|
521
|
+
// this.logr.toggled = obj_logr;
|
|
522
|
+
// }
|
|
523
|
+
|
|
524
|
+
clear() {
|
|
525
|
+
this.cancelAllTimeouts(); // Clear all timeouts before clearing events
|
|
526
|
+
this.arr_events.length = 0;
|
|
527
|
+
this.map_index_id.clear();
|
|
528
|
+
}
|
|
529
|
+
}
|
|
880
530
|
|
|
531
|
+
const k_TTL= 20000;
|
|
532
|
+
|
|
533
|
+
//-------------------------------------------------------------------------------------------------
|
|
534
|
+
|
|
535
|
+
// LOGR
|
|
536
|
+
// - l_LL and l_RR should accept 0b1 << 0 style shift
|
|
537
|
+
// - function to debug output bit values of labels instead of binary
|
|
538
|
+
// - rollup example in READMNE
|
|
539
|
+
|
|
540
|
+
const LOGR_= LOGR.get_instance();
|
|
541
|
+
const log_= LOGR_.log;
|
|
542
|
+
|
|
543
|
+
const l_msgs_ = l_array_(['PROTOCOL_STATE','RECVER','SENDER']); // b0, b1, b2
|
|
544
|
+
|
|
545
|
+
Object.assign(l_$1, l_array_(['EVENTS','TIMEOUTS'], 0b1 << 3)); // b3, b4
|
|
546
|
+
|
|
547
|
+
const l_= l_concat_( l_msgs_, l_concat_(l_$1, l_array_(['HANDLERS','DROPS','VALIDATION'])) );
|
|
548
|
+
LOGR_.labels= l_;
|
|
549
|
+
|
|
550
|
+
//-------------------------------------------------------------------------------------------------
|
|
551
|
+
|
|
552
|
+
let map_Context_= new Map();
|
|
553
|
+
|
|
554
|
+
function get_ctx(key) {
|
|
555
|
+
return map_Context_.get(key);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
//-------------------------------------------------------------------------------------------------
|
|
559
|
+
|
|
560
|
+
class Config {
|
|
561
|
+
constructor() {
|
|
562
|
+
// Initialize callbacks as no-op functions
|
|
563
|
+
this._callback_msgWarp = () => { console.assert(false, 'callback_msgWarp uninitialized'); };
|
|
564
|
+
this._callback_msgEngage = () => { console.assert(false, 'callback_msgEngage uninitialized'); };
|
|
565
|
+
this._callback_msgAckWarp = () => { console.assert(false, 'callback_msgAckWarp uninitialized'); };
|
|
566
|
+
this._callback_msgAckEngage = () => { console.assert(false, 'callback_msgAckEngage uninitialized'); };
|
|
567
|
+
this._callback_msg_sent_expired = () => { console.assert(false, '_callback_msg_sent_expired uninitialized'); };
|
|
568
|
+
this._callback_msgOptions = () => { console.assert(false, 'callback_msgOptions uninitialized'); };
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
receiver(callback_msgWarp, callback_msgEngage, callback_msg_sent_expired) {
|
|
572
|
+
this._callback_msgWarp= callback_msgWarp;
|
|
573
|
+
this._callback_msgEngage= callback_msgEngage;
|
|
574
|
+
this._callback_msg_sent_expired= callback_msg_sent_expired;
|
|
575
|
+
}
|
|
576
|
+
sender(callback_msgAckWarp, callback_msgAckEngage, callback_msg_sent_expired ) {
|
|
577
|
+
this._callback_msgAckWarp= callback_msgAckWarp;
|
|
578
|
+
this._callback_msgAckEngage= callback_msgAckEngage;
|
|
579
|
+
this._callback_msg_sent_expired= callback_msg_sent_expired;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
set callback_msgOptions(callback) {
|
|
583
|
+
this._callback_msgOptions= callback;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// var config = (function() {
|
|
588
|
+
// let _options;
|
|
589
|
+
|
|
590
|
+
// return {
|
|
591
|
+
// get callback_Abort() { return _options.callback_Abort; },
|
|
592
|
+
|
|
593
|
+
// set options(obj) {
|
|
594
|
+
// _options= obj;
|
|
595
|
+
// if (_options.logr) // && options_.logr[ __name(l_) ] )
|
|
596
|
+
// LOGR_.toggled= _options.logr
|
|
597
|
+
// }
|
|
598
|
+
// }
|
|
599
|
+
// })();
|
|
600
|
+
|
|
601
|
+
//-------------------------------------------------------------------------------------------------
|
|
602
|
+
|
|
603
|
+
let k_VERSION= "0.8";
|
|
604
|
+
|
|
605
|
+
function t_ttl_rtt_(ttl_msec, rtt_msec) {
|
|
606
|
+
if (ttl_msec === undefined)
|
|
607
|
+
return undefined;
|
|
608
|
+
if (rtt_msec === undefined)
|
|
609
|
+
return ttl_msec *2;
|
|
610
|
+
|
|
611
|
+
console.assert(rtt_msec <= ttl_msec *2, `t_ttl_rtt_: INVALID rtt_msec ttl_msec[${ttl_msec}] rtt_msec[${rtt_msec}]`);
|
|
612
|
+
return (ttl_msec *2) + rtt_msec;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
var JSON_MsgOptions = (function() {
|
|
616
|
+
return {
|
|
617
|
+
create_with_msg_options : function(_msg, options) {
|
|
618
|
+
return jsonMsg_cjsExports.JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
619
|
+
options: options,
|
|
620
|
+
"2PhW": k_VERSION
|
|
621
|
+
});
|
|
622
|
+
},
|
|
623
|
+
validate : function(json) {
|
|
624
|
+
if (! jsonMsg_cjsExports.JSON_Msg_msg.validate(json))
|
|
625
|
+
return false;
|
|
626
|
+
if (! schema_MsgOptions(json)) {
|
|
627
|
+
log_(l_.VALIDATION, () => ['JSON_MsgOptions: schema_MsgOptions_: ', schema_MsgOptions.errors]);
|
|
628
|
+
return false;
|
|
629
|
+
}
|
|
630
|
+
return true;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
})();
|
|
634
|
+
|
|
635
|
+
var JSON_MsgWarp = (function() {
|
|
636
|
+
return {
|
|
637
|
+
create_with_id_user_hyperport : function(json_warp_id, json_user, json_hyperport, nr_ttl_msec) {
|
|
638
|
+
return jsonMsg_cjsExports.JSON_Msg.merge({
|
|
639
|
+
warp : {
|
|
640
|
+
id : json_warp_id,
|
|
641
|
+
user : json_user,
|
|
642
|
+
hyperport : json_hyperport,
|
|
643
|
+
ttl_msec : nr_ttl_msec
|
|
644
|
+
},
|
|
645
|
+
"2PhW": k_VERSION
|
|
646
|
+
});
|
|
647
|
+
},
|
|
648
|
+
compare : function(json_L, json_R) {
|
|
649
|
+
if (json_L.dt !== json_R.dt)
|
|
650
|
+
return false;
|
|
651
|
+
if (json_L.warp.id !== json_R.warp.id)
|
|
652
|
+
return false;
|
|
653
|
+
if (json_L.referer[0] !== json_R.referer[0])
|
|
654
|
+
return false;
|
|
655
|
+
if (json_L.referer[1] !== json_R.referer[1])
|
|
656
|
+
return false;
|
|
657
|
+
return true;
|
|
658
|
+
},
|
|
659
|
+
validate : function(json) {
|
|
660
|
+
if (! jsonMsg_cjsExports.JSON_Msg.validate(json))
|
|
661
|
+
return false;
|
|
662
|
+
if (! schema_MsgWarp(json)) {
|
|
663
|
+
log_(l_.VALIDATION, () => ['JSON_MsgWarp: schema_MsgWarp_: ', schema_MsgWarp.errors]);
|
|
664
|
+
return false;
|
|
665
|
+
}
|
|
666
|
+
return true;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
})();
|
|
670
|
+
|
|
671
|
+
var JSON_MsgEngage = (function() {
|
|
672
|
+
return {
|
|
673
|
+
create_with_MsgWarp : function(json_msgWarp) {
|
|
674
|
+
console.assert(json_msgWarp.ack === undefined, "Engage with a Warp, not an Ack");
|
|
675
|
+
return jsonMsg_cjsExports.JSON_Msg_msg.create_with_msg_merge(json_msgWarp, {
|
|
676
|
+
engage : {
|
|
677
|
+
"ok" : true,
|
|
678
|
+
"rtt_msec" : Date.now() - Date.parse(json_msgWarp.dt)
|
|
679
|
+
},
|
|
680
|
+
"2PhW": k_VERSION
|
|
681
|
+
});
|
|
682
|
+
},
|
|
683
|
+
validate : function(json) {
|
|
684
|
+
if (! jsonMsg_cjsExports.JSON_Msg_msg.validate(json))
|
|
685
|
+
return false;
|
|
686
|
+
if (! JSON_MsgWarp.validate(json._msg))
|
|
687
|
+
return false;
|
|
688
|
+
if (! schema_MsgEngage(json)) {
|
|
689
|
+
log_(l_.VALIDATION, () => ['JSON_MsgEngage: schema_MsgEngage_: ', schema_MsgEngage.errors]);
|
|
690
|
+
return false;
|
|
691
|
+
}
|
|
692
|
+
return true;
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
})();
|
|
696
|
+
|
|
697
|
+
var JSON_MsgAckWarp = (function() {
|
|
698
|
+
return {
|
|
699
|
+
// createWith_msgAck_msg : function(json_msgAck, _msg) {
|
|
700
|
+
// const json_msgAckWarp= structuredClone(json_msgAck);
|
|
701
|
+
// json_msgAckWarp._msg= _msg;
|
|
702
|
+
// return json_msgAckWarp;
|
|
703
|
+
// }
|
|
704
|
+
create_with_msgOk : function(json_msgWarp, b_ok= true) {
|
|
705
|
+
const json_msgAck_msg= jsonMsg_cjsExports.JSON_MsgAck_msg.create_with_msgOk(json_msgWarp, b_ok);
|
|
706
|
+
json_msgAck_msg["2PhW"]= k_VERSION;
|
|
707
|
+
return json_msgAck_msg;
|
|
708
|
+
},
|
|
709
|
+
create_with_msgExpired : function( json_msgWarp ) {
|
|
710
|
+
const json_msgAck_msg= jsonMsg_cjsExports.JSON_MsgAck_msg.create_with_msgExpired( json_msgWarp );
|
|
711
|
+
json_msgAck_msg["2PhW"]= k_VERSION;
|
|
712
|
+
return json_msgAck_msg;
|
|
713
|
+
},
|
|
714
|
+
validate : function(json) {
|
|
715
|
+
if (! jsonMsg_cjsExports.JSON_MsgAck_msg.validate(json))
|
|
716
|
+
return false;
|
|
717
|
+
if (! JSON_MsgWarp.validate(json._msg))
|
|
718
|
+
return false;
|
|
719
|
+
return true;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
})();
|
|
723
|
+
|
|
724
|
+
var JSON_MsgAckEngage = (function() {
|
|
725
|
+
return {
|
|
726
|
+
// createWith_msgAck_msg : function(json_msgAck, _msg) {
|
|
727
|
+
// const json_msgAckEngage= structuredClone(json_msgAck);
|
|
728
|
+
// json_msgAckEngage._msg= _msg;
|
|
729
|
+
// return json_msgAckEngage;
|
|
730
|
+
// }
|
|
731
|
+
create_with_msgOk : function(json_msgEngage, b_ok= true) {
|
|
732
|
+
const json_msgAck_msg= jsonMsg_cjsExports.JSON_MsgAck_msg.create_with_msgOk(json_msgEngage, b_ok);
|
|
733
|
+
json_msgAck_msg["2PhW"]= k_VERSION;
|
|
734
|
+
return json_msgAck_msg;
|
|
735
|
+
},
|
|
736
|
+
create_with_msgExpired : function( json_msgEngage ) {
|
|
737
|
+
const json_msgAck_msg= jsonMsg_cjsExports.JSON_MsgAck_msg.create_with_msgExpired( json_msgEngage );
|
|
738
|
+
json_msgAck_msg["2PhW"]= k_VERSION;
|
|
739
|
+
return json_msgAck_msg;
|
|
740
|
+
},
|
|
741
|
+
validate : function(json) {
|
|
742
|
+
if (! jsonMsg_cjsExports.JSON_MsgAck_msg.validate(json))
|
|
743
|
+
return false;
|
|
744
|
+
if (! JSON_MsgEngage.validate(json._msg))
|
|
745
|
+
return false;
|
|
746
|
+
return true;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
})();
|
|
750
|
+
|
|
751
|
+
//-------------------------------------------------------------------------------------------------
|
|
752
|
+
// States
|
|
753
|
+
|
|
754
|
+
class StateState {
|
|
755
|
+
constructor(idx_event, event) {
|
|
756
|
+
this._protocol_parent = null;
|
|
757
|
+
this._idx_event = idx_event;
|
|
758
|
+
this._event = event;
|
|
759
|
+
}
|
|
760
|
+
get protocol() {
|
|
761
|
+
return this._protocol_parent;
|
|
762
|
+
}
|
|
763
|
+
set protocol(parent) {
|
|
764
|
+
this._protocol_parent = parent;
|
|
765
|
+
}
|
|
766
|
+
get idx_event() {
|
|
767
|
+
return this._idx_event;
|
|
768
|
+
}
|
|
769
|
+
get event() {
|
|
770
|
+
return this._event;
|
|
771
|
+
}
|
|
772
|
+
get json_msg() {
|
|
773
|
+
return this._event.data;
|
|
774
|
+
}
|
|
775
|
+
changeState(state_new) {
|
|
776
|
+
if (this.canChangeState(state_new))
|
|
777
|
+
this.protocol.state = state_new;
|
|
778
|
+
else
|
|
779
|
+
this.protocol.state = new StateINVALID(state_new._idx_event, state_new._event);
|
|
780
|
+
}
|
|
781
|
+
canChangeState(_state_new) {
|
|
782
|
+
throw new Error("Subclass must implement canChangeState()");
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
class StateAckEngage extends StateState {
|
|
787
|
+
constructor(idx_event, event) { super(idx_event, event); }
|
|
788
|
+
get evt_AckEngage() { return this.event }
|
|
789
|
+
get json_msgAckEngage() { return this.json_msg }
|
|
790
|
+
canChangeState(_state_new) {
|
|
791
|
+
return false;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
class StateEngage extends StateState {
|
|
796
|
+
constructor(idx_event, event) { super(idx_event, event); }
|
|
797
|
+
get evt_Engage() { return this.event }
|
|
798
|
+
get json_msgEngage() { return this.json_msg }
|
|
799
|
+
canChangeState(state_new) {
|
|
800
|
+
if (false === state_new instanceof StateAckEngage)
|
|
801
|
+
return false;
|
|
802
|
+
|
|
803
|
+
// The Engage gets to live for THE WARP'S creation time + TTL*2 + RTT
|
|
804
|
+
const json_msgWarp= this.json_msgEngage._msg;
|
|
805
|
+
const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec, this.json_msgEngage.engage.rtt_msec);
|
|
806
|
+
log_(l_.PROTOCOL_STATE, () => [`StateEngage: evt_AckEngage.dt@${state_new.evt_AckEngage.dt}, msgWarp.dt@${Date.parse(json_msgWarp.dt)} + ${timeout_ms}`]);
|
|
807
|
+
if (state_new.evt_AckEngage.dt >= Date.parse(json_msgWarp.dt) + timeout_ms)
|
|
808
|
+
return false;
|
|
809
|
+
|
|
810
|
+
return true;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
class StateAckWarp extends StateState {
|
|
815
|
+
constructor(idx_event, event) { super(idx_event, event); }
|
|
816
|
+
get evt_AckWarp() { return this.event }
|
|
817
|
+
get json_msgAckWarp() { return this.json_msg }
|
|
818
|
+
canChangeState(state_new) {
|
|
819
|
+
if (false === state_new instanceof StateEngage)
|
|
820
|
+
return false;
|
|
821
|
+
|
|
822
|
+
if (! this.json_msgAckWarp.ack.ok)
|
|
823
|
+
return false;
|
|
824
|
+
|
|
825
|
+
if (this.json_msgAckWarp._msg.warp.instantaneous)
|
|
826
|
+
return false;
|
|
827
|
+
|
|
828
|
+
// The Ack gets to live for THE ACK'S creation time + TTL*2 + STT
|
|
829
|
+
// +STT, so lines up with the Engage timeout
|
|
830
|
+
// - Engage: T0 +2TTL +RTT; RTT= STT+STT
|
|
831
|
+
// - AckWarp: TAckWarp +2TLL +STT; TAckWarp= T0 +STT;
|
|
832
|
+
// - STT: Date of the Ack minus date of the Warp.
|
|
833
|
+
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);
|
|
834
|
+
log_(l_.PROTOCOL_STATE, () => [`StateAckWarp: evt_Engage.dt@${state_new.evt_Engage.dt}, msgAckWarp.dt@${Date.parse(this.json_msgAckWarp.dt)} + ${timeout_ms}`]);
|
|
835
|
+
if (state_new.evt_Engage.dt >= Date.parse(this.json_msgAckWarp.dt) + timeout_ms)
|
|
836
|
+
return false;
|
|
837
|
+
|
|
838
|
+
return true;
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
class StateWarp extends StateState {
|
|
843
|
+
constructor(idx_event, event) { super(idx_event, event); }
|
|
844
|
+
get evt_Warp() { return this.event }
|
|
845
|
+
get json_msgWarp() { return this.json_msg }
|
|
846
|
+
canChangeState(state_new) {
|
|
847
|
+
if (false === state_new instanceof StateAckWarp)
|
|
848
|
+
return false;
|
|
849
|
+
|
|
850
|
+
// The Warp gets to live for THE WARP'S creation time + TTL*2
|
|
851
|
+
const timeout_ms= t_ttl_rtt_(this.json_msgWarp.warp.ttl_msec);
|
|
852
|
+
log_(l_.PROTOCOL_STATE, () => [`StateWarp: evt_AckWarp.dt@${state_new.evt_AckWarp.dt}, msgWarp.dt@${Date.parse(this.json_msgWarp.dt)} + ${timeout_ms}`]);
|
|
853
|
+
if (state_new.evt_AckWarp.dt >= Date.parse(this.json_msgWarp.dt) + timeout_ms)
|
|
854
|
+
return false;
|
|
855
|
+
|
|
856
|
+
return true;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
class StateINVALID extends StateState {
|
|
861
|
+
constructor(idx_event, event) { super(idx_event, event); }
|
|
862
|
+
canChangeState(_state_new) {
|
|
863
|
+
// INVALID can't transition to any state
|
|
864
|
+
return false;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
class StateInitialState extends StateState {
|
|
869
|
+
constructor() { super(null, null); }
|
|
870
|
+
canChangeState(state_new) {
|
|
871
|
+
if (false === state_new instanceof StateWarp)
|
|
872
|
+
return false;
|
|
873
|
+
|
|
874
|
+
// The NEWLY created Warp only gets to live for its creation time + TTL
|
|
875
|
+
const timeout_ms= state_new.json_msgWarp.warp.ttl_msec;
|
|
876
|
+
log_(l_.PROTOCOL_STATE, () => [`StateInitialState: evt_Warp.dt@${state_new.evt_Warp.dt}, msgWarp.dt@${Date.parse(state_new.json_msgWarp.dt)} + ${timeout_ms}`]);
|
|
877
|
+
if (state_new.evt_Warp.dt >= Date.parse(state_new.json_msgWarp.dt) + timeout_ms)
|
|
878
|
+
return false;
|
|
879
|
+
|
|
880
|
+
return true;
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
//----
|
|
885
|
+
|
|
886
|
+
class ProtocolStateState {
|
|
887
|
+
constructor(state_initial) {
|
|
888
|
+
this.state = state_initial || new StateInitialState();
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
set state(state_new) {
|
|
892
|
+
if (! (state_new instanceof StateState)) {
|
|
893
|
+
throw new Error("initState must be an instance of StateState or its subclasses");
|
|
894
|
+
}
|
|
895
|
+
this._state = state_new;
|
|
896
|
+
if (state_new && state_new.protocol !== this) {
|
|
897
|
+
state_new.protocol = this;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
get state() {
|
|
901
|
+
return this._state;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
canChangeState(state_new) {
|
|
905
|
+
return this._state.canChangeState(state_new);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
//-------------------------------------------------------------------------------------------------
|
|
910
|
+
|
|
911
|
+
class TwoPhW_EventLog {
|
|
912
|
+
constructor(eventLog) {
|
|
913
|
+
this._eventLog = eventLog;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
_warp(json_msg) {
|
|
917
|
+
const b_Ack = json_msg.ack !== undefined;
|
|
918
|
+
const json = b_Ack ? json_msg["_msg"] : json_msg;
|
|
919
|
+
|
|
920
|
+
if (json.warp) {
|
|
921
|
+
return {
|
|
922
|
+
str_type: `2PhW[JSON_Msg${b_Ack ? 'Ack' : ''}Warp]`,
|
|
923
|
+
json_msgWarp: json
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
if (json.engage) {
|
|
927
|
+
return {
|
|
928
|
+
str_type: `2PhW[JSON_Msg${b_Ack ? 'Ack' : ''}Engage]`,
|
|
929
|
+
json_msgWarp: json["_msg"]
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
return null; // Explicit default for invalid cases
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
_addEvent(json_msg, timeout_ms, timeout_callback) {
|
|
936
|
+
const { str_type, json_msgWarp }= this._warp(json_msg);
|
|
937
|
+
const { idx }= this._eventLog.addEvent(str_type, json_msgWarp.warp.id, json_msg, timeout_ms, timeout_callback);
|
|
938
|
+
log_(l_.PROTOCOL_STATE, () => [`_addEvent: [${idx}] timeout:${Date.now()}+${timeout_ms} `, json_msg]);
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
_dropEvent(json_msg) {
|
|
942
|
+
const { json_msgWarp }= this._warp(json_msg);
|
|
943
|
+
this._eventLog.addEvent('2PhW[DROPPED!]', json_msgWarp.warp.id, json_msg);
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
_cancelTimeout(idx_event) {
|
|
947
|
+
log_(l_.PROTOCOL_STATE, () => [`_cancelTimeout: [${idx_event}] timeout cancelled`]);
|
|
948
|
+
this._eventLog.cancelTimeout(idx_event);
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
get_protocolState(json_msgWarp) {
|
|
952
|
+
const protocolState = new ProtocolStateState();
|
|
953
|
+
|
|
954
|
+
const arr_evts= this._eventLog.getEventById(json_msgWarp.warp.id, k_TTL);
|
|
955
|
+
log_(l_.EVENTS, () => ['get_protocolState: arr_evts: ', arr_evts]);
|
|
956
|
+
|
|
957
|
+
for (let i = arr_evts.length - 1; i >= 0; i--) {
|
|
958
|
+
const { idx, evt } = arr_evts[i];
|
|
959
|
+
|
|
960
|
+
if (evt.type === '2PhW[DROPPED!]')
|
|
961
|
+
continue;
|
|
962
|
+
|
|
963
|
+
const json_msg= evt.data;
|
|
964
|
+
if (! JSON_MsgWarp.compare(json_msgWarp, this._warp(json_msg).json_msgWarp) )
|
|
965
|
+
protocolState.state.changeState( new StateINVALID(idx, evt) );
|
|
966
|
+
|
|
967
|
+
switch(evt.type) {
|
|
968
|
+
case '2PhW[JSON_MsgWarp]' :
|
|
969
|
+
protocolState.state.changeState( new StateWarp(idx, evt) );
|
|
970
|
+
break;
|
|
971
|
+
case '2PhW[JSON_MsgAckWarp]' :
|
|
972
|
+
protocolState.state.changeState( new StateAckWarp(idx, evt) );
|
|
973
|
+
break;
|
|
974
|
+
case '2PhW[JSON_MsgEngage]' :
|
|
975
|
+
protocolState.state.changeState( new StateEngage(idx, evt) );
|
|
976
|
+
break;
|
|
977
|
+
case '2PhW[JSON_MsgAckEngage]' :
|
|
978
|
+
protocolState.state.changeState( new StateAckEngage(idx, evt) );
|
|
979
|
+
break;
|
|
980
|
+
default :
|
|
981
|
+
protocolState.state.changeState( new StateINVALID(idx, evt) );
|
|
982
|
+
break;
|
|
983
|
+
}
|
|
984
|
+
log_(l_.PROTOCOL_STATE, () => ['get_protocolState: type,state: ',
|
|
985
|
+
evt.type,
|
|
986
|
+
(protocolState.state.json_msg.ack?.ok === false) ? '--NACK--' : '',
|
|
987
|
+
protocolState.state
|
|
988
|
+
]);
|
|
989
|
+
}
|
|
990
|
+
return protocolState;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
class TwoPhW {
|
|
996
|
+
constructor(eventLog) {
|
|
997
|
+
this._2PhW_eventLog = new TwoPhW_EventLog(eventLog);
|
|
998
|
+
this._config= new Config();
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
get config() {
|
|
1002
|
+
return this._config;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
get eventLog() {
|
|
1006
|
+
return this._2PhW_eventLog;
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
//-------------------------------------------------------------------------------------------------
|
|
1010
|
+
|
|
1011
|
+
_handler_msgOptions(cfg, msg, json_msgOptions)
|
|
1012
|
+
{
|
|
1013
|
+
log_(l_.HANDLERS, () => ['handler_msgOptions_: ...']);
|
|
1014
|
+
|
|
1015
|
+
if (! validate_Options_delay_(json_msgOptions.options))
|
|
1016
|
+
return false;
|
|
1017
|
+
|
|
1018
|
+
if (JSON_MsgWarp.validate(json_msgOptions._msg)) {
|
|
1019
|
+
const json_msgWarp= json_msgOptions._msg;
|
|
1020
|
+
|
|
1021
|
+
const ctx= get_ctx(json_msgWarp.id);
|
|
1022
|
+
if (!ctx )
|
|
1023
|
+
return false;
|
|
1024
|
+
|
|
1025
|
+
json_msgOptions.options.ttl_msec_incr[0];
|
|
1026
|
+
const ttl_incr= json_msgOptions.options.ttl_msec_incr[1];
|
|
1027
|
+
|
|
1028
|
+
//TODO: double check ttl_msec is the same as current accummulated ttl for idempotence.
|
|
1029
|
+
|
|
1030
|
+
log_(l_.HANDLERS, () => ['handler_msgOptions_: ', json_msgOptions]);
|
|
1031
|
+
|
|
1032
|
+
ctx.protocol.delayTimeout(ttl_incr);
|
|
1033
|
+
|
|
1034
|
+
if (cfg._callback_msgOptions)
|
|
1035
|
+
return cfg._callback_msgOptions(msg, json_msgOptions);
|
|
1036
|
+
|
|
1037
|
+
return true;
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
return false;
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
//-------------------------------------------------------------------------------------------------
|
|
1044
|
+
// sender Source (src)
|
|
1045
|
+
|
|
1046
|
+
register_msgWarp(json_msgWarp)
|
|
1047
|
+
{
|
|
1048
|
+
log_(l_.SENDER, () => ['register_msgWarp: ', json_msgWarp]);
|
|
1049
|
+
|
|
1050
|
+
const protocolState= this.eventLog.get_protocolState(json_msgWarp);
|
|
1051
|
+
const b_changeState= protocolState.canChangeState( new StateWarp(null, {
|
|
1052
|
+
dt: Date.now(),
|
|
1053
|
+
data: json_msgWarp
|
|
1054
|
+
}) );
|
|
1055
|
+
|
|
1056
|
+
log_(l_.HANDLERS | l_.SENDER, () => ['register_msgWarp: protocolState.canChangeState ', b_changeState]);
|
|
1057
|
+
if (! b_changeState)
|
|
1058
|
+
return false;
|
|
1059
|
+
|
|
1060
|
+
const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec);
|
|
1061
|
+
const elapsed_ms= Date.now() - Date.parse(json_msgWarp.dt);
|
|
1062
|
+
|
|
1063
|
+
this.eventLog._addEvent(json_msgWarp, timeout_ms-elapsed_ms, () => {
|
|
1064
|
+
this.config._callback_msg_sent_expired(json_msgWarp);
|
|
1065
|
+
});
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
_handler_msgAckWarp(msg, json_msgAckWarp)
|
|
1069
|
+
{
|
|
1070
|
+
log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckWarp: ', json_msgAckWarp]);
|
|
1071
|
+
|
|
1072
|
+
const json_msgWarp= json_msgAckWarp._msg;
|
|
1073
|
+
|
|
1074
|
+
const protocolState= this.eventLog.get_protocolState(json_msgWarp);
|
|
1075
|
+
const b_changeState= protocolState.canChangeState( new StateAckWarp(null, {
|
|
1076
|
+
dt: Date.now(),
|
|
1077
|
+
data: json_msgAckWarp
|
|
1078
|
+
}) );
|
|
1079
|
+
|
|
1080
|
+
log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckWarp: protocolState.canChangeState ', b_changeState]);
|
|
1081
|
+
if (! b_changeState)
|
|
1082
|
+
return false;
|
|
1083
|
+
|
|
1084
|
+
this.eventLog._cancelTimeout(protocolState.state.idx_event);
|
|
1085
|
+
this.eventLog._addEvent(json_msgAckWarp);
|
|
1086
|
+
if (! this.config._callback_msgAckWarp(msg, json_msgAckWarp))
|
|
1087
|
+
return false;
|
|
1088
|
+
|
|
1089
|
+
return true;
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
register_msgEngage(json_msgEngage)
|
|
1093
|
+
{
|
|
1094
|
+
log_(l_.SENDER, () => ['register_msgEngage: ', json_msgEngage]);
|
|
1095
|
+
|
|
1096
|
+
const json_msgWarp= json_msgEngage._msg;
|
|
1097
|
+
|
|
1098
|
+
const protocolState= this.eventLog.get_protocolState(json_msgWarp);
|
|
1099
|
+
const b_changeState= protocolState.canChangeState( new StateEngage(null, {
|
|
1100
|
+
dt: Date.now(),
|
|
1101
|
+
data : json_msgEngage
|
|
1102
|
+
}) );
|
|
1103
|
+
|
|
1104
|
+
log_(l_.HANDLERS | l_.SENDER, () => ['register_msgEngage: protocolState.canChangeState ', b_changeState]);
|
|
1105
|
+
if (! b_changeState)
|
|
1106
|
+
return false;
|
|
1107
|
+
|
|
1108
|
+
const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec, json_msgEngage.engage.rtt_msec);
|
|
1109
|
+
const elapsed_ms= Date.now() - Date.parse(json_msgWarp.dt);
|
|
1110
|
+
|
|
1111
|
+
this.eventLog._addEvent(json_msgEngage, timeout_ms-elapsed_ms, () => {
|
|
1112
|
+
this.config._callback_msg_sent_expired(json_msgEngage);
|
|
1113
|
+
});
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
_handler_msgAckEngage(msg, json_msgAckEngage)
|
|
1117
|
+
{
|
|
1118
|
+
log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckEngage: ', json_msgAckEngage]);
|
|
1119
|
+
|
|
1120
|
+
const json_msgWarp= json_msgAckEngage._msg._msg;
|
|
1121
|
+
|
|
1122
|
+
const protocolState= this.eventLog.get_protocolState(json_msgWarp);
|
|
1123
|
+
const b_changeState= protocolState.canChangeState( new StateAckEngage(null, {
|
|
1124
|
+
dt: Date.now(),
|
|
1125
|
+
data : json_msgAckEngage
|
|
1126
|
+
}) );
|
|
1127
|
+
|
|
1128
|
+
log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckEngage_: protocolState.canChangeState ', b_changeState]);
|
|
1129
|
+
if (! b_changeState)
|
|
1130
|
+
return false;
|
|
1131
|
+
|
|
1132
|
+
this.eventLog._cancelTimeout(protocolState.state.idx_event);
|
|
1133
|
+
this.eventLog._addEvent(json_msgAckEngage);
|
|
1134
|
+
if (! this.config._callback_msgAckEngage(msg, json_msgAckEngage))
|
|
1135
|
+
return false;
|
|
1136
|
+
|
|
1137
|
+
return true;
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
//-------------------------------------------------------------------------------------------------
|
|
1141
|
+
// receiver Destination (dst)
|
|
1142
|
+
|
|
1143
|
+
_handler_msgWarp(msg, json_msgWarp)
|
|
1144
|
+
{
|
|
1145
|
+
log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgWarp: ', json_msgWarp]);
|
|
1146
|
+
|
|
1147
|
+
const protocolState= this.eventLog.get_protocolState(json_msgWarp);
|
|
1148
|
+
const b_changeState= protocolState.canChangeState( new StateWarp(null, {
|
|
1149
|
+
dt: Date.now(),
|
|
1150
|
+
data: json_msgWarp
|
|
1151
|
+
}) );
|
|
1152
|
+
|
|
1153
|
+
log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgWarp: protocolState.canChangeState: ', b_changeState]);
|
|
1154
|
+
if (! b_changeState)
|
|
1155
|
+
return false;
|
|
1156
|
+
|
|
1157
|
+
this.eventLog._addEvent(json_msgWarp);
|
|
1158
|
+
if (! this.config._callback_msgWarp(msg, json_msgWarp))
|
|
1159
|
+
return false;
|
|
1160
|
+
|
|
1161
|
+
return true;
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
register_msgAckWarp(json_msgAckWarp)
|
|
1165
|
+
{
|
|
1166
|
+
log_(l_.RECVER, () => ['register_msgAckWarp: ', json_msgAckWarp]);
|
|
1167
|
+
|
|
1168
|
+
const json_msgWarp= json_msgAckWarp._msg;
|
|
1169
|
+
|
|
1170
|
+
const protocolState= this.eventLog.get_protocolState(json_msgWarp);
|
|
1171
|
+
const b_changeState= protocolState.canChangeState( new StateAckWarp(null, {
|
|
1172
|
+
dt: Date.now(),
|
|
1173
|
+
data: json_msgAckWarp
|
|
1174
|
+
}) );
|
|
1175
|
+
|
|
1176
|
+
log_(l_.HANDLERS | l_.RECVER, () => ['register_msgAckWarp: protocolState.canChangeState: ', b_changeState]);
|
|
1177
|
+
if (! b_changeState)
|
|
1178
|
+
return false;
|
|
1179
|
+
|
|
1180
|
+
const elapsed_ms= Date.now() - Date.parse(json_msgAckWarp.dt);
|
|
1181
|
+
const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec) + Date.parse(json_msgAckWarp.dt) - Date.parse(json_msgWarp.dt);
|
|
1182
|
+
|
|
1183
|
+
this.eventLog._addEvent(json_msgAckWarp, timeout_ms-elapsed_ms, () => {
|
|
1184
|
+
this.config._callback_msg_sent_expired(json_msgAckWarp);
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
_handler_msgEngage(msg, json_msgEngage)
|
|
1189
|
+
{
|
|
1190
|
+
log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgEngage: ', json_msgEngage]);
|
|
1191
|
+
|
|
1192
|
+
const json_msgWarp= json_msgEngage["_msg"];
|
|
1193
|
+
|
|
1194
|
+
const protocolState= this.eventLog.get_protocolState(json_msgWarp);
|
|
1195
|
+
const b_changeState= protocolState.canChangeState( new StateEngage(null, {
|
|
1196
|
+
dt: Date.now(),
|
|
1197
|
+
data: json_msgEngage
|
|
1198
|
+
}) );
|
|
1199
|
+
|
|
1200
|
+
log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgEngage: protocolState.canChangeState: ', b_changeState]);
|
|
1201
|
+
if (! b_changeState)
|
|
1202
|
+
return false;
|
|
1203
|
+
|
|
1204
|
+
this.eventLog._cancelTimeout(protocolState.state.idx_event);
|
|
1205
|
+
this.eventLog._addEvent(json_msgEngage);
|
|
1206
|
+
if (! this.config._callback_msgEngage(msg, json_msgEngage))
|
|
1207
|
+
return false;
|
|
1208
|
+
|
|
1209
|
+
return true;
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
register_msgAckEngage(json_msgAckEngage)
|
|
1213
|
+
{
|
|
1214
|
+
log_(l_.RECVER, () => ['register_msgAckEngage: ', json_msgAckEngage]);
|
|
1215
|
+
|
|
1216
|
+
const json_msgWarp= json_msgAckEngage._msg._msg;
|
|
1217
|
+
|
|
1218
|
+
const protocolState= this.eventLog.get_protocolState(json_msgWarp);
|
|
1219
|
+
const b_changeState= protocolState.canChangeState( new StateAckEngage(null, {
|
|
1220
|
+
dt: Date.now(),
|
|
1221
|
+
data: json_msgAckEngage
|
|
1222
|
+
}) );
|
|
1223
|
+
|
|
1224
|
+
log_(l_.HANDLERS | l_.RECVER, () => ['register_msgAckEngage: protocolState.canChangeState: ', b_changeState]);
|
|
1225
|
+
if (! b_changeState)
|
|
1226
|
+
return false;
|
|
1227
|
+
|
|
1228
|
+
this.eventLog._addEvent(json_msgAckEngage);
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
//-------------------------------------------------------------------------------------------------
|
|
1232
|
+
|
|
1233
|
+
handler_msg_json(msg, json_msg)
|
|
1234
|
+
{
|
|
1235
|
+
log_(l_.HANDLERS, () => ['handler_msg_json: ', json_msg]);
|
|
1236
|
+
|
|
1237
|
+
// ADR: I could parse json_Msg_msg first and then MsgAck or MsgOptions, but
|
|
1238
|
+
// doing so means super classes get parsed multiple times for no reason
|
|
1239
|
+
|
|
1240
|
+
// if (JSON_MsgOptions.validate(json_msg) && this.handler_msgOptions_(this._config, msg, json_msg))
|
|
1241
|
+
// return true;
|
|
1242
|
+
|
|
1243
|
+
if (JSON_MsgWarp.validate(json_msg) && this._handler_msgWarp(msg, json_msg))
|
|
1244
|
+
return true;
|
|
1245
|
+
|
|
1246
|
+
if (JSON_MsgEngage.validate(json_msg) && this._handler_msgEngage(msg, json_msg))
|
|
1247
|
+
return true;
|
|
1248
|
+
|
|
1249
|
+
if (JSON_MsgAckWarp.validate(json_msg) && this._handler_msgAckWarp(msg, json_msg))
|
|
1250
|
+
return true;
|
|
1251
|
+
|
|
1252
|
+
if (JSON_MsgAckEngage.validate(json_msg) && this._handler_msgAckEngage(msg, json_msg))
|
|
1253
|
+
return true;
|
|
1254
|
+
|
|
1255
|
+
// https://attacomsian.com/blog/javascript-iterate-objects
|
|
1256
|
+
// Object.keys(json_msg).forEach((key, index) => {
|
|
1257
|
+
// if (key == 'dt')
|
|
1258
|
+
// return;
|
|
1259
|
+
// if (key == 'referer')
|
|
1260
|
+
// return;
|
|
1261
|
+
// console.log(`${key}: ${json_msg[key]}`);
|
|
1262
|
+
// });
|
|
1263
|
+
|
|
1264
|
+
this.eventLog._dropEvent(json_msg);
|
|
1265
|
+
log_(l_.DROPS, () => ["TwoPhW: handler_msg: DROP! ", msg]);
|
|
1266
|
+
return false;
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
handler_msg_str(msg, str_msg)
|
|
1270
|
+
{
|
|
1271
|
+
log_(l_.HANDLERS, () => ['handler_msg_str: ['+ str_msg +']']);
|
|
1272
|
+
|
|
1273
|
+
// ... test strings here
|
|
1274
|
+
|
|
1275
|
+
let json_msg;
|
|
1276
|
+
try {
|
|
1277
|
+
json_msg= JSON.parse(str_msg);
|
|
1278
|
+
}
|
|
1279
|
+
catch (err) {
|
|
1280
|
+
return false;
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
return this.handler_msg_json(msg, json_msg);
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
handler_msg(msg)
|
|
1287
|
+
{
|
|
1288
|
+
log_(l_.HANDLERS, () => ['handler_msg: ', msg]);
|
|
1289
|
+
|
|
1290
|
+
if (typeof (msg) === 'string' && this.handler_msg_str(msg, msg))
|
|
1291
|
+
return true;
|
|
1292
|
+
|
|
1293
|
+
if (typeof (msg) === 'object' && this.handler_msg_json(msg, msg))
|
|
1294
|
+
return true;
|
|
1295
|
+
|
|
1296
|
+
return false
|
|
1297
|
+
}
|
|
1298
|
+
|
|
881
1299
|
}
|
|
882
1300
|
|
|
1301
|
+
exports.EventLog = EventLog;
|
|
883
1302
|
exports.JSON_MsgAckEngage = JSON_MsgAckEngage;
|
|
884
1303
|
exports.JSON_MsgAckWarp = JSON_MsgAckWarp;
|
|
885
1304
|
exports.JSON_MsgEngage = JSON_MsgEngage;
|
|
886
1305
|
exports.JSON_MsgOptions = JSON_MsgOptions;
|
|
887
1306
|
exports.JSON_MsgWarp = JSON_MsgWarp;
|
|
1307
|
+
exports.StateAckEngage = StateAckEngage;
|
|
1308
|
+
exports.StateAckWarp = StateAckWarp;
|
|
1309
|
+
exports.StateEngage = StateEngage;
|
|
1310
|
+
exports.StateINVALID = StateINVALID;
|
|
1311
|
+
exports.StateInitialState = StateInitialState;
|
|
1312
|
+
exports.StateWarp = StateWarp;
|
|
888
1313
|
exports.TwoPhW = TwoPhW;
|
|
889
1314
|
exports.l = l_;
|
|
1315
|
+
exports.t_ttl_rtt = t_ttl_rtt_;
|