@streamr/dht 0.0.1-tatum.6 → 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 -40
- package/dist/src/connection/ConnectionManager.js +138 -204
- 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 +15 -51
- package/dist/src/dht/DhtNode.js +107 -129
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/{DhtPeer.d.ts → RemoteDhtNode.d.ts} +2 -3
- package/dist/src/dht/{DhtPeer.js → RemoteDhtNode.js} +21 -19
- package/dist/src/dht/RemoteDhtNode.js.map +1 -0
- package/dist/src/dht/contact/ContactList.d.ts +0 -1
- package/dist/src/dht/contact/ContactList.js +0 -3
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/RandomContactList.d.ts +0 -1
- package/dist/src/dht/contact/RandomContactList.js +0 -3
- package/dist/src/dht/contact/RandomContactList.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +0 -3
- package/dist/src/dht/contact/SortedContactList.js +0 -9
- 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 +1 -1
- 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 -4
- package/dist/src/dht/routing/RoutingSession.js +4 -3
- 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 -7
- package/dist/src/exports.js +2 -14
- 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 +157 -254
- 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 +155 -181
- package/src/dht/{DhtPeer.ts → RemoteDhtNode.ts} +11 -9
- package/src/dht/contact/ContactList.ts +0 -4
- package/src/dht/contact/RandomContactList.ts +0 -4
- package/src/dht/contact/SortedContactList.ts +0 -12
- 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 +2 -2
- package/src/dht/routing/Router.ts +35 -39
- package/src/dht/routing/RoutingSession.ts +9 -9
- package/src/dht/store/DataStore.ts +11 -11
- package/src/exports.ts +1 -7
- 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 +4 -2
- package/test/benchmark/RecursiveFind.test.ts +6 -6
- package/test/end-to-end/Layer0-Layer1.test.ts +9 -9
- 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 +1 -1
- package/test/unit/RecursiveFinder.test.ts +8 -12
- package/test/unit/Router.test.ts +18 -21
- 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,40 +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
|
-
const AddressTools_1 = require("../helpers/AddressTools");
|
|
46
|
-
class ConnectionManagerConfig {
|
|
47
|
-
constructor(conf) {
|
|
48
|
-
this.maxConnections = 80;
|
|
49
|
-
// assign given non-undefined config vars over defaults
|
|
50
|
-
let k;
|
|
51
|
-
for (k in conf) {
|
|
52
|
-
if (conf[k] === undefined) {
|
|
53
|
-
delete conf[k];
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
Object.assign(this, conf);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
exports.ConnectionManagerConfig = ConnectionManagerConfig;
|
|
40
|
+
const RemoteConnectionLocker_1 = require("./RemoteConnectionLocker");
|
|
41
|
+
const NodeWebRtcConnection_1 = require("./WebRTC/NodeWebRtcConnection");
|
|
60
42
|
var NatType;
|
|
61
43
|
(function (NatType) {
|
|
62
44
|
NatType["OPEN_INTERNET"] = "open_internet";
|
|
@@ -70,44 +52,62 @@ var ConnectionManagerState;
|
|
|
70
52
|
ConnectionManagerState["STOPPING"] = "stopping";
|
|
71
53
|
ConnectionManagerState["STOPPED"] = "stopped";
|
|
72
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;
|
|
73
74
|
class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
74
|
-
constructor(
|
|
75
|
+
constructor(config) {
|
|
75
76
|
super();
|
|
76
|
-
this.
|
|
77
|
+
this.duplicateMessageDetector = new DuplicateDetector_1.DuplicateDetector(100000, 100);
|
|
77
78
|
this.locks = new ConnectionLockHandler_1.ConnectionLockHandler();
|
|
78
79
|
this.connections = new Map();
|
|
79
80
|
this.state = ConnectionManagerState.IDLE;
|
|
80
81
|
this.onConnected = (connection) => {
|
|
81
82
|
const peerDescriptor = connection.getPeerDescriptor();
|
|
82
83
|
this.emit('connected', peerDescriptor);
|
|
83
|
-
logger.trace(
|
|
84
|
+
logger.trace((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor) + ' onConnected() ' + connection.connectionType);
|
|
84
85
|
this.onConnectionCountChange();
|
|
85
86
|
};
|
|
86
87
|
this.onDisconnected = (connection, disconnectionType) => {
|
|
87
|
-
logger.trace(
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
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);
|
|
91
91
|
if (storedConnection && storedConnection.connectionId.equals(connection.connectionId)) {
|
|
92
|
-
this.locks.clearAllLocks(
|
|
93
|
-
this.connections.delete(
|
|
94
|
-
logger.trace(
|
|
95
|
-
' 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);
|
|
96
96
|
this.emit('disconnected', connection.getPeerDescriptor(), disconnectionType);
|
|
97
97
|
this.onConnectionCountChange();
|
|
98
98
|
}
|
|
99
99
|
else {
|
|
100
|
-
logger.trace(
|
|
101
|
-
' 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');
|
|
102
102
|
if (storedConnection) {
|
|
103
|
-
logger.trace(
|
|
104
|
-
' 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);
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
};
|
|
108
|
-
this.config =
|
|
108
|
+
this.config = config;
|
|
109
109
|
this.onData = this.onData.bind(this);
|
|
110
|
-
this.
|
|
110
|
+
this.onIncomingConnection = this.onIncomingConnection.bind(this);
|
|
111
111
|
this.metricsContext = this.config.metricsContext || new utils_1.MetricsContext();
|
|
112
112
|
this.metrics = {
|
|
113
113
|
sendMessagesPerSecond: new utils_1.RateMetric(),
|
|
@@ -118,78 +118,46 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
118
118
|
connectionTotalFailureCount: new utils_1.CountMetric()
|
|
119
119
|
};
|
|
120
120
|
this.metricsContext.addMetrics('node', this.metrics);
|
|
121
|
-
|
|
122
|
-
logger.trace(`Creating SimulatorConnector`);
|
|
123
|
-
this.simulatorConnector = new SimulatorConnector_1.SimulatorConnector(ConnectionManager.PROTOCOL_VERSION, this.config.ownPeerDescriptor, this.config.simulator, this.incomingConnectionCallback);
|
|
124
|
-
this.config.simulator.addConnector(this.simulatorConnector);
|
|
125
|
-
this.ownPeerDescriptor = this.config.ownPeerDescriptor;
|
|
126
|
-
this.state = ConnectionManagerState.RUNNING;
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
logger.trace(`Creating WebSocketConnector`);
|
|
130
|
-
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);
|
|
131
|
-
logger.trace(`Creating WebRTCConnector`);
|
|
132
|
-
this.webrtcConnector = new WebRtcConnector_1.WebRtcConnector({
|
|
133
|
-
rpcTransport: this.config.transportLayer,
|
|
134
|
-
protocolVersion: ConnectionManager.PROTOCOL_VERSION,
|
|
135
|
-
iceServers: this.config.iceServers,
|
|
136
|
-
allowPrivateAddresses: this.config.webrtcAllowPrivateAddresses,
|
|
137
|
-
bufferThresholdLow: this.config.webrtcDatachannelBufferThresholdLow,
|
|
138
|
-
bufferThresholdHigh: this.config.webrtcDatachannelBufferThresholdHigh,
|
|
139
|
-
connectionTimeout: this.config.webrtcNewConnectionTimeout,
|
|
140
|
-
externalIp: this.config.externalIp,
|
|
141
|
-
portRange: this.config.webrtcPortRange
|
|
142
|
-
}, this.incomingConnectionCallback);
|
|
143
|
-
}
|
|
144
|
-
this.serviceId = (this.config.serviceIdPrefix ? this.config.serviceIdPrefix : '') + 'ConnectionManager';
|
|
121
|
+
this.connectorFacade = this.config.createConnectorFacade();
|
|
145
122
|
this.send = this.send.bind(this);
|
|
146
|
-
this.rpcCommunicator = new RoutingRpcCommunicator_1.RoutingRpcCommunicator(
|
|
123
|
+
this.rpcCommunicator = new RoutingRpcCommunicator_1.RoutingRpcCommunicator(INTERNAL_SERVICE_ID, this.send, {
|
|
147
124
|
rpcRequestTimeout: 10000
|
|
148
125
|
});
|
|
149
|
-
this.rpcCommunicator.registerRpcMethod(DhtRpc_1.LockRequest, DhtRpc_1.LockResponse, 'lockRequest', (req
|
|
150
|
-
this.rpcCommunicator.registerRpcNotification(DhtRpc_1.UnlockRequest, 'unlockRequest', (req
|
|
151
|
-
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));
|
|
152
129
|
}
|
|
153
130
|
garbageCollectConnections(maxConnections, lastUsedLimit) {
|
|
154
131
|
if (this.connections.size <= maxConnections) {
|
|
155
132
|
return;
|
|
156
133
|
}
|
|
157
|
-
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);
|
|
158
135
|
this.connections.forEach((connection) => {
|
|
159
136
|
if (!this.locks.isLocked(connection.peerIdKey) && Date.now() - connection.getLastUsed() > lastUsedLimit) {
|
|
160
|
-
logger.trace('disconnecting in timeout interval: ' +
|
|
161
|
-
+ connection.getPeerDescriptor()?.nodeName + ' ');
|
|
137
|
+
logger.trace('disconnecting in timeout interval: ' + (0, exports.keyOrUnknownFromPeerDescriptor)(connection.getPeerDescriptor()));
|
|
162
138
|
disconnectionCandidates.addContact(new Contact_1.Contact(connection.getPeerDescriptor()));
|
|
163
139
|
}
|
|
164
140
|
});
|
|
165
141
|
const sortedCandidates = disconnectionCandidates.getAllContacts();
|
|
166
142
|
const targetNum = this.connections.size - maxConnections;
|
|
167
143
|
for (let i = 0; i < sortedCandidates.length && i < targetNum; i++) {
|
|
168
|
-
logger.trace(
|
|
169
|
-
+ sortedCandidates[sortedCandidates.length - 1 - i].getPeerDescriptor().nodeName);
|
|
144
|
+
logger.trace('garbageCollecting ' + (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(sortedCandidates[sortedCandidates.length - 1 - i].getPeerDescriptor()));
|
|
170
145
|
this.gracefullyDisconnectAsync(sortedCandidates[sortedCandidates.length - 1 - i].getPeerDescriptor(), DhtRpc_1.DisconnectMode.NORMAL).catch((_e) => { });
|
|
171
146
|
}
|
|
172
147
|
}
|
|
173
|
-
async start(
|
|
148
|
+
async start() {
|
|
174
149
|
if (this.state === ConnectionManagerState.RUNNING || this.state === ConnectionManagerState.STOPPED) {
|
|
175
150
|
throw new Err.CouldNotStart(`Cannot start already ${this.state} module`);
|
|
176
151
|
}
|
|
177
152
|
this.state = ConnectionManagerState.RUNNING;
|
|
178
153
|
logger.trace(`Starting ConnectionManager...`);
|
|
154
|
+
await this.connectorFacade.start((connection) => this.onIncomingConnection(connection), (peerDescriptor) => this.canConnect(peerDescriptor));
|
|
179
155
|
// Garbage collection of connections
|
|
180
156
|
this.disconnectorIntervalRef = setInterval(() => {
|
|
181
157
|
logger.trace('disconnectorInterval');
|
|
182
158
|
const LAST_USED_LIMIT = 20000;
|
|
183
|
-
this.garbageCollectConnections(this.config.maxConnections, LAST_USED_LIMIT);
|
|
159
|
+
this.garbageCollectConnections(this.config.maxConnections ?? 80, LAST_USED_LIMIT);
|
|
184
160
|
}, 5000);
|
|
185
|
-
if (!this.config.simulator) {
|
|
186
|
-
await this.webSocketConnector.start();
|
|
187
|
-
const connectivityResponse = await this.webSocketConnector.checkConnectivity();
|
|
188
|
-
const ownPeerDescriptor = peerDescriptorGeneratorCallback(connectivityResponse);
|
|
189
|
-
this.ownPeerDescriptor = ownPeerDescriptor;
|
|
190
|
-
this.webSocketConnector.setOwnPeerDescriptor(ownPeerDescriptor);
|
|
191
|
-
this.webrtcConnector.setOwnPeerDescriptor(ownPeerDescriptor);
|
|
192
|
-
}
|
|
193
161
|
}
|
|
194
162
|
async stop() {
|
|
195
163
|
if (this.state === ConnectionManagerState.STOPPED || this.state === ConnectionManagerState.STOPPING) {
|
|
@@ -200,16 +168,7 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
200
168
|
if (this.disconnectorIntervalRef) {
|
|
201
169
|
clearInterval(this.disconnectorIntervalRef);
|
|
202
170
|
}
|
|
203
|
-
|
|
204
|
-
await this.webSocketConnector.destroy();
|
|
205
|
-
this.webSocketConnector = undefined;
|
|
206
|
-
await this.webrtcConnector.stop();
|
|
207
|
-
this.webrtcConnector = undefined;
|
|
208
|
-
}
|
|
209
|
-
else {
|
|
210
|
-
await this.simulatorConnector.stop();
|
|
211
|
-
this.simulatorConnector = undefined;
|
|
212
|
-
}
|
|
171
|
+
await this.connectorFacade.stop();
|
|
213
172
|
await Promise.all(Array.from(this.connections.values()).map(async (peer) => {
|
|
214
173
|
if (peer.isHandshakeCompleted()) {
|
|
215
174
|
try {
|
|
@@ -234,16 +193,13 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
234
193
|
}));
|
|
235
194
|
this.state = ConnectionManagerState.STOPPED;
|
|
236
195
|
this.rpcCommunicator.stop();
|
|
237
|
-
this.
|
|
238
|
-
this.messageDuplicateDetector.clear();
|
|
196
|
+
this.duplicateMessageDetector.clear();
|
|
239
197
|
this.locks.clear();
|
|
240
198
|
this.removeAllListeners();
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
getConnectionTo(id) {
|
|
246
|
-
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();
|
|
247
203
|
}
|
|
248
204
|
getNumberOfLocalLockedConnections() {
|
|
249
205
|
return this.locks.getNumberOfLocalLockedConnections();
|
|
@@ -262,17 +218,17 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
262
218
|
if (this.isConnectionToSelf(peerDescriptor)) {
|
|
263
219
|
throw new Err.CannotConnectToSelf('Cannot send to self');
|
|
264
220
|
}
|
|
265
|
-
logger.trace(`Sending message to: ${
|
|
221
|
+
logger.trace(`Sending message to: ${(0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor)}`);
|
|
266
222
|
message = {
|
|
267
223
|
...message,
|
|
268
224
|
targetDescriptor: message.targetDescriptor || peerDescriptor,
|
|
269
|
-
sourceDescriptor: message.sourceDescriptor || this.
|
|
225
|
+
sourceDescriptor: message.sourceDescriptor || this.getOwnPeerDescriptor(),
|
|
270
226
|
};
|
|
271
|
-
const
|
|
272
|
-
let connection = this.connections.get(
|
|
227
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
228
|
+
let connection = this.connections.get(peerIdKey);
|
|
273
229
|
if (!connection && !doNotConnect) {
|
|
274
|
-
connection = this.createConnection(peerDescriptor);
|
|
275
|
-
this.
|
|
230
|
+
connection = this.connectorFacade.createConnection(peerDescriptor);
|
|
231
|
+
this.onIncomingConnection(connection);
|
|
276
232
|
}
|
|
277
233
|
else if (!connection) {
|
|
278
234
|
throw new Err.SendFailed('No connection to target, doNotConnect flag is true');
|
|
@@ -283,59 +239,42 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
283
239
|
return connection.send(binary, doNotConnect);
|
|
284
240
|
}
|
|
285
241
|
isConnectionToSelf(peerDescriptor) {
|
|
286
|
-
return (0, peerIdFromPeerDescriptor_1.
|
|
242
|
+
return (0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(peerDescriptor, this.getOwnPeerDescriptor()) || this.isOwnWebSocketServer(peerDescriptor);
|
|
287
243
|
}
|
|
288
244
|
isOwnWebSocketServer(peerDescriptor) {
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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));
|
|
292
249
|
}
|
|
293
250
|
else {
|
|
294
251
|
return false;
|
|
295
252
|
}
|
|
296
253
|
}
|
|
297
|
-
createConnection(peerDescriptor) {
|
|
298
|
-
if (this.simulatorConnector) {
|
|
299
|
-
return this.simulatorConnector.connect(peerDescriptor);
|
|
300
|
-
}
|
|
301
|
-
else if ((peerDescriptor.websocket || this.ownPeerDescriptor.websocket)) {
|
|
302
|
-
if (this.canOpenWsConnection(peerDescriptor)) {
|
|
303
|
-
return this.webSocketConnector.connect(peerDescriptor);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
return this.webrtcConnector.connect(peerDescriptor);
|
|
307
|
-
}
|
|
308
|
-
canOpenWsConnection(peerDescriptor) {
|
|
309
|
-
if (!(this.ownPeerDescriptor.type === DhtRpc_1.NodeType.BROWSER || peerDescriptor.type === DhtRpc_1.NodeType.BROWSER)) {
|
|
310
|
-
return true;
|
|
311
|
-
}
|
|
312
|
-
if (this.ownPeerDescriptor.websocket) {
|
|
313
|
-
return (peerDescriptor.type === DhtRpc_1.NodeType.BROWSER && this.ownPeerDescriptor.websocket.tls)
|
|
314
|
-
|| (this.ownPeerDescriptor.websocket.host === 'localhost' || ((0, AddressTools_1.isPrivateIPv4)(this.ownPeerDescriptor.websocket.host)));
|
|
315
|
-
}
|
|
316
|
-
return (this.ownPeerDescriptor.type === DhtRpc_1.NodeType.BROWSER && peerDescriptor.websocket.tls)
|
|
317
|
-
|| (peerDescriptor.websocket.host === 'localhost' || ((0, AddressTools_1.isPrivateIPv4)(peerDescriptor.websocket.host)));
|
|
318
|
-
}
|
|
319
254
|
getConnection(peerDescriptor) {
|
|
320
|
-
const
|
|
321
|
-
return this.connections.get(
|
|
255
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
256
|
+
return this.connections.get(peerIdKey);
|
|
322
257
|
}
|
|
258
|
+
// TODO remove this method or getOwnPeerDescriptor
|
|
323
259
|
getPeerDescriptor() {
|
|
324
|
-
return this.
|
|
260
|
+
return this.getOwnPeerDescriptor();
|
|
261
|
+
}
|
|
262
|
+
getOwnPeerDescriptor() {
|
|
263
|
+
return this.connectorFacade.getOwnPeerDescriptor();
|
|
325
264
|
}
|
|
326
265
|
hasConnection(peerDescriptor) {
|
|
327
|
-
const
|
|
328
|
-
return this.connections.has(
|
|
266
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
267
|
+
return this.connections.has(peerIdKey);
|
|
329
268
|
}
|
|
330
|
-
hasLocalLockedConnection(peerDescriptor
|
|
331
|
-
const
|
|
332
|
-
return this.locks.isLocalLocked(
|
|
269
|
+
hasLocalLockedConnection(peerDescriptor) {
|
|
270
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
271
|
+
return this.locks.isLocalLocked(peerIdKey);
|
|
333
272
|
}
|
|
334
|
-
hasRemoteLockedConnection(peerDescriptor
|
|
335
|
-
const
|
|
336
|
-
return this.locks.isRemoteLocked(
|
|
273
|
+
hasRemoteLockedConnection(peerDescriptor) {
|
|
274
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
275
|
+
return this.locks.isRemoteLocked(peerIdKey);
|
|
337
276
|
}
|
|
338
|
-
canConnect(peerDescriptor
|
|
277
|
+
canConnect(peerDescriptor) {
|
|
339
278
|
// Perhaps the connection's state should be checked here
|
|
340
279
|
return !this.hasConnection(peerDescriptor); // TODO: Add port range check
|
|
341
280
|
}
|
|
@@ -345,18 +284,17 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
345
284
|
logger.trace('Filtered out non-RPC message of type ' + message.messageType);
|
|
346
285
|
return;
|
|
347
286
|
}
|
|
348
|
-
if (this.
|
|
349
|
-
logger.trace('handleMessage filtered duplicate ' +
|
|
350
|
-
+
|
|
287
|
+
if (this.duplicateMessageDetector.isMostLikelyDuplicate(message.messageId)) {
|
|
288
|
+
logger.trace('handleMessage filtered duplicate ' + (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(message.sourceDescriptor)
|
|
289
|
+
+ ' ' + message.serviceId + ' ' + message.messageId);
|
|
351
290
|
return;
|
|
352
291
|
}
|
|
353
|
-
this.
|
|
354
|
-
if (message.serviceId ===
|
|
292
|
+
this.duplicateMessageDetector.add(message.messageId);
|
|
293
|
+
if (message.serviceId === INTERNAL_SERVICE_ID) {
|
|
355
294
|
this.rpcCommunicator?.handleMessageFromPeer(message);
|
|
356
295
|
}
|
|
357
296
|
else {
|
|
358
|
-
logger.trace('emit "message" ' +
|
|
359
|
-
+ ' ' + message.serviceId + ' ' + message.messageId);
|
|
297
|
+
logger.trace('emit "message" ' + (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(message.sourceDescriptor) + ' ' + message.serviceId + ' ' + message.messageId);
|
|
360
298
|
this.emit('message', message);
|
|
361
299
|
}
|
|
362
300
|
}
|
|
@@ -369,7 +307,7 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
369
307
|
let message;
|
|
370
308
|
try {
|
|
371
309
|
message = DhtRpc_1.Message.fromBinary(data);
|
|
372
|
-
logger.trace(
|
|
310
|
+
logger.trace(`received protojson: ${(0, protoToString_1.protoToString)(message, DhtRpc_1.Message)}`);
|
|
373
311
|
}
|
|
374
312
|
catch (e) {
|
|
375
313
|
logger.debug(`Parsing incoming data into Message failed: ${e}`);
|
|
@@ -383,17 +321,17 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
383
321
|
logger.debug(`Handling incoming data failed: ${e}`);
|
|
384
322
|
}
|
|
385
323
|
}
|
|
386
|
-
|
|
324
|
+
onIncomingConnection(connection) {
|
|
387
325
|
if (this.state === ConnectionManagerState.STOPPED) {
|
|
388
326
|
return false;
|
|
389
327
|
}
|
|
390
|
-
logger.trace('
|
|
328
|
+
logger.trace('onIncomingConnection()');
|
|
391
329
|
connection.offeredAsIncoming = true;
|
|
392
330
|
if (!this.acceptIncomingConnection(connection)) {
|
|
393
331
|
return false;
|
|
394
332
|
}
|
|
395
333
|
connection.on('managedData', this.onData);
|
|
396
|
-
connection.on('disconnected', (disconnectionType
|
|
334
|
+
connection.on('disconnected', (disconnectionType) => {
|
|
397
335
|
this.onDisconnected(connection, disconnectionType);
|
|
398
336
|
});
|
|
399
337
|
this.emit('newConnection', connection);
|
|
@@ -401,23 +339,23 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
401
339
|
this.onConnected(connection);
|
|
402
340
|
}
|
|
403
341
|
else {
|
|
404
|
-
connection.once('handshakeCompleted', (
|
|
342
|
+
connection.once('handshakeCompleted', () => {
|
|
405
343
|
this.onConnected(connection);
|
|
406
344
|
});
|
|
407
345
|
}
|
|
408
346
|
return true;
|
|
409
347
|
}
|
|
410
348
|
acceptIncomingConnection(newConnection) {
|
|
411
|
-
logger.trace(
|
|
349
|
+
logger.trace((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(newConnection.getPeerDescriptor()) + ' acceptIncomingConnection()');
|
|
412
350
|
const newPeerID = (0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(newConnection.getPeerDescriptor());
|
|
413
|
-
const
|
|
414
|
-
if (this.connections.has(
|
|
415
|
-
if (newPeerID.hasSmallerHashThan((0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(this.
|
|
416
|
-
logger.trace(
|
|
417
|
-
' 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');
|
|
418
356
|
// replace the current connection
|
|
419
357
|
const oldConnection = this.connections.get(newPeerID.toKey());
|
|
420
|
-
logger.trace('replaced: ' +
|
|
358
|
+
logger.trace('replaced: ' + (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(newConnection.getPeerDescriptor()));
|
|
421
359
|
const buffer = oldConnection.stealOutputBuffer();
|
|
422
360
|
for (const data of buffer) {
|
|
423
361
|
newConnection.sendNoWait(data);
|
|
@@ -430,13 +368,12 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
430
368
|
return false;
|
|
431
369
|
}
|
|
432
370
|
}
|
|
433
|
-
logger.trace(
|
|
434
|
-
|
|
435
|
-
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);
|
|
436
373
|
return true;
|
|
437
374
|
}
|
|
438
375
|
async closeConnection(peerDescriptor, disconnectionType, reason) {
|
|
439
|
-
logger.trace(
|
|
376
|
+
logger.trace((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor) + ' ' + 'closeConnection() ' + reason);
|
|
440
377
|
const id = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor);
|
|
441
378
|
this.locks.clearAllLocks(id);
|
|
442
379
|
if (this.connections.has(id)) {
|
|
@@ -444,46 +381,45 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
444
381
|
await connectionToClose.close(disconnectionType);
|
|
445
382
|
}
|
|
446
383
|
else {
|
|
447
|
-
logger.trace(
|
|
448
|
-
'closeConnection() this.connections did not have the id');
|
|
384
|
+
logger.trace((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(peerDescriptor) + ' ' + 'closeConnection() this.connections did not have the id');
|
|
449
385
|
this.emit('disconnected', peerDescriptor, 'OTHER');
|
|
450
386
|
}
|
|
451
387
|
}
|
|
452
388
|
lockConnection(targetDescriptor, serviceId) {
|
|
453
|
-
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.
|
|
389
|
+
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(targetDescriptor, this.getOwnPeerDescriptor())) {
|
|
454
390
|
return;
|
|
455
391
|
}
|
|
456
|
-
const
|
|
457
|
-
const remoteConnectionLocker = new RemoteConnectionLocker_1.RemoteConnectionLocker(this.
|
|
458
|
-
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);
|
|
459
395
|
remoteConnectionLocker.lockRequest(serviceId)
|
|
460
396
|
.then((_accepted) => logger.trace('LockRequest successful'))
|
|
461
397
|
.catch((err) => { logger.debug(err); });
|
|
462
398
|
}
|
|
463
399
|
unlockConnection(targetDescriptor, serviceId) {
|
|
464
|
-
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.
|
|
400
|
+
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(targetDescriptor, this.getOwnPeerDescriptor())) {
|
|
465
401
|
return;
|
|
466
402
|
}
|
|
467
|
-
const
|
|
468
|
-
this.locks.removeLocalLocked(
|
|
469
|
-
const remoteConnectionLocker = new RemoteConnectionLocker_1.RemoteConnectionLocker(this.
|
|
470
|
-
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)) {
|
|
471
407
|
remoteConnectionLocker.unlockRequest(serviceId);
|
|
472
408
|
}
|
|
473
409
|
}
|
|
474
410
|
weakLockConnection(targetDescriptor) {
|
|
475
|
-
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.
|
|
411
|
+
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(targetDescriptor, this.getOwnPeerDescriptor())) {
|
|
476
412
|
return;
|
|
477
413
|
}
|
|
478
|
-
const
|
|
479
|
-
this.locks.addWeakLocked(
|
|
414
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(targetDescriptor);
|
|
415
|
+
this.locks.addWeakLocked(peerIdKey);
|
|
480
416
|
}
|
|
481
417
|
weakUnlockConnection(targetDescriptor) {
|
|
482
|
-
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.
|
|
418
|
+
if (this.state === ConnectionManagerState.STOPPED || (0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(targetDescriptor, this.getOwnPeerDescriptor())) {
|
|
483
419
|
return;
|
|
484
420
|
}
|
|
485
|
-
const
|
|
486
|
-
this.locks.removeWeakLocked(
|
|
421
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(targetDescriptor);
|
|
422
|
+
this.locks.removeWeakLocked(peerIdKey);
|
|
487
423
|
}
|
|
488
424
|
async gracefullyDisconnectAsync(targetDescriptor, disconnectMode) {
|
|
489
425
|
const connection = this.connections.get((0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(targetDescriptor).toKey());
|
|
@@ -514,14 +450,13 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
514
450
|
await promise;
|
|
515
451
|
}
|
|
516
452
|
async doGracefullyDisconnectAsync(targetDescriptor, disconnectMode) {
|
|
517
|
-
logger.trace(
|
|
518
|
-
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())));
|
|
519
455
|
try {
|
|
520
456
|
await remoteConnectionLocker.gracefulDisconnect(disconnectMode);
|
|
521
457
|
}
|
|
522
458
|
catch (ex) {
|
|
523
|
-
logger.trace(
|
|
524
|
-
' remoteConnectionLocker.gracefulDisconnect() failed' + ex);
|
|
459
|
+
logger.trace((0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(targetDescriptor) + ' remoteConnectionLocker.gracefulDisconnect() failed' + ex);
|
|
525
460
|
}
|
|
526
461
|
}
|
|
527
462
|
getAllConnectionPeerDescriptors() {
|
|
@@ -530,9 +465,9 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
530
465
|
.map((managedConnection) => managedConnection.getPeerDescriptor());
|
|
531
466
|
}
|
|
532
467
|
// IConnectionLocker server implementation
|
|
533
|
-
async lockRequest(lockRequest
|
|
468
|
+
async lockRequest(lockRequest) {
|
|
534
469
|
const remotePeerId = (0, peerIdFromPeerDescriptor_1.peerIdFromPeerDescriptor)(lockRequest.peerDescriptor);
|
|
535
|
-
if ((0, peerIdFromPeerDescriptor_1.
|
|
470
|
+
if ((0, peerIdFromPeerDescriptor_1.areEqualPeerDescriptors)(lockRequest.peerDescriptor, this.getOwnPeerDescriptor())) {
|
|
536
471
|
const response = {
|
|
537
472
|
accepted: false
|
|
538
473
|
};
|
|
@@ -545,15 +480,14 @@ class ConnectionManager extends eventemitter3_1.EventEmitter {
|
|
|
545
480
|
return response;
|
|
546
481
|
}
|
|
547
482
|
// IConnectionLocker server implementation
|
|
548
|
-
async unlockRequest(unlockRequest
|
|
549
|
-
const
|
|
550
|
-
this.locks.removeRemoteLocked(
|
|
483
|
+
async unlockRequest(unlockRequest) {
|
|
484
|
+
const peerIdKey = (0, peerIdFromPeerDescriptor_1.keyFromPeerDescriptor)(unlockRequest.peerDescriptor);
|
|
485
|
+
this.locks.removeRemoteLocked(peerIdKey, unlockRequest.serviceId);
|
|
551
486
|
return {};
|
|
552
487
|
}
|
|
553
488
|
// IConnectionLocker server implementation
|
|
554
|
-
async gracefulDisconnect(disconnectNotice
|
|
555
|
-
logger.trace(
|
|
556
|
-
+ ' received gracefulDisconnect notice');
|
|
489
|
+
async gracefulDisconnect(disconnectNotice) {
|
|
490
|
+
logger.trace((0, exports.keyOrUnknownFromPeerDescriptor)(disconnectNotice.peerDescriptor) + ' received gracefulDisconnect notice');
|
|
557
491
|
if (disconnectNotice.disconnecMode === DhtRpc_1.DisconnectMode.LEAVING) {
|
|
558
492
|
this.closeConnection(disconnectNotice.peerDescriptor, 'INCOMING_GRACEFUL_LEAVE', 'graceful leave notified');
|
|
559
493
|
}
|