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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
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("1" !== data["2PhW"]){validate19.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"definitions_2PhW#/definitions/2PhW/const",keyword:"const",params:{allowedValue: "1"},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("1" !== data["2PhW"]){validate20.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"definitions_2PhW#/definitions/2PhW/const",keyword:"const",params:{allowedValue: "1"},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("1" !== data["2PhW"]){validate21.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"definitions_2PhW#/definitions/2PhW/const",keyword:"const",params:{allowedValue: "1"},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};
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#/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("1" !== data["2PhW"]){validate19.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"definitions#/definitions/2PhW/const",keyword:"const",params:{allowedValue: "1"},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("1" !== data["2PhW"]){validate20.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"definitions#/definitions/2PhW/const",keyword:"const",params:{allowedValue: "1"},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};
4
4
 
5
5
  // console.log('OUT', __name({variableName}) );
6
6
 
@@ -515,7 +515,7 @@ class EventLog {
515
515
  this.map_timeouts.clear();
516
516
  }
517
517
 
518
- getEventById(id, lookback_ms = null) {
518
+ getEventsById(id, lookback_ms = null) {
519
519
  const arr_indices = this.map_index_id.get(id);
520
520
  if (!arr_indices?.length) return [];
521
521
 
@@ -568,772 +568,660 @@ class EventLog {
568
568
  }
569
569
  }
570
570
 
571
- const k_TTL= 20000;
572
-
573
- //-------------------------------------------------------------------------------------------------
574
-
575
-
576
- const LOGR_= LOGR.get_instance();
577
- const log_= LOGR_.log;
578
-
579
- Object.assign(jsonMsg_cjsExports.l, l_LL_(jsonMsg_cjsExports.l, 2)); // VALIDATION : 4
580
-
581
- const l_twoPhW_ = l_array_(['HANDLERS','DROPS', 'PROTOCOL_STATE','RECVER','SENDER']);
582
- const l_ = l_concat_(l_$1, l_concat_(jsonMsg_cjsExports.l, l_twoPhW_) );
583
-
584
- LOGR_.labels= l_;
585
-
586
- //-------------------------------------------------------------------------------------------------
587
-
588
- let map_Context_= new Map();
589
-
590
- function get_ctx(key) {
591
- return map_Context_.get(key);
592
- }
593
-
594
- //-------------------------------------------------------------------------------------------------
595
-
596
- class Config {
597
- constructor() {
598
- // Initialize callbacks as no-op functions
599
- this._callback_msgWarp = () => { console.assert(false, 'callback_msgWarp uninitialized'); };
600
- this._callback_msgEngage = () => { console.assert(false, 'callback_msgEngage uninitialized'); };
601
- this._callback_msgAckWarp = () => { console.assert(false, 'callback_msgAckWarp uninitialized'); };
602
- this._callback_msgAckEngage = () => { console.assert(false, 'callback_msgAckEngage uninitialized'); };
603
- this._callback_msg_sent_expired = () => { console.assert(false, '_callback_msg_sent_expired uninitialized'); };
604
- this._callback_msgOptions = () => { console.assert(false, 'callback_msgOptions uninitialized'); };
605
- }
606
-
607
- receiver(callback_msgWarp, callback_msgEngage, callback_msg_sent_expired) {
608
- this._callback_msgWarp= callback_msgWarp;
609
- this._callback_msgEngage= callback_msgEngage;
610
- this._callback_msg_sent_expired= callback_msg_sent_expired;
611
- }
612
- sender(callback_msgAckWarp, callback_msgAckEngage, callback_msg_sent_expired ) {
613
- this._callback_msgAckWarp= callback_msgAckWarp;
614
- this._callback_msgAckEngage= callback_msgAckEngage;
615
- this._callback_msg_sent_expired= callback_msg_sent_expired;
616
- }
617
-
618
- set callback_msgOptions(callback) {
619
- this._callback_msgOptions= callback;
620
- }
621
- }
622
-
623
- // var config = (function() {
624
- // let _options;
625
-
626
- // return {
627
- // get callback_Abort() { return _options.callback_Abort; },
628
-
629
- // set options(obj) {
630
- // _options= obj;
631
- // if (_options.logr) // && options_.logr[ __name(l_) ] )
632
- // LOGR_.toggled= _options.logr
633
- // }
634
- // }
635
- // })();
636
-
637
- //-------------------------------------------------------------------------------------------------
638
-
639
- let k_VERSION= "1";
640
-
641
- function t_ttl_rtt_(ttl_msec, rtt_msec) {
642
- if (ttl_msec === undefined)
643
- return undefined;
644
- if (rtt_msec === undefined)
645
- return ttl_msec *2;
646
-
647
- console.assert(rtt_msec <= ttl_msec *2, `t_ttl_rtt_: INVALID rtt_msec ttl_msec[${ttl_msec}] rtt_msec[${rtt_msec}]`);
648
- return (ttl_msec *2) + rtt_msec;
649
- }
650
-
651
- var JSON_MsgOptions = (function() {
652
- return {
653
- create_with_msg_options : function(_msg, options) {
654
- return jsonMsg_cjsExports.JSON_Msg_msg.create_with_msg_merge(_msg, {
655
- options: options,
656
- "2PhW": k_VERSION
657
- });
658
- },
659
- validate : function(json) {
660
- if (! jsonMsg_cjsExports.JSON_Msg_msg.validate(json))
661
- return false;
662
- if (! schema_MsgOptions(json)) {
663
- log_(l_.VALIDATION, () => ['JSON_MsgOptions: schema_MsgOptions_: ', schema_MsgOptions.errors]);
664
- return false;
665
- }
666
- return true;
667
- }
668
- }
669
- })();
670
-
671
- var JSON_MsgWarp = (function() {
672
- return {
673
- create_with_id_user_hyperport : function(json_warp_id, json_user, json_hyperport, nr_ttl_msec) {
674
- return jsonMsg_cjsExports.JSON_Msg.merge({
675
- warp : {
676
- id : json_warp_id,
677
- user : json_user,
678
- hyperport : json_hyperport,
679
- ttl_msec : nr_ttl_msec
680
- },
681
- "2PhW": k_VERSION
682
- });
683
- },
684
- compare : function(json_L, json_R) {
685
- if (json_L.dt !== json_R.dt)
686
- return false;
687
- if (json_L.warp.id !== json_R.warp.id)
688
- return false;
689
- if (json_L.referer[0] !== json_R.referer[0])
690
- return false;
691
- if (json_L.referer[1] !== json_R.referer[1])
692
- return false;
693
- return true;
694
- },
695
- validate : function(json) {
696
- if (! jsonMsg_cjsExports.JSON_Msg.validate(json))
697
- return false;
698
- if (! schema_MsgWarp(json)) {
699
- log_(l_.VALIDATION, () => ['JSON_MsgWarp: schema_MsgWarp_: ', schema_MsgWarp.errors]);
700
- return false;
701
- }
702
- return true;
703
- }
704
- }
705
- })();
706
-
707
- var JSON_MsgEngage = (function() {
708
- return {
709
- create_with_MsgWarp : function(json_msgWarp) {
710
- console.assert(json_msgWarp.ack === undefined, "Engage with a Warp, not an Ack");
711
- return jsonMsg_cjsExports.JSON_Msg_msg.create_with_msg_merge(json_msgWarp, {
712
- engage : {
713
- "ok" : true,
714
- "rtt_msec" : Date.now() - Date.parse(json_msgWarp.dt)
715
- },
716
- "2PhW": k_VERSION
717
- });
718
- },
719
- validate : function(json) {
720
- if (! jsonMsg_cjsExports.JSON_Msg_msg.validate(json))
721
- return false;
722
- if (! JSON_MsgWarp.validate(json._msg))
723
- return false;
724
- if (! schema_MsgEngage(json)) {
725
- log_(l_.VALIDATION, () => ['JSON_MsgEngage: schema_MsgEngage_: ', schema_MsgEngage.errors]);
726
- return false;
727
- }
728
- return true;
729
- }
730
- };
731
- })();
732
-
733
- var JSON_MsgAckWarp = (function() {
734
- return {
735
- // createWith_msgAck_msg : function(json_msgAck, _msg) {
736
- // const json_msgAckWarp= structuredClone(json_msgAck);
737
- // json_msgAckWarp._msg= _msg;
738
- // return json_msgAckWarp;
739
- // }
740
- create_with_msgOk : function(json_msgWarp, b_ok= true) {
741
- const json_msgAck_msg= jsonMsg_cjsExports.JSON_MsgAck_msg.create_with_msgOk(json_msgWarp, b_ok);
742
- json_msgAck_msg["2PhW"]= k_VERSION;
743
- return json_msgAck_msg;
744
- },
745
- create_with_msgExpired : function( json_msgWarp ) {
746
- const json_msgAck_msg= jsonMsg_cjsExports.JSON_MsgAck_msg.create_with_msgExpired( json_msgWarp );
747
- json_msgAck_msg["2PhW"]= k_VERSION;
748
- return json_msgAck_msg;
749
- },
750
- validate : function(json) {
751
- if (! jsonMsg_cjsExports.JSON_MsgAck_msg.validate(json))
752
- return false;
753
- if (! JSON_MsgWarp.validate(json._msg))
754
- return false;
755
- return true;
756
- }
757
- }
758
- })();
759
-
760
- var JSON_MsgAckEngage = (function() {
761
- return {
762
- // createWith_msgAck_msg : function(json_msgAck, _msg) {
763
- // const json_msgAckEngage= structuredClone(json_msgAck);
764
- // json_msgAckEngage._msg= _msg;
765
- // return json_msgAckEngage;
766
- // }
767
- create_with_msgOk : function(json_msgEngage, b_ok= true) {
768
- const json_msgAck_msg= jsonMsg_cjsExports.JSON_MsgAck_msg.create_with_msgOk(json_msgEngage, b_ok);
769
- json_msgAck_msg["2PhW"]= k_VERSION;
770
- return json_msgAck_msg;
771
- },
772
- create_with_msgExpired : function( json_msgEngage ) {
773
- const json_msgAck_msg= jsonMsg_cjsExports.JSON_MsgAck_msg.create_with_msgExpired( json_msgEngage );
774
- json_msgAck_msg["2PhW"]= k_VERSION;
775
- return json_msgAck_msg;
776
- },
777
- validate : function(json) {
778
- if (! jsonMsg_cjsExports.JSON_MsgAck_msg.validate(json))
779
- return false;
780
- if (! JSON_MsgEngage.validate(json._msg))
781
- return false;
782
- return true;
783
- }
784
- }
785
- })();
786
-
787
- //-------------------------------------------------------------------------------------------------
788
- // States
789
-
790
- class StateState {
791
- constructor(idx_event, event) {
792
- this._protocol_parent = null;
793
- this._idx_event = idx_event;
794
- this._event = event;
795
- }
796
- get protocol() {
797
- return this._protocol_parent;
798
- }
799
- set protocol(parent) {
800
- this._protocol_parent = parent;
801
- }
802
- get idx_event() {
803
- return this._idx_event;
804
- }
805
- get event() {
806
- return this._event;
807
- }
808
- get json_msg() {
809
- return this._event.data;
810
- }
811
- changeState(state_new) {
812
- if (this.canChangeState(state_new))
813
- this.protocol.state = state_new;
814
- else
815
- this.protocol.state = new StateINVALID(state_new._idx_event, state_new._event);
816
- }
817
- canChangeState(_state_new) {
818
- throw new Error("Subclass must implement canChangeState()");
819
- }
820
- }
821
-
822
- class StateAckEngage extends StateState {
823
- constructor(idx_event, event) { super(idx_event, event); }
824
- get evt_AckEngage() { return this.event }
825
- get json_msgAckEngage() { return this.json_msg }
826
- canChangeState(_state_new) {
827
- return false;
828
- }
829
- }
830
-
831
- class StateEngage extends StateState {
832
- constructor(idx_event, event) { super(idx_event, event); }
833
- get evt_Engage() { return this.event }
834
- get json_msgEngage() { return this.json_msg }
835
- canChangeState(state_new) {
836
- if (false === state_new instanceof StateAckEngage)
837
- return false;
838
-
839
- // The Engage gets to live for THE WARP'S creation time + TTL*2 + RTT
840
- const json_msgWarp= this.json_msgEngage._msg;
841
- const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec, this.json_msgEngage.engage.rtt_msec);
842
- log_(l_.PROTOCOL_STATE, () => [`StateEngage: evt_AckEngage.dt@${state_new.evt_AckEngage.dt}, msgWarp.dt@${Date.parse(json_msgWarp.dt)} + ${timeout_ms}`]);
843
- if (state_new.evt_AckEngage.dt >= Date.parse(json_msgWarp.dt) + timeout_ms)
844
- return false;
845
-
846
- return true;
847
- }
848
- }
849
-
850
- class StateAckWarp extends StateState {
851
- constructor(idx_event, event) { super(idx_event, event); }
852
- get evt_AckWarp() { return this.event }
853
- get json_msgAckWarp() { return this.json_msg }
854
- canChangeState(state_new) {
855
- if (false === state_new instanceof StateEngage)
856
- return false;
857
-
858
- if (! this.json_msgAckWarp.ack.ok)
859
- return false;
860
-
861
- if (this.json_msgAckWarp._msg.warp.instantaneous)
862
- return false;
863
-
864
- // The Ack gets to live for THE ACK'S creation time + TTL*2 + STT
865
- // +STT, so lines up with the Engage timeout
866
- // - Engage: T0 +2TTL +RTT; RTT= STT+STT
867
- // - AckWarp: TAckWarp +2TLL +STT; TAckWarp= T0 +STT;
868
- // - STT: Date of the Ack minus date of the Warp.
869
- 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);
870
- log_(l_.PROTOCOL_STATE, () => [`StateAckWarp: evt_Engage.dt@${state_new.evt_Engage.dt}, msgAckWarp.dt@${Date.parse(this.json_msgAckWarp.dt)} + ${timeout_ms}`]);
871
- if (state_new.evt_Engage.dt >= Date.parse(this.json_msgAckWarp.dt) + timeout_ms)
872
- return false;
873
-
874
- return true;
875
- }
876
- }
877
-
878
- class StateWarp extends StateState {
879
- constructor(idx_event, event) { super(idx_event, event); }
880
- get evt_Warp() { return this.event }
881
- get json_msgWarp() { return this.json_msg }
882
- canChangeState(state_new) {
883
- if (false === state_new instanceof StateAckWarp)
884
- return false;
885
-
886
- // The Warp gets to live for THE WARP'S creation time + TTL*2
887
- const timeout_ms= t_ttl_rtt_(this.json_msgWarp.warp.ttl_msec);
888
- log_(l_.PROTOCOL_STATE, () => [`StateWarp: evt_AckWarp.dt@${state_new.evt_AckWarp.dt}, msgWarp.dt@${Date.parse(this.json_msgWarp.dt)} + ${timeout_ms}`]);
889
- if (state_new.evt_AckWarp.dt >= Date.parse(this.json_msgWarp.dt) + timeout_ms)
890
- return false;
891
-
892
- return true;
893
- }
894
- }
895
-
896
- class StateINVALID extends StateState {
897
- constructor(idx_event, event) { super(idx_event, event); }
898
- canChangeState(_state_new) {
899
- // INVALID can't transition to any state
900
- return false;
901
- }
902
- }
903
-
904
- class StateInitialState extends StateState {
905
- constructor() { super(null, null); }
906
- canChangeState(state_new) {
907
- if (false === state_new instanceof StateWarp)
908
- return false;
909
-
910
- // The NEWLY created Warp only gets to live for its creation time + TTL
911
- const timeout_ms= state_new.json_msgWarp.warp.ttl_msec;
912
- log_(l_.PROTOCOL_STATE, () => [`StateInitialState: evt_Warp.dt@${state_new.evt_Warp.dt}, msgWarp.dt@${Date.parse(state_new.json_msgWarp.dt)} + ${timeout_ms}`]);
913
- if (state_new.evt_Warp.dt >= Date.parse(state_new.json_msgWarp.dt) + timeout_ms)
914
- return false;
915
-
916
- return true;
917
- }
918
- }
919
-
920
- //----
921
-
922
- class ProtocolStateState {
923
- constructor(state_initial) {
924
- this.state = state_initial || new StateInitialState();
925
- }
926
-
927
- set state(state_new) {
928
- if (! (state_new instanceof StateState)) {
929
- throw new Error("initState must be an instance of StateState or its subclasses");
930
- }
931
- this._state = state_new;
932
- if (state_new && state_new.protocol !== this) {
933
- state_new.protocol = this;
934
- }
935
- }
936
- get state() {
937
- return this._state;
938
- }
939
-
940
- canChangeState(state_new) {
941
- return this._state.canChangeState(state_new);
942
- }
943
- }
944
-
945
- //-------------------------------------------------------------------------------------------------
946
-
947
- class TwoPhW_EventLog {
948
- constructor(eventLog) {
949
- this._eventLog = eventLog;
950
- }
951
-
952
- _warp(json_msg) {
953
- const b_Ack = json_msg.ack !== undefined;
954
- const json = b_Ack ? json_msg["_msg"] : json_msg;
955
-
956
- if (json.warp) {
957
- return {
958
- str_type: `2PhW[JSON_Msg${b_Ack ? 'Ack' : ''}Warp]`,
959
- json_msgWarp: json
960
- };
961
- }
962
- if (json.engage) {
963
- return {
964
- str_type: `2PhW[JSON_Msg${b_Ack ? 'Ack' : ''}Engage]`,
965
- json_msgWarp: json["_msg"]
966
- };
967
- }
968
- return { str_type: null, json_msgWarp: null }; // Explicit default for invalid cases
969
- }
970
-
971
- _addEvent(json_msg, timeout_ms, timeout_callback) {
972
- const { str_type, json_msgWarp }= this._warp(json_msg);
973
- const { idx }= this._eventLog.addEvent(str_type, json_msgWarp.warp.id, json_msg, timeout_ms, timeout_callback);
974
- log_(l_.PROTOCOL_STATE, () => [`_addEvent: [${idx}] timeout:${Date.now()}+${timeout_ms} `, json_msg]);
975
- }
976
-
977
- _dropEvent(json_msg) {
978
- const { json_msgWarp }= this._warp(json_msg);
979
- if (! json_msgWarp)
980
- return;
981
- this._eventLog.addEvent('2PhW[DROPPED!]', json_msgWarp.warp.id, json_msg);
982
- }
983
-
984
- _cancelTimeout(idx_event) {
985
- log_(l_.PROTOCOL_STATE, () => [`_cancelTimeout: [${idx_event}] timeout cancelled`]);
986
- this._eventLog.cancelTimeout(idx_event);
987
- }
988
-
989
- get_protocolState(json_msgWarp) {
990
- const protocolState = new ProtocolStateState();
991
-
992
- const arr_evts= this._eventLog.getEventById(json_msgWarp.warp.id, k_TTL);
993
- log_(l_.EVENTS, () => ['get_protocolState: arr_evts: ', arr_evts]);
994
-
995
- for (let i = arr_evts.length - 1; i >= 0; i--) {
996
- const { idx, evt } = arr_evts[i];
997
-
998
- if (evt.type === '2PhW[DROPPED!]')
999
- continue;
1000
-
1001
- const json_msg= evt.data;
1002
- if (! JSON_MsgWarp.compare(json_msgWarp, this._warp(json_msg).json_msgWarp) )
1003
- protocolState.state.changeState( new StateINVALID(idx, evt) );
1004
-
1005
- switch(evt.type) {
1006
- case '2PhW[JSON_MsgWarp]' :
1007
- protocolState.state.changeState( new StateWarp(idx, evt) );
1008
- break;
1009
- case '2PhW[JSON_MsgAckWarp]' :
1010
- protocolState.state.changeState( new StateAckWarp(idx, evt) );
1011
- break;
1012
- case '2PhW[JSON_MsgEngage]' :
1013
- protocolState.state.changeState( new StateEngage(idx, evt) );
1014
- break;
1015
- case '2PhW[JSON_MsgAckEngage]' :
1016
- protocolState.state.changeState( new StateAckEngage(idx, evt) );
1017
- break;
1018
- default :
1019
- protocolState.state.changeState( new StateINVALID(idx, evt) );
1020
- break;
1021
- }
1022
- log_(l_.PROTOCOL_STATE, () => ['get_protocolState: type,state: ',
1023
- evt.type,
1024
- (protocolState.state.json_msg.ack?.ok === false) ? '--NACK--' : '',
1025
- protocolState.state
1026
- ]);
1027
- }
1028
- return protocolState;
1029
- }
1030
-
1031
- }
1032
-
1033
- class TwoPhW {
1034
- constructor(eventLog) {
1035
- this._2PhW_eventLog = new TwoPhW_EventLog(eventLog);
1036
- this._config= new Config();
1037
- }
1038
-
1039
- get config() {
1040
- return this._config;
1041
- }
1042
-
1043
- get eventLog() {
1044
- return this._2PhW_eventLog;
1045
- }
1046
-
1047
- //-------------------------------------------------------------------------------------------------
1048
-
1049
- _handler_msgOptions(cfg, msg, json_msgOptions)
1050
- {
1051
- log_(l_.HANDLERS, () => ['handler_msgOptions_: ...']);
1052
-
1053
- if (! validate_Options_delay_(json_msgOptions.options))
1054
- return false;
1055
-
1056
- if (JSON_MsgWarp.validate(json_msgOptions._msg)) {
1057
- const json_msgWarp= json_msgOptions._msg;
1058
-
1059
- const ctx= get_ctx(json_msgWarp.id);
1060
- if (!ctx )
1061
- return false;
1062
-
1063
- json_msgOptions.options.ttl_msec_incr[0];
1064
- const ttl_incr= json_msgOptions.options.ttl_msec_incr[1];
1065
-
1066
- //TODO: double check ttl_msec is the same as current accummulated ttl for idempotence.
1067
-
1068
- log_(l_.HANDLERS, () => ['handler_msgOptions_: ', json_msgOptions]);
1069
-
1070
- ctx.protocol.delayTimeout(ttl_incr);
1071
-
1072
- if (cfg._callback_msgOptions)
1073
- return cfg._callback_msgOptions(msg, json_msgOptions);
1074
-
1075
- return true;
1076
- }
1077
-
1078
- return false;
1079
- }
1080
-
1081
- //-------------------------------------------------------------------------------------------------
1082
- // sender Source (src)
1083
-
1084
- register_msgWarp(json_msgWarp)
1085
- {
1086
- log_(l_.SENDER, () => ['register_msgWarp: ', json_msgWarp]);
1087
-
1088
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
1089
- const b_changeState= protocolState.canChangeState( new StateWarp(null, {
1090
- dt: Date.now(),
1091
- data: json_msgWarp
1092
- }) );
1093
-
1094
- log_(l_.HANDLERS | l_.SENDER, () => ['register_msgWarp: protocolState.canChangeState ', b_changeState]);
1095
- if (! b_changeState)
1096
- return false;
1097
-
1098
- const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec);
1099
- const elapsed_ms= Date.now() - Date.parse(json_msgWarp.dt);
1100
-
1101
- this.eventLog._addEvent(json_msgWarp, timeout_ms-elapsed_ms, ({ _idx, _evt}) => {
1102
- this.config._callback_msg_sent_expired(json_msgWarp);
1103
- });
1104
- }
1105
-
1106
- _handler_msgAckWarp(msg, json_msgAckWarp)
1107
- {
1108
- log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckWarp: ', json_msgAckWarp]);
1109
-
1110
- const json_msgWarp= json_msgAckWarp._msg;
1111
-
1112
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
1113
- const b_changeState= protocolState.canChangeState( new StateAckWarp(null, {
1114
- dt: Date.now(),
1115
- data: json_msgAckWarp
1116
- }) );
1117
-
1118
- log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckWarp: protocolState.canChangeState ', b_changeState]);
1119
- if (! b_changeState)
1120
- return false;
1121
-
1122
- this.eventLog._cancelTimeout(protocolState.state.idx_event);
1123
- this.eventLog._addEvent(json_msgAckWarp);
1124
- if (! this.config._callback_msgAckWarp(msg, json_msgAckWarp))
1125
- return false;
1126
-
1127
- return true;
1128
- }
1129
-
1130
- register_msgEngage(json_msgEngage)
1131
- {
1132
- log_(l_.SENDER, () => ['register_msgEngage: ', json_msgEngage]);
1133
-
1134
- const json_msgWarp= json_msgEngage._msg;
1135
-
1136
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
1137
- const b_changeState= protocolState.canChangeState( new StateEngage(null, {
1138
- dt: Date.now(),
1139
- data : json_msgEngage
1140
- }) );
1141
-
1142
- log_(l_.HANDLERS | l_.SENDER, () => ['register_msgEngage: protocolState.canChangeState ', b_changeState]);
1143
- if (! b_changeState)
1144
- return false;
1145
-
1146
- const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec, json_msgEngage.engage.rtt_msec);
1147
- const elapsed_ms= Date.now() - Date.parse(json_msgWarp.dt);
1148
-
1149
- this.eventLog._addEvent(json_msgEngage, timeout_ms-elapsed_ms, () => {
1150
- this.config._callback_msg_sent_expired(json_msgEngage);
1151
- });
1152
- }
1153
-
1154
- _handler_msgAckEngage(msg, json_msgAckEngage)
1155
- {
1156
- log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckEngage: ', json_msgAckEngage]);
1157
-
1158
- const json_msgWarp= json_msgAckEngage._msg._msg;
1159
-
1160
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
1161
- const b_changeState= protocolState.canChangeState( new StateAckEngage(null, {
1162
- dt: Date.now(),
1163
- data : json_msgAckEngage
1164
- }) );
1165
-
1166
- log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckEngage_: protocolState.canChangeState ', b_changeState]);
1167
- if (! b_changeState)
1168
- return false;
1169
-
1170
- this.eventLog._cancelTimeout(protocolState.state.idx_event);
1171
- this.eventLog._addEvent(json_msgAckEngage);
1172
- if (! this.config._callback_msgAckEngage(msg, json_msgAckEngage))
1173
- return false;
1174
-
1175
- return true;
1176
- }
1177
-
1178
- //-------------------------------------------------------------------------------------------------
1179
- // receiver Destination (dst)
1180
-
1181
- _handler_msgWarp(msg, json_msgWarp)
1182
- {
1183
- log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgWarp: ', json_msgWarp]);
1184
-
1185
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
1186
- const b_changeState= protocolState.canChangeState( new StateWarp(null, {
1187
- dt: Date.now(),
1188
- data: json_msgWarp
1189
- }) );
1190
-
1191
- log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgWarp: protocolState.canChangeState: ', b_changeState]);
1192
- if (! b_changeState)
1193
- return false;
1194
-
1195
- this.eventLog._addEvent(json_msgWarp);
1196
- if (! this.config._callback_msgWarp(msg, json_msgWarp))
1197
- return false;
1198
-
1199
- return true;
1200
- }
1201
-
1202
- register_msgAckWarp(json_msgAckWarp)
1203
- {
1204
- log_(l_.RECVER, () => ['register_msgAckWarp: ', json_msgAckWarp]);
1205
-
1206
- const json_msgWarp= json_msgAckWarp._msg;
1207
-
1208
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
1209
- const b_changeState= protocolState.canChangeState( new StateAckWarp(null, {
1210
- dt: Date.now(),
1211
- data: json_msgAckWarp
1212
- }) );
1213
-
1214
- log_(l_.HANDLERS | l_.RECVER, () => ['register_msgAckWarp: protocolState.canChangeState: ', b_changeState]);
1215
- if (! b_changeState)
1216
- return false;
1217
-
1218
- const elapsed_ms= Date.now() - Date.parse(json_msgAckWarp.dt);
1219
- const timeout_ms= t_ttl_rtt_(json_msgWarp.warp.ttl_msec) + Date.parse(json_msgAckWarp.dt) - Date.parse(json_msgWarp.dt);
1220
-
1221
- this.eventLog._addEvent(json_msgAckWarp, timeout_ms-elapsed_ms, ({ _idx, _evt}) => {
1222
- this.config._callback_msg_sent_expired(json_msgAckWarp);
1223
- });
1224
- }
1225
-
1226
- _handler_msgEngage(msg, json_msgEngage)
1227
- {
1228
- log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgEngage: ', json_msgEngage]);
1229
-
1230
- const json_msgWarp= json_msgEngage["_msg"];
1231
-
1232
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
1233
- const b_changeState= protocolState.canChangeState( new StateEngage(null, {
1234
- dt: Date.now(),
1235
- data: json_msgEngage
1236
- }) );
1237
-
1238
- log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgEngage: protocolState.canChangeState: ', b_changeState]);
1239
- if (! b_changeState)
1240
- return false;
1241
-
1242
- this.eventLog._cancelTimeout(protocolState.state.idx_event);
1243
- this.eventLog._addEvent(json_msgEngage);
1244
- if (! this.config._callback_msgEngage(msg, json_msgEngage))
1245
- return false;
1246
-
1247
- return true;
1248
- }
1249
-
1250
- register_msgAckEngage(json_msgAckEngage)
1251
- {
1252
- log_(l_.RECVER, () => ['register_msgAckEngage: ', json_msgAckEngage]);
1253
-
1254
- const json_msgWarp= json_msgAckEngage._msg._msg;
1255
-
1256
- const protocolState= this.eventLog.get_protocolState(json_msgWarp);
1257
- const b_changeState= protocolState.canChangeState( new StateAckEngage(null, {
1258
- dt: Date.now(),
1259
- data: json_msgAckEngage
1260
- }) );
1261
-
1262
- log_(l_.HANDLERS | l_.RECVER, () => ['register_msgAckEngage: protocolState.canChangeState: ', b_changeState]);
1263
- if (! b_changeState)
1264
- return false;
1265
-
1266
- this.eventLog._addEvent(json_msgAckEngage);
1267
- }
1268
-
1269
- //-------------------------------------------------------------------------------------------------
1270
-
1271
- handler_msg_json(msg, json_msg)
1272
- {
1273
- log_(l_.HANDLERS, () => ['handler_msg_json: ', json_msg]);
1274
-
1275
- // ADR: I could parse json_Msg_msg first and then MsgAck or MsgOptions, but
1276
- // doing so means super classes get parsed multiple times for no reason
1277
-
1278
- // if (JSON_MsgOptions.validate(json_msg) && this.handler_msgOptions_(this._config, msg, json_msg))
1279
- // return true;
1280
-
1281
- if (JSON_MsgWarp.validate(json_msg) && this._handler_msgWarp(msg, json_msg))
1282
- return true;
1283
-
1284
- if (JSON_MsgEngage.validate(json_msg) && this._handler_msgEngage(msg, json_msg))
1285
- return true;
1286
-
1287
- if (JSON_MsgAckWarp.validate(json_msg) && this._handler_msgAckWarp(msg, json_msg))
1288
- return true;
1289
-
1290
- if (JSON_MsgAckEngage.validate(json_msg) && this._handler_msgAckEngage(msg, json_msg))
1291
- return true;
1292
-
1293
- // https://attacomsian.com/blog/javascript-iterate-objects
1294
- // Object.keys(json_msg).forEach((key, index) => {
1295
- // if (key == 'dt')
1296
- // return;
1297
- // if (key == 'referer')
1298
- // return;
1299
- // console.log(`${key}: ${json_msg[key]}`);
1300
- // });
1301
-
1302
- this.eventLog._dropEvent(json_msg);
1303
- log_(l_.DROPS, () => ["TwoPhW: handler_msg: DROP! ", msg]);
1304
- return false;
1305
- }
1306
-
1307
- handler_msg_str(msg, str_msg)
1308
- {
1309
- log_(l_.HANDLERS, () => ['handler_msg_str: ['+ str_msg +']']);
1310
-
1311
- // ... test strings here
1312
-
1313
- let json_msg;
1314
- try {
1315
- json_msg= JSON.parse(str_msg);
1316
- }
1317
- catch (err) {
1318
- return false;
1319
- }
1320
-
1321
- return this.handler_msg_json(msg, json_msg);
1322
- }
1323
-
1324
- handler_msg(msg)
1325
- {
1326
- log_(l_.HANDLERS, () => ['handler_msg: ', msg]);
1327
-
1328
- if (typeof (msg) === 'string' && this.handler_msg_str(msg, msg))
1329
- return true;
1330
-
1331
- if (typeof (msg) === 'object' && this.handler_msg_json(msg, msg))
1332
- return true;
1333
-
1334
- return false
1335
- }
1336
-
571
+ // interface Event extends Required<EventMinimal> {
572
+ // type : string;
573
+ // id : string | number;
574
+ // timeout_id : NodeJS.Timeout | null;
575
+ // };
576
+ const k_TTL = 20000;
577
+ //-------------------------------------------------------------------------------------------------
578
+ const LOGR_ = LOGR.get_instance();
579
+ const log_ = LOGR_.log;
580
+ Object.assign(jsonMsg_cjsExports.l, l_LL_(jsonMsg_cjsExports.l, 2)); // VALIDATION : 4
581
+ const l_twoPhW_ = l_array_(['HANDLERS', 'DROPS', 'PROTOCOL_STATE', 'RECVER', 'SENDER']);
582
+ const l_ = l_concat_(l_$1, l_concat_(jsonMsg_cjsExports.l, l_twoPhW_));
583
+ LOGR_.labels = l_;
584
+ //-------------------------------------------------------------------------------------------------
585
+ class Config {
586
+ _callback_msgWarp;
587
+ _callback_msgEngage;
588
+ _callback_msgAckWarp;
589
+ _callback_msgAckEngage;
590
+ _callback_msg_sent_expired;
591
+ _callback_msgOptions;
592
+ constructor() {
593
+ // Initialize callbacks as no-op functions
594
+ this._callback_msgWarp = (_msg, _json_msg) => {
595
+ console.assert(false, 'callback_msgWarp uninitialized');
596
+ return false;
597
+ };
598
+ this._callback_msgEngage = (_msg, _json_msg) => {
599
+ console.assert(false, 'callback_msgEngage uninitialized');
600
+ return false;
601
+ };
602
+ this._callback_msgAckWarp = (_msg, _json_msg) => {
603
+ console.assert(false, 'callback_msgAckWarp uninitialized');
604
+ return false;
605
+ };
606
+ this._callback_msgAckEngage = (_msg, _json_msg) => {
607
+ console.assert(false, 'callback_msgAckEngage uninitialized');
608
+ return false;
609
+ };
610
+ this._callback_msg_sent_expired = (_msg, _json_msg) => {
611
+ console.assert(false, '_callback_msg_sent_expired uninitialized');
612
+ };
613
+ this._callback_msgOptions = () => {
614
+ console.assert(false, 'callback_msgOptions uninitialized');
615
+ };
616
+ }
617
+ receiver(callback_msgWarp, callback_msgEngage, callback_msg_sent_expired) {
618
+ this._callback_msgWarp = callback_msgWarp;
619
+ this._callback_msgEngage = callback_msgEngage;
620
+ this._callback_msg_sent_expired = callback_msg_sent_expired;
621
+ }
622
+ sender(callback_msgAckWarp, callback_msgAckEngage, callback_msg_sent_expired) {
623
+ this._callback_msgAckWarp = callback_msgAckWarp;
624
+ this._callback_msgAckEngage = callback_msgAckEngage;
625
+ this._callback_msg_sent_expired = callback_msg_sent_expired;
626
+ }
627
+ set callback_msgOptions(callback) {
628
+ this._callback_msgOptions = callback;
629
+ }
630
+ }
631
+ // var config = (function() {
632
+ // let _options;
633
+ // return {
634
+ // get callback_Abort() { return _options.callback_Abort; },
635
+ // set options(obj) {
636
+ // _options= obj;
637
+ // if (_options.logr) // && options_.logr[ __name(l_) ] )
638
+ // LOGR_.toggled= _options.logr
639
+ // }
640
+ // }
641
+ // })();
642
+ //-------------------------------------------------------------------------------------------------
643
+ let k_VERSION = "1";
644
+ // rtt_msec <= ttl_msec *2 => rtt_msec = ttl_msec *2
645
+ // t_ttl_rtt_(TTL, RTT) = t_ttl_rtt_(TTL, TTL *2) = TTL *2 + TTL *2 = TTL *4 MAX
646
+ function t_ttl_rtt_(ttl_msec, rtt_msec) {
647
+ if (ttl_msec === undefined)
648
+ return undefined;
649
+ if (rtt_msec === undefined)
650
+ return ttl_msec * 2;
651
+ console.assert(rtt_msec <= ttl_msec * 2, `t_ttl_rtt_: INVALID rtt_msec ttl_msec[${ttl_msec}] rtt_msec[${rtt_msec}]`);
652
+ return (ttl_msec * 2) + rtt_msec;
653
+ }
654
+ var JSON_MsgOptions = (function () {
655
+ return {
656
+ create_with_msg_options: function (_msg, options) {
657
+ return jsonMsg_cjsExports.JSON_Msg_msg.create_with_msg_merge(_msg, {
658
+ options: options,
659
+ "2PhW": k_VERSION
660
+ });
661
+ },
662
+ validate: function (json) {
663
+ // if (! JSON_Msg_msg.validate(json))
664
+ // return false;
665
+ // if (! schema_MsgOptions_(json)) {
666
+ // log_(l_.VALIDATION, () => ['JSON_MsgOptions: schema_MsgOptions_: ', (schema_MsgOptions_ as any).errors])
667
+ // return false;
668
+ // }
669
+ return true;
670
+ }
671
+ };
672
+ })();
673
+ var JSON_MsgWarp = (function () {
674
+ return {
675
+ create_with_id_user_hyperport: function (json_warp_id, json_user, json_hyperport, nr_ttl_msec) {
676
+ return jsonMsg_cjsExports.JSON_Msg.merge({
677
+ warp: {
678
+ id: json_warp_id,
679
+ user: json_user,
680
+ hyperport: json_hyperport,
681
+ ttl_msec: nr_ttl_msec
682
+ },
683
+ "2PhW": k_VERSION
684
+ });
685
+ },
686
+ compare: function (json_L, json_R) {
687
+ if (json_L.dt !== json_R.dt)
688
+ return false;
689
+ if (json_L.warp.id !== json_R.warp.id)
690
+ return false;
691
+ if (json_L.referer[0] !== json_R.referer[0])
692
+ return false;
693
+ if (json_L.referer[1] !== json_R.referer[1])
694
+ return false;
695
+ return true;
696
+ },
697
+ validate: function (json_msg) {
698
+ // if (! JSON_Msg.validate(json))
699
+ // return false;
700
+ if (!schema_MsgWarp(json_msg)) {
701
+ log_(l_.VALIDATION, () => ['JSON_MsgWarp: schema_MsgWarp_: ', schema_MsgWarp.errors]);
702
+ return false;
703
+ }
704
+ return true;
705
+ }
706
+ };
707
+ })();
708
+ var JSON_MsgEngage = (function () {
709
+ return {
710
+ create_with_MsgWarp: function (json_msgWarp) {
711
+ console.assert(json_msgWarp.ack === undefined, "Engage with a Warp, not an Ack");
712
+ return jsonMsg_cjsExports.JSON_Msg_msg.create_with_msg_merge(json_msgWarp, {
713
+ engage: {
714
+ "ok": true,
715
+ "rtt_msec": Date.now() - Date.parse(json_msgWarp.dt)
716
+ },
717
+ "2PhW": k_VERSION
718
+ });
719
+ },
720
+ validate: function (json_msg_msg) {
721
+ // if (! JSON_Msg_msg.validate(json))
722
+ // return false;
723
+ if (!JSON_MsgWarp.validate(json_msg_msg._msg))
724
+ return false;
725
+ if (!schema_MsgEngage(json_msg_msg)) {
726
+ log_(l_.VALIDATION, () => ['JSON_MsgEngage: schema_MsgEngage_: ', schema_MsgEngage.errors]);
727
+ return false;
728
+ }
729
+ return true;
730
+ }
731
+ };
732
+ })();
733
+ var JSON_MsgAckWarp = (function () {
734
+ return {
735
+ // createWith_msgAck_msg : function(json_msgAck, _msg) {
736
+ // const json_msgAckWarp= structuredClone(json_msgAck);
737
+ // json_msgAckWarp._msg= _msg;
738
+ // return json_msgAckWarp;
739
+ // }
740
+ create_with_msgOk: function (json_msgWarp, b_ok = true) {
741
+ const json_msgAck_msg = jsonMsg_cjsExports.JSON_MsgAck_msg.create_with_msgOk(json_msgWarp, b_ok);
742
+ json_msgAck_msg["2PhW"] = k_VERSION;
743
+ return json_msgAck_msg;
744
+ },
745
+ // create_with_msgExpired : function( json_msgWarp ) {
746
+ // const json_msgAck_msg= JSON_MsgAck_msg.create_with_msgExpired( json_msgWarp )
747
+ // json_msgAck_msg["2PhW"]= k_VERSION;
748
+ // return json_msgAck_msg;
749
+ // },
750
+ validate: function (json_msgAck_msg) {
751
+ // if (! JSON_MsgAck_msg.validate(json))
752
+ // return false;
753
+ if (!JSON_MsgWarp.validate(json_msgAck_msg._msg))
754
+ return false;
755
+ return true;
756
+ }
757
+ };
758
+ })();
759
+ var JSON_MsgAckEngage = (function () {
760
+ return {
761
+ // createWith_msgAck_msg : function(json_msgAck, _msg) {
762
+ // const json_msgAckEngage= structuredClone(json_msgAck);
763
+ // json_msgAckEngage._msg= _msg;
764
+ // return json_msgAckEngage;
765
+ // }
766
+ create_with_msgOk: function (json_msgEngage, b_ok = true) {
767
+ const json_msgAck_msg = jsonMsg_cjsExports.JSON_MsgAck_msg.create_with_msgOk(json_msgEngage, b_ok);
768
+ json_msgAck_msg["2PhW"] = k_VERSION;
769
+ return json_msgAck_msg;
770
+ },
771
+ // create_with_msgExpired : function( json_msgEngage ) {
772
+ // const json_msgAck_msg= JSON_MsgAck_msg.create_with_msgExpired( json_msgEngage )
773
+ // json_msgAck_msg["2PhW"]= k_VERSION;
774
+ // return json_msgAck_msg;
775
+ // },
776
+ validate: function (json_msgAck_msg) {
777
+ // if (! JSON_MsgAck_msg.validate(json_msgAck_msg))
778
+ // return false;
779
+ if (!JSON_MsgEngage.validate(json_msgAck_msg._msg))
780
+ return false;
781
+ return true;
782
+ }
783
+ };
784
+ })();
785
+ //-------------------------------------------------------------------------------------------------
786
+ // States
787
+ class StateState {
788
+ _idx_event;
789
+ _event;
790
+ _statekeeper;
791
+ constructor(idx_event, event) {
792
+ this._statekeeper = null;
793
+ this._idx_event = idx_event;
794
+ this._event = event;
795
+ }
796
+ get protocol() {
797
+ return this._statekeeper;
798
+ }
799
+ set protocol(parent) {
800
+ this._statekeeper = parent;
801
+ }
802
+ get idx_event() {
803
+ return this._idx_event;
804
+ }
805
+ get event() {
806
+ return this._event;
807
+ }
808
+ get json_msg() {
809
+ return this._event.data.json_msg;
810
+ }
811
+ changeState(state_new) {
812
+ if (this.canChangeState(state_new))
813
+ this.protocol.state = state_new;
814
+ else
815
+ this.protocol.state = new StateINVALID(state_new._idx_event, state_new._event);
816
+ }
817
+ canChangeState(_state_new) {
818
+ throw new Error("Subclass must implement canChangeState()");
819
+ }
820
+ }
821
+ class StateAckEngage extends StateState {
822
+ constructor(idx_event, event) { super(idx_event, event); }
823
+ get evt_AckEngage() { return this.event; }
824
+ get json_msgAckEngage() { return this.json_msg; }
825
+ canChangeState(_state_new) {
826
+ return false;
827
+ }
828
+ }
829
+ class StateEngage extends StateState {
830
+ constructor(idx_event, event) { super(idx_event, event); }
831
+ get evt_Engage() { return this.event; }
832
+ get json_msgEngage() { return this.json_msg; }
833
+ canChangeState(state_new) {
834
+ if (false === state_new instanceof StateAckEngage)
835
+ return false;
836
+ // The Engage gets to live for THE WARP'S creation time + TTL*2 + RTT
837
+ const json_msgWarp = this.json_msgEngage._msg;
838
+ const timeout_ms = t_ttl_rtt_(json_msgWarp.warp.ttl_msec, this.json_msgEngage.engage.rtt_msec);
839
+ log_(l_.PROTOCOL_STATE, () => [`StateEngage: evt_AckEngage.dt@${state_new.evt_AckEngage.dt}, msgWarp.dt@${Date.parse(json_msgWarp.dt)} + ${timeout_ms}`]);
840
+ if (state_new.evt_AckEngage.dt >= Date.parse(json_msgWarp.dt) + timeout_ms)
841
+ return false;
842
+ return true;
843
+ }
844
+ }
845
+ class StateAckWarp extends StateState {
846
+ constructor(idx_event, event) { super(idx_event, event); }
847
+ get evt_AckWarp() { return this.event; }
848
+ get json_msgAckWarp() { return this.json_msg; }
849
+ canChangeState(state_new) {
850
+ if (false === state_new instanceof StateEngage)
851
+ return false;
852
+ if (!this.json_msgAckWarp.ack.ok)
853
+ return false;
854
+ if (this.json_msgAckWarp._msg.warp.instantaneous)
855
+ return false;
856
+ // The Ack gets to live for THE ACK'S creation time + TTL*2 + STT
857
+ // +STT, so lines up with the Engage timeout
858
+ // - Engage: T0 +2TTL +RTT; RTT= STT+STT
859
+ // - AckWarp: TAckWarp +2TLL +STT; TAckWarp= T0 +STT;
860
+ // - STT: Date of the Ack minus date of the Warp.
861
+ 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);
862
+ log_(l_.PROTOCOL_STATE, () => [`StateAckWarp: evt_Engage.dt@${state_new.evt_Engage.dt}, msgAckWarp.dt@${Date.parse(this.json_msgAckWarp.dt)} + ${timeout_ms}`]);
863
+ if (state_new.evt_Engage.dt >= Date.parse(this.json_msgAckWarp.dt) + timeout_ms)
864
+ return false;
865
+ return true;
866
+ }
867
+ }
868
+ class StateWarp extends StateState {
869
+ constructor(idx_event, event) { super(idx_event, event); }
870
+ get evt_Warp() { return this.event; }
871
+ get json_msgWarp() { return this.json_msg; }
872
+ canChangeState(state_new) {
873
+ if (false === state_new instanceof StateAckWarp)
874
+ return false;
875
+ // The Warp gets to live for THE WARP'S creation time + TTL*2
876
+ const timeout_ms = t_ttl_rtt_(this.json_msgWarp.warp.ttl_msec);
877
+ log_(l_.PROTOCOL_STATE, () => [`StateWarp: evt_AckWarp.dt@${state_new.evt_AckWarp.dt}, msgWarp.dt@${Date.parse(this.json_msgWarp.dt)} + ${timeout_ms}`]);
878
+ if (state_new.evt_AckWarp.dt >= Date.parse(this.json_msgWarp.dt) + timeout_ms)
879
+ return false;
880
+ return true;
881
+ }
882
+ }
883
+ class StateINVALID extends StateState {
884
+ constructor(idx_event, event) { super(idx_event, event); }
885
+ canChangeState(_state_new) {
886
+ // INVALID can't transition to any state
887
+ return false;
888
+ }
889
+ }
890
+ class StateInitialState extends StateState {
891
+ constructor() { super(null, null); }
892
+ canChangeState(state_new) {
893
+ if (false === state_new instanceof StateWarp)
894
+ return false;
895
+ // The NEWLY created Warp only gets to live for its creation time + TTL
896
+ const timeout_ms = state_new.json_msgWarp.warp.ttl_msec;
897
+ log_(l_.PROTOCOL_STATE, () => [`StateInitialState: evt_Warp.dt@${state_new.evt_Warp.dt}, msgWarp.dt@${Date.parse(state_new.json_msgWarp.dt)} + ${timeout_ms}`]);
898
+ if (state_new.evt_Warp.dt >= Date.parse(state_new.json_msgWarp.dt) + timeout_ms)
899
+ return false;
900
+ return true;
901
+ }
902
+ }
903
+ //----
904
+ class ProtocolStateState {
905
+ _state;
906
+ constructor(state_initial) {
907
+ this.state = state_initial || new StateInitialState();
908
+ }
909
+ set state(state_new) {
910
+ if (!(state_new instanceof StateState)) {
911
+ throw new Error("initState must be an instance of StateState or its subclasses");
912
+ }
913
+ this._state = state_new;
914
+ if (state_new && state_new.protocol !== this) {
915
+ state_new.protocol = this;
916
+ }
917
+ }
918
+ get state() {
919
+ return this._state;
920
+ }
921
+ canChangeState(state_new) {
922
+ return this._state.canChangeState(state_new);
923
+ }
924
+ }
925
+ //-------------------------------------------------------------------------------------------------
926
+ class TwoPhW_EventLog {
927
+ _eventLog;
928
+ constructor(eventLog) {
929
+ this._eventLog = eventLog;
930
+ }
931
+ _warp(json_msg) {
932
+ const b_Ack = json_msg.ack !== undefined;
933
+ const json = b_Ack ? json_msg["_msg"] : json_msg;
934
+ if (json.warp) {
935
+ return {
936
+ str_type: `2PhW[JSON_Msg${b_Ack ? 'Ack' : ''}Warp]`,
937
+ json_msgWarp: json
938
+ };
939
+ }
940
+ if (json.engage) {
941
+ return {
942
+ str_type: `2PhW[JSON_Msg${b_Ack ? 'Ack' : ''}Engage]`,
943
+ json_msgWarp: json["_msg"]
944
+ };
945
+ }
946
+ return { str_type: null, json_msgWarp: null }; // Explicit default for invalid cases
947
+ }
948
+ // _addEvent(msg: t_msg, json_msg : JSON_Msg, timeout_ms? : number, timeout_callback? : ( args? : {idx_event : number, event : EventMinimal} ) => {}) {
949
+ _addEvent(msg, json_msg, timeout_ms, timeout_callback) {
950
+ const { str_type, json_msgWarp } = this._warp(json_msg);
951
+ const { idx } = this._eventLog.addEvent(str_type, json_msgWarp.warp.id, { msg, json_msg }, timeout_ms, timeout_callback);
952
+ log_(l_.PROTOCOL_STATE, () => [`_addEvent: [${idx}] timeout:${Date.now()}+${timeout_ms} `, json_msg]);
953
+ }
954
+ _dropEvent(json_msg) {
955
+ const { json_msgWarp } = this._warp(json_msg);
956
+ if (!json_msgWarp)
957
+ return;
958
+ this._eventLog.addEvent('2PhW[DROPPED!]', json_msgWarp.warp.id, json_msg);
959
+ }
960
+ _cancelTimeout(idx_event) {
961
+ log_(l_.PROTOCOL_STATE, () => [`_cancelTimeout: [${idx_event}] timeout cancelled`]);
962
+ this._eventLog.cancelTimeout(idx_event);
963
+ }
964
+ traverse_State(json_msgWarp) {
965
+ const protocolState = new ProtocolStateState();
966
+ const arr_evts = this._eventLog.getEventsById(json_msgWarp.warp.id, k_TTL);
967
+ log_(l_.EVENTS, () => ['traverse_State: arr_evts: ', arr_evts]);
968
+ //TODO: what is the right TTL? should it be a constant or calculated
969
+ for (let i = arr_evts.length - 1; i >= 0; i--) {
970
+ const { idx, evt } = arr_evts[i];
971
+ const json_msg = evt.data.json_msg;
972
+ if (!JSON_MsgWarp.compare(json_msgWarp, this._warp(json_msg).json_msgWarp))
973
+ protocolState.state.changeState(new StateINVALID(idx, evt));
974
+ switch (evt.type) {
975
+ case '2PhW[JSON_MsgWarp]':
976
+ protocolState.state.changeState(new StateWarp(idx, evt));
977
+ break;
978
+ case '2PhW[JSON_MsgAckWarp]':
979
+ protocolState.state.changeState(new StateAckWarp(idx, evt));
980
+ break;
981
+ case '2PhW[JSON_MsgEngage]':
982
+ protocolState.state.changeState(new StateEngage(idx, evt));
983
+ break;
984
+ case '2PhW[JSON_MsgAckEngage]':
985
+ protocolState.state.changeState(new StateAckEngage(idx, evt));
986
+ break;
987
+ }
988
+ log_(l_.PROTOCOL_STATE, () => ['traverse_State: type,state: ',
989
+ evt.type,
990
+ (protocolState.state.json_msg.ack?.ok === false) ? '--NACK--' : '',
991
+ protocolState.state
992
+ ]);
993
+ }
994
+ return protocolState;
995
+ }
996
+ }
997
+ class TwoPhW {
998
+ _2PhW_eventLog;
999
+ _config;
1000
+ constructor(eventLog) {
1001
+ this._2PhW_eventLog = new TwoPhW_EventLog(eventLog);
1002
+ this._config = new Config();
1003
+ }
1004
+ get config() {
1005
+ return this._config;
1006
+ }
1007
+ get eventLog() {
1008
+ return this._2PhW_eventLog;
1009
+ }
1010
+ //-------------------------------------------------------------------------------------------------
1011
+ _handler_msgOptions(cfg, msg, json_msgOptions) {
1012
+ log_(l_.HANDLERS, () => ['handler_msgOptions_: ...']);
1013
+ /*
1014
+ if (! validate_Options_delay_(json_msgOptions.options))
1015
+ return false;
1016
+
1017
+ if (JSON_MsgWarp.validate(json_msgOptions._msg)) {
1018
+ const json_msgWarp= json_msgOptions._msg;
1019
+
1020
+ const ctx= get_ctx(json_msgWarp.id);
1021
+ if (!ctx )
1022
+ return false;
1023
+
1024
+ const ttl_msec= json_msgOptions.options.ttl_msec_incr[0];
1025
+ const ttl_incr= json_msgOptions.options.ttl_msec_incr[1];
1026
+
1027
+ //TODO: double check ttl_msec is the same as current accummulated ttl for idempotence.
1028
+
1029
+ log_(l_.HANDLERS, () => ['handler_msgOptions_: ', json_msgOptions]);
1030
+
1031
+ ctx.protocol.delayTimeout(ttl_incr);
1032
+
1033
+ if (cfg._callback_msgOptions)
1034
+ return cfg._callback_msgOptions(msg, json_msgOptions);
1035
+
1036
+ return true;
1037
+ }
1038
+ */
1039
+ return false;
1040
+ }
1041
+ //-------------------------------------------------------------------------------------------------
1042
+ // sender Source (src)
1043
+ register_msgWarp(msg, json_msgWarp) {
1044
+ log_(l_.SENDER, () => ['register_msgWarp: ', json_msgWarp]);
1045
+ const protocolState = this.eventLog.traverse_State(json_msgWarp);
1046
+ const b_changeState = protocolState.canChangeState(new StateWarp(null, {
1047
+ dt: Date.now(),
1048
+ data: { msg, json_msg: json_msgWarp }
1049
+ }));
1050
+ log_(l_.HANDLERS | l_.SENDER, () => ['register_msgWarp: protocolState.canChangeState ', b_changeState]);
1051
+ if (!b_changeState)
1052
+ return false;
1053
+ const timeout_ms = t_ttl_rtt_(json_msgWarp.warp.ttl_msec);
1054
+ const elapsed_ms = Date.now() - Date.parse(json_msgWarp.dt);
1055
+ this.eventLog._addEvent(msg, json_msgWarp, timeout_ms - elapsed_ms, () => {
1056
+ this.config._callback_msg_sent_expired(msg, json_msgWarp);
1057
+ });
1058
+ }
1059
+ _handler_msgAckWarp(msg, json_msgAckWarp) {
1060
+ log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckWarp: ', json_msgAckWarp]);
1061
+ const json_msgWarp = json_msgAckWarp._msg;
1062
+ const protocolState = this.eventLog.traverse_State(json_msgWarp);
1063
+ const b_changeState = protocolState.canChangeState(new StateAckWarp(null, {
1064
+ dt: Date.now(),
1065
+ data: { msg, json_msg: json_msgAckWarp }
1066
+ }));
1067
+ log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckWarp: protocolState.canChangeState ', b_changeState]);
1068
+ if (!b_changeState)
1069
+ return false;
1070
+ this.eventLog._cancelTimeout(protocolState.state.idx_event);
1071
+ this.eventLog._addEvent(msg, json_msgAckWarp);
1072
+ if (!this.config._callback_msgAckWarp(msg, json_msgAckWarp))
1073
+ return false;
1074
+ return true;
1075
+ }
1076
+ register_msgEngage(msg, json_msgEngage) {
1077
+ log_(l_.SENDER, () => ['register_msgEngage: ', json_msgEngage]);
1078
+ const json_msgWarp = json_msgEngage._msg;
1079
+ const protocolState = this.eventLog.traverse_State(json_msgWarp);
1080
+ const b_changeState = protocolState.canChangeState(new StateEngage(null, {
1081
+ dt: Date.now(),
1082
+ data: { msg, json_msg: json_msgEngage }
1083
+ }));
1084
+ log_(l_.HANDLERS | l_.SENDER, () => ['register_msgEngage: protocolState.canChangeState ', b_changeState]);
1085
+ if (!b_changeState)
1086
+ return false;
1087
+ const timeout_ms = t_ttl_rtt_(json_msgWarp.warp.ttl_msec, json_msgEngage.engage.rtt_msec);
1088
+ const elapsed_ms = Date.now() - Date.parse(json_msgWarp.dt);
1089
+ this.eventLog._addEvent(msg, json_msgEngage, timeout_ms - elapsed_ms, () => {
1090
+ this.config._callback_msg_sent_expired(msg, json_msgEngage);
1091
+ });
1092
+ }
1093
+ _handler_msgAckEngage(msg, json_msgAckEngage) {
1094
+ log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckEngage: ', json_msgAckEngage]);
1095
+ const json_msgWarp = json_msgAckEngage._msg._msg;
1096
+ const protocolState = this.eventLog.traverse_State(json_msgWarp);
1097
+ const b_changeState = protocolState.canChangeState(new StateAckEngage(null, {
1098
+ dt: Date.now(),
1099
+ data: { msg, json_msg: json_msgAckEngage }
1100
+ }));
1101
+ log_(l_.HANDLERS | l_.SENDER, () => ['handler_msgAckEngage_: protocolState.canChangeState ', b_changeState]);
1102
+ if (!b_changeState)
1103
+ return false;
1104
+ this.eventLog._cancelTimeout(protocolState.state.idx_event);
1105
+ this.eventLog._addEvent(msg, json_msgAckEngage);
1106
+ if (!this.config._callback_msgAckEngage(msg, json_msgAckEngage))
1107
+ return false;
1108
+ return true;
1109
+ }
1110
+ //-------------------------------------------------------------------------------------------------
1111
+ // receiver Destination (dst)
1112
+ _handler_msgWarp(msg, json_msgWarp) {
1113
+ log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgWarp: ', json_msgWarp]);
1114
+ const protocolState = this.eventLog.traverse_State(json_msgWarp);
1115
+ const b_changeState = protocolState.canChangeState(new StateWarp(null, {
1116
+ dt: Date.now(),
1117
+ data: { msg, json_msg: json_msgWarp }
1118
+ }));
1119
+ log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgWarp: protocolState.canChangeState: ', b_changeState]);
1120
+ if (!b_changeState)
1121
+ return false;
1122
+ this.eventLog._addEvent(msg, json_msgWarp);
1123
+ if (!this.config._callback_msgWarp(msg, json_msgWarp))
1124
+ return false;
1125
+ return true;
1126
+ }
1127
+ register_msgAckWarp(msg, json_msgAckWarp) {
1128
+ log_(l_.RECVER, () => ['register_msgAckWarp: ', json_msgAckWarp]);
1129
+ const json_msgWarp = json_msgAckWarp._msg;
1130
+ const protocolState = this.eventLog.traverse_State(json_msgWarp);
1131
+ const b_changeState = protocolState.canChangeState(new StateAckWarp(null, {
1132
+ dt: Date.now(),
1133
+ data: { msg, json_msg: json_msgAckWarp }
1134
+ }));
1135
+ log_(l_.HANDLERS | l_.RECVER, () => ['register_msgAckWarp: protocolState.canChangeState: ', b_changeState]);
1136
+ if (!b_changeState)
1137
+ return false;
1138
+ const elapsed_ms = Date.now() - Date.parse(json_msgAckWarp.dt);
1139
+ const timeout_ms = t_ttl_rtt_(json_msgWarp.warp.ttl_msec) + Date.parse(json_msgAckWarp.dt) - Date.parse(json_msgWarp.dt);
1140
+ this.eventLog._addEvent(msg, json_msgAckWarp, timeout_ms - elapsed_ms, () => {
1141
+ this.config._callback_msg_sent_expired(msg, json_msgAckWarp);
1142
+ });
1143
+ }
1144
+ _handler_msgEngage(msg, json_msgEngage) {
1145
+ log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgEngage: ', json_msgEngage]);
1146
+ const json_msgWarp = json_msgEngage["_msg"];
1147
+ const protocolState = this.eventLog.traverse_State(json_msgWarp);
1148
+ const b_changeState = protocolState.canChangeState(new StateEngage(null, {
1149
+ dt: Date.now(),
1150
+ data: { msg, json_msg: json_msgEngage }
1151
+ }));
1152
+ log_(l_.HANDLERS | l_.RECVER, () => ['handler_msgEngage: protocolState.canChangeState: ', b_changeState]);
1153
+ if (!b_changeState)
1154
+ return false;
1155
+ this.eventLog._cancelTimeout(protocolState.state.idx_event);
1156
+ this.eventLog._addEvent(msg, json_msgEngage);
1157
+ if (!this.config._callback_msgEngage(msg, json_msgEngage))
1158
+ return false;
1159
+ return true;
1160
+ }
1161
+ register_msgAckEngage(msg, json_msgAckEngage) {
1162
+ log_(l_.RECVER, () => ['register_msgAckEngage: ', json_msgAckEngage]);
1163
+ const json_msgWarp = json_msgAckEngage._msg._msg;
1164
+ const protocolState = this.eventLog.traverse_State(json_msgWarp);
1165
+ const b_changeState = protocolState.canChangeState(new StateAckEngage(null, {
1166
+ dt: Date.now(),
1167
+ data: { msg, json_msg: json_msgAckEngage }
1168
+ }));
1169
+ log_(l_.HANDLERS | l_.RECVER, () => ['register_msgAckEngage: protocolState.canChangeState: ', b_changeState]);
1170
+ if (!b_changeState)
1171
+ return false;
1172
+ this.eventLog._addEvent(msg, json_msgAckEngage);
1173
+ }
1174
+ //-------------------------------------------------------------------------------------------------
1175
+ handler_json_msgAck_msg(msg, json_msgAck_msg) {
1176
+ log_(l_.HANDLERS, () => ['handler_json_msgAck_msg: ', json_msgAck_msg]);
1177
+ if (JSON_MsgAckWarp.validate(json_msgAck_msg) && this._handler_msgAckWarp(msg, json_msgAck_msg))
1178
+ return true;
1179
+ if (JSON_MsgAckEngage.validate(json_msgAck_msg) && this._handler_msgAckEngage(msg, json_msgAck_msg))
1180
+ return true;
1181
+ return true;
1182
+ }
1183
+ handler_json_msg_msg(msg, json_msg_msg) {
1184
+ log_(l_.HANDLERS, () => ['handler_json_msg_msg: ', json_msg_msg]);
1185
+ if (JSON_MsgEngage.validate(json_msg_msg) && this._handler_msgEngage(msg, json_msg_msg))
1186
+ return true;
1187
+ // if (JSON_MsgOptions.validate(json_msg) && this.handler_msgOptions_(this._config, msg, json_msg))
1188
+ // return true;
1189
+ return false;
1190
+ }
1191
+ handler_json_msg(msg, json_msg) {
1192
+ log_(l_.HANDLERS, () => ['handler_json_msg: ', json_msg]);
1193
+ if (JSON_MsgWarp.validate(json_msg) && this._handler_msgWarp(msg, json_msg))
1194
+ return true;
1195
+ // https://attacomsian.com/blog/javascript-iterate-objects
1196
+ // Object.keys(json_msg).forEach((key, index) => {
1197
+ // if (key == 'dt')
1198
+ // return;
1199
+ // if (key == 'referer')
1200
+ // return;
1201
+ // console.log(`${key}: ${json_msg[key]}`);
1202
+ // });
1203
+ return false;
1204
+ }
1205
+ handler_json(msg, json) {
1206
+ if (jsonMsg_cjsExports.JSON_Msg.validate(json)) {
1207
+ if (jsonMsg_cjsExports.JSON_Msg_msg.validate(json)) {
1208
+ if (jsonMsg_cjsExports.JSON_MsgAck_msg.validate(json)) {
1209
+ if (JSON_MsgAckWarp.validate(json) && this._handler_msgAckWarp(msg, json))
1210
+ return true;
1211
+ if (JSON_MsgAckEngage.validate(json) && this._handler_msgAckEngage(msg, json))
1212
+ return true;
1213
+ return false;
1214
+ }
1215
+ if (JSON_MsgEngage.validate(json) && this._handler_msgEngage(msg, json))
1216
+ return true;
1217
+ return false;
1218
+ }
1219
+ if (JSON_MsgWarp.validate(json) && this._handler_msgWarp(msg, json))
1220
+ return true;
1221
+ return false;
1222
+ }
1223
+ return false;
1224
+ }
1337
1225
  }
1338
1226
 
1339
1227
  exports.EventLog = EventLog;