@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
|
@@ -21,7 +21,7 @@ const logger = new Logger(module)
|
|
|
21
21
|
export enum ConnectionMode { REQUEST = 'connectivityRequest', PROBE = 'connectivityProbe' }
|
|
22
22
|
export class ConnectivityChecker {
|
|
23
23
|
|
|
24
|
-
private static readonly CONNECTIVITY_CHECKER_SERVICE_ID = 'system/
|
|
24
|
+
private static readonly CONNECTIVITY_CHECKER_SERVICE_ID = 'system/connectivity-checker'
|
|
25
25
|
private static readonly CONNECTIVITY_CHECKER_TIMEOUT = 5000
|
|
26
26
|
private destroyed = false
|
|
27
27
|
private readonly webSocketPort: number
|
|
@@ -49,7 +49,7 @@ export class ConnectivityChecker {
|
|
|
49
49
|
mode: ConnectionMode.REQUEST
|
|
50
50
|
})
|
|
51
51
|
} catch (e) {
|
|
52
|
-
throw new Err.ConnectionFailed(
|
|
52
|
+
throw new Err.ConnectionFailed(`Failed to connect to the entrypoint ${connectivityMethodToWebSocketUrl(entryPoint.websocket!)}`, e)
|
|
53
53
|
}
|
|
54
54
|
// send connectivity request
|
|
55
55
|
const connectivityRequestMessage: ConnectivityRequest = { port: this.webSocketPort, host: this.host, tls: this.tls }
|
|
@@ -72,6 +72,7 @@ export class ConnectivityChecker {
|
|
|
72
72
|
try {
|
|
73
73
|
const message: Message = Message.fromBinary(bytes)
|
|
74
74
|
if (message.body.oneofKind === 'connectivityResponse') {
|
|
75
|
+
logger.trace('ConnectivityResponse received: ' + JSON.stringify(Message.toJson(message)))
|
|
75
76
|
const connectivityResponseMessage = message.body.connectivityResponse
|
|
76
77
|
outgoingConnection!.off('data', listener)
|
|
77
78
|
clearTimeout(timeoutId)
|
|
@@ -88,9 +89,8 @@ export class ConnectivityChecker {
|
|
|
88
89
|
}
|
|
89
90
|
try {
|
|
90
91
|
const retPromise = responseAwaiter()
|
|
91
|
-
logger.trace('trying to send connectivity request')
|
|
92
92
|
outgoingConnection.send(Message.toBinary(msg))
|
|
93
|
-
logger.
|
|
93
|
+
logger.trace('ConnectivityRequest sent: ' + JSON.stringify(Message.toJson(msg)))
|
|
94
94
|
return await retPromise
|
|
95
95
|
} catch (e) {
|
|
96
96
|
logger.error('error getting connectivityresponse')
|
|
@@ -104,7 +104,7 @@ export class ConnectivityChecker {
|
|
|
104
104
|
try {
|
|
105
105
|
const message = Message.fromBinary(data)
|
|
106
106
|
if (message.body.oneofKind === 'connectivityRequest') {
|
|
107
|
-
logger.trace('received
|
|
107
|
+
logger.trace('ConnectivityRequest received: ' + JSON.stringify(Message.toJson(message)))
|
|
108
108
|
this.handleIncomingConnectivityRequest(connectionToListenTo, message.body.connectivityRequest).then(() => {
|
|
109
109
|
logger.trace('handleIncomingConnectivityRequest ok')
|
|
110
110
|
return
|
|
@@ -130,12 +130,14 @@ export class ConnectivityChecker {
|
|
|
130
130
|
let connectivityResponseMessage: ConnectivityResponse | undefined
|
|
131
131
|
const host = connectivityRequest.host ?? connection.getRemoteAddress()
|
|
132
132
|
try {
|
|
133
|
+
const wsServerInfo = {
|
|
134
|
+
host,
|
|
135
|
+
port: connectivityRequest.port,
|
|
136
|
+
tls: connectivityRequest.tls
|
|
137
|
+
}
|
|
138
|
+
logger.trace(`Attempting Connectivity Check to ${connectivityMethodToWebSocketUrl(wsServerInfo)}`)
|
|
133
139
|
outgoingConnection = await this.connectAsync({
|
|
134
|
-
wsServerInfo
|
|
135
|
-
host,
|
|
136
|
-
port: connectivityRequest.port,
|
|
137
|
-
tls: connectivityRequest.tls
|
|
138
|
-
},
|
|
140
|
+
wsServerInfo,
|
|
139
141
|
mode: ConnectionMode.PROBE
|
|
140
142
|
})
|
|
141
143
|
} catch (err) {
|
|
@@ -148,7 +150,7 @@ export class ConnectivityChecker {
|
|
|
148
150
|
}
|
|
149
151
|
if (outgoingConnection) {
|
|
150
152
|
outgoingConnection.close('OTHER')
|
|
151
|
-
logger.trace('Connectivity test produced positive result, communicating reply to the requester')
|
|
153
|
+
logger.trace('Connectivity test produced positive result, communicating reply to the requester ' + host + ':' + connectivityRequest.port)
|
|
152
154
|
connectivityResponseMessage = {
|
|
153
155
|
openInternet: true,
|
|
154
156
|
host,
|
|
@@ -165,6 +167,7 @@ export class ConnectivityChecker {
|
|
|
165
167
|
}
|
|
166
168
|
}
|
|
167
169
|
connection.send(Message.toBinary(msg))
|
|
170
|
+
logger.trace('ConnectivityResponse sent: ' + JSON.stringify(Message.toJson(msg)))
|
|
168
171
|
}
|
|
169
172
|
|
|
170
173
|
// eslint-disable-next-line class-methods-use-this
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Logger } from '@streamr/utils'
|
|
2
|
+
import {
|
|
3
|
+
ConnectivityResponse,
|
|
4
|
+
PeerDescriptor
|
|
5
|
+
} from '../proto/packages/dht/protos/DhtRpc'
|
|
6
|
+
import { ITransport } from '../transport/ITransport'
|
|
7
|
+
import { ConnectionManager, PortRange, TlsCertificate } from './ConnectionManager'
|
|
8
|
+
import { ManagedConnection } from './ManagedConnection'
|
|
9
|
+
import { Simulator } from './Simulator/Simulator'
|
|
10
|
+
import { SimulatorConnector } from './Simulator/SimulatorConnector'
|
|
11
|
+
import { IceServer, WebRtcConnector } from './WebRTC/WebRtcConnector'
|
|
12
|
+
import { WebSocketConnector } from './WebSocket/WebSocketConnector'
|
|
13
|
+
|
|
14
|
+
export interface ConnectorFacade {
|
|
15
|
+
createConnection: (peerDescriptor: PeerDescriptor) => ManagedConnection
|
|
16
|
+
getOwnPeerDescriptor: () => PeerDescriptor | undefined
|
|
17
|
+
start: (
|
|
18
|
+
onIncomingConnection: (connection: ManagedConnection) => boolean,
|
|
19
|
+
canConnect: (peerDescriptor: PeerDescriptor) => boolean
|
|
20
|
+
) => Promise<void>
|
|
21
|
+
stop: () => Promise<void>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const logger = new Logger(module)
|
|
25
|
+
|
|
26
|
+
export interface DefaultConnectorFacadeConfig {
|
|
27
|
+
transport: ITransport
|
|
28
|
+
websocketHost?: string
|
|
29
|
+
websocketPortRange?: PortRange
|
|
30
|
+
entryPoints?: PeerDescriptor[]
|
|
31
|
+
iceServers?: IceServer[]
|
|
32
|
+
webrtcAllowPrivateAddresses?: boolean
|
|
33
|
+
webrtcDatachannelBufferThresholdLow?: number
|
|
34
|
+
webrtcDatachannelBufferThresholdHigh?: number
|
|
35
|
+
webrtcNewConnectionTimeout?: number
|
|
36
|
+
externalIp?: string
|
|
37
|
+
webrtcPortRange?: PortRange
|
|
38
|
+
maxMessageSize?: number
|
|
39
|
+
tlsCertificate?: TlsCertificate
|
|
40
|
+
createOwnPeerDescriptor: (connectivityResponse: ConnectivityResponse) => PeerDescriptor
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class DefaultConnectorFacade implements ConnectorFacade {
|
|
44
|
+
|
|
45
|
+
private readonly config: DefaultConnectorFacadeConfig
|
|
46
|
+
private ownPeerDescriptor?: PeerDescriptor
|
|
47
|
+
private webSocketConnector?: WebSocketConnector
|
|
48
|
+
private webrtcConnector?: WebRtcConnector
|
|
49
|
+
|
|
50
|
+
constructor(config: DefaultConnectorFacadeConfig) {
|
|
51
|
+
this.config = config
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async start(
|
|
55
|
+
onIncomingConnection: (connection: ManagedConnection) => boolean,
|
|
56
|
+
canConnect: (peerDescriptor: PeerDescriptor) => boolean
|
|
57
|
+
): Promise<void> {
|
|
58
|
+
logger.trace(`Creating WebSocketConnector`)
|
|
59
|
+
this.webSocketConnector = new WebSocketConnector({
|
|
60
|
+
protocolVersion: ConnectionManager.PROTOCOL_VERSION,
|
|
61
|
+
transport: this.config.transport!,
|
|
62
|
+
// TODO should we use canConnect also for WebRtcConnector? (NET-1142)
|
|
63
|
+
canConnect: (peerDescriptor: PeerDescriptor) => canConnect(peerDescriptor),
|
|
64
|
+
onIncomingConnection,
|
|
65
|
+
portRange: this.config.websocketPortRange,
|
|
66
|
+
host: this.config.websocketHost,
|
|
67
|
+
entrypoints: this.config.entryPoints,
|
|
68
|
+
tlsCertificate: this.config.tlsCertificate,
|
|
69
|
+
maxMessageSize: this.config.maxMessageSize
|
|
70
|
+
})
|
|
71
|
+
logger.trace(`Creating WebRTCConnector`)
|
|
72
|
+
this.webrtcConnector = new WebRtcConnector({
|
|
73
|
+
transport: this.config.transport!,
|
|
74
|
+
protocolVersion: ConnectionManager.PROTOCOL_VERSION,
|
|
75
|
+
iceServers: this.config.iceServers,
|
|
76
|
+
allowPrivateAddresses: this.config.webrtcAllowPrivateAddresses,
|
|
77
|
+
bufferThresholdLow: this.config.webrtcDatachannelBufferThresholdLow,
|
|
78
|
+
bufferThresholdHigh: this.config.webrtcDatachannelBufferThresholdHigh,
|
|
79
|
+
connectionTimeout: this.config.webrtcNewConnectionTimeout,
|
|
80
|
+
externalIp: this.config.externalIp,
|
|
81
|
+
portRange: this.config.webrtcPortRange,
|
|
82
|
+
maxMessageSize: this.config.maxMessageSize
|
|
83
|
+
}, onIncomingConnection)
|
|
84
|
+
await this.webSocketConnector.start()
|
|
85
|
+
const connectivityResponse = await this.webSocketConnector.checkConnectivity()
|
|
86
|
+
const ownPeerDescriptor = this.config.createOwnPeerDescriptor(connectivityResponse)
|
|
87
|
+
this.ownPeerDescriptor = ownPeerDescriptor
|
|
88
|
+
this.webSocketConnector.setOwnPeerDescriptor(ownPeerDescriptor)
|
|
89
|
+
this.webrtcConnector.setOwnPeerDescriptor(ownPeerDescriptor)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
createConnection(peerDescriptor: PeerDescriptor): ManagedConnection {
|
|
93
|
+
if (this.webSocketConnector!.isPossibleToFormConnection(peerDescriptor)) {
|
|
94
|
+
return this.webSocketConnector!.connect(peerDescriptor)
|
|
95
|
+
} else {
|
|
96
|
+
return this.webrtcConnector!.connect(peerDescriptor)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
getOwnPeerDescriptor(): PeerDescriptor | undefined {
|
|
101
|
+
return this.ownPeerDescriptor
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async stop(): Promise<void> {
|
|
105
|
+
await this.webSocketConnector!.destroy()
|
|
106
|
+
await this.webrtcConnector!.stop()
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export class SimulatorConnectorFacade implements ConnectorFacade {
|
|
111
|
+
|
|
112
|
+
private readonly ownPeerDescriptor: PeerDescriptor
|
|
113
|
+
private simulatorConnector?: SimulatorConnector
|
|
114
|
+
private simulator: Simulator
|
|
115
|
+
|
|
116
|
+
constructor(ownPeerDescriptor: PeerDescriptor, simulator: Simulator) {
|
|
117
|
+
this.ownPeerDescriptor = ownPeerDescriptor
|
|
118
|
+
this.simulator = simulator
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async start(onIncomingConnection: (connection: ManagedConnection) => boolean): Promise<void> {
|
|
122
|
+
logger.trace(`Creating SimulatorConnector`)
|
|
123
|
+
this.simulatorConnector = new SimulatorConnector(
|
|
124
|
+
ConnectionManager.PROTOCOL_VERSION,
|
|
125
|
+
this.ownPeerDescriptor,
|
|
126
|
+
this.simulator,
|
|
127
|
+
onIncomingConnection
|
|
128
|
+
)
|
|
129
|
+
this.simulator.addConnector(this.simulatorConnector)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
createConnection(peerDescriptor: PeerDescriptor): ManagedConnection {
|
|
133
|
+
return this.simulatorConnector!.connect(peerDescriptor)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
getOwnPeerDescriptor(): PeerDescriptor {
|
|
137
|
+
return this.ownPeerDescriptor
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async stop(): Promise<void> {
|
|
141
|
+
await this.simulatorConnector!.stop()
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -7,6 +7,7 @@ import EventEmitter from 'eventemitter3'
|
|
|
7
7
|
import { PeerIDKey } from '../helpers/PeerID'
|
|
8
8
|
import { keyFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
|
|
9
9
|
import { DisconnectionType } from '../transport/ITransport'
|
|
10
|
+
import { keyOrUnknownFromPeerDescriptor } from './ConnectionManager'
|
|
10
11
|
|
|
11
12
|
export interface ManagedConnectionEvents {
|
|
12
13
|
managedData: (bytes: Uint8Array, remotePeerDescriptor: PeerDescriptor) => void
|
|
@@ -23,8 +24,6 @@ const logger = new Logger(module)
|
|
|
23
24
|
export type Events = ManagedConnectionEvents & ConnectionEvents
|
|
24
25
|
export class ManagedConnection extends EventEmitter<Events> {
|
|
25
26
|
|
|
26
|
-
private static objectCounter = 0
|
|
27
|
-
public objectId = 0
|
|
28
27
|
private implementation?: IConnection
|
|
29
28
|
|
|
30
29
|
private outputBuffer: Uint8Array[] = []
|
|
@@ -58,8 +57,6 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
58
57
|
incomingConnection?: IConnection,
|
|
59
58
|
) {
|
|
60
59
|
super()
|
|
61
|
-
this.objectId = ManagedConnection.objectCounter
|
|
62
|
-
ManagedConnection.objectCounter++
|
|
63
60
|
|
|
64
61
|
this.send = this.send.bind(this)
|
|
65
62
|
|
|
@@ -72,7 +69,7 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
72
69
|
|
|
73
70
|
this.onDisconnected = this.onDisconnected.bind(this)
|
|
74
71
|
|
|
75
|
-
logger.trace('creating ManagedConnection of type: ' + connectionType
|
|
72
|
+
logger.trace('creating ManagedConnection of type: ' + connectionType)
|
|
76
73
|
if (incomingConnection && outgoingConnection) {
|
|
77
74
|
throw new Err.IllegalArguments('Managed connection constructor only accepts either an incoming connection OR a outgoing connection')
|
|
78
75
|
}
|
|
@@ -81,15 +78,14 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
81
78
|
this.handshaker = new Handshaker(this.ownPeerDescriptor, this.protocolVersion, outgoingConnection)
|
|
82
79
|
|
|
83
80
|
this.handshaker.once('handshakeFailed', (errorMessage) => {
|
|
84
|
-
logger.trace(
|
|
81
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor) + ' handshakeFailed: ' + errorMessage)
|
|
85
82
|
this.emit('handshakeFailed')
|
|
86
83
|
})
|
|
87
84
|
|
|
88
85
|
this.handshaker.on('handshakeCompleted', (peerDescriptor: PeerDescriptor) => {
|
|
89
|
-
logger.trace('handshake completed for outgoing connection '
|
|
90
|
-
', ' + this.peerDescriptor
|
|
86
|
+
logger.trace('handshake completed for outgoing connection '
|
|
87
|
+
+ ', ' + keyOrUnknownFromPeerDescriptor(this.peerDescriptor)
|
|
91
88
|
+ ' outputBuffer.length: ' + this.outputBuffer.length)
|
|
92
|
-
|
|
93
89
|
this.attachImplementation(outgoingConnection)
|
|
94
90
|
this.onHandshakeCompleted(peerDescriptor)
|
|
95
91
|
})
|
|
@@ -124,7 +120,7 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
124
120
|
): this {
|
|
125
121
|
if (event === 'managedData' && this.listenerCount('managedData') === 0) {
|
|
126
122
|
while (this.inputBuffer.length > 0) {
|
|
127
|
-
logger.trace('emptying inputBuffer
|
|
123
|
+
logger.trace('emptying inputBuffer')
|
|
128
124
|
const data = this.inputBuffer.shift()!
|
|
129
125
|
fn(data, this.getPeerDescriptor())
|
|
130
126
|
}
|
|
@@ -142,7 +138,7 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
142
138
|
if (event === 'managedData' && this.listenerCount('managedData') === 0) {
|
|
143
139
|
if (this.inputBuffer.length > 0) {
|
|
144
140
|
while (this.inputBuffer.length > 0) {
|
|
145
|
-
logger.trace('emptying inputBuffer
|
|
141
|
+
logger.trace('emptying inputBuffer')
|
|
146
142
|
const data = this.inputBuffer.shift()!
|
|
147
143
|
fn(data, this.getPeerDescriptor())
|
|
148
144
|
}
|
|
@@ -179,17 +175,16 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
179
175
|
this.handshakeCompleted = true
|
|
180
176
|
|
|
181
177
|
while (this.outputBuffer.length > 0) {
|
|
182
|
-
logger.trace('emptying outputBuffer
|
|
183
|
-
|
|
178
|
+
logger.trace('emptying outputBuffer')
|
|
184
179
|
this.implementation!.send(this.outputBuffer.shift()!)
|
|
185
180
|
}
|
|
186
181
|
|
|
187
|
-
logger.trace('emitting handshake_completed
|
|
182
|
+
logger.trace('emitting handshake_completed')
|
|
188
183
|
this.emit('handshakeCompleted', peerDescriptor)
|
|
189
184
|
}
|
|
190
185
|
|
|
191
|
-
public attachImplementation(impl: IConnection
|
|
192
|
-
logger.trace('attachImplementation()
|
|
186
|
+
public attachImplementation(impl: IConnection): void {
|
|
187
|
+
logger.trace('attachImplementation()')
|
|
193
188
|
this.implementation = impl
|
|
194
189
|
|
|
195
190
|
impl.on('data', (bytes: Uint8Array) => {
|
|
@@ -215,8 +210,8 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
215
210
|
impl.on('disconnected', this.onDisconnected)
|
|
216
211
|
}
|
|
217
212
|
|
|
218
|
-
private onDisconnected(disconnectionType: DisconnectionType
|
|
219
|
-
logger.trace(
|
|
213
|
+
private onDisconnected(disconnectionType: DisconnectionType): void {
|
|
214
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor) + ' onDisconnected() ' + disconnectionType)
|
|
220
215
|
if (this.bufferSentbyOtherConnection) {
|
|
221
216
|
return
|
|
222
217
|
}
|
|
@@ -238,7 +233,7 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
238
233
|
} else if (this.implementation) {
|
|
239
234
|
this.implementation.send(data)
|
|
240
235
|
} else {
|
|
241
|
-
logger.trace('adding data to outputBuffer
|
|
236
|
+
logger.trace('adding data to outputBuffer')
|
|
242
237
|
|
|
243
238
|
let result: RunAndRaceEventsReturnType<Events>
|
|
244
239
|
|
|
@@ -248,7 +243,7 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
248
243
|
result = await runAndRaceEvents3<Events>([() => { this.outputBuffer.push(data) }], this, ['handshakeCompleted', 'handshakeFailed',
|
|
249
244
|
'bufferSentByOtherConnection', 'closing', 'internal_disconnected'], 15000)
|
|
250
245
|
} catch (e) {
|
|
251
|
-
logger.debug(`Connection
|
|
246
|
+
logger.debug(`Connection to ${keyOrUnknownFromPeerDescriptor(this.peerDescriptor)} timed out`)
|
|
252
247
|
throw e
|
|
253
248
|
}
|
|
254
249
|
|
|
@@ -256,7 +251,7 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
256
251
|
this.doNotEmitDisconnected = false
|
|
257
252
|
this.doDisconnect('OTHER')
|
|
258
253
|
} else if (result.winnerName === 'handshakeFailed') {
|
|
259
|
-
logger.trace(
|
|
254
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor) + ' handshakeFailed received')
|
|
260
255
|
|
|
261
256
|
if (this.bufferSentbyOtherConnection) {
|
|
262
257
|
logger.trace('bufferSentByOtherConnection already true')
|
|
@@ -269,8 +264,8 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
269
264
|
result2 = await raceEvents3<Events>(this,
|
|
270
265
|
['bufferSentByOtherConnection', 'closing', 'disconnected'], 15000)
|
|
271
266
|
} catch (ex) {
|
|
272
|
-
logger.trace(
|
|
273
|
-
' Exception from raceEvents3 while waiting bufferSentByOtherConnection or closing ' + ex)
|
|
267
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor)
|
|
268
|
+
+ ' Exception from raceEvents3 while waiting bufferSentByOtherConnection or closing ' + ex)
|
|
274
269
|
throw ex
|
|
275
270
|
}
|
|
276
271
|
if (result2.winnerName === 'bufferSentByOtherConnection') {
|
|
@@ -294,13 +289,13 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
294
289
|
if (this.implementation) {
|
|
295
290
|
this.implementation.send(data)
|
|
296
291
|
} else {
|
|
297
|
-
logger.trace('adding data to outputBuffer
|
|
292
|
+
logger.trace('adding data to outputBuffer')
|
|
298
293
|
this.outputBuffer.push(data)
|
|
299
294
|
}
|
|
300
295
|
}
|
|
301
296
|
|
|
302
297
|
public reportBufferSentByOtherConnection(): void {
|
|
303
|
-
logger.trace(
|
|
298
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor) + ' reportBufferSentByOtherConnection')
|
|
304
299
|
if (this.handshaker) {
|
|
305
300
|
this.handshaker.removeAllListeners()
|
|
306
301
|
}
|
|
@@ -309,11 +304,6 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
309
304
|
this.emit('bufferSentByOtherConnection')
|
|
310
305
|
}
|
|
311
306
|
|
|
312
|
-
public reportBufferSendingByOtherConnectionFailed(): void {
|
|
313
|
-
logger.trace('reportBufferSendingByOtherConnectionFailed')
|
|
314
|
-
this.doDisconnect('OTHER')
|
|
315
|
-
}
|
|
316
|
-
|
|
317
307
|
public acceptHandshake(): void {
|
|
318
308
|
// This happens when connectionRequest has been made and answered
|
|
319
309
|
if (this.implementation) {
|
|
@@ -336,14 +326,13 @@ export class ManagedConnection extends EventEmitter<Events> {
|
|
|
336
326
|
}
|
|
337
327
|
|
|
338
328
|
private doDisconnect(disconnectionType: DisconnectionType) {
|
|
339
|
-
logger.trace(
|
|
329
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor) + ' doDisconnect() emitting')
|
|
340
330
|
|
|
341
331
|
if (!this.doNotEmitDisconnected) {
|
|
342
|
-
logger.trace(
|
|
332
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor) + ' emitting disconnected')
|
|
343
333
|
this.emit('disconnected', disconnectionType)
|
|
344
334
|
} else {
|
|
345
|
-
logger.trace(
|
|
346
|
-
' not emitting disconnected because doNotEmitDisconnected flag is set')
|
|
335
|
+
logger.trace(keyOrUnknownFromPeerDescriptor(this.peerDescriptor) + ' not emitting disconnected because doNotEmitDisconnected flag is set')
|
|
347
336
|
}
|
|
348
337
|
}
|
|
349
338
|
|
|
@@ -5,6 +5,7 @@ import { LockRequest, UnlockRequest, PeerDescriptor, DisconnectNotice, Disconnec
|
|
|
5
5
|
import { DhtRpcOptions } from '../rpc-protocol/DhtRpcOptions'
|
|
6
6
|
|
|
7
7
|
import * as Err from '../helpers/errors'
|
|
8
|
+
import { keyFromPeerDescriptor } from '../helpers/peerIdFromPeerDescriptor'
|
|
8
9
|
|
|
9
10
|
const logger = new Logger(module)
|
|
10
11
|
|
|
@@ -27,7 +28,7 @@ export class RemoteConnectionLocker {
|
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
public async lockRequest(serviceId: string): Promise<boolean> {
|
|
30
|
-
logger.trace(`Requesting locked connection to ${this.targetPeerDescriptor
|
|
31
|
+
logger.trace(`Requesting locked connection to ${keyFromPeerDescriptor(this.targetPeerDescriptor)}`)
|
|
31
32
|
const request: LockRequest = {
|
|
32
33
|
peerDescriptor: this.ownPeerDescriptor,
|
|
33
34
|
protocolVersion: this.protocolVersion,
|
|
@@ -47,7 +48,7 @@ export class RemoteConnectionLocker {
|
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
public unlockRequest(serviceId: string): void {
|
|
50
|
-
logger.trace(`Requesting connection to be unlocked from ${this.targetPeerDescriptor
|
|
51
|
+
logger.trace(`Requesting connection to be unlocked from ${keyFromPeerDescriptor(this.targetPeerDescriptor)}`)
|
|
51
52
|
const request: UnlockRequest = {
|
|
52
53
|
peerDescriptor: this.ownPeerDescriptor,
|
|
53
54
|
protocolVersion: this.protocolVersion,
|
|
@@ -66,7 +67,7 @@ export class RemoteConnectionLocker {
|
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
public async gracefulDisconnect(disconnecMode: DisconnectMode): Promise<void> {
|
|
69
|
-
logger.trace(`Notifying a graceful disconnect to ${this.targetPeerDescriptor
|
|
70
|
+
logger.trace(`Notifying a graceful disconnect to ${keyFromPeerDescriptor(this.targetPeerDescriptor)}`)
|
|
70
71
|
const request: DisconnectNotice = {
|
|
71
72
|
peerDescriptor: this.ownPeerDescriptor,
|
|
72
73
|
protocolVersion: this.protocolVersion,
|
|
@@ -110,7 +110,6 @@ export class Simulator extends EventEmitter<ConnectionSourceEvents> {
|
|
|
110
110
|
private fixedLatency?: number
|
|
111
111
|
|
|
112
112
|
private loopCounter = 0
|
|
113
|
-
private operationCounter = 0
|
|
114
113
|
private MAX_LOOPS = 1000
|
|
115
114
|
|
|
116
115
|
private operationQueue: Heap<SimulatorOperation> = new Heap<SimulatorOperation>((a: SimulatorOperation, b: SimulatorOperation) => {
|
|
@@ -273,8 +272,6 @@ export class Simulator extends EventEmitter<ConnectionSourceEvents> {
|
|
|
273
272
|
&& this.operationQueue.peek()!.executionTime <= currentTime) {
|
|
274
273
|
const operation = this.operationQueue.pop()
|
|
275
274
|
|
|
276
|
-
this.operationCounter++
|
|
277
|
-
|
|
278
275
|
if (operation instanceof ConnectOperation) {
|
|
279
276
|
this.executeConnectOperation(operation)
|
|
280
277
|
} else if (operation instanceof CloseOperation) {
|
|
@@ -296,10 +293,6 @@ export class Simulator extends EventEmitter<ConnectionSourceEvents> {
|
|
|
296
293
|
this.scheduleNextTimeout()
|
|
297
294
|
}
|
|
298
295
|
|
|
299
|
-
public getOperationCounter(): number {
|
|
300
|
-
return this.operationCounter
|
|
301
|
-
}
|
|
302
|
-
|
|
303
296
|
private scheduleNextTimeout(): void {
|
|
304
297
|
if (this.simulatorTimeout) {
|
|
305
298
|
clearTimeout(this.simulatorTimeout)
|
|
@@ -5,6 +5,7 @@ import { Connection } from '../Connection'
|
|
|
5
5
|
import { Logger } from '@streamr/utils'
|
|
6
6
|
import { protoToString } from '../../helpers/protoToString'
|
|
7
7
|
import { DisconnectionType } from '../../transport/ITransport'
|
|
8
|
+
import { keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
8
9
|
|
|
9
10
|
const logger = new Logger(module)
|
|
10
11
|
|
|
@@ -45,35 +46,36 @@ export class SimulatorConnection extends Connection implements IConnection {
|
|
|
45
46
|
this.simulator.send(this, data)
|
|
46
47
|
|
|
47
48
|
} else {
|
|
48
|
-
logger.error(
|
|
49
|
+
logger.error(keyFromPeerDescriptor(this.ownPeerDescriptor) + ', ' + keyFromPeerDescriptor(this.targetPeerDescriptor) +
|
|
49
50
|
'tried to send() on a stopped connection')
|
|
50
51
|
}
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
public async close(disconnectionType: DisconnectionType): Promise<void> {
|
|
54
|
-
logger.trace(
|
|
55
|
+
logger.trace(keyFromPeerDescriptor(this.ownPeerDescriptor) + ', ' + keyFromPeerDescriptor(this.targetPeerDescriptor) + ' close()')
|
|
55
56
|
|
|
56
57
|
if (!this.stopped) {
|
|
57
|
-
logger.trace(
|
|
58
|
+
logger.trace(keyFromPeerDescriptor(this.ownPeerDescriptor) + ', '
|
|
59
|
+
+ keyFromPeerDescriptor(this.targetPeerDescriptor) + ' close() not stopped')
|
|
58
60
|
this.stopped = true
|
|
59
61
|
|
|
60
62
|
try {
|
|
61
|
-
logger.trace(
|
|
63
|
+
logger.trace(keyFromPeerDescriptor(this.ownPeerDescriptor) + ', ' + keyFromPeerDescriptor(this.targetPeerDescriptor) +
|
|
62
64
|
' close() calling simulator.disconnect()')
|
|
63
65
|
this.simulator.close(this)
|
|
64
|
-
logger.trace(
|
|
66
|
+
logger.trace(keyFromPeerDescriptor(this.ownPeerDescriptor) + ', ' + keyFromPeerDescriptor(this.targetPeerDescriptor) +
|
|
65
67
|
' close() simulator.disconnect returned')
|
|
66
68
|
} catch (e) {
|
|
67
|
-
logger.trace(
|
|
69
|
+
logger.trace(keyFromPeerDescriptor(this.ownPeerDescriptor) + ', ' + keyFromPeerDescriptor(this.targetPeerDescriptor) +
|
|
68
70
|
'close aborted' + e)
|
|
69
71
|
} finally {
|
|
70
|
-
logger.trace(
|
|
72
|
+
logger.trace(keyFromPeerDescriptor(this.ownPeerDescriptor) + ', ' + keyFromPeerDescriptor(this.targetPeerDescriptor) +
|
|
71
73
|
' calling this.doDisconnect')
|
|
72
74
|
this.doDisconnect(disconnectionType)
|
|
73
75
|
}
|
|
74
76
|
|
|
75
77
|
} else {
|
|
76
|
-
logger.trace(
|
|
78
|
+
logger.trace(keyFromPeerDescriptor(this.ownPeerDescriptor) + ', ' + keyFromPeerDescriptor(this.targetPeerDescriptor) +
|
|
77
79
|
' close() tried to close a stopped connection')
|
|
78
80
|
}
|
|
79
81
|
}
|
|
@@ -107,7 +109,7 @@ export class SimulatorConnection extends Connection implements IConnection {
|
|
|
107
109
|
|
|
108
110
|
public handleIncomingDisconnection(): void {
|
|
109
111
|
if (!this.stopped) {
|
|
110
|
-
logger.trace(this.ownPeerDescriptor
|
|
112
|
+
logger.trace(keyFromPeerDescriptor(this.ownPeerDescriptor) + ' handleIncomingDisconnection()')
|
|
111
113
|
this.stopped = true
|
|
112
114
|
this.doDisconnect('OTHER')
|
|
113
115
|
} else {
|
|
@@ -117,19 +119,20 @@ export class SimulatorConnection extends Connection implements IConnection {
|
|
|
117
119
|
|
|
118
120
|
public destroy(): void {
|
|
119
121
|
if (!this.stopped) {
|
|
120
|
-
logger.trace(this.ownPeerDescriptor
|
|
122
|
+
logger.trace(keyFromPeerDescriptor(this.ownPeerDescriptor) + ' destroy()')
|
|
121
123
|
this.removeAllListeners()
|
|
122
124
|
this.close('OTHER').catch((_e) => { })
|
|
123
125
|
} else {
|
|
124
|
-
logger.trace(this.ownPeerDescriptor
|
|
126
|
+
logger.trace(keyFromPeerDescriptor(this.ownPeerDescriptor) + ' tried to call destroy() a stopped connection')
|
|
125
127
|
}
|
|
126
128
|
}
|
|
127
129
|
|
|
128
130
|
private doDisconnect(disconnectionType: DisconnectionType) {
|
|
129
|
-
logger.trace(this.ownPeerDescriptor
|
|
131
|
+
logger.trace(keyFromPeerDescriptor(this.ownPeerDescriptor) + ' doDisconnect()')
|
|
130
132
|
this.stopped = true
|
|
131
133
|
|
|
132
|
-
logger.trace(
|
|
134
|
+
logger.trace(keyFromPeerDescriptor(this.ownPeerDescriptor) + ', '
|
|
135
|
+
+ keyFromPeerDescriptor(this.targetPeerDescriptor) + ' doDisconnect emitting')
|
|
133
136
|
|
|
134
137
|
this.emit('disconnected', disconnectionType)
|
|
135
138
|
|
|
@@ -19,22 +19,22 @@ export class SimulatorConnector {
|
|
|
19
19
|
private protocolVersion: string
|
|
20
20
|
private ownPeerDescriptor: PeerDescriptor
|
|
21
21
|
private simulator: Simulator
|
|
22
|
-
private
|
|
22
|
+
private onIncomingConnection: (connection: ManagedConnection) => boolean
|
|
23
23
|
|
|
24
24
|
constructor(
|
|
25
25
|
protocolVersion: string,
|
|
26
26
|
ownPeerDescriptor: PeerDescriptor,
|
|
27
27
|
simulator: Simulator,
|
|
28
|
-
|
|
28
|
+
onIncomingConnection: (connection: ManagedConnection) => boolean
|
|
29
29
|
) {
|
|
30
30
|
this.protocolVersion = protocolVersion
|
|
31
31
|
this.ownPeerDescriptor = ownPeerDescriptor
|
|
32
32
|
this.simulator = simulator
|
|
33
|
-
this.
|
|
33
|
+
this.onIncomingConnection = onIncomingConnection
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
public connect(targetPeerDescriptor: PeerDescriptor): ManagedConnection {
|
|
37
|
-
logger.trace('connect() ' +
|
|
37
|
+
logger.trace('connect() ' + keyFromPeerDescriptor(targetPeerDescriptor))
|
|
38
38
|
const peerKey = keyFromPeerDescriptor(targetPeerDescriptor)
|
|
39
39
|
const existingConnection = this.connectingConnections.get(peerKey)
|
|
40
40
|
if (existingConnection) {
|
|
@@ -65,8 +65,7 @@ export class SimulatorConnector {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
public handleIncomingConnection(sourceConnection: SimulatorConnection): void {
|
|
68
|
-
logger.trace(
|
|
69
|
-
' incoming connection, stopped: ' + this.stopped)
|
|
68
|
+
logger.trace(keyFromPeerDescriptor(sourceConnection.ownPeerDescriptor) + ' incoming connection, stopped: ' + this.stopped)
|
|
70
69
|
if (this.stopped) {
|
|
71
70
|
return
|
|
72
71
|
}
|
|
@@ -76,14 +75,14 @@ export class SimulatorConnector {
|
|
|
76
75
|
const managedConnection = new ManagedConnection(this.ownPeerDescriptor, this.protocolVersion,
|
|
77
76
|
ConnectionType.SIMULATOR_SERVER, undefined, connection)
|
|
78
77
|
|
|
79
|
-
logger.trace('connected
|
|
78
|
+
logger.trace('connected')
|
|
80
79
|
|
|
81
|
-
managedConnection.once('handshakeRequest', (
|
|
82
|
-
logger.trace(
|
|
83
|
-
logger.trace('incoming handshake request
|
|
80
|
+
managedConnection.once('handshakeRequest', () => {
|
|
81
|
+
logger.trace(keyFromPeerDescriptor(sourceConnection.ownPeerDescriptor) + ' incoming handshake request')
|
|
82
|
+
logger.trace('incoming handshake request')
|
|
84
83
|
|
|
85
|
-
if (this.
|
|
86
|
-
logger.trace(
|
|
84
|
+
if (this.onIncomingConnection(managedConnection)) {
|
|
85
|
+
logger.trace(keyFromPeerDescriptor(sourceConnection.ownPeerDescriptor) + ' calling acceptHandshake')
|
|
87
86
|
managedConnection.acceptHandshake()
|
|
88
87
|
} else {
|
|
89
88
|
managedConnection.rejectHandshake('Duplicate connection')
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import { MetricsContext } from '@streamr/utils'
|
|
1
2
|
import { PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
2
3
|
import { ConnectionManager } from '../ConnectionManager'
|
|
3
4
|
import { Simulator } from './Simulator'
|
|
5
|
+
import { SimulatorConnectorFacade } from '../ConnectorFacade'
|
|
4
6
|
|
|
5
7
|
export class SimulatorTransport extends ConnectionManager {
|
|
6
8
|
constructor(ownPeerDescriptor: PeerDescriptor, simulator: Simulator) {
|
|
7
|
-
super({
|
|
9
|
+
super({
|
|
10
|
+
createConnectorFacade: () => new SimulatorConnectorFacade(ownPeerDescriptor, simulator),
|
|
11
|
+
metricsContext: new MetricsContext()
|
|
12
|
+
})
|
|
8
13
|
}
|
|
9
14
|
}
|