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