@streamr/dht 0.0.1-tatum.5 → 0.0.1-tatum.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/connection/ConnectionLockHandler.d.ts +1 -1
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +17 -39
- package/dist/src/connection/ConnectionManager.js +138 -192
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectivityChecker.js +14 -11
- package/dist/src/connection/ConnectivityChecker.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +49 -0
- package/dist/src/connection/ConnectorFacade.js +86 -0
- package/dist/src/connection/ConnectorFacade.js.map +1 -0
- package/dist/src/connection/ManagedConnection.d.ts +1 -4
- package/dist/src/connection/ManagedConnection.js +23 -31
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/RemoteConnectionLocker.js +4 -3
- package/dist/src/connection/RemoteConnectionLocker.js.map +1 -1
- package/dist/src/connection/Simulator/Simulator.d.ts +0 -2
- package/dist/src/connection/Simulator/Simulator.js +0 -5
- package/dist/src/connection/Simulator/Simulator.js.map +1 -1
- package/dist/src/connection/Simulator/SimulatorConnection.js +16 -13
- package/dist/src/connection/Simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/Simulator/SimulatorConnector.d.ts +2 -2
- package/dist/src/connection/Simulator/SimulatorConnector.js +10 -11
- package/dist/src/connection/Simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/Simulator/SimulatorTransport.js +6 -1
- package/dist/src/connection/Simulator/SimulatorTransport.js.map +1 -1
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.d.ts +2 -0
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js +12 -12
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js.map +1 -1
- package/dist/src/connection/WebRTC/WebRtcConnector.d.ts +9 -9
- package/dist/src/connection/WebRTC/WebRtcConnector.js +22 -22
- package/dist/src/connection/WebRTC/WebRtcConnector.js.map +1 -1
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js +2 -1
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js.map +1 -1
- package/dist/src/connection/WebSocket/WebSocketConnector.d.ts +19 -8
- package/dist/src/connection/WebSocket/WebSocketConnector.js +67 -46
- package/dist/src/connection/WebSocket/WebSocketConnector.js.map +1 -1
- package/dist/src/connection/WebSocket/WebSocketServer.d.ts +11 -1
- package/dist/src/connection/WebSocket/WebSocketServer.js +15 -10
- package/dist/src/connection/WebSocket/WebSocketServer.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +16 -54
- package/dist/src/dht/DhtNode.js +116 -137
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/{DhtPeer.d.ts → RemoteDhtNode.d.ts} +3 -2
- package/dist/src/dht/{DhtPeer.js → RemoteDhtNode.js} +22 -16
- package/dist/src/dht/RemoteDhtNode.js.map +1 -0
- package/dist/src/dht/contact/Contact.d.ts +1 -15
- package/dist/src/dht/contact/Contact.js +1 -9
- package/dist/src/dht/contact/Contact.js.map +1 -1
- package/dist/src/dht/contact/ContactList.d.ts +13 -3
- package/dist/src/dht/contact/ContactList.js +9 -4
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/RandomContactList.d.ts +3 -3
- package/dist/src/dht/contact/RandomContactList.js +4 -8
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/Remote.d.ts +1 -5
- package/dist/src/dht/contact/Remote.js +0 -5
- package/dist/src/dht/contact/Remote.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +3 -5
- package/dist/src/dht/contact/SortedContactList.js +9 -19
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +5 -7
- package/dist/src/dht/discovery/DiscoverySession.js +9 -10
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +11 -10
- package/dist/src/dht/discovery/PeerDiscovery.js +32 -37
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/find/RecursiveFindSession.d.ts +5 -6
- package/dist/src/dht/find/RecursiveFindSession.js +8 -8
- package/dist/src/dht/find/RecursiveFindSession.js.map +1 -1
- package/dist/src/dht/find/RecursiveFinder.d.ts +2 -4
- package/dist/src/dht/find/RecursiveFinder.js +11 -12
- package/dist/src/dht/find/RecursiveFinder.js.map +1 -1
- package/dist/src/dht/registerExternalApiRpcMethods.d.ts +1 -1
- package/dist/src/dht/routing/DuplicateDetector.d.ts +1 -2
- package/dist/src/dht/routing/DuplicateDetector.js +2 -7
- package/dist/src/dht/routing/DuplicateDetector.js.map +1 -1
- package/dist/src/dht/routing/RemoteRouter.js +4 -4
- package/dist/src/dht/routing/RemoteRouter.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +10 -13
- package/dist/src/dht/routing/Router.js +28 -29
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -5
- package/dist/src/dht/routing/RoutingSession.js +19 -13
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/DataStore.d.ts +2 -2
- package/dist/src/dht/store/DataStore.js +7 -7
- package/dist/src/dht/store/DataStore.js.map +1 -1
- package/dist/src/exports.d.ts +1 -8
- package/dist/src/exports.js +2 -16
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/PeerID.d.ts +0 -1
- package/dist/src/helpers/PeerID.js +0 -6
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/browser/isBrowserEnvironment.d.ts +1 -0
- package/dist/src/helpers/browser/isBrowserEnvironment.js +6 -0
- package/dist/src/helpers/browser/isBrowserEnvironment.js.map +1 -0
- package/dist/src/helpers/browser/isBrowserEnvironment_override.d.ts +1 -0
- package/dist/src/helpers/browser/isBrowserEnvironment_override.js +7 -0
- package/dist/src/helpers/browser/isBrowserEnvironment_override.js.map +1 -0
- package/dist/src/helpers/kademliaId.d.ts +1 -0
- package/dist/src/helpers/kademliaId.js +14 -0
- package/dist/src/helpers/kademliaId.js.map +1 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +1 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +3 -3
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +0 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +1 -2
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/package.json +10 -9
- package/protos/DhtRpc.proto +0 -1
- package/src/connection/ConnectionLockHandler.ts +1 -1
- package/src/connection/ConnectionManager.ts +156 -240
- package/src/connection/ConnectivityChecker.ts +14 -11
- package/src/connection/ConnectorFacade.ts +143 -0
- package/src/connection/ManagedConnection.ts +23 -34
- package/src/connection/RemoteConnectionLocker.ts +4 -3
- package/src/connection/Simulator/Simulator.ts +0 -7
- package/src/connection/Simulator/SimulatorConnection.ts +16 -13
- package/src/connection/Simulator/SimulatorConnector.ts +11 -12
- package/src/connection/Simulator/SimulatorTransport.ts +6 -1
- package/src/connection/WebRTC/NodeWebRtcConnection.ts +14 -13
- package/src/connection/WebRTC/WebRtcConnector.ts +31 -31
- package/src/connection/WebSocket/RemoteWebSocketConnector.ts +2 -1
- package/src/connection/WebSocket/WebSocketConnector.ts +85 -62
- package/src/connection/WebSocket/WebSocketServer.ts +26 -8
- package/src/dht/DhtNode.ts +164 -189
- package/src/dht/{DhtPeer.ts → RemoteDhtNode.ts} +14 -7
- package/src/dht/contact/Contact.ts +1 -18
- package/src/dht/contact/ContactList.ts +16 -6
- package/src/dht/contact/RandomContactList.ts +6 -11
- package/src/dht/contact/Remote.ts +1 -10
- package/src/dht/contact/SortedContactList.ts +12 -25
- package/src/dht/discovery/DiscoverySession.ts +20 -23
- package/src/dht/discovery/PeerDiscovery.ts +45 -44
- package/src/dht/find/RecursiveFindSession.ts +12 -13
- package/src/dht/find/RecursiveFinder.ts +16 -19
- package/src/dht/registerExternalApiRpcMethods.ts +1 -1
- package/src/dht/routing/DuplicateDetector.ts +3 -10
- package/src/dht/routing/RemoteRouter.ts +5 -5
- package/src/dht/routing/Router.ts +35 -39
- package/src/dht/routing/RoutingSession.ts +37 -28
- package/src/dht/store/DataStore.ts +11 -11
- package/src/exports.ts +1 -8
- package/src/helpers/PeerID.ts +0 -7
- package/src/helpers/browser/isBrowserEnvironment.ts +1 -0
- package/src/helpers/browser/isBrowserEnvironment_override.ts +3 -0
- package/src/helpers/kademliaId.ts +8 -0
- package/src/helpers/peerIdFromPeerDescriptor.ts +1 -1
- package/src/proto/packages/dht/protos/DhtRpc.ts +1 -6
- package/test/benchmark/KademliaCorrectness.test.ts +5 -2
- package/test/benchmark/RecursiveFind.test.ts +6 -6
- package/test/end-to-end/Layer0-Layer1.test.ts +14 -14
- package/test/end-to-end/Layer0WebRTC-Layer1.test.ts +5 -5
- package/test/end-to-end/Layer0WebRTC.test.ts +5 -6
- package/test/end-to-end/Layer1-Scale-WebRTC.test.ts +13 -8
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +15 -10
- package/test/end-to-end/WebSocketConnectionRequest.test.ts +5 -5
- package/test/integration/ConnectionLocking.test.ts +32 -26
- package/test/integration/ConnectionManager.test.ts +90 -93
- package/test/integration/DhtJoinPeerDiscovery.test.ts +53 -0
- package/test/integration/DhtRpc.test.ts +4 -6
- package/test/integration/Layer1-scale.test.ts +8 -8
- package/test/integration/MigrateData.test.ts +9 -9
- package/test/integration/Mock-Layer1-Layer0.test.ts +1 -2
- package/test/integration/RecursiveFind.test.ts +5 -5
- package/test/integration/{DhtPeer.test.ts → RemoteDhtNode.test.ts} +11 -12
- package/test/integration/RemoteRouter.test.ts +5 -6
- package/test/integration/RemoteStore.test.ts +4 -5
- package/test/integration/RouteMessage.test.ts +7 -9
- package/test/integration/RpcErrors.test.ts +25 -10
- package/test/integration/ScaleDownDht.test.ts +8 -8
- package/test/integration/SimultaneousConnections.test.ts +35 -36
- package/test/integration/Store.test.ts +8 -9
- package/test/integration/StoreAndDelete.test.ts +11 -11
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +7 -7
- package/test/integration/WebRtcConnectionManagement.test.ts +26 -19
- package/test/integration/WebRtcConnectorRpc.test.ts +6 -8
- package/test/integration/WebSocket.test.ts +4 -2
- package/test/integration/WebSocketConnectionManagement.test.ts +30 -17
- package/test/integration/WebSocketConnectorRpc.test.ts +2 -3
- package/test/unit/DuplicateDetector.test.ts +3 -4
- package/test/unit/LocalDataStore.test.ts +6 -8
- package/test/unit/RandomContactList.test.ts +25 -74
- package/test/unit/RecursiveFinder.test.ts +8 -12
- package/test/unit/Router.test.ts +18 -21
- package/test/unit/SortedContactList.test.ts +62 -112
- package/test/unit/WebSocketConnector.test.ts +64 -0
- package/test/unit/WebSocketServer.test.ts +24 -12
- package/test/utils/mock/RecursiveFinder.ts +2 -2
- package/test/utils/mock/Router.ts +9 -11
- package/test/utils/mock/Transport.ts +2 -2
- package/test/utils/utils.ts +40 -49
- package/dist/src/dht/DhtPeer.js.map +0 -1
- package/dist/src/helpers/browser/isBrowser.d.ts +0 -1
- package/dist/src/helpers/browser/isBrowser.js +0 -6
- package/dist/src/helpers/browser/isBrowser.js.map +0 -1
- package/dist/src/helpers/browser/isNodeJS.d.ts +0 -1
- package/dist/src/helpers/browser/isNodeJS.js +0 -6
- package/dist/src/helpers/browser/isNodeJS.js.map +0 -1
- package/src/helpers/browser/isBrowser.ts +0 -1
- package/src/helpers/browser/isNodeJS.ts +0 -1
- package/test/integration/DhtWithMockConnectionLatencies.test.ts +0 -46
- package/test/integration/DhtWithMockConnections.test.ts +0 -46
- package/test/integration/DhtWithRealConnectionLatencies.test.ts +0 -47
- /package/test/unit/{webrtcReplaceInternalIpWithExternalIp.ts → webrtcReplaceInternalIpWithExternalIp.test.ts} +0 -0
|
@@ -23,39 +23,22 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.ConnectionManager = exports.
|
|
27
|
-
const
|
|
28
|
-
const DhtRpc_1 = require("../proto/packages/dht/protos/DhtRpc");
|
|
29
|
-
const WebSocketConnector_1 = require("./WebSocket/WebSocketConnector");
|
|
30
|
-
const protoToString_1 = require("../helpers/protoToString");
|
|
31
|
-
const WebRtcConnector_1 = require("./WebRTC/WebRtcConnector");
|
|
26
|
+
exports.ConnectionManager = exports.keyOrUnknownFromPeerDescriptor = exports.NatType = void 0;
|
|
27
|
+
const proto_rpc_1 = require("@streamr/proto-rpc");
|
|
32
28
|
const utils_1 = require("@streamr/utils");
|
|
29
|
+
const eventemitter3_1 = require("eventemitter3");
|
|
30
|
+
const Contact_1 = require("../dht/contact/Contact");
|
|
31
|
+
const SortedContactList_1 = require("../dht/contact/SortedContactList");
|
|
32
|
+
const DuplicateDetector_1 = require("../dht/routing/DuplicateDetector");
|
|
33
33
|
const Err = __importStar(require("../helpers/errors"));
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const
|
|
34
|
+
const peerIdFromPeerDescriptor_1 = require("../helpers/peerIdFromPeerDescriptor");
|
|
35
|
+
const protoToString_1 = require("../helpers/protoToString");
|
|
36
|
+
const DhtRpc_1 = require("../proto/packages/dht/protos/DhtRpc");
|
|
37
37
|
const DhtRpc_client_1 = require("../proto/packages/dht/protos/DhtRpc.client");
|
|
38
|
-
const
|
|
39
|
-
const SimulatorConnector_1 = require("./Simulator/SimulatorConnector");
|
|
38
|
+
const RoutingRpcCommunicator_1 = require("../transport/RoutingRpcCommunicator");
|
|
40
39
|
const ConnectionLockHandler_1 = require("./ConnectionLockHandler");
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
const Contact_1 = require("../dht/contact/Contact");
|
|
44
|
-
const peerIdFromPeerDescriptor_1 = require("../helpers/peerIdFromPeerDescriptor");
|
|
45
|
-
class ConnectionManagerConfig {
|
|
46
|
-
constructor(conf) {
|
|
47
|
-
this.maxConnections = 80;
|
|
48
|
-
// assign given non-undefined config vars over defaults
|
|
49
|
-
let k;
|
|
50
|
-
for (k in conf) {
|
|
51
|
-
if (conf[k] === undefined) {
|
|
52
|
-
delete conf[k];
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
Object.assign(this, conf);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
exports.ConnectionManagerConfig = ConnectionManagerConfig;
|
|
40
|
+
const RemoteConnectionLocker_1 = require("./RemoteConnectionLocker");
|
|
41
|
+
const NodeWebRtcConnection_1 = require("./WebRTC/NodeWebRtcConnection");
|
|
59
42
|
var NatType;
|
|
60
43
|
(function (NatType) {
|
|
61
44
|
NatType["OPEN_INTERNET"] = "open_internet";
|
|
@@ -69,44 +52,62 @@ var ConnectionManagerState;
|
|
|
69
52
|
ConnectionManagerState["STOPPING"] = "stopping";
|
|
70
53
|
ConnectionManagerState["STOPPED"] = "stopped";
|
|
71
54
|
})(ConnectionManagerState || (ConnectionManagerState = {}));
|
|
55
|
+
const INTERNAL_SERVICE_ID = 'system/connection-manager';
|
|
56
|
+
// Form an string representation from a peer description which can be undefined. This output
|
|
57
|
+
// should only be used only for log output. TODO remove this method if we no longer use
|
|
58
|
+
// peerDescriptors which can be undefined, e.g.
|
|
59
|
+
// - if we refactor ConnectionManager so that it doesn't process handshake requests too early
|
|
60
|
+
// and therefore this.ownPeerDescriptor can't be undefine (NET-1129)
|
|
61
|
+
// - if the peerDescriptor of ManagedConnection is always available
|
|
62
|
+
// - if we create stricter types for incoming messages (message.sourceDescriptor or
|
|
63
|
+
// disconnectNotice.peerDescriptor)
|
|
64
|
+
// - if ManagedConnection#peerDescriptor is never undefined
|
|
65
|
+
const keyOrUnknownFromPeerDescriptor = (peerDescriptor) => {
|
|
66
|
+
if (peerDescriptor !== undefined) {
|
|
67
|
+
return (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
return 'unknown';
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
exports.keyOrUnknownFromPeerDescriptor = keyOrUnknownFromPeerDescriptor;
|
|
72
74
|
class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
73
|
-
constructor(
|
|
75
|
+
constructor(config) {
|
|
74
76
|
super();
|
|
75
|
-
this.
|
|
77
|
+
this.duplicateMessageDetector = new DuplicateDetector_1.DuplicateDetector(100000, 100);
|
|
76
78
|
this.locks = new ConnectionLockHandler_1.ConnectionLockHandler();
|
|
77
79
|
this.connections = new Map();
|
|
78
80
|
this.state = ConnectionManagerState.IDLE;
|
|
79
81
|
this.onConnected = (connection) => {
|
|
80
82
|
const peerDescriptor = connection.getPeerDescriptor();
|
|
81
83
|
this.emit('connected', peerDescriptor);
|
|
82
|
-
logger.trace(
|
|
84
|
+
logger.trace((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor) + ' onConnected() ' + connection.connectionType);
|
|
83
85
|
this.onConnectionCountChange();
|
|
84
86
|
};
|
|
85
87
|
this.onDisconnected = (connection, disconnectionType) => {
|
|
86
|
-
logger.trace(
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
const storedConnection = this.connections.get(hexKey);
|
|
88
|
+
logger.trace((0, exports.keyOrUnknownFromPeerDescriptor)(connection.getPeerDescriptor()) + ' onDisconnected() ' + disconnectionType);
|
|
89
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(connection.getPeerDescriptor());
|
|
90
|
+
const storedConnection = this.connections.get(peerIdKey);
|
|
90
91
|
if (storedConnection && storedConnection.connectionId.equals(connection.connectionId)) {
|
|
91
|
-
this.locks.clearAllLocks(
|
|
92
|
-
this.connections.delete(
|
|
93
|
-
logger.trace(
|
|
94
|
-
' deleted connection in onDisconnected() ' + disconnectionType);
|
|
92
|
+
this.locks.clearAllLocks(peerIdKey);
|
|
93
|
+
this.connections.delete(peerIdKey);
|
|
94
|
+
logger.trace((0, exports.keyOrUnknownFromPeerDescriptor)(connection.getPeerDescriptor())
|
|
95
|
+
+ ' deleted connection in onDisconnected() ' + disconnectionType);
|
|
95
96
|
this.emit('disconnected', connection.getPeerDescriptor(), disconnectionType);
|
|
96
97
|
this.onConnectionCountChange();
|
|
97
98
|
}
|
|
98
99
|
else {
|
|
99
|
-
logger.trace(
|
|
100
|
-
' onDisconnected() did nothing, no such connection in connectionManager');
|
|
100
|
+
logger.trace((0, exports.keyOrUnknownFromPeerDescriptor)(connection.getPeerDescriptor())
|
|
101
|
+
+ ' onDisconnected() did nothing, no such connection in connectionManager');
|
|
101
102
|
if (storedConnection) {
|
|
102
|
-
logger.trace(
|
|
103
|
-
' connectionIds do not match ' + storedConnection.connectionId + ' ' + connection.connectionId);
|
|
103
|
+
logger.trace((0, exports.keyOrUnknownFromPeerDescriptor)(connection.getPeerDescriptor())
|
|
104
|
+
+ ' connectionIds do not match ' + storedConnection.connectionId + ' ' + connection.connectionId);
|
|
104
105
|
}
|
|
105
106
|
}
|
|
106
107
|
};
|
|
107
|
-
this.config =
|
|
108
|
+
this.config = config;
|
|
108
109
|
this.onData = this.onData.bind(this);
|
|
109
|
-
this.
|
|
110
|
+
this.onIncomingConnection = this.onIncomingConnection.bind(this);
|
|
110
111
|
this.metricsContext = this.config.metricsContext || new utils_1.MetricsContext();
|
|
111
112
|
this.metrics = {
|
|
112
113
|
sendMessagesPerSecond: new utils_1.RateMetric(),
|
|
@@ -117,78 +118,46 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
117
118
|
connectionTotalFailureCount: new utils_1.CountMetric()
|
|
118
119
|
};
|
|
119
120
|
this.metricsContext.addMetrics('node', this.metrics);
|
|
120
|
-
|
|
121
|
-
logger.trace(`Creating SimulatorConnector`);
|
|
122
|
-
this.simulatorConnector = new SimulatorConnector_1.SimulatorConnector(ConnectionManager.PROTOCOL_VERSION, this.config.ownPeerDescriptor, this.config.simulator, this.incomingConnectionCallback);
|
|
123
|
-
this.config.simulator.addConnector(this.simulatorConnector);
|
|
124
|
-
this.ownPeerDescriptor = this.config.ownPeerDescriptor;
|
|
125
|
-
this.state = ConnectionManagerState.RUNNING;
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
logger.trace(`Creating WebSocketConnector`);
|
|
129
|
-
this.webSocketConnector = new WebSocketConnector_1.WebSocketConnector(ConnectionManager.PROTOCOL_VERSION, this.config.transportLayer, this.canConnect.bind(this), this.incomingConnectionCallback, this.config.websocketPortRange, this.config.websocketHost, this.config.entryPoints, this.config.tlsCertificate);
|
|
130
|
-
logger.trace(`Creating WebRTCConnector`);
|
|
131
|
-
this.webrtcConnector = new WebRtcConnector_1.WebRtcConnector({
|
|
132
|
-
rpcTransport: this.config.transportLayer,
|
|
133
|
-
protocolVersion: ConnectionManager.PROTOCOL_VERSION,
|
|
134
|
-
iceServers: this.config.iceServers,
|
|
135
|
-
allowPrivateAddresses: this.config.webrtcAllowPrivateAddresses,
|
|
136
|
-
bufferThresholdLow: this.config.webrtcDatachannelBufferThresholdLow,
|
|
137
|
-
bufferThresholdHigh: this.config.webrtcDatachannelBufferThresholdHigh,
|
|
138
|
-
connectionTimeout: this.config.webrtcNewConnectionTimeout,
|
|
139
|
-
externalIp: this.config.externalIp,
|
|
140
|
-
portRange: this.config.webrtcPortRange
|
|
141
|
-
}, this.incomingConnectionCallback);
|
|
142
|
-
}
|
|
143
|
-
this.serviceId = (this.config.serviceIdPrefix ? this.config.serviceIdPrefix : '') + 'ConnectionManager';
|
|
121
|
+
this.connectorFacade = this.config.createConnectorFacade();
|
|
144
122
|
this.send = this.send.bind(this);
|
|
145
|
-
this.rpcCommunicator = new RoutingRpcCommunicator_1.RoutingRpcCommunicator(
|
|
123
|
+
this.rpcCommunicator = new RoutingRpcCommunicator_1.RoutingRpcCommunicator(INTERNAL_SERVICE_ID, this.send, {
|
|
146
124
|
rpcRequestTimeout: 10000
|
|
147
125
|
});
|
|
148
|
-
this.rpcCommunicator.registerRpcMethod(DhtRpc_1.LockRequest, DhtRpc_1.LockResponse, 'lockRequest', (req
|
|
149
|
-
this.rpcCommunicator.registerRpcNotification(DhtRpc_1.UnlockRequest, 'unlockRequest', (req
|
|
150
|
-
this.rpcCommunicator.registerRpcMethod(DhtRpc_1.DisconnectNotice, DhtRpc_1.DisconnectNoticeResponse, 'gracefulDisconnect', (req
|
|
126
|
+
this.rpcCommunicator.registerRpcMethod(DhtRpc_1.LockRequest, DhtRpc_1.LockResponse, 'lockRequest', (req) => this.lockRequest(req));
|
|
127
|
+
this.rpcCommunicator.registerRpcNotification(DhtRpc_1.UnlockRequest, 'unlockRequest', (req) => this.unlockRequest(req));
|
|
128
|
+
this.rpcCommunicator.registerRpcMethod(DhtRpc_1.DisconnectNotice, DhtRpc_1.DisconnectNoticeResponse, 'gracefulDisconnect', (req) => this.gracefulDisconnect(req));
|
|
151
129
|
}
|
|
152
130
|
garbageCollectConnections(maxConnections, lastUsedLimit) {
|
|
153
131
|
if (this.connections.size <= maxConnections) {
|
|
154
132
|
return;
|
|
155
133
|
}
|
|
156
|
-
const disconnectionCandidates = new SortedContactList_1.SortedContactList((0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(this.
|
|
134
|
+
const disconnectionCandidates = new SortedContactList_1.SortedContactList((0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(this.getOwnPeerDescriptor()), 100000);
|
|
157
135
|
this.connections.forEach((connection) => {
|
|
158
136
|
if (!this.locks.isLocked(connection.peerIdKey) && Date.now() - connection.getLastUsed() > lastUsedLimit) {
|
|
159
|
-
logger.trace('disconnecting in timeout interval: ' +
|
|
160
|
-
+ connection.getPeerDescriptor()?.nodeName + ' ');
|
|
137
|
+
logger.trace('disconnecting in timeout interval: ' + (0, exports.keyOrUnknownFromPeerDescriptor)(connection.getPeerDescriptor()));
|
|
161
138
|
disconnectionCandidates.addContact(new Contact_1.Contact(connection.getPeerDescriptor()));
|
|
162
139
|
}
|
|
163
140
|
});
|
|
164
141
|
const sortedCandidates = disconnectionCandidates.getAllContacts();
|
|
165
142
|
const targetNum = this.connections.size - maxConnections;
|
|
166
143
|
for (let i = 0; i < sortedCandidates.length && i < targetNum; i++) {
|
|
167
|
-
logger.trace(
|
|
168
|
-
+ sortedCandidates[sortedCandidates.length - 1 - i].getPeerDescriptor().nodeName);
|
|
144
|
+
logger.trace('garbageCollecting ' + (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(sortedCandidates[sortedCandidates.length - 1 - i].getPeerDescriptor()));
|
|
169
145
|
this.gracefullyDisconnectAsync(sortedCandidates[sortedCandidates.length - 1 - i].getPeerDescriptor(), DhtRpc_1.DisconnectMode.NORMAL).catch((_e) => { });
|
|
170
146
|
}
|
|
171
147
|
}
|
|
172
|
-
async start(
|
|
148
|
+
async start() {
|
|
173
149
|
if (this.state === ConnectionManagerState.RUNNING || this.state === ConnectionManagerState.STOPPED) {
|
|
174
150
|
throw new Err.CouldNotStart(`Cannot start already ${this.state} module`);
|
|
175
151
|
}
|
|
176
152
|
this.state = ConnectionManagerState.RUNNING;
|
|
177
153
|
logger.trace(`Starting ConnectionManager...`);
|
|
154
|
+
await this.connectorFacade.start((connection) => this.onIncomingConnection(connection), (peerDescriptor) => this.canConnect(peerDescriptor));
|
|
178
155
|
// Garbage collection of connections
|
|
179
156
|
this.disconnectorIntervalRef = setInterval(() => {
|
|
180
157
|
logger.trace('disconnectorInterval');
|
|
181
158
|
const LAST_USED_LIMIT = 20000;
|
|
182
|
-
this.garbageCollectConnections(this.config.maxConnections, LAST_USED_LIMIT);
|
|
159
|
+
this.garbageCollectConnections(this.config.maxConnections ?? 80, LAST_USED_LIMIT);
|
|
183
160
|
}, 5000);
|
|
184
|
-
if (!this.config.simulator) {
|
|
185
|
-
await this.webSocketConnector.start();
|
|
186
|
-
const connectivityResponse = await this.webSocketConnector.checkConnectivity();
|
|
187
|
-
const ownPeerDescriptor = peerDescriptorGeneratorCallback(connectivityResponse);
|
|
188
|
-
this.ownPeerDescriptor = ownPeerDescriptor;
|
|
189
|
-
this.webSocketConnector.setOwnPeerDescriptor(ownPeerDescriptor);
|
|
190
|
-
this.webrtcConnector.setOwnPeerDescriptor(ownPeerDescriptor);
|
|
191
|
-
}
|
|
192
161
|
}
|
|
193
162
|
async stop() {
|
|
194
163
|
if (this.state === ConnectionManagerState.STOPPED || this.state === ConnectionManagerState.STOPPING) {
|
|
@@ -199,16 +168,7 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
199
168
|
if (this.disconnectorIntervalRef) {
|
|
200
169
|
clearInterval(this.disconnectorIntervalRef);
|
|
201
170
|
}
|
|
202
|
-
|
|
203
|
-
await this.webSocketConnector.destroy();
|
|
204
|
-
this.webSocketConnector = undefined;
|
|
205
|
-
await this.webrtcConnector.stop();
|
|
206
|
-
this.webrtcConnector = undefined;
|
|
207
|
-
}
|
|
208
|
-
else {
|
|
209
|
-
await this.simulatorConnector.stop();
|
|
210
|
-
this.simulatorConnector = undefined;
|
|
211
|
-
}
|
|
171
|
+
await this.connectorFacade.stop();
|
|
212
172
|
await Promise.all(Array.from(this.connections.values()).map(async (peer) => {
|
|
213
173
|
if (peer.isHandshakeCompleted()) {
|
|
214
174
|
try {
|
|
@@ -233,16 +193,13 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
233
193
|
}));
|
|
234
194
|
this.state = ConnectionManagerState.STOPPED;
|
|
235
195
|
this.rpcCommunicator.stop();
|
|
236
|
-
this.
|
|
237
|
-
this.messageDuplicateDetector.clear();
|
|
196
|
+
this.duplicateMessageDetector.clear();
|
|
238
197
|
this.locks.clear();
|
|
239
198
|
this.removeAllListeners();
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
getConnectionTo(id) {
|
|
245
|
-
return this.connections.get(id);
|
|
199
|
+
// TODO would it make sense to move this call to WebRtcConnector#stop()?
|
|
200
|
+
// - but note that we should call this only after connections have been closed
|
|
201
|
+
// (i.e the this.gracefullyDisconnectAsync() calls above)
|
|
202
|
+
NodeWebRtcConnection_1.WEB_RTC_CLEANUP.cleanUp();
|
|
246
203
|
}
|
|
247
204
|
getNumberOfLocalLockedConnections() {
|
|
248
205
|
return this.locks.getNumberOfLocalLockedConnections();
|
|
@@ -261,17 +218,17 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
261
218
|
if (this.isConnectionToSelf(peerDescriptor)) {
|
|
262
219
|
throw new Err.CannotConnectToSelf('Cannot send to self');
|
|
263
220
|
}
|
|
264
|
-
logger.trace(`Sending message to: ${
|
|
221
|
+
logger.trace(`Sending message to: ${(0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor)}`);
|
|
265
222
|
message = {
|
|
266
223
|
...message,
|
|
267
224
|
targetDescriptor: message.targetDescriptor || peerDescriptor,
|
|
268
|
-
sourceDescriptor: message.sourceDescriptor || this.
|
|
225
|
+
sourceDescriptor: message.sourceDescriptor || this.getOwnPeerDescriptor(),
|
|
269
226
|
};
|
|
270
|
-
const
|
|
271
|
-
let connection = this.connections.get(
|
|
227
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
228
|
+
let connection = this.connections.get(peerIdKey);
|
|
272
229
|
if (!connection && !doNotConnect) {
|
|
273
|
-
connection = this.createConnection(peerDescriptor);
|
|
274
|
-
this.
|
|
230
|
+
connection = this.connectorFacade.createConnection(peerDescriptor);
|
|
231
|
+
this.onIncomingConnection(connection);
|
|
275
232
|
}
|
|
276
233
|
else if (!connection) {
|
|
277
234
|
throw new Err.SendFailed('No connection to target, doNotConnect flag is true');
|
|
@@ -282,48 +239,42 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
282
239
|
return connection.send(binary, doNotConnect);
|
|
283
240
|
}
|
|
284
241
|
isConnectionToSelf(peerDescriptor) {
|
|
285
|
-
return (0, peerIdFromPeerDescriptor_1.
|
|
242
|
+
return (0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(peerDescriptor, this.getOwnPeerDescriptor()) || this.isOwnWebSocketServer(peerDescriptor);
|
|
286
243
|
}
|
|
287
244
|
isOwnWebSocketServer(peerDescriptor) {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
245
|
+
const ownPeerDescriptor = this.getOwnPeerDescriptor();
|
|
246
|
+
if ((peerDescriptor.websocket !== undefined) && (ownPeerDescriptor.websocket !== undefined)) {
|
|
247
|
+
return ((peerDescriptor.websocket.port === ownPeerDescriptor.websocket.port)
|
|
248
|
+
&& (peerDescriptor.websocket.host === ownPeerDescriptor.websocket.host));
|
|
291
249
|
}
|
|
292
250
|
else {
|
|
293
251
|
return false;
|
|
294
252
|
}
|
|
295
253
|
}
|
|
296
|
-
createConnection(peerDescriptor) {
|
|
297
|
-
if (this.simulatorConnector) {
|
|
298
|
-
return this.simulatorConnector.connect(peerDescriptor);
|
|
299
|
-
}
|
|
300
|
-
else if (peerDescriptor.websocket || this.ownPeerDescriptor.websocket) {
|
|
301
|
-
if (!(peerDescriptor.type === DhtRpc_1.NodeType.BROWSER && this.ownPeerDescriptor.websocket?.tls)) {
|
|
302
|
-
return this.webSocketConnector.connect(peerDescriptor);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
return this.webrtcConnector.connect(peerDescriptor);
|
|
306
|
-
}
|
|
307
254
|
getConnection(peerDescriptor) {
|
|
308
|
-
const
|
|
309
|
-
return this.connections.get(
|
|
255
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
256
|
+
return this.connections.get(peerIdKey);
|
|
310
257
|
}
|
|
258
|
+
// TODO remove this method or getOwnPeerDescriptor
|
|
311
259
|
getPeerDescriptor() {
|
|
312
|
-
return this.
|
|
260
|
+
return this.getOwnPeerDescriptor();
|
|
261
|
+
}
|
|
262
|
+
getOwnPeerDescriptor() {
|
|
263
|
+
return this.connectorFacade.getOwnPeerDescriptor();
|
|
313
264
|
}
|
|
314
265
|
hasConnection(peerDescriptor) {
|
|
315
|
-
const
|
|
316
|
-
return this.connections.has(
|
|
266
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
267
|
+
return this.connections.has(peerIdKey);
|
|
317
268
|
}
|
|
318
|
-
hasLocalLockedConnection(peerDescriptor
|
|
319
|
-
const
|
|
320
|
-
return this.locks.isLocalLocked(
|
|
269
|
+
hasLocalLockedConnection(peerDescriptor) {
|
|
270
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
271
|
+
return this.locks.isLocalLocked(peerIdKey);
|
|
321
272
|
}
|
|
322
|
-
hasRemoteLockedConnection(peerDescriptor
|
|
323
|
-
const
|
|
324
|
-
return this.locks.isRemoteLocked(
|
|
273
|
+
hasRemoteLockedConnection(peerDescriptor) {
|
|
274
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
275
|
+
return this.locks.isRemoteLocked(peerIdKey);
|
|
325
276
|
}
|
|
326
|
-
canConnect(peerDescriptor
|
|
277
|
+
canConnect(peerDescriptor) {
|
|
327
278
|
// Perhaps the connection's state should be checked here
|
|
328
279
|
return !this.hasConnection(peerDescriptor); // TODO: Add port range check
|
|
329
280
|
}
|
|
@@ -333,18 +284,17 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
333
284
|
logger.trace('Filtered out non-RPC message of type ' + message.messageType);
|
|
334
285
|
return;
|
|
335
286
|
}
|
|
336
|
-
if (this.
|
|
337
|
-
logger.trace('handleMessage filtered duplicate ' +
|
|
338
|
-
+
|
|
287
|
+
if (this.duplicateMessageDetector.isMostLikelyDuplicate(message.messageId)) {
|
|
288
|
+
logger.trace('handleMessage filtered duplicate ' + (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(message.sourceDescriptor)
|
|
289
|
+
+ ' ' + message.serviceId + ' ' + message.messageId);
|
|
339
290
|
return;
|
|
340
291
|
}
|
|
341
|
-
this.
|
|
342
|
-
if (message.serviceId ===
|
|
292
|
+
this.duplicateMessageDetector.add(message.messageId);
|
|
293
|
+
if (message.serviceId === INTERNAL_SERVICE_ID) {
|
|
343
294
|
this.rpcCommunicator?.handleMessageFromPeer(message);
|
|
344
295
|
}
|
|
345
296
|
else {
|
|
346
|
-
logger.trace('emit "message" ' +
|
|
347
|
-
+ ' ' + message.serviceId + ' ' + message.messageId);
|
|
297
|
+
logger.trace('emit "message" ' + (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(message.sourceDescriptor) + ' ' + message.serviceId + ' ' + message.messageId);
|
|
348
298
|
this.emit('message', message);
|
|
349
299
|
}
|
|
350
300
|
}
|
|
@@ -357,7 +307,7 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
357
307
|
let message;
|
|
358
308
|
try {
|
|
359
309
|
message = DhtRpc_1.Message.fromBinary(data);
|
|
360
|
-
logger.trace(
|
|
310
|
+
logger.trace(`received protojson: ${(0, protoToString_1.protoToString)(message, DhtRpc_1.Message)}`);
|
|
361
311
|
}
|
|
362
312
|
catch (e) {
|
|
363
313
|
logger.debug(`Parsing incoming data into Message failed: ${e}`);
|
|
@@ -371,17 +321,17 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
371
321
|
logger.debug(`Handling incoming data failed: ${e}`);
|
|
372
322
|
}
|
|
373
323
|
}
|
|
374
|
-
|
|
324
|
+
onIncomingConnection(connection) {
|
|
375
325
|
if (this.state === ConnectionManagerState.STOPPED) {
|
|
376
326
|
return false;
|
|
377
327
|
}
|
|
378
|
-
logger.trace('
|
|
328
|
+
logger.trace('onIncomingConnection()');
|
|
379
329
|
connection.offeredAsIncoming = true;
|
|
380
330
|
if (!this.acceptIncomingConnection(connection)) {
|
|
381
331
|
return false;
|
|
382
332
|
}
|
|
383
333
|
connection.on('managedData', this.onData);
|
|
384
|
-
connection.on('disconnected', (disconnectionType
|
|
334
|
+
connection.on('disconnected', (disconnectionType) => {
|
|
385
335
|
this.onDisconnected(connection, disconnectionType);
|
|
386
336
|
});
|
|
387
337
|
this.emit('newConnection', connection);
|
|
@@ -389,23 +339,23 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
389
339
|
this.onConnected(connection);
|
|
390
340
|
}
|
|
391
341
|
else {
|
|
392
|
-
connection.once('handshakeCompleted', (
|
|
342
|
+
connection.once('handshakeCompleted', () => {
|
|
393
343
|
this.onConnected(connection);
|
|
394
344
|
});
|
|
395
345
|
}
|
|
396
346
|
return true;
|
|
397
347
|
}
|
|
398
348
|
acceptIncomingConnection(newConnection) {
|
|
399
|
-
logger.trace(
|
|
349
|
+
logger.trace((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(newConnection.getPeerDescriptor()) + ' acceptIncomingConnection()');
|
|
400
350
|
const newPeerID = (0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(newConnection.getPeerDescriptor());
|
|
401
|
-
const
|
|
402
|
-
if (this.connections.has(
|
|
403
|
-
if (newPeerID.hasSmallerHashThan((0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(this.
|
|
404
|
-
logger.trace(
|
|
405
|
-
' acceptIncomingConnection() replace current connection');
|
|
351
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(newConnection.getPeerDescriptor());
|
|
352
|
+
if (this.connections.has(peerIdKey)) {
|
|
353
|
+
if (newPeerID.hasSmallerHashThan((0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(this.getOwnPeerDescriptor()))) {
|
|
354
|
+
logger.trace((0, exports.keyOrUnknownFromPeerDescriptor)(newConnection.getPeerDescriptor())
|
|
355
|
+
+ ' acceptIncomingConnection() replace current connection');
|
|
406
356
|
// replace the current connection
|
|
407
357
|
const oldConnection = this.connections.get(newPeerID.toKey());
|
|
408
|
-
logger.trace('replaced: ' +
|
|
358
|
+
logger.trace('replaced: ' + (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(newConnection.getPeerDescriptor()));
|
|
409
359
|
const buffer = oldConnection.stealOutputBuffer();
|
|
410
360
|
for (const data of buffer) {
|
|
411
361
|
newConnection.sendNoWait(data);
|
|
@@ -418,13 +368,12 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
418
368
|
return false;
|
|
419
369
|
}
|
|
420
370
|
}
|
|
421
|
-
logger.trace(
|
|
422
|
-
|
|
423
|
-
this.connections.set(hexKey, newConnection);
|
|
371
|
+
logger.trace((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(newConnection.getPeerDescriptor()) + ' added to connections at acceptIncomingConnection');
|
|
372
|
+
this.connections.set(peerIdKey, newConnection);
|
|
424
373
|
return true;
|
|
425
374
|
}
|
|
426
375
|
async closeConnection(peerDescriptor, disconnectionType, reason) {
|
|
427
|
-
logger.trace(
|
|
376
|
+
logger.trace((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor) + ' ' + 'closeConnection() ' + reason);
|
|
428
377
|
const id = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
429
378
|
this.locks.clearAllLocks(id);
|
|
430
379
|
if (this.connections.has(id)) {
|
|
@@ -432,46 +381,45 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
432
381
|
await connectionToClose.close(disconnectionType);
|
|
433
382
|
}
|
|
434
383
|
else {
|
|
435
|
-
logger.trace(
|
|
436
|
-
'closeConnection() this.connections did not have the id');
|
|
384
|
+
logger.trace((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor) + ' ' + 'closeConnection() this.connections did not have the id');
|
|
437
385
|
this.emit('disconnected', peerDescriptor, 'OTHER');
|
|
438
386
|
}
|
|
439
387
|
}
|
|
440
388
|
lockConnection(targetDescriptor, serviceId) {
|
|
441
|
-
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.
|
|
389
|
+
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(targetDescriptor, this.getOwnPeerDescriptor())) {
|
|
442
390
|
return;
|
|
443
391
|
}
|
|
444
|
-
const
|
|
445
|
-
const remoteConnectionLocker = new RemoteConnectionLocker_1.RemoteConnectionLocker(this.
|
|
446
|
-
this.locks.addLocalLocked(
|
|
392
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(targetDescriptor);
|
|
393
|
+
const remoteConnectionLocker = new RemoteConnectionLocker_1.RemoteConnectionLocker(this.getOwnPeerDescriptor(), targetDescriptor, ConnectionManager.PROTOCOL_VERSION, (0, proto_rpc_1.toProtoRpcClient)(new DhtRpc_client_1.ConnectionLockerClient(this.rpcCommunicator.getRpcClientTransport())));
|
|
394
|
+
this.locks.addLocalLocked(peerIdKey, serviceId);
|
|
447
395
|
remoteConnectionLocker.lockRequest(serviceId)
|
|
448
396
|
.then((_accepted) => logger.trace('LockRequest successful'))
|
|
449
397
|
.catch((err) => { logger.debug(err); });
|
|
450
398
|
}
|
|
451
399
|
unlockConnection(targetDescriptor, serviceId) {
|
|
452
|
-
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.
|
|
400
|
+
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(targetDescriptor, this.getOwnPeerDescriptor())) {
|
|
453
401
|
return;
|
|
454
402
|
}
|
|
455
|
-
const
|
|
456
|
-
this.locks.removeLocalLocked(
|
|
457
|
-
const remoteConnectionLocker = new RemoteConnectionLocker_1.RemoteConnectionLocker(this.
|
|
458
|
-
if (this.connections.has(
|
|
403
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(targetDescriptor);
|
|
404
|
+
this.locks.removeLocalLocked(peerIdKey, serviceId);
|
|
405
|
+
const remoteConnectionLocker = new RemoteConnectionLocker_1.RemoteConnectionLocker(this.getOwnPeerDescriptor(), targetDescriptor, ConnectionManager.PROTOCOL_VERSION, (0, proto_rpc_1.toProtoRpcClient)(new DhtRpc_client_1.ConnectionLockerClient(this.rpcCommunicator.getRpcClientTransport())));
|
|
406
|
+
if (this.connections.has(peerIdKey)) {
|
|
459
407
|
remoteConnectionLocker.unlockRequest(serviceId);
|
|
460
408
|
}
|
|
461
409
|
}
|
|
462
410
|
weakLockConnection(targetDescriptor) {
|
|
463
|
-
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.
|
|
411
|
+
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(targetDescriptor, this.getOwnPeerDescriptor())) {
|
|
464
412
|
return;
|
|
465
413
|
}
|
|
466
|
-
const
|
|
467
|
-
this.locks.addWeakLocked(
|
|
414
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(targetDescriptor);
|
|
415
|
+
this.locks.addWeakLocked(peerIdKey);
|
|
468
416
|
}
|
|
469
417
|
weakUnlockConnection(targetDescriptor) {
|
|
470
|
-
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.
|
|
418
|
+
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(targetDescriptor, this.getOwnPeerDescriptor())) {
|
|
471
419
|
return;
|
|
472
420
|
}
|
|
473
|
-
const
|
|
474
|
-
this.locks.removeWeakLocked(
|
|
421
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(targetDescriptor);
|
|
422
|
+
this.locks.removeWeakLocked(peerIdKey);
|
|
475
423
|
}
|
|
476
424
|
async gracefullyDisconnectAsync(targetDescriptor, disconnectMode) {
|
|
477
425
|
const connection = this.connections.get((0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(targetDescriptor).toKey());
|
|
@@ -502,14 +450,13 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
502
450
|
await promise;
|
|
503
451
|
}
|
|
504
452
|
async doGracefullyDisconnectAsync(targetDescriptor, disconnectMode) {
|
|
505
|
-
logger.trace(
|
|
506
|
-
const remoteConnectionLocker = new RemoteConnectionLocker_1.RemoteConnectionLocker(this.
|
|
453
|
+
logger.trace((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(targetDescriptor) + ' gracefullyDisconnectAsync()');
|
|
454
|
+
const remoteConnectionLocker = new RemoteConnectionLocker_1.RemoteConnectionLocker(this.getOwnPeerDescriptor(), targetDescriptor, ConnectionManager.PROTOCOL_VERSION, (0, proto_rpc_1.toProtoRpcClient)(new DhtRpc_client_1.ConnectionLockerClient(this.rpcCommunicator.getRpcClientTransport())));
|
|
507
455
|
try {
|
|
508
456
|
await remoteConnectionLocker.gracefulDisconnect(disconnectMode);
|
|
509
457
|
}
|
|
510
458
|
catch (ex) {
|
|
511
|
-
logger.trace(
|
|
512
|
-
' remoteConnectionLocker.gracefulDisconnect() failed' + ex);
|
|
459
|
+
logger.trace((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(targetDescriptor) + ' remoteConnectionLocker.gracefulDisconnect() failed' + ex);
|
|
513
460
|
}
|
|
514
461
|
}
|
|
515
462
|
getAllConnectionPeerDescriptors() {
|
|
@@ -518,9 +465,9 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
518
465
|
.map((managedConnection) => managedConnection.getPeerDescriptor());
|
|
519
466
|
}
|
|
520
467
|
// IConnectionLocker server implementation
|
|
521
|
-
async lockRequest(lockRequest
|
|
468
|
+
async lockRequest(lockRequest) {
|
|
522
469
|
const remotePeerId = (0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(lockRequest.peerDescriptor);
|
|
523
|
-
if ((0, peerIdFromPeerDescriptor_1.
|
|
470
|
+
if ((0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(lockRequest.peerDescriptor, this.getOwnPeerDescriptor())) {
|
|
524
471
|
const response = {
|
|
525
472
|
accepted: false
|
|
526
473
|
};
|
|
@@ -533,15 +480,14 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
533
480
|
return response;
|
|
534
481
|
}
|
|
535
482
|
// IConnectionLocker server implementation
|
|
536
|
-
async unlockRequest(unlockRequest
|
|
537
|
-
const
|
|
538
|
-
this.locks.removeRemoteLocked(
|
|
483
|
+
async unlockRequest(unlockRequest) {
|
|
484
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(unlockRequest.peerDescriptor);
|
|
485
|
+
this.locks.removeRemoteLocked(peerIdKey, unlockRequest.serviceId);
|
|
539
486
|
return {};
|
|
540
487
|
}
|
|
541
488
|
// IConnectionLocker server implementation
|
|
542
|
-
async gracefulDisconnect(disconnectNotice
|
|
543
|
-
logger.trace(
|
|
544
|
-
+ ' received gracefulDisconnect notice');
|
|
489
|
+
async gracefulDisconnect(disconnectNotice) {
|
|
490
|
+
logger.trace((0, exports.keyOrUnknownFromPeerDescriptor)(disconnectNotice.peerDescriptor) + ' received gracefulDisconnect notice');
|
|
545
491
|
if (disconnectNotice.disconnecMode === DhtRpc_1.DisconnectMode.LEAVING) {
|
|
546
492
|
this.closeConnection(disconnectNotice.peerDescriptor, 'INCOMING_GRACEFUL_LEAVE', 'graceful leave notified');
|
|
547
493
|
}
|