@streamr/dht 0.0.1-tatum.6 → 0.0.1-tatum.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/connection/ConnectionLockHandler.d.ts +1 -1
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.d.ts +10 -0
- package/dist/src/connection/{RemoteConnectionLocker.js → ConnectionLockRpcRemote.js} +21 -35
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -0
- package/dist/src/connection/ConnectionManager.d.ts +17 -41
- package/dist/src/connection/ConnectionManager.js +148 -212
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectivityChecker.js +16 -13
- package/dist/src/connection/ConnectivityChecker.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +49 -0
- package/dist/src/connection/ConnectorFacade.js +83 -0
- package/dist/src/connection/ConnectorFacade.js.map +1 -0
- package/dist/src/connection/Handshaker.d.ts +1 -2
- package/dist/src/connection/Handshaker.js +1 -4
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +2 -6
- package/dist/src/connection/ManagedConnection.js +27 -36
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/ManagedWebRtcConnection.d.ts +1 -1
- package/dist/src/connection/ManagedWebRtcConnection.js +2 -2
- package/dist/src/connection/ManagedWebRtcConnection.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 -3
- package/dist/src/connection/Simulator/SimulatorConnector.js +12 -14
- 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 +3 -1
- 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 → WebRtcConnectorRpcLocal.d.ts} +13 -13
- package/dist/src/connection/WebRTC/{WebRtcConnector.js → WebRtcConnectorRpcLocal.js} +46 -38
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcRemote.d.ts +11 -0
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcRemote.js +55 -0
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcRemote.js.map +1 -0
- package/dist/src/connection/WebRTC/iceServerAsString.d.ts +1 -1
- package/dist/src/connection/WebSocket/{WebSocketConnector.d.ts → WebSocketConnectorRpcLocal.d.ts} +21 -11
- package/dist/src/connection/WebSocket/{WebSocketConnector.js → WebSocketConnectorRpcLocal.js} +80 -59
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcRemote.d.ts +8 -0
- package/dist/src/connection/WebSocket/{RemoteWebSocketConnector.js → WebSocketConnectorRpcRemote.js} +12 -16
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcRemote.js.map +1 -0
- 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 +18 -55
- package/dist/src/dht/DhtNode.js +122 -145
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/{RemoteExternalApi.d.ts → ExternalApiRpcRemote.d.ts} +2 -2
- package/dist/src/dht/{RemoteExternalApi.js → ExternalApiRpcRemote.js} +5 -6
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -0
- 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 +12 -11
- package/dist/src/dht/discovery/PeerDiscovery.js +33 -37
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/find/RecursiveFindSession.d.ts +9 -10
- package/dist/src/dht/find/RecursiveFindSession.js +13 -13
- package/dist/src/dht/find/RecursiveFindSession.js.map +1 -1
- package/dist/src/dht/find/RecursiveFinder.d.ts +9 -11
- package/dist/src/dht/find/RecursiveFinder.js +35 -36
- package/dist/src/dht/find/RecursiveFinder.js.map +1 -1
- package/dist/src/dht/find/RemoteRecursiveFindSession.d.ts +1 -1
- package/dist/src/dht/find/RemoteRecursiveFindSession.js +4 -4
- package/dist/src/dht/find/RemoteRecursiveFindSession.js.map +1 -1
- package/dist/src/dht/registerExternalApiRpcMethods.d.ts +1 -1
- package/dist/src/dht/registerExternalApiRpcMethods.js +4 -3
- package/dist/src/dht/registerExternalApiRpcMethods.js.map +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/Router.d.ts +12 -15
- package/dist/src/dht/routing/Router.js +30 -33
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/{RemoteRouter.d.ts → RouterRpcRemote.d.ts} +2 -2
- package/dist/src/dht/routing/{RemoteRouter.js → RouterRpcRemote.js} +5 -5
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -0
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -4
- package/dist/src/dht/routing/RoutingSession.js +6 -5
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/{DataStore.d.ts → StoreRpcLocal.d.ts} +6 -6
- package/dist/src/dht/store/{DataStore.js → StoreRpcLocal.js} +34 -34
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -0
- package/dist/src/dht/store/{RemoteStore.d.ts → StoreRpcRemote.d.ts} +2 -2
- package/dist/src/dht/store/{RemoteStore.js → StoreRpcRemote.js} +4 -4
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -0
- package/dist/src/exports.d.ts +2 -8
- 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/helpers/protoClasses.js +2 -2
- package/dist/src/helpers/protoClasses.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +29 -29
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +39 -39
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +49 -162
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +47 -88
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +15 -15
- package/dist/src/transport/RoutingRpcCommunicator.js +1 -0
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/package.json +10 -9
- package/protos/DhtRpc.proto +30 -60
- package/src/connection/ConnectionLockHandler.ts +1 -1
- package/src/connection/ConnectionLockRpcRemote.ts +62 -0
- package/src/connection/ConnectionManager.ts +178 -274
- package/src/connection/ConnectivityChecker.ts +15 -12
- package/src/connection/ConnectorFacade.ts +140 -0
- package/src/connection/Handshaker.ts +0 -5
- package/src/connection/ManagedConnection.ts +26 -40
- package/src/connection/ManagedWebRtcConnection.ts +0 -2
- package/src/connection/Simulator/Simulator.ts +0 -7
- package/src/connection/Simulator/SimulatorConnection.ts +16 -13
- package/src/connection/Simulator/SimulatorConnector.ts +13 -19
- package/src/connection/Simulator/SimulatorTransport.ts +6 -1
- package/src/connection/WebRTC/NodeWebRtcConnection.ts +15 -14
- package/src/connection/WebRTC/{WebRtcConnector.ts → WebRtcConnectorRpcLocal.ts} +68 -56
- package/src/connection/WebRTC/WebRtcConnectorRpcRemote.ts +71 -0
- package/src/connection/WebRTC/iceServerAsString.ts +1 -1
- package/src/connection/WebSocket/{WebSocketConnector.ts → WebSocketConnectorRpcLocal.ts} +102 -79
- package/src/connection/WebSocket/WebSocketConnectorRpcRemote.ts +45 -0
- package/src/connection/WebSocket/WebSocketServer.ts +26 -8
- package/src/dht/DhtNode.ts +176 -203
- package/src/dht/{RemoteExternalApi.ts → ExternalApiRpcRemote.ts} +3 -4
- 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 +47 -45
- package/src/dht/find/RecursiveFindSession.ts +21 -22
- package/src/dht/find/RecursiveFinder.ts +45 -49
- package/src/dht/find/RemoteRecursiveFindSession.ts +6 -6
- package/src/dht/registerExternalApiRpcMethods.ts +8 -5
- package/src/dht/routing/DuplicateDetector.ts +3 -10
- package/src/dht/routing/Router.ts +39 -45
- package/src/dht/routing/{RemoteRouter.ts → RouterRpcRemote.ts} +4 -4
- package/src/dht/routing/RoutingSession.ts +15 -15
- package/src/dht/store/{DataStore.ts → StoreRpcLocal.ts} +42 -42
- package/src/dht/store/{RemoteStore.ts → StoreRpcRemote.ts} +2 -2
- package/src/exports.ts +2 -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/helpers/protoClasses.ts +4 -4
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +54 -54
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +15 -15
- package/src/proto/packages/dht/protos/DhtRpc.ts +77 -216
- package/src/transport/RoutingRpcCommunicator.ts +1 -0
- 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/RouteMessage.test.ts +7 -9
- package/test/integration/{RemoteRouter.test.ts → RouterRpcRemote.test.ts} +13 -14
- 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/{RemoteStore.test.ts → StoreRpcRemote.test.ts} +17 -18
- package/test/integration/WebRtcConnectionManagement.test.ts +26 -19
- package/test/integration/WebRtcConnectorRpc.test.ts +17 -32
- package/test/integration/WebSocket.test.ts +4 -2
- package/test/integration/WebSocketConnectionManagement.test.ts +30 -17
- package/test/integration/WebSocketConnectorRpc.test.ts +10 -15
- 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 +13 -18
- package/test/unit/Router.test.ts +18 -21
- package/test/unit/WebSocketConnectorRpcLocal.test.ts +64 -0
- package/test/unit/WebSocketServer.test.ts +24 -12
- package/test/unit/{webrtcReplaceInternalIpWithExternalIp.ts → webrtcReplaceInternalIpWithExternalIp.test.ts} +1 -1
- 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 +55 -74
- package/dist/src/connection/RemoteConnectionLocker.d.ts +0 -13
- package/dist/src/connection/RemoteConnectionLocker.js.map +0 -1
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.d.ts +0 -12
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js +0 -74
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js.map +0 -1
- package/dist/src/connection/WebRTC/WebRtcConnector.js.map +0 -1
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.d.ts +0 -9
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js.map +0 -1
- package/dist/src/connection/WebSocket/WebSocketConnector.js.map +0 -1
- package/dist/src/dht/DhtPeer.js.map +0 -1
- package/dist/src/dht/RemoteExternalApi.js.map +0 -1
- package/dist/src/dht/routing/RemoteRouter.js.map +0 -1
- package/dist/src/dht/store/DataStore.js.map +0 -1
- package/dist/src/dht/store/RemoteStore.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/connection/RemoteConnectionLocker.ts +0 -84
- package/src/connection/WebRTC/RemoteWebrtcConnector.ts +0 -93
- package/src/connection/WebSocket/RemoteWebSocketConnector.ts +0 -49
- 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
|
@@ -1,6 +1,19 @@
|
|
|
1
|
+
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
2
|
+
import { CountMetric, LevelMetric, Logger, Metric, MetricsContext, MetricsDefinition, RateMetric, waitForEvent3 } from '@streamr/utils'
|
|
1
3
|
import { EventEmitter } from 'eventemitter3'
|
|
4
|
+
import { Contact } from '../dht/contact/Contact'
|
|
5
|
+
import { SortedContactList } from '../dht/contact/SortedContactList'
|
|
6
|
+
import { DuplicateDetector } from '../dht/routing/DuplicateDetector'
|
|
7
|
+
import { PeerIDKey } from '../helpers/PeerID'
|
|
8
|
+
import * as Err from '../helpers/errors'
|
|
9
|
+
import {
|
|
10
|
+
areEqualPeerDescriptors,
|
|
11
|
+
keyFromPeerDescriptor,
|
|
12
|
+
peerIdFromPeerDescriptor
|
|
13
|
+
} from '../helpers/peerIdFromPeerDescriptor'
|
|
14
|
+
import { protoToString } from '../helpers/protoToString'
|
|
15
|
+
import { Empty } from '../proto/google/protobuf/empty'
|
|
2
16
|
import {
|
|
3
|
-
ConnectivityResponse,
|
|
4
17
|
DisconnectMode,
|
|
5
18
|
DisconnectNotice,
|
|
6
19
|
DisconnectNoticeResponse,
|
|
@@ -8,69 +21,24 @@ import {
|
|
|
8
21
|
LockResponse,
|
|
9
22
|
Message,
|
|
10
23
|
MessageType,
|
|
11
|
-
NodeType,
|
|
12
24
|
PeerDescriptor,
|
|
13
25
|
UnlockRequest
|
|
14
26
|
} from '../proto/packages/dht/protos/DhtRpc'
|
|
15
|
-
import {
|
|
16
|
-
import { PeerIDKey } from '../helpers/PeerID'
|
|
17
|
-
import { protoToString } from '../helpers/protoToString'
|
|
27
|
+
import { ConnectionLockRpcClient } from '../proto/packages/dht/protos/DhtRpc.client'
|
|
18
28
|
import { DisconnectionType, ITransport, TransportEvents } from '../transport/ITransport'
|
|
19
|
-
import { IceServer, WebRtcConnector } from './WebRTC/WebRtcConnector'
|
|
20
|
-
import { CountMetric, LevelMetric, Logger, Metric, MetricsContext, MetricsDefinition, RateMetric, waitForEvent3 } from '@streamr/utils'
|
|
21
|
-
import * as Err from '../helpers/errors'
|
|
22
|
-
import { WEB_RTC_CLEANUP } from './WebRTC/NodeWebRtcConnection'
|
|
23
|
-
import { ManagedConnection, Events as ManagedConnectionEvents } from './ManagedConnection'
|
|
24
29
|
import { RoutingRpcCommunicator } from '../transport/RoutingRpcCommunicator'
|
|
25
|
-
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
26
|
-
import { ConnectionLockerClient } from '../proto/packages/dht/protos/DhtRpc.client'
|
|
27
|
-
import { RemoteConnectionLocker } from './RemoteConnectionLocker'
|
|
28
|
-
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
29
|
-
import { Empty } from '../proto/google/protobuf/empty'
|
|
30
|
-
import { Simulator } from './Simulator/Simulator'
|
|
31
|
-
import { SimulatorConnector } from './Simulator/SimulatorConnector'
|
|
32
30
|
import { ConnectionLockHandler } from './ConnectionLockHandler'
|
|
33
|
-
import {
|
|
34
|
-
import {
|
|
35
|
-
import {
|
|
36
|
-
import {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
transportLayer?: ITransport
|
|
45
|
-
websocketHost?: string
|
|
46
|
-
websocketPortRange?: PortRange
|
|
47
|
-
entryPoints?: PeerDescriptor[]
|
|
48
|
-
maxConnections: number = 80
|
|
49
|
-
iceServers?: IceServer[]
|
|
50
|
-
metricsContext?: MetricsContext
|
|
51
|
-
webrtcAllowPrivateAddresses?: boolean
|
|
52
|
-
webrtcDatachannelBufferThresholdLow?: number
|
|
53
|
-
webrtcDatachannelBufferThresholdHigh?: number
|
|
54
|
-
webrtcNewConnectionTimeout?: number
|
|
55
|
-
externalIp?: string
|
|
56
|
-
webrtcPortRange?: PortRange
|
|
57
|
-
tlsCertificate?: TlsCertificate
|
|
58
|
-
|
|
59
|
-
// the following fields are used in simulation only
|
|
60
|
-
simulator?: Simulator
|
|
61
|
-
ownPeerDescriptor?: PeerDescriptor
|
|
62
|
-
serviceIdPrefix?: string
|
|
63
|
-
|
|
64
|
-
constructor(conf: Partial<ConnectionManagerConfig>) {
|
|
65
|
-
// assign given non-undefined config vars over defaults
|
|
66
|
-
let k: keyof typeof conf
|
|
67
|
-
for (k in conf) {
|
|
68
|
-
if (conf[k] === undefined) {
|
|
69
|
-
delete conf[k]
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
Object.assign(this, conf)
|
|
73
|
-
}
|
|
31
|
+
import { ConnectorFacade } from './ConnectorFacade'
|
|
32
|
+
import { ManagedConnection, Events as ManagedConnectionEvents } from './ManagedConnection'
|
|
33
|
+
import { ConnectionLockRpcRemote } from './ConnectionLockRpcRemote'
|
|
34
|
+
import { WEB_RTC_CLEANUP } from './WebRTC/NodeWebRtcConnection'
|
|
35
|
+
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
36
|
+
import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
|
|
37
|
+
|
|
38
|
+
export interface ConnectionManagerConfig {
|
|
39
|
+
maxConnections?: number
|
|
40
|
+
metricsContext: MetricsContext
|
|
41
|
+
createConnectorFacade: () => ConnectorFacade
|
|
74
42
|
}
|
|
75
43
|
|
|
76
44
|
export enum NatType {
|
|
@@ -87,10 +55,9 @@ interface ConnectionManagerMetrics extends MetricsDefinition {
|
|
|
87
55
|
connectionTotalFailureCount: Metric
|
|
88
56
|
}
|
|
89
57
|
|
|
58
|
+
// TODO move this type identifiers.ts and use also in other classes (and rename to ServiceID)
|
|
90
59
|
type ServiceId = string
|
|
91
60
|
|
|
92
|
-
export type PeerDescriptorGeneratorCallback = (connectivityResponse: ConnectivityResponse) => PeerDescriptor
|
|
93
|
-
|
|
94
61
|
const logger = new Logger(module)
|
|
95
62
|
|
|
96
63
|
enum ConnectionManagerState {
|
|
@@ -123,28 +90,43 @@ export interface TlsCertificate {
|
|
|
123
90
|
|
|
124
91
|
export type Events = TransportEvents & ConnectionManagerEvents
|
|
125
92
|
|
|
93
|
+
const INTERNAL_SERVICE_ID = 'system/connection-manager'
|
|
94
|
+
|
|
95
|
+
// Form an string representation from a peer description which can be undefined. This output
|
|
96
|
+
// should only be used only for log output. TODO remove this method if we no longer use
|
|
97
|
+
// peerDescriptors which can be undefined, e.g.
|
|
98
|
+
// - if we refactor ConnectionManager so that it doesn't process handshake requests too early
|
|
99
|
+
// and therefore this.ownPeerDescriptor can't be undefine (NET-1129)
|
|
100
|
+
// - if the peerDescriptor of ManagedConnection is always available
|
|
101
|
+
// - if we create stricter types for incoming messages (message.sourceDescriptor or
|
|
102
|
+
// disconnectNotice.peerDescriptor)
|
|
103
|
+
// - if ManagedConnection#peerDescriptor is never undefined
|
|
104
|
+
export const keyOrUnknownFromPeerDescriptor = (peerDescriptor: PeerDescriptor | undefined): string => {
|
|
105
|
+
if (peerDescriptor !== undefined) {
|
|
106
|
+
return keyFromPeerDescriptor(peerDescriptor)
|
|
107
|
+
} else {
|
|
108
|
+
return 'unknown'
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
126
112
|
export class ConnectionManager extends EventEmitter<Events> implements ITransport, ConnectionLocker {
|
|
127
|
-
|
|
113
|
+
|
|
128
114
|
private config: ConnectionManagerConfig
|
|
129
115
|
private readonly metricsContext: MetricsContext
|
|
130
|
-
private
|
|
131
|
-
private readonly messageDuplicateDetector: DuplicateDetector = new DuplicateDetector(100000, 100)
|
|
116
|
+
private readonly duplicateMessageDetector: DuplicateDetector = new DuplicateDetector(100000, 100)
|
|
132
117
|
private readonly metrics: ConnectionManagerMetrics
|
|
133
118
|
private locks = new ConnectionLockHandler()
|
|
134
119
|
private connections: Map<PeerIDKey, ManagedConnection> = new Map()
|
|
135
|
-
private
|
|
136
|
-
private webrtcConnector?: WebRtcConnector
|
|
137
|
-
private simulatorConnector?: SimulatorConnector
|
|
120
|
+
private readonly connectorFacade: ConnectorFacade
|
|
138
121
|
private rpcCommunicator?: RoutingRpcCommunicator
|
|
139
122
|
private disconnectorIntervalRef?: NodeJS.Timeout
|
|
140
|
-
private serviceId: ServiceId
|
|
141
123
|
private state = ConnectionManagerState.IDLE
|
|
142
124
|
|
|
143
|
-
constructor(
|
|
125
|
+
constructor(config: ConnectionManagerConfig) {
|
|
144
126
|
super()
|
|
145
|
-
this.config =
|
|
127
|
+
this.config = config
|
|
146
128
|
this.onData = this.onData.bind(this)
|
|
147
|
-
this.
|
|
129
|
+
this.onIncomingConnection = this.onIncomingConnection.bind(this)
|
|
148
130
|
this.metricsContext = this.config.metricsContext || new MetricsContext()
|
|
149
131
|
this.metrics = {
|
|
150
132
|
sendMessagesPerSecond: new RateMetric(),
|
|
@@ -155,97 +137,55 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
155
137
|
connectionTotalFailureCount: new CountMetric()
|
|
156
138
|
}
|
|
157
139
|
this.metricsContext.addMetrics('node', this.metrics)
|
|
158
|
-
|
|
159
|
-
logger.trace(`Creating SimulatorConnector`)
|
|
160
|
-
this.simulatorConnector = new SimulatorConnector(
|
|
161
|
-
ConnectionManager.PROTOCOL_VERSION,
|
|
162
|
-
this.config.ownPeerDescriptor!,
|
|
163
|
-
this.config.simulator,
|
|
164
|
-
this.incomingConnectionCallback
|
|
165
|
-
)
|
|
166
|
-
this.config.simulator.addConnector(this.simulatorConnector)
|
|
167
|
-
this.ownPeerDescriptor = this.config.ownPeerDescriptor
|
|
168
|
-
this.state = ConnectionManagerState.RUNNING
|
|
169
|
-
} else {
|
|
170
|
-
logger.trace(`Creating WebSocketConnector`)
|
|
171
|
-
this.webSocketConnector = new WebSocketConnector(
|
|
172
|
-
ConnectionManager.PROTOCOL_VERSION,
|
|
173
|
-
this.config.transportLayer!,
|
|
174
|
-
this.canConnect.bind(this),
|
|
175
|
-
this.incomingConnectionCallback,
|
|
176
|
-
this.config.websocketPortRange,
|
|
177
|
-
this.config.websocketHost,
|
|
178
|
-
this.config.entryPoints,
|
|
179
|
-
this.config.tlsCertificate
|
|
180
|
-
)
|
|
181
|
-
logger.trace(`Creating WebRTCConnector`)
|
|
182
|
-
this.webrtcConnector = new WebRtcConnector({
|
|
183
|
-
rpcTransport: this.config.transportLayer!,
|
|
184
|
-
protocolVersion: ConnectionManager.PROTOCOL_VERSION,
|
|
185
|
-
iceServers: this.config.iceServers,
|
|
186
|
-
allowPrivateAddresses: this.config.webrtcAllowPrivateAddresses,
|
|
187
|
-
bufferThresholdLow: this.config.webrtcDatachannelBufferThresholdLow,
|
|
188
|
-
bufferThresholdHigh: this.config.webrtcDatachannelBufferThresholdHigh,
|
|
189
|
-
connectionTimeout: this.config.webrtcNewConnectionTimeout,
|
|
190
|
-
externalIp: this.config.externalIp,
|
|
191
|
-
portRange: this.config.webrtcPortRange
|
|
192
|
-
}, this.incomingConnectionCallback)
|
|
193
|
-
}
|
|
194
|
-
this.serviceId = (this.config.serviceIdPrefix ? this.config.serviceIdPrefix : '') + 'ConnectionManager'
|
|
140
|
+
this.connectorFacade = this.config.createConnectorFacade()
|
|
195
141
|
this.send = this.send.bind(this)
|
|
196
|
-
this.rpcCommunicator = new RoutingRpcCommunicator(
|
|
142
|
+
this.rpcCommunicator = new RoutingRpcCommunicator(INTERNAL_SERVICE_ID, this.send, {
|
|
197
143
|
rpcRequestTimeout: 10000
|
|
198
144
|
})
|
|
199
145
|
this.rpcCommunicator.registerRpcMethod(LockRequest, LockResponse, 'lockRequest',
|
|
200
|
-
(req: LockRequest, context) => this.lockRequest(req, context))
|
|
146
|
+
(req: LockRequest, context: ServerCallContext) => this.lockRequest(req, context))
|
|
201
147
|
this.rpcCommunicator.registerRpcNotification(UnlockRequest, 'unlockRequest',
|
|
202
|
-
(req: UnlockRequest, context) => this.unlockRequest(req, context))
|
|
148
|
+
(req: UnlockRequest, context: ServerCallContext) => this.unlockRequest(req, context))
|
|
203
149
|
this.rpcCommunicator.registerRpcMethod(DisconnectNotice, DisconnectNoticeResponse, 'gracefulDisconnect',
|
|
204
|
-
(req: DisconnectNotice, context) => this.gracefulDisconnect(req, context))
|
|
150
|
+
(req: DisconnectNotice, context: ServerCallContext) => this.gracefulDisconnect(req, context))
|
|
205
151
|
}
|
|
206
152
|
|
|
207
153
|
public garbageCollectConnections(maxConnections: number, lastUsedLimit: number): void {
|
|
208
154
|
if (this.connections.size <= maxConnections) {
|
|
209
155
|
return
|
|
210
156
|
}
|
|
211
|
-
const disconnectionCandidates = new SortedContactList<Contact>(peerIdFromPeerDescriptor(this.
|
|
157
|
+
const disconnectionCandidates = new SortedContactList<Contact>(peerIdFromPeerDescriptor(this.getOwnPeerDescriptor()), 100000)
|
|
212
158
|
this.connections.forEach((connection) => {
|
|
213
159
|
if (!this.locks.isLocked(connection.peerIdKey) && Date.now() - connection.getLastUsed() > lastUsedLimit) {
|
|
214
|
-
logger.trace('disconnecting in timeout interval: ' +
|
|
215
|
-
+ connection.getPeerDescriptor()?.nodeName + ' ')
|
|
160
|
+
logger.trace('disconnecting in timeout interval: ' + keyOrUnknownFromPeerDescriptor(connection.getPeerDescriptor()))
|
|
216
161
|
disconnectionCandidates.addContact(new Contact(connection.getPeerDescriptor()!))
|
|
217
162
|
}
|
|
218
163
|
})
|
|
219
164
|
const sortedCandidates = disconnectionCandidates.getAllContacts()
|
|
220
165
|
const targetNum = this.connections.size - maxConnections
|
|
221
166
|
for (let i = 0; i < sortedCandidates.length && i < targetNum; i++) {
|
|
222
|
-
logger.trace(
|
|
223
|
-
+ sortedCandidates[sortedCandidates.length - 1 - i].getPeerDescriptor().nodeName)
|
|
167
|
+
logger.trace('garbageCollecting ' + keyFromPeerDescriptor(sortedCandidates[sortedCandidates.length - 1 - i].getPeerDescriptor()))
|
|
224
168
|
this.gracefullyDisconnectAsync(sortedCandidates[sortedCandidates.length - 1 - i].getPeerDescriptor(),
|
|
225
169
|
DisconnectMode.NORMAL).catch((_e) => { })
|
|
226
170
|
}
|
|
227
171
|
}
|
|
228
172
|
|
|
229
|
-
public async start(
|
|
173
|
+
public async start(): Promise<void> {
|
|
230
174
|
if (this.state === ConnectionManagerState.RUNNING || this.state === ConnectionManagerState.STOPPED) {
|
|
231
175
|
throw new Err.CouldNotStart(`Cannot start already ${this.state} module`)
|
|
232
176
|
}
|
|
233
177
|
this.state = ConnectionManagerState.RUNNING
|
|
234
178
|
logger.trace(`Starting ConnectionManager...`)
|
|
179
|
+
await this.connectorFacade.start(
|
|
180
|
+
(connection: ManagedConnection) => this.onIncomingConnection(connection),
|
|
181
|
+
(peerDescriptor: PeerDescriptor) => this.canConnect(peerDescriptor)
|
|
182
|
+
)
|
|
235
183
|
// Garbage collection of connections
|
|
236
184
|
this.disconnectorIntervalRef = setInterval(() => {
|
|
237
185
|
logger.trace('disconnectorInterval')
|
|
238
186
|
const LAST_USED_LIMIT = 20000
|
|
239
|
-
this.garbageCollectConnections(this.config.maxConnections, LAST_USED_LIMIT)
|
|
187
|
+
this.garbageCollectConnections(this.config.maxConnections ?? 80, LAST_USED_LIMIT)
|
|
240
188
|
}, 5000)
|
|
241
|
-
if (!this.config.simulator) {
|
|
242
|
-
await this.webSocketConnector!.start()
|
|
243
|
-
const connectivityResponse = await this.webSocketConnector!.checkConnectivity()
|
|
244
|
-
const ownPeerDescriptor = peerDescriptorGeneratorCallback!(connectivityResponse)
|
|
245
|
-
this.ownPeerDescriptor = ownPeerDescriptor
|
|
246
|
-
this.webSocketConnector!.setOwnPeerDescriptor(ownPeerDescriptor)
|
|
247
|
-
this.webrtcConnector!.setOwnPeerDescriptor(ownPeerDescriptor)
|
|
248
|
-
}
|
|
249
189
|
}
|
|
250
190
|
|
|
251
191
|
public async stop(): Promise<void> {
|
|
@@ -257,15 +197,7 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
257
197
|
if (this.disconnectorIntervalRef) {
|
|
258
198
|
clearInterval(this.disconnectorIntervalRef)
|
|
259
199
|
}
|
|
260
|
-
|
|
261
|
-
await this.webSocketConnector!.destroy()
|
|
262
|
-
this.webSocketConnector = undefined
|
|
263
|
-
await this.webrtcConnector!.stop()
|
|
264
|
-
this.webrtcConnector = undefined
|
|
265
|
-
} else {
|
|
266
|
-
await this.simulatorConnector!.stop()
|
|
267
|
-
this.simulatorConnector = undefined
|
|
268
|
-
}
|
|
200
|
+
await this.connectorFacade.stop()
|
|
269
201
|
|
|
270
202
|
await Promise.all(Array.from(this.connections.values()).map(async (peer) => {
|
|
271
203
|
if (peer.isHandshakeCompleted()) {
|
|
@@ -289,18 +221,13 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
289
221
|
|
|
290
222
|
this.state = ConnectionManagerState.STOPPED
|
|
291
223
|
this.rpcCommunicator!.stop()
|
|
292
|
-
this.
|
|
293
|
-
this.messageDuplicateDetector.clear()
|
|
224
|
+
this.duplicateMessageDetector.clear()
|
|
294
225
|
this.locks.clear()
|
|
295
226
|
this.removeAllListeners()
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
public getConnectionTo(id: PeerIDKey): ManagedConnection {
|
|
303
|
-
return this.connections.get(id)!
|
|
227
|
+
// TODO would it make sense to move this call to WebRtcConnector#stop()?
|
|
228
|
+
// - but note that we should call this only after connections have been closed
|
|
229
|
+
// (i.e the this.gracefullyDisconnectAsync() calls above)
|
|
230
|
+
WEB_RTC_CLEANUP.cleanUp()
|
|
304
231
|
}
|
|
305
232
|
|
|
306
233
|
public getNumberOfLocalLockedConnections(): number {
|
|
@@ -324,17 +251,17 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
324
251
|
if (this.isConnectionToSelf(peerDescriptor)) {
|
|
325
252
|
throw new Err.CannotConnectToSelf('Cannot send to self')
|
|
326
253
|
}
|
|
327
|
-
logger.trace(`Sending message to: ${peerDescriptor
|
|
254
|
+
logger.trace(`Sending message to: ${keyFromPeerDescriptor(peerDescriptor)}`)
|
|
328
255
|
message = {
|
|
329
256
|
...message,
|
|
330
257
|
targetDescriptor: message.targetDescriptor || peerDescriptor,
|
|
331
|
-
sourceDescriptor: message.sourceDescriptor || this.
|
|
258
|
+
sourceDescriptor: message.sourceDescriptor || this.getOwnPeerDescriptor(),
|
|
332
259
|
}
|
|
333
|
-
const
|
|
334
|
-
let connection = this.connections.get(
|
|
260
|
+
const peerIdKey = keyFromPeerDescriptor(peerDescriptor)
|
|
261
|
+
let connection = this.connections.get(peerIdKey)
|
|
335
262
|
if (!connection && !doNotConnect) {
|
|
336
|
-
connection = this.createConnection(peerDescriptor)
|
|
337
|
-
this.
|
|
263
|
+
connection = this.connectorFacade.createConnection(peerDescriptor)
|
|
264
|
+
this.onIncomingConnection(connection)
|
|
338
265
|
} else if (!connection) {
|
|
339
266
|
throw new Err.SendFailed('No connection to target, doNotConnect flag is true')
|
|
340
267
|
}
|
|
@@ -345,66 +272,49 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
345
272
|
}
|
|
346
273
|
|
|
347
274
|
private isConnectionToSelf(peerDescriptor: PeerDescriptor): boolean {
|
|
348
|
-
return
|
|
275
|
+
return areEqualPeerDescriptors(peerDescriptor, this.getOwnPeerDescriptor()) || this.isOwnWebSocketServer(peerDescriptor)
|
|
349
276
|
}
|
|
350
277
|
|
|
351
278
|
private isOwnWebSocketServer(peerDescriptor: PeerDescriptor): boolean {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
279
|
+
const ownPeerDescriptor = this.getOwnPeerDescriptor()
|
|
280
|
+
if ((peerDescriptor.websocket !== undefined) && (ownPeerDescriptor.websocket !== undefined)) {
|
|
281
|
+
return ((peerDescriptor.websocket.port === ownPeerDescriptor.websocket.port)
|
|
282
|
+
&& (peerDescriptor.websocket.host === ownPeerDescriptor.websocket.host))
|
|
355
283
|
} else {
|
|
356
284
|
return false
|
|
357
285
|
}
|
|
358
286
|
}
|
|
359
287
|
|
|
360
|
-
private createConnection(peerDescriptor: PeerDescriptor): ManagedConnection {
|
|
361
|
-
if (this.simulatorConnector) {
|
|
362
|
-
return this.simulatorConnector.connect(peerDescriptor)
|
|
363
|
-
} else if ((peerDescriptor.websocket || this.ownPeerDescriptor!.websocket)) {
|
|
364
|
-
if (this.canOpenWsConnection(peerDescriptor)) {
|
|
365
|
-
return this.webSocketConnector!.connect(peerDescriptor)
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
return this.webrtcConnector!.connect(peerDescriptor)
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
private canOpenWsConnection(peerDescriptor: PeerDescriptor): boolean {
|
|
372
|
-
if (!(this.ownPeerDescriptor!.type === NodeType.BROWSER || peerDescriptor.type === NodeType.BROWSER)) {
|
|
373
|
-
return true
|
|
374
|
-
}
|
|
375
|
-
if (this.ownPeerDescriptor!.websocket) {
|
|
376
|
-
return (peerDescriptor.type === NodeType.BROWSER && this.ownPeerDescriptor!.websocket!.tls)
|
|
377
|
-
|| (this.ownPeerDescriptor!.websocket!.host === 'localhost' || (isPrivateIPv4(this.ownPeerDescriptor!.websocket!.host)))
|
|
378
|
-
}
|
|
379
|
-
return (this.ownPeerDescriptor!.type === NodeType.BROWSER && peerDescriptor.websocket!.tls)
|
|
380
|
-
|| (peerDescriptor.websocket!.host === 'localhost' || (isPrivateIPv4(peerDescriptor.websocket!.host)))
|
|
381
|
-
}
|
|
382
|
-
|
|
383
288
|
public getConnection(peerDescriptor: PeerDescriptor): ManagedConnection | undefined {
|
|
384
|
-
const
|
|
385
|
-
return this.connections.get(
|
|
289
|
+
const peerIdKey = keyFromPeerDescriptor(peerDescriptor)
|
|
290
|
+
return this.connections.get(peerIdKey)
|
|
386
291
|
}
|
|
387
292
|
|
|
293
|
+
// TODO remove this method or getOwnPeerDescriptor
|
|
388
294
|
public getPeerDescriptor(): PeerDescriptor {
|
|
389
|
-
return this.
|
|
295
|
+
return this.getOwnPeerDescriptor()
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private getOwnPeerDescriptor(): PeerDescriptor {
|
|
299
|
+
return this.connectorFacade.getOwnPeerDescriptor()!
|
|
390
300
|
}
|
|
391
301
|
|
|
392
302
|
public hasConnection(peerDescriptor: PeerDescriptor): boolean {
|
|
393
|
-
const
|
|
394
|
-
return this.connections.has(
|
|
303
|
+
const peerIdKey = keyFromPeerDescriptor(peerDescriptor)
|
|
304
|
+
return this.connections.has(peerIdKey)
|
|
395
305
|
}
|
|
396
306
|
|
|
397
|
-
public hasLocalLockedConnection(peerDescriptor: PeerDescriptor
|
|
398
|
-
const
|
|
399
|
-
return this.locks.isLocalLocked(
|
|
307
|
+
public hasLocalLockedConnection(peerDescriptor: PeerDescriptor): boolean {
|
|
308
|
+
const peerIdKey = keyFromPeerDescriptor(peerDescriptor)
|
|
309
|
+
return this.locks.isLocalLocked(peerIdKey)
|
|
400
310
|
}
|
|
401
311
|
|
|
402
|
-
public hasRemoteLockedConnection(peerDescriptor: PeerDescriptor
|
|
403
|
-
const
|
|
404
|
-
return this.locks.isRemoteLocked(
|
|
312
|
+
public hasRemoteLockedConnection(peerDescriptor: PeerDescriptor): boolean {
|
|
313
|
+
const peerIdKey = keyFromPeerDescriptor(peerDescriptor)
|
|
314
|
+
return this.locks.isRemoteLocked(peerIdKey)
|
|
405
315
|
}
|
|
406
316
|
|
|
407
|
-
|
|
317
|
+
private canConnect(peerDescriptor: PeerDescriptor): boolean {
|
|
408
318
|
// Perhaps the connection's state should be checked here
|
|
409
319
|
return !this.hasConnection(peerDescriptor) // TODO: Add port range check
|
|
410
320
|
}
|
|
@@ -415,17 +325,16 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
415
325
|
logger.trace('Filtered out non-RPC message of type ' + message.messageType)
|
|
416
326
|
return
|
|
417
327
|
}
|
|
418
|
-
if (this.
|
|
419
|
-
logger.trace('handleMessage filtered duplicate ' +
|
|
420
|
-
+
|
|
328
|
+
if (this.duplicateMessageDetector.isMostLikelyDuplicate(message.messageId)) {
|
|
329
|
+
logger.trace('handleMessage filtered duplicate ' + keyFromPeerDescriptor(message.sourceDescriptor!)
|
|
330
|
+
+ ' ' + message.serviceId + ' ' + message.messageId)
|
|
421
331
|
return
|
|
422
332
|
}
|
|
423
|
-
this.
|
|
424
|
-
if (message.serviceId ===
|
|
333
|
+
this.duplicateMessageDetector.add(message.messageId)
|
|
334
|
+
if (message.serviceId === INTERNAL_SERVICE_ID) {
|
|
425
335
|
this.rpcCommunicator?.handleMessageFromPeer(message)
|
|
426
336
|
} else {
|
|
427
|
-
logger.trace('emit "message" ' +
|
|
428
|
-
+ ' ' + message.serviceId + ' ' + message.messageId)
|
|
337
|
+
logger.trace('emit "message" ' + keyFromPeerDescriptor(message.sourceDescriptor!) + ' ' + message.serviceId + ' ' + message.messageId)
|
|
429
338
|
this.emit('message', message)
|
|
430
339
|
}
|
|
431
340
|
}
|
|
@@ -439,7 +348,7 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
439
348
|
let message: Message | undefined
|
|
440
349
|
try {
|
|
441
350
|
message = Message.fromBinary(data)
|
|
442
|
-
logger.trace(
|
|
351
|
+
logger.trace(`received protojson: ${protoToString(message, Message)}`)
|
|
443
352
|
} catch (e) {
|
|
444
353
|
logger.debug(`Parsing incoming data into Message failed: ${e}`)
|
|
445
354
|
return
|
|
@@ -455,52 +364,51 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
455
364
|
private onConnected = (connection: ManagedConnection) => {
|
|
456
365
|
const peerDescriptor = connection.getPeerDescriptor()!
|
|
457
366
|
this.emit('connected', peerDescriptor)
|
|
458
|
-
logger.trace(
|
|
367
|
+
logger.trace(keyFromPeerDescriptor(peerDescriptor) + ' onConnected() ' + connection.connectionType)
|
|
459
368
|
this.onConnectionCountChange()
|
|
460
369
|
}
|
|
461
370
|
|
|
462
371
|
private onDisconnected = (connection: ManagedConnection, disconnectionType: DisconnectionType) => {
|
|
463
|
-
logger.trace(
|
|
464
|
-
' onDisconnected() ' + disconnectionType)
|
|
372
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(connection.getPeerDescriptor()) + ' onDisconnected() ' + disconnectionType)
|
|
465
373
|
|
|
466
|
-
const
|
|
467
|
-
const storedConnection = this.connections.get(
|
|
374
|
+
const peerIdKey = keyFromPeerDescriptor(connection.getPeerDescriptor()!)
|
|
375
|
+
const storedConnection = this.connections.get(peerIdKey)
|
|
468
376
|
if (storedConnection && storedConnection.connectionId.equals(connection.connectionId)) {
|
|
469
|
-
this.locks.clearAllLocks(
|
|
470
|
-
this.connections.delete(
|
|
471
|
-
logger.trace(
|
|
472
|
-
' deleted connection in onDisconnected() ' + disconnectionType)
|
|
377
|
+
this.locks.clearAllLocks(peerIdKey)
|
|
378
|
+
this.connections.delete(peerIdKey)
|
|
379
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(connection.getPeerDescriptor())
|
|
380
|
+
+ ' deleted connection in onDisconnected() ' + disconnectionType)
|
|
473
381
|
this.emit('disconnected', connection.getPeerDescriptor()!, disconnectionType)
|
|
474
382
|
this.onConnectionCountChange()
|
|
475
383
|
} else {
|
|
476
|
-
logger.trace(
|
|
477
|
-
' onDisconnected() did nothing, no such connection in connectionManager')
|
|
384
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(connection.getPeerDescriptor())
|
|
385
|
+
+ ' onDisconnected() did nothing, no such connection in connectionManager')
|
|
478
386
|
if (storedConnection) {
|
|
479
|
-
logger.trace(
|
|
480
|
-
|
|
387
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(connection.getPeerDescriptor())
|
|
388
|
+
+ ' connectionIds do not match ' + storedConnection.connectionId + ' ' + connection.connectionId)
|
|
481
389
|
}
|
|
482
390
|
}
|
|
483
391
|
|
|
484
392
|
}
|
|
485
393
|
|
|
486
|
-
private
|
|
394
|
+
private onIncomingConnection(connection: ManagedConnection): boolean {
|
|
487
395
|
if (this.state === ConnectionManagerState.STOPPED) {
|
|
488
396
|
return false
|
|
489
397
|
}
|
|
490
|
-
logger.trace('
|
|
398
|
+
logger.trace('onIncomingConnection()')
|
|
491
399
|
connection.offeredAsIncoming = true
|
|
492
400
|
if (!this.acceptIncomingConnection(connection)) {
|
|
493
401
|
return false
|
|
494
402
|
}
|
|
495
403
|
connection.on('managedData', this.onData)
|
|
496
|
-
connection.on('disconnected', (disconnectionType: DisconnectionType
|
|
404
|
+
connection.on('disconnected', (disconnectionType: DisconnectionType) => {
|
|
497
405
|
this.onDisconnected(connection, disconnectionType)
|
|
498
406
|
})
|
|
499
407
|
this.emit('newConnection', connection)
|
|
500
408
|
if (connection.isHandshakeCompleted()) {
|
|
501
409
|
this.onConnected(connection)
|
|
502
410
|
} else {
|
|
503
|
-
connection.once('handshakeCompleted', (
|
|
411
|
+
connection.once('handshakeCompleted', ()=> {
|
|
504
412
|
this.onConnected(connection)
|
|
505
413
|
})
|
|
506
414
|
}
|
|
@@ -508,16 +416,16 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
508
416
|
}
|
|
509
417
|
|
|
510
418
|
private acceptIncomingConnection(newConnection: ManagedConnection): boolean {
|
|
511
|
-
logger.trace(
|
|
419
|
+
logger.trace(keyFromPeerDescriptor(newConnection.getPeerDescriptor()!) + ' acceptIncomingConnection()')
|
|
512
420
|
const newPeerID = peerIdFromPeerDescriptor(newConnection.getPeerDescriptor()!)
|
|
513
|
-
const
|
|
514
|
-
if (this.connections.has(
|
|
515
|
-
if (newPeerID.hasSmallerHashThan(peerIdFromPeerDescriptor(this.
|
|
516
|
-
logger.trace(
|
|
517
|
-
' acceptIncomingConnection() replace current connection')
|
|
421
|
+
const peerIdKey = keyFromPeerDescriptor(newConnection.getPeerDescriptor()!)
|
|
422
|
+
if (this.connections.has(peerIdKey)) {
|
|
423
|
+
if (newPeerID.hasSmallerHashThan(peerIdFromPeerDescriptor(this.getOwnPeerDescriptor()))) {
|
|
424
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(newConnection.getPeerDescriptor())
|
|
425
|
+
+ ' acceptIncomingConnection() replace current connection')
|
|
518
426
|
// replace the current connection
|
|
519
427
|
const oldConnection = this.connections.get(newPeerID.toKey())!
|
|
520
|
-
logger.trace('replaced: ' +
|
|
428
|
+
logger.trace('replaced: ' + keyFromPeerDescriptor(newConnection.getPeerDescriptor()!))
|
|
521
429
|
const buffer = oldConnection.stealOutputBuffer()
|
|
522
430
|
|
|
523
431
|
for (const data of buffer) {
|
|
@@ -532,15 +440,14 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
532
440
|
}
|
|
533
441
|
}
|
|
534
442
|
|
|
535
|
-
logger.trace(
|
|
536
|
-
|
|
537
|
-
this.connections.set(hexKey, newConnection)
|
|
443
|
+
logger.trace(keyFromPeerDescriptor(newConnection.getPeerDescriptor()!) + ' added to connections at acceptIncomingConnection')
|
|
444
|
+
this.connections.set(peerIdKey, newConnection)
|
|
538
445
|
|
|
539
446
|
return true
|
|
540
447
|
}
|
|
541
448
|
|
|
542
449
|
private async closeConnection(peerDescriptor: PeerDescriptor, disconnectionType: DisconnectionType, reason?: string): Promise<void> {
|
|
543
|
-
logger.trace(
|
|
450
|
+
logger.trace(keyFromPeerDescriptor(peerDescriptor) + ' ' + 'closeConnection() ' + reason)
|
|
544
451
|
const id = keyFromPeerDescriptor(peerDescriptor)
|
|
545
452
|
this.locks.clearAllLocks(id)
|
|
546
453
|
if (this.connections.has(id)) {
|
|
@@ -548,60 +455,57 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
548
455
|
await connectionToClose.close(disconnectionType)
|
|
549
456
|
|
|
550
457
|
} else {
|
|
551
|
-
logger.trace(' ' + this.
|
|
552
|
-
'closeConnection() this.connections did not have the id')
|
|
458
|
+
logger.trace(keyFromPeerDescriptor(peerDescriptor) + ' ' + 'closeConnection() this.connections did not have the id')
|
|
553
459
|
this.emit('disconnected', peerDescriptor, 'OTHER')
|
|
554
460
|
}
|
|
555
461
|
}
|
|
556
462
|
|
|
557
463
|
public lockConnection(targetDescriptor: PeerDescriptor, serviceId: ServiceId): void {
|
|
558
|
-
if (this.state === ConnectionManagerState.STOPPED ||
|
|
464
|
+
if (this.state === ConnectionManagerState.STOPPED || areEqualPeerDescriptors(targetDescriptor, this.getOwnPeerDescriptor())) {
|
|
559
465
|
return
|
|
560
466
|
}
|
|
561
|
-
const
|
|
562
|
-
const
|
|
563
|
-
this.
|
|
467
|
+
const peerIdKey = keyFromPeerDescriptor(targetDescriptor)
|
|
468
|
+
const rpcRemote = new ConnectionLockRpcRemote(
|
|
469
|
+
this.getOwnPeerDescriptor(),
|
|
564
470
|
targetDescriptor,
|
|
565
|
-
|
|
566
|
-
toProtoRpcClient(new ConnectionLockerClient(this.rpcCommunicator!.getRpcClientTransport()))
|
|
471
|
+
toProtoRpcClient(new ConnectionLockRpcClient(this.rpcCommunicator!.getRpcClientTransport()))
|
|
567
472
|
)
|
|
568
|
-
this.locks.addLocalLocked(
|
|
569
|
-
|
|
473
|
+
this.locks.addLocalLocked(peerIdKey, serviceId)
|
|
474
|
+
rpcRemote.lockRequest(serviceId)
|
|
570
475
|
.then((_accepted) => logger.trace('LockRequest successful'))
|
|
571
476
|
.catch((err) => { logger.debug(err) })
|
|
572
477
|
}
|
|
573
478
|
|
|
574
479
|
public unlockConnection(targetDescriptor: PeerDescriptor, serviceId: ServiceId): void {
|
|
575
|
-
if (this.state === ConnectionManagerState.STOPPED ||
|
|
480
|
+
if (this.state === ConnectionManagerState.STOPPED || areEqualPeerDescriptors(targetDescriptor, this.getOwnPeerDescriptor())) {
|
|
576
481
|
return
|
|
577
482
|
}
|
|
578
|
-
const
|
|
579
|
-
this.locks.removeLocalLocked(
|
|
580
|
-
const
|
|
581
|
-
this.
|
|
483
|
+
const peerIdKey = keyFromPeerDescriptor(targetDescriptor)
|
|
484
|
+
this.locks.removeLocalLocked(peerIdKey, serviceId)
|
|
485
|
+
const rpcRemote = new ConnectionLockRpcRemote(
|
|
486
|
+
this.getOwnPeerDescriptor(),
|
|
582
487
|
targetDescriptor,
|
|
583
|
-
|
|
584
|
-
toProtoRpcClient(new ConnectionLockerClient(this.rpcCommunicator!.getRpcClientTransport()))
|
|
488
|
+
toProtoRpcClient(new ConnectionLockRpcClient(this.rpcCommunicator!.getRpcClientTransport()))
|
|
585
489
|
)
|
|
586
|
-
if (this.connections.has(
|
|
587
|
-
|
|
490
|
+
if (this.connections.has(peerIdKey)) {
|
|
491
|
+
rpcRemote.unlockRequest(serviceId)
|
|
588
492
|
}
|
|
589
493
|
}
|
|
590
494
|
|
|
591
495
|
public weakLockConnection(targetDescriptor: PeerDescriptor): void {
|
|
592
|
-
if (this.state === ConnectionManagerState.STOPPED ||
|
|
496
|
+
if (this.state === ConnectionManagerState.STOPPED || areEqualPeerDescriptors(targetDescriptor, this.getOwnPeerDescriptor())) {
|
|
593
497
|
return
|
|
594
498
|
}
|
|
595
|
-
const
|
|
596
|
-
this.locks.addWeakLocked(
|
|
499
|
+
const peerIdKey = keyFromPeerDescriptor(targetDescriptor)
|
|
500
|
+
this.locks.addWeakLocked(peerIdKey)
|
|
597
501
|
}
|
|
598
502
|
|
|
599
503
|
public weakUnlockConnection(targetDescriptor: PeerDescriptor): void {
|
|
600
|
-
if (this.state === ConnectionManagerState.STOPPED ||
|
|
504
|
+
if (this.state === ConnectionManagerState.STOPPED || areEqualPeerDescriptors(targetDescriptor, this.getOwnPeerDescriptor())) {
|
|
601
505
|
return
|
|
602
506
|
}
|
|
603
|
-
const
|
|
604
|
-
this.locks.removeWeakLocked(
|
|
507
|
+
const peerIdKey = keyFromPeerDescriptor(targetDescriptor)
|
|
508
|
+
this.locks.removeWeakLocked(peerIdKey)
|
|
605
509
|
|
|
606
510
|
}
|
|
607
511
|
|
|
@@ -640,18 +544,16 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
640
544
|
}
|
|
641
545
|
|
|
642
546
|
private async doGracefullyDisconnectAsync(targetDescriptor: PeerDescriptor, disconnectMode: DisconnectMode): Promise<void> {
|
|
643
|
-
logger.trace(
|
|
644
|
-
const
|
|
645
|
-
this.
|
|
547
|
+
logger.trace(keyFromPeerDescriptor(targetDescriptor) + ' gracefullyDisconnectAsync()')
|
|
548
|
+
const rpcRemote = new ConnectionLockRpcRemote(
|
|
549
|
+
this.getOwnPeerDescriptor(),
|
|
646
550
|
targetDescriptor,
|
|
647
|
-
|
|
648
|
-
toProtoRpcClient(new ConnectionLockerClient(this.rpcCommunicator!.getRpcClientTransport()))
|
|
551
|
+
toProtoRpcClient(new ConnectionLockRpcClient(this.rpcCommunicator!.getRpcClientTransport()))
|
|
649
552
|
)
|
|
650
553
|
try {
|
|
651
|
-
await
|
|
554
|
+
await rpcRemote.gracefulDisconnect(disconnectMode)
|
|
652
555
|
} catch (ex) {
|
|
653
|
-
logger.trace(
|
|
654
|
-
' remoteConnectionLocker.gracefulDisconnect() failed' + ex)
|
|
556
|
+
logger.trace(keyFromPeerDescriptor(targetDescriptor) + ' remote.gracefulDisconnect() failed' + ex)
|
|
655
557
|
}
|
|
656
558
|
}
|
|
657
559
|
|
|
@@ -661,10 +563,11 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
661
563
|
.map((managedConnection: ManagedConnection) => managedConnection.getPeerDescriptor()!)
|
|
662
564
|
}
|
|
663
565
|
|
|
664
|
-
//
|
|
665
|
-
private async lockRequest(lockRequest: LockRequest,
|
|
666
|
-
const
|
|
667
|
-
|
|
566
|
+
// ConnectionLockRpc local RPC method
|
|
567
|
+
private async lockRequest(lockRequest: LockRequest, context: ServerCallContext): Promise<LockResponse> {
|
|
568
|
+
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
569
|
+
const remotePeerId = peerIdFromPeerDescriptor(senderPeerDescriptor)
|
|
570
|
+
if (areEqualPeerDescriptors(senderPeerDescriptor, this.getOwnPeerDescriptor())) {
|
|
668
571
|
const response: LockResponse = {
|
|
669
572
|
accepted: false
|
|
670
573
|
}
|
|
@@ -677,22 +580,23 @@ export class ConnectionManager extends EventEmitter<Events> implements ITranspor
|
|
|
677
580
|
return response
|
|
678
581
|
}
|
|
679
582
|
|
|
680
|
-
//
|
|
681
|
-
private async unlockRequest(unlockRequest: UnlockRequest,
|
|
682
|
-
const
|
|
683
|
-
|
|
583
|
+
// ConnectionLockRpc local RPC method
|
|
584
|
+
private async unlockRequest(unlockRequest: UnlockRequest, context: ServerCallContext): Promise<Empty> {
|
|
585
|
+
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
586
|
+
const peerIdKey = keyFromPeerDescriptor(senderPeerDescriptor)
|
|
587
|
+
this.locks.removeRemoteLocked(peerIdKey, unlockRequest.serviceId)
|
|
684
588
|
return {}
|
|
685
589
|
}
|
|
686
590
|
|
|
687
|
-
//
|
|
688
|
-
private async gracefulDisconnect(disconnectNotice: DisconnectNotice,
|
|
689
|
-
|
|
690
|
-
|
|
591
|
+
// ConnectionLockRpc local RPC method
|
|
592
|
+
private async gracefulDisconnect(disconnectNotice: DisconnectNotice, context: ServerCallContext): Promise<Empty> {
|
|
593
|
+
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
594
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(senderPeerDescriptor) + ' received gracefulDisconnect notice')
|
|
691
595
|
|
|
692
|
-
if (disconnectNotice.
|
|
693
|
-
this.closeConnection(
|
|
596
|
+
if (disconnectNotice.disconnectMode === DisconnectMode.LEAVING) {
|
|
597
|
+
this.closeConnection(senderPeerDescriptor, 'INCOMING_GRACEFUL_LEAVE', 'graceful leave notified')
|
|
694
598
|
} else {
|
|
695
|
-
this.closeConnection(
|
|
599
|
+
this.closeConnection(senderPeerDescriptor, 'INCOMING_GRACEFUL_DISCONNECT', 'graceful disconnect notified')
|
|
696
600
|
}
|
|
697
601
|
return {}
|
|
698
602
|
}
|