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