@streamr/dht 102.0.0-beta.1 → 102.0.0-beta.2
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/generated/google/protobuf/any.d.ts +180 -0
- package/dist/generated/google/protobuf/any.js +155 -0
- package/dist/generated/google/protobuf/any.js.map +1 -0
- package/dist/generated/google/protobuf/empty.d.ts +31 -0
- package/dist/generated/google/protobuf/empty.js +32 -0
- package/dist/generated/google/protobuf/empty.js.map +1 -0
- package/dist/generated/google/protobuf/timestamp.d.ts +155 -0
- package/dist/generated/google/protobuf/timestamp.js +136 -0
- package/dist/generated/google/protobuf/timestamp.js.map +1 -0
- package/dist/generated/packages/dht/protos/DhtRpc.client.d.ts +361 -0
- package/dist/generated/packages/dht/protos/DhtRpc.client.js +285 -0
- package/dist/generated/packages/dht/protos/DhtRpc.client.js.map +1 -0
- package/dist/generated/packages/dht/protos/DhtRpc.d.ts +999 -0
- package/dist/generated/packages/dht/protos/DhtRpc.js +677 -0
- package/dist/generated/packages/dht/protos/DhtRpc.js.map +1 -0
- package/dist/generated/packages/dht/protos/DhtRpc.server.d.ts +162 -0
- package/dist/generated/packages/dht/protos/DhtRpc.server.js +3 -0
- package/dist/generated/packages/dht/protos/DhtRpc.server.js.map +1 -0
- package/dist/generated/packages/proto-rpc/protos/ProtoRpc.d.ts +87 -0
- package/dist/generated/packages/proto-rpc/protos/ProtoRpc.js +66 -0
- package/dist/generated/packages/proto-rpc/protos/ProtoRpc.js.map +1 -0
- package/dist/package.json +7 -7
- package/package.json +7 -7
- package/src/connection/Connection.ts +0 -28
- package/src/connection/ConnectionLockRpcLocal.ts +0 -78
- package/src/connection/ConnectionLockRpcRemote.ts +0 -64
- package/src/connection/ConnectionLockStates.ts +0 -131
- package/src/connection/ConnectionManager.ts +0 -661
- package/src/connection/ConnectionsView.ts +0 -8
- package/src/connection/ConnectorFacade.ts +0 -217
- package/src/connection/Handshaker.ts +0 -209
- package/src/connection/IConnection.ts +0 -40
- package/src/connection/ManagedConnection.ts +0 -113
- package/src/connection/OutputBuffer.ts +0 -28
- package/src/connection/PendingConnection.ts +0 -68
- package/src/connection/connectivityChecker.ts +0 -108
- package/src/connection/connectivityRequestHandler.ts +0 -116
- package/src/connection/simulator/Simulator.ts +0 -369
- package/src/connection/simulator/SimulatorConnection.ts +0 -137
- package/src/connection/simulator/SimulatorConnector.ts +0 -98
- package/src/connection/simulator/SimulatorTransport.ts +0 -15
- package/src/connection/simulator/pings.ts +0 -42
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -242
- package/src/connection/webrtc/IWebrtcConnection.ts +0 -24
- package/src/connection/webrtc/NodeWebrtcConnection.ts +0 -245
- package/src/connection/webrtc/WebrtcConnector.ts +0 -234
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +0 -108
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +0 -60
- package/src/connection/webrtc/iceServerAsString.ts +0 -15
- package/src/connection/websocket/AbstractWebsocketClientConnection.ts +0 -122
- package/src/connection/websocket/AutoCertifierClientFacade.ts +0 -89
- package/src/connection/websocket/BrowserWebsocketClientConnection.ts +0 -44
- package/src/connection/websocket/NodeWebsocketClientConnection.ts +0 -39
- package/src/connection/websocket/WebsocketClientConnector.ts +0 -119
- package/src/connection/websocket/WebsocketClientConnectorRpcLocal.ts +0 -38
- package/src/connection/websocket/WebsocketClientConnectorRpcRemote.ts +0 -19
- package/src/connection/websocket/WebsocketServer.ts +0 -164
- package/src/connection/websocket/WebsocketServerConnection.ts +0 -109
- package/src/connection/websocket/WebsocketServerConnector.ts +0 -290
- package/src/dht/DhtNode.ts +0 -683
- package/src/dht/DhtNodeRpcLocal.ts +0 -84
- package/src/dht/DhtNodeRpcRemote.ts +0 -107
- package/src/dht/ExternalApiRpcLocal.ts +0 -58
- package/src/dht/ExternalApiRpcRemote.ts +0 -41
- package/src/dht/PeerManager.ts +0 -305
- package/src/dht/contact/Contact.ts +0 -19
- package/src/dht/contact/ContactList.ts +0 -43
- package/src/dht/contact/RandomContactList.ts +0 -56
- package/src/dht/contact/RingContactList.ts +0 -143
- package/src/dht/contact/RpcRemote.ts +0 -72
- package/src/dht/contact/SortedContactList.ts +0 -173
- package/src/dht/contact/getClosestNodes.ts +0 -24
- package/src/dht/contact/ringIdentifiers.ts +0 -62
- package/src/dht/discovery/DiscoverySession.ts +0 -129
- package/src/dht/discovery/PeerDiscovery.ts +0 -244
- package/src/dht/discovery/RingDiscoverySession.ts +0 -148
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +0 -251
- package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +0 -34
- package/src/dht/recursive-operation/RecursiveOperationRpcRemote.ts +0 -43
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +0 -231
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +0 -35
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcRemote.ts +0 -30
- package/src/dht/routing/DuplicateDetector.ts +0 -34
- package/src/dht/routing/Router.ts +0 -246
- package/src/dht/routing/RouterRpcLocal.ts +0 -78
- package/src/dht/routing/RouterRpcRemote.ts +0 -80
- package/src/dht/routing/RoutingSession.ts +0 -243
- package/src/dht/routing/RoutingTablesCache.ts +0 -60
- package/src/dht/routing/getPreviousPeer.ts +0 -6
- package/src/dht/store/LocalDataStore.ts +0 -84
- package/src/dht/store/StoreManager.ts +0 -170
- package/src/dht/store/StoreRpcLocal.ts +0 -89
- package/src/dht/store/StoreRpcRemote.ts +0 -32
- package/src/exports.ts +0 -33
- package/src/helpers/AddressTools.ts +0 -28
- package/src/helpers/Connectivity.ts +0 -19
- package/src/helpers/browser/isBrowserEnvironment.ts +0 -1
- package/src/helpers/browser/isBrowserEnvironment_override.ts +0 -3
- package/src/helpers/createPeerDescriptor.ts +0 -57
- package/src/helpers/createPeerDescriptorSignaturePayload.ts +0 -28
- package/src/helpers/debugHelpers.ts +0 -9
- package/src/helpers/errors.ts +0 -49
- package/src/helpers/offering.ts +0 -15
- package/src/helpers/protoClasses.ts +0 -57
- package/src/helpers/protoToString.ts +0 -21
- package/src/helpers/version.ts +0 -32
- package/src/identifiers.ts +0 -29
- package/src/rpc-protocol/DhtCallContext.ts +0 -14
- package/src/rpc-protocol/DhtRpcOptions.ts +0 -10
- package/src/transport/ITransport.ts +0 -37
- package/src/transport/ListeningRpcCommunicator.ts +0 -32
- package/src/transport/RoutingRpcCommunicator.ts +0 -66
- package/src/types/ServiceID.ts +0 -1
- package/src/types/textencoding.d.ts +0 -6
- package/test/benchmark/Find.test.ts +0 -72
- package/test/benchmark/KademliaCorrectness.test.ts +0 -114
- package/test/benchmark/RingCorrectness.test.ts +0 -157
- package/test/benchmark/SortedContactListBenchmark.test.ts +0 -108
- package/test/benchmark/WebsocketServerMemoryLeak.test.ts +0 -41
- package/test/benchmark/hybrid-network-simulation/RingContactList.test.ts +0 -71
- package/test/end-to-end/GeoIpLayer0.test.ts +0 -55
- package/test/end-to-end/Layer0-Layer1.test.ts +0 -93
- package/test/end-to-end/Layer0.test.ts +0 -76
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +0 -110
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +0 -137
- package/test/end-to-end/Layer0Webrtc.test.ts +0 -85
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +0 -82
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +0 -76
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +0 -52
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +0 -69
- package/test/end-to-end/memory-leak.test.ts +0 -80
- package/test/integration/ConnectionLocking.test.ts +0 -192
- package/test/integration/ConnectionManager.test.ts +0 -528
- package/test/integration/ConnectivityChecking.test.ts +0 -53
- package/test/integration/DhtJoinPeerDiscovery.test.ts +0 -49
- package/test/integration/DhtNode.test.ts +0 -66
- package/test/integration/DhtNodeExternalAPI.test.ts +0 -48
- package/test/integration/DhtNodeRpcRemote.test.ts +0 -66
- package/test/integration/DhtRpc.test.ts +0 -121
- package/test/integration/Find.test.ts +0 -45
- package/test/integration/GeoIpConnectivityChecking.test.ts +0 -72
- package/test/integration/Layer1-scale.test.ts +0 -189
- package/test/integration/Mock-Layer1-Layer0.test.ts +0 -85
- package/test/integration/MultipleEntryPointJoining.test.ts +0 -105
- package/test/integration/ReplicateData.test.ts +0 -104
- package/test/integration/RouteMessage.test.ts +0 -230
- package/test/integration/RouterRpcRemote.test.ts +0 -77
- package/test/integration/SimultaneousConnections.test.ts +0 -316
- package/test/integration/Store.test.ts +0 -85
- package/test/integration/StoreAndDelete.test.ts +0 -77
- package/test/integration/StoreOnDhtWithThreeNodes.test.ts +0 -59
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +0 -51
- package/test/integration/StoreRpcRemote.test.ts +0 -54
- package/test/integration/WebrtcConnectionManagement.test.ts +0 -191
- package/test/integration/WebrtcConnectorRpc.test.ts +0 -125
- package/test/integration/Websocket.test.ts +0 -65
- package/test/integration/WebsocketClientConnectorRpc.test.ts +0 -69
- package/test/integration/WebsocketConnectionManagement.test.ts +0 -191
- package/test/integration/rpc-connections-over-webrtc.test.ts +0 -123
- package/test/kademlia-simulation/data/nodeids.json +0 -13002
- package/test/kademlia-simulation/data/orderedneighbors.json +0 -1001
- package/test/types/global.d.ts +0 -1
- package/test/unit/AddressTools.test.ts +0 -44
- package/test/unit/AutoCertifierClientFacade.test.ts +0 -58
- package/test/unit/ConnectionManager.test.ts +0 -65
- package/test/unit/ConnectivityHelpers.test.ts +0 -61
- package/test/unit/DiscoverySession.test.ts +0 -87
- package/test/unit/DuplicateDetector.test.ts +0 -31
- package/test/unit/Handshaker.test.ts +0 -169
- package/test/unit/ListeningRpcCommunicator.test.ts +0 -52
- package/test/unit/LocalDataStore.test.ts +0 -108
- package/test/unit/ManagedConnection.test.ts +0 -58
- package/test/unit/PeerManager.test.ts +0 -93
- package/test/unit/PendingConnection.test.ts +0 -57
- package/test/unit/ProtobufMessage.test.ts +0 -21
- package/test/unit/RandomContactList.test.ts +0 -58
- package/test/unit/RecursiveOperationManager.test.ts +0 -161
- package/test/unit/RecursiveOperationSession.test.ts +0 -68
- package/test/unit/Router.test.ts +0 -137
- package/test/unit/RoutingSession.test.ts +0 -86
- package/test/unit/SortedContactList.test.ts +0 -115
- package/test/unit/StoreManager.test.ts +0 -146
- package/test/unit/StoreRpcLocal.test.ts +0 -167
- package/test/unit/WebrtcConnection.test.ts +0 -29
- package/test/unit/WebrtcConnector.test.ts +0 -56
- package/test/unit/WebsocketClientConnector.test.ts +0 -101
- package/test/unit/WebsocketServer.test.ts +0 -66
- package/test/unit/WebsocketServerConnector.test.ts +0 -102
- package/test/unit/connectivityRequestHandler.test.ts +0 -104
- package/test/unit/createPeerDescriptor.test.ts +0 -69
- package/test/unit/customMatchers.test.ts +0 -34
- package/test/unit/getClosestNodes.test.ts +0 -30
- package/test/unit/version.test.ts +0 -18
- package/test/unit/webrtcReplaceInternalIpWithExternalIp.test.ts +0 -18
- package/test/utils/FakeConnectorFacade.ts +0 -41
- package/test/utils/FakeRpcCommunicator.ts +0 -23
- package/test/utils/FakeTransport.ts +0 -79
- package/test/utils/customMatchers.ts +0 -71
- package/test/utils/mock/MockConnection.ts +0 -26
- package/test/utils/mock/MockConnectionsView.ts +0 -18
- package/test/utils/mock/MockRouter.ts +0 -62
- package/test/utils/mock/MockRpcCommunicator.ts +0 -7
- package/test/utils/mock/MockTransport.ts +0 -26
- package/test/utils/mock/mockDataEntry.ts +0 -38
- package/test/utils/topology.ts +0 -79
- package/test/utils/utils.ts +0 -268
- package/tsconfig.browser.json +0 -17
- package/tsconfig.jest.json +0 -25
- package/tsconfig.json +0 -3
- package/tsconfig.node.json +0 -24
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { ConnectionType, IConnection } from '../IConnection'
|
|
2
|
-
import { Simulator } from './Simulator'
|
|
3
|
-
import { Message, PeerDescriptor } from '../../../generated/packages/dht/protos/DhtRpc'
|
|
4
|
-
import { Connection } from '../Connection'
|
|
5
|
-
import { Logger } from '@streamr/utils'
|
|
6
|
-
import { protoToString } from '../../helpers/protoToString'
|
|
7
|
-
import { toNodeId } from '../../identifiers'
|
|
8
|
-
|
|
9
|
-
const logger = new Logger(module)
|
|
10
|
-
|
|
11
|
-
export class SimulatorConnection extends Connection implements IConnection {
|
|
12
|
-
|
|
13
|
-
private stopped = false
|
|
14
|
-
public localPeerDescriptor: PeerDescriptor
|
|
15
|
-
private targetPeerDescriptor: PeerDescriptor
|
|
16
|
-
private simulator: Simulator
|
|
17
|
-
|
|
18
|
-
constructor(
|
|
19
|
-
localPeerDescriptor: PeerDescriptor,
|
|
20
|
-
targetPeerDescriptor: PeerDescriptor,
|
|
21
|
-
connectionType: ConnectionType,
|
|
22
|
-
simulator: Simulator
|
|
23
|
-
) {
|
|
24
|
-
super(connectionType)
|
|
25
|
-
|
|
26
|
-
this.localPeerDescriptor = localPeerDescriptor
|
|
27
|
-
this.setPeerDescriptor(targetPeerDescriptor)
|
|
28
|
-
this.targetPeerDescriptor = targetPeerDescriptor
|
|
29
|
-
this.connectionType = connectionType
|
|
30
|
-
this.simulator = simulator
|
|
31
|
-
|
|
32
|
-
this.send = this.send.bind(this)
|
|
33
|
-
this.close = this.close.bind(this)
|
|
34
|
-
this.connect = this.connect.bind(this)
|
|
35
|
-
this.handleIncomingData = this.handleIncomingData.bind(this)
|
|
36
|
-
this.handleIncomingDisconnection = this.handleIncomingDisconnection.bind(this)
|
|
37
|
-
this.destroy = this.destroy.bind(this)
|
|
38
|
-
this.doDisconnect = this.doDisconnect.bind(this)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public send(data: Uint8Array): void {
|
|
42
|
-
logger.trace('send()')
|
|
43
|
-
if (!this.stopped) {
|
|
44
|
-
|
|
45
|
-
this.simulator.send(this, data)
|
|
46
|
-
|
|
47
|
-
} else {
|
|
48
|
-
const localNodeId = toNodeId(this.localPeerDescriptor)
|
|
49
|
-
const targetNodeId = toNodeId(this.targetPeerDescriptor)
|
|
50
|
-
logger.error(localNodeId + ', ' + targetNodeId + 'tried to send() on a stopped connection')
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public async close(gracefulLeave: boolean): Promise<void> {
|
|
55
|
-
const localNodeId = toNodeId(this.localPeerDescriptor)
|
|
56
|
-
const targetNodeId = toNodeId(this.targetPeerDescriptor)
|
|
57
|
-
|
|
58
|
-
logger.trace(localNodeId + ', ' + targetNodeId + ' close()')
|
|
59
|
-
if (!this.stopped) {
|
|
60
|
-
logger.trace(localNodeId + ', ' + targetNodeId + ' close() not stopped')
|
|
61
|
-
this.stopped = true
|
|
62
|
-
|
|
63
|
-
try {
|
|
64
|
-
logger.trace(localNodeId + ', ' + targetNodeId + ' close() calling simulator.disconnect()')
|
|
65
|
-
this.simulator.close(this)
|
|
66
|
-
logger.trace(localNodeId + ', ' + targetNodeId + ' close() simulator.disconnect returned')
|
|
67
|
-
} catch (e) {
|
|
68
|
-
logger.trace(localNodeId + ', ' + targetNodeId + 'close aborted' + e)
|
|
69
|
-
} finally {
|
|
70
|
-
logger.trace(localNodeId + ', ' + targetNodeId + ' calling this.doDisconnect')
|
|
71
|
-
this.doDisconnect(gracefulLeave)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
} else {
|
|
75
|
-
logger.trace(localNodeId + ', ' + targetNodeId + ' close() tried to close a stopped connection')
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public connect(): void {
|
|
80
|
-
if (!this.stopped) {
|
|
81
|
-
logger.trace('connect() called')
|
|
82
|
-
|
|
83
|
-
this.simulator.connect(this, this.targetPeerDescriptor, (error?: string) => {
|
|
84
|
-
if (error !== undefined) {
|
|
85
|
-
logger.trace(error)
|
|
86
|
-
this.doDisconnect(false)
|
|
87
|
-
} else {
|
|
88
|
-
this.emit('connected')
|
|
89
|
-
}
|
|
90
|
-
})
|
|
91
|
-
} else {
|
|
92
|
-
logger.trace('tried to connect() a stopped connection')
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
public handleIncomingData(data: Uint8Array): void {
|
|
97
|
-
if (!this.stopped) {
|
|
98
|
-
logger.trace('handleIncomingData() ' + protoToString(Message.fromBinary(data), Message))
|
|
99
|
-
this.emit('data', data)
|
|
100
|
-
} else {
|
|
101
|
-
logger.trace('tried to call handleIncomingData() a stopped connection')
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public handleIncomingDisconnection(): void {
|
|
106
|
-
if (!this.stopped) {
|
|
107
|
-
const localNodeId = toNodeId(this.localPeerDescriptor)
|
|
108
|
-
logger.trace(localNodeId + ' handleIncomingDisconnection()')
|
|
109
|
-
this.stopped = true
|
|
110
|
-
this.doDisconnect(false)
|
|
111
|
-
} else {
|
|
112
|
-
logger.trace('tried to call handleIncomingDisconnection() a stopped connection')
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
public destroy(): void {
|
|
117
|
-
const localNodeId = toNodeId(this.localPeerDescriptor)
|
|
118
|
-
if (!this.stopped) {
|
|
119
|
-
logger.trace(localNodeId + ' destroy()')
|
|
120
|
-
this.removeAllListeners()
|
|
121
|
-
this.close(false).catch((_e) => { })
|
|
122
|
-
} else {
|
|
123
|
-
logger.trace(localNodeId + ' tried to call destroy() a stopped connection')
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
private doDisconnect(gracefulLeave: boolean) {
|
|
128
|
-
const localNodeId = toNodeId(this.localPeerDescriptor)
|
|
129
|
-
const targetNodeId = toNodeId(this.targetPeerDescriptor)
|
|
130
|
-
logger.trace(localNodeId + ' doDisconnect()')
|
|
131
|
-
this.stopped = true
|
|
132
|
-
|
|
133
|
-
logger.trace(localNodeId + ', ' + targetNodeId + ' doDisconnect emitting')
|
|
134
|
-
|
|
135
|
-
this.emit('disconnected', gracefulLeave)
|
|
136
|
-
}
|
|
137
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { ConnectionType } from '../IConnection'
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
HandshakeError,
|
|
5
|
-
PeerDescriptor,
|
|
6
|
-
} from '../../../generated/packages/dht/protos/DhtRpc'
|
|
7
|
-
import { Logger } from '@streamr/utils'
|
|
8
|
-
import { Simulator } from './Simulator'
|
|
9
|
-
import { SimulatorConnection } from './SimulatorConnection'
|
|
10
|
-
import { DhtAddress, toNodeId } from '../../identifiers'
|
|
11
|
-
import { acceptHandshake, createIncomingHandshaker, createOutgoingHandshaker, rejectHandshake } from '../Handshaker'
|
|
12
|
-
import { PendingConnection } from '../PendingConnection'
|
|
13
|
-
|
|
14
|
-
const logger = new Logger(module)
|
|
15
|
-
|
|
16
|
-
export class SimulatorConnector {
|
|
17
|
-
|
|
18
|
-
private connectingConnections: Map<DhtAddress, PendingConnection> = new Map()
|
|
19
|
-
private stopped = false
|
|
20
|
-
private localPeerDescriptor: PeerDescriptor
|
|
21
|
-
private simulator: Simulator
|
|
22
|
-
private onNewConnection: (connection: PendingConnection) => boolean
|
|
23
|
-
|
|
24
|
-
constructor(
|
|
25
|
-
localPeerDescriptor: PeerDescriptor,
|
|
26
|
-
simulator: Simulator,
|
|
27
|
-
onNewConnection: (connection: PendingConnection) => boolean,
|
|
28
|
-
) {
|
|
29
|
-
this.localPeerDescriptor = localPeerDescriptor
|
|
30
|
-
this.simulator = simulator
|
|
31
|
-
this.onNewConnection = onNewConnection
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public connect(targetPeerDescriptor: PeerDescriptor): PendingConnection {
|
|
35
|
-
logger.trace('connect() ' + toNodeId(targetPeerDescriptor))
|
|
36
|
-
const nodeId = toNodeId(targetPeerDescriptor)
|
|
37
|
-
const existingConnection = this.connectingConnections.get(nodeId)
|
|
38
|
-
if (existingConnection) {
|
|
39
|
-
return existingConnection
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const connection = new SimulatorConnection(this.localPeerDescriptor, targetPeerDescriptor, ConnectionType.SIMULATOR_CLIENT, this.simulator)
|
|
43
|
-
|
|
44
|
-
const pendingConnection = new PendingConnection(targetPeerDescriptor)
|
|
45
|
-
createOutgoingHandshaker(this.localPeerDescriptor, pendingConnection, connection, targetPeerDescriptor)
|
|
46
|
-
this.connectingConnections.set(nodeId, pendingConnection)
|
|
47
|
-
const delFunc = () => {
|
|
48
|
-
this.connectingConnections.delete(nodeId)
|
|
49
|
-
connection.off('disconnected', delFunc)
|
|
50
|
-
pendingConnection.off('connected', delFunc)
|
|
51
|
-
pendingConnection.off('disconnected', delFunc)
|
|
52
|
-
}
|
|
53
|
-
connection.once('disconnected', delFunc)
|
|
54
|
-
pendingConnection.once('connected', delFunc)
|
|
55
|
-
pendingConnection.once('disconnected', delFunc)
|
|
56
|
-
|
|
57
|
-
connection.connect()
|
|
58
|
-
return pendingConnection
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
public getPeerDescriptor(): PeerDescriptor {
|
|
62
|
-
return this.localPeerDescriptor
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public handleIncomingConnection(sourceConnection: SimulatorConnection): void {
|
|
66
|
-
// connection is incoming, so remotePeerDescriptor is localPeerDescriptor
|
|
67
|
-
const remotePeerDescriptor = sourceConnection.localPeerDescriptor
|
|
68
|
-
const remoteNodeId = toNodeId(sourceConnection.localPeerDescriptor)
|
|
69
|
-
logger.trace(remoteNodeId + ' incoming connection, stopped: ' + this.stopped)
|
|
70
|
-
if (this.stopped) {
|
|
71
|
-
return
|
|
72
|
-
}
|
|
73
|
-
const connection = new SimulatorConnection(this.localPeerDescriptor, remotePeerDescriptor, ConnectionType.SIMULATOR_SERVER, this.simulator)
|
|
74
|
-
|
|
75
|
-
const pendingConnection = new PendingConnection(remotePeerDescriptor)
|
|
76
|
-
const handshaker = createIncomingHandshaker(this.localPeerDescriptor, pendingConnection, connection)
|
|
77
|
-
logger.trace('connected')
|
|
78
|
-
|
|
79
|
-
handshaker.once('handshakeRequest', () => {
|
|
80
|
-
logger.trace(remoteNodeId + ' incoming handshake request')
|
|
81
|
-
|
|
82
|
-
if (this.onNewConnection(pendingConnection)) {
|
|
83
|
-
logger.trace(remoteNodeId + ' calling acceptHandshake')
|
|
84
|
-
acceptHandshake(handshaker, pendingConnection, connection)
|
|
85
|
-
} else {
|
|
86
|
-
rejectHandshake(pendingConnection, connection, handshaker, HandshakeError.DUPLICATE_CONNECTION)
|
|
87
|
-
}
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
this.simulator.accept(sourceConnection, connection)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
public async stop(): Promise<void> {
|
|
94
|
-
this.stopped = true
|
|
95
|
-
const conns = Array.from(this.connectingConnections.values())
|
|
96
|
-
await Promise.allSettled(conns.map(async (conn) => conn.close(false)))
|
|
97
|
-
}
|
|
98
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { MetricsContext } from '@streamr/utils'
|
|
2
|
-
import { PeerDescriptor } from '../../../generated/packages/dht/protos/DhtRpc'
|
|
3
|
-
import { ConnectionManager } from '../ConnectionManager'
|
|
4
|
-
import { Simulator } from './Simulator'
|
|
5
|
-
import { SimulatorConnectorFacade } from '../ConnectorFacade'
|
|
6
|
-
|
|
7
|
-
export class SimulatorTransport extends ConnectionManager {
|
|
8
|
-
constructor(localPeerDescriptor: PeerDescriptor, simulator: Simulator) {
|
|
9
|
-
super({
|
|
10
|
-
createConnectorFacade: () => new SimulatorConnectorFacade(localPeerDescriptor, simulator),
|
|
11
|
-
metricsContext: new MetricsContext(),
|
|
12
|
-
allowIncomingPrivateConnections: false
|
|
13
|
-
})
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/* eslint-disable max-len, @typescript-eslint/prefer-for-of */
|
|
2
|
-
|
|
3
|
-
// This file contains a 16x16 matrix of ping times measured between
|
|
4
|
-
// amazon regions. Converted to typescript from the file pings.csv
|
|
5
|
-
// in order to make loading into browser easier.
|
|
6
|
-
|
|
7
|
-
export enum RegionIndex { eu_central_1, eu_west_1, eu_west_2, eu_west_3, eu_north_1, us_east_1, us_east_2, us_west_1, us_west_2, ca_central_1, ap_south_1, ap_northeast_2, ap_southeast_1, ap_southeast_2, ap_northeast_1, sa_east_1 }
|
|
8
|
-
|
|
9
|
-
export const regionPingMatrix = [
|
|
10
|
-
[0.325, 23.163, 12.942, 8.923, 20.699, 89.208, 99.98, 150.077, 139.224, 90.454, 117.0, 226.823, 154.592, 286.753, 222.719, 205.563],
|
|
11
|
-
[24.911, 0.195, 10.022, 18.544, 39.802, 66.161, 93.877, 136.271, 127.362, 73.733, 122.187, 229.064, 177.256, 254.277, 200.207, 186.817],
|
|
12
|
-
[12.879, 9.994, 0.167, 8.626, 31.896, 74.663, 85.231, 136.185, 125.328, 77.811, 111.29, 236.34, 164.357, 262.577, 207.745, 187.383],
|
|
13
|
-
[9.007, 16.823, 8.761, 0.191, 28.449, 81.969, 92.599, 140.961, 132.269, 85.444, 107.421, 243.981, 167.17, 279.322, 214.088, 197.986],
|
|
14
|
-
[20.661, 40.0, 31.176, 27.645, 0.137, 104.171, 115.704, 165.01, 155.741, 107.635, 136.75, 267.321, 173.951, 292.266, 238.492, 216.048],
|
|
15
|
-
[89.076, 67.701, 75.414, 81.598, 104.866, 0.284, 11.501, 61.662, 78.612, 14.115, 186.697, 174.546, 210.93, 196.071, 145.622, 114.919],
|
|
16
|
-
[100.018, 93.968, 85.06, 92.582, 115.719, 11.514, 0.184, 51.668, 48.447, 25.808, 195.433, 160.031, 197.27, 186.913, 133.115, 127.954],
|
|
17
|
-
[148.544, 137.18, 136.137, 141.333, 164.73, 60.257, 50.886, 0.197, 21.509, 77.668, 229.32, 134.858, 174.472, 137.329, 107.566, 175.944],
|
|
18
|
-
[137.885, 128.304, 125.316, 132.271, 155.726, 63.376, 48.457, 20.743, 0.293, 59.645, 218.285, 126.988, 162.72, 138.599, 100.203, 174.612],
|
|
19
|
-
[90.494, 78.203, 77.998, 84.348, 109.549, 14.136, 25.917, 77.323, 59.692, 0.163, 187.854, 169.253, 206.944, 195.467, 141.054, 126.962],
|
|
20
|
-
[120.19, 119.978, 112.023, 107.434, 139.361, 185.043, 195.679, 233.008, 216.025, 186.888, 0.177, 130.21, 56.019, 145.643, 128.57, 298.98],
|
|
21
|
-
[226.296, 229.187, 237.024, 243.839, 266.919, 174.727, 160.066, 136.623, 124.799, 169.854, 129.131, 0.189, 81.065, 139.967, 33.24, 284.578],
|
|
22
|
-
[154.691, 178.279, 170.997, 154.261, 173.776, 211.511, 197.551, 173.735, 162.834, 206.659, 56.964, 68.853, 0.215, 91.601, 69.684, 323.146],
|
|
23
|
-
[289.784, 253.234, 261.752, 277.504, 292.125, 198.099, 185.709, 136.413, 138.613, 194.986, 142.484, 147.706, 91.615, 0.156, 114.518, 311.12],
|
|
24
|
-
[223.926, 201.084, 210.133, 216.779, 237.762, 145.336, 132.887, 108.26, 98.057, 142.613, 122.716, 33.185, 68.547, 114.901, 0.162, 257.403],
|
|
25
|
-
[205.555, 178.082, 186.533, 195.774, 216.701, 117.089, 128.001, 175.761, 176.018, 125.551, 303.904, 285.782, 323.15, 309.618, 255.904, 0.262]
|
|
26
|
-
]
|
|
27
|
-
|
|
28
|
-
export function getRegionDelayMatrix(): number[][] {
|
|
29
|
-
const ret = []
|
|
30
|
-
for (let i = 0; i < regionPingMatrix.length; i++) {
|
|
31
|
-
const row = []
|
|
32
|
-
for (let j = 0; j < regionPingMatrix[i].length; j++) {
|
|
33
|
-
row.push(regionPingMatrix[i][j] / 2)
|
|
34
|
-
}
|
|
35
|
-
ret.push(row)
|
|
36
|
-
}
|
|
37
|
-
return ret
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function getRandomRegion(): number {
|
|
41
|
-
return Math.floor(Math.random() * 15)
|
|
42
|
-
}
|
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
import EventEmitter from 'eventemitter3'
|
|
2
|
-
import { WebrtcConnectionEvents, IWebrtcConnection, RtcDescription } from './IWebrtcConnection'
|
|
3
|
-
import { IConnection, ConnectionID, ConnectionEvents, ConnectionType } from '../IConnection'
|
|
4
|
-
import { Logger } from '@streamr/utils'
|
|
5
|
-
import { EARLY_TIMEOUT, IceServer } from './WebrtcConnector'
|
|
6
|
-
import { createRandomConnectionId } from '../Connection'
|
|
7
|
-
|
|
8
|
-
enum DisconnectedRtcPeerConnectionStateEnum {
|
|
9
|
-
DISCONNECTED = 'disconnected',
|
|
10
|
-
FAILED = 'failed',
|
|
11
|
-
CLOSED = 'closed',
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const logger = new Logger(module)
|
|
15
|
-
|
|
16
|
-
type Events = WebrtcConnectionEvents & ConnectionEvents
|
|
17
|
-
|
|
18
|
-
interface Params {
|
|
19
|
-
iceServers?: IceServer[]
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export class NodeWebrtcConnection extends EventEmitter<Events> implements IWebrtcConnection, IConnection {
|
|
23
|
-
|
|
24
|
-
public connectionId: ConnectionID
|
|
25
|
-
public readonly connectionType: ConnectionType = ConnectionType.WEBRTC
|
|
26
|
-
// We need to keep track of connection state ourselves because
|
|
27
|
-
// RTCPeerConnection.connectionState is not supported on Firefox
|
|
28
|
-
private lastState: RTCPeerConnectionState = 'connecting'
|
|
29
|
-
private readonly iceServers: IceServer[]
|
|
30
|
-
private peerConnection?: RTCPeerConnection
|
|
31
|
-
private dataChannel?: RTCDataChannel
|
|
32
|
-
private makingOffer = false
|
|
33
|
-
private isOffering = false
|
|
34
|
-
private closed = false
|
|
35
|
-
private earlyTimeout: NodeJS.Timeout
|
|
36
|
-
|
|
37
|
-
constructor(params: Params) {
|
|
38
|
-
super()
|
|
39
|
-
this.connectionId = createRandomConnectionId()
|
|
40
|
-
this.iceServers = params.iceServers ?? []
|
|
41
|
-
this.earlyTimeout = setTimeout(() => {
|
|
42
|
-
this.doClose(false, 'timed out due to remote descriptor not being set')
|
|
43
|
-
}, EARLY_TIMEOUT)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public start(isOffering: boolean): void {
|
|
47
|
-
this.isOffering = isOffering
|
|
48
|
-
const urls: RTCIceServer[] = this.iceServers.map(({ url, port, username, password }) => ({
|
|
49
|
-
urls: `${url}:${port}`,
|
|
50
|
-
username,
|
|
51
|
-
credential: password
|
|
52
|
-
}))
|
|
53
|
-
this.peerConnection = new RTCPeerConnection({ iceServers: urls })
|
|
54
|
-
|
|
55
|
-
this.peerConnection.onicecandidate = (event) => {
|
|
56
|
-
if ((event.candidate !== null) && (event.candidate.sdpMid !== null)) {
|
|
57
|
-
this.emit('localCandidate', event.candidate.candidate, event.candidate.sdpMid)
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
this.peerConnection.onicegatheringstatechange = () => {
|
|
62
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
63
|
-
logger.trace(`conn.onGatheringStateChange: ${this.peerConnection?.iceGatheringState}`)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
this.peerConnection.onconnectionstatechange = () => this.onStateChange()
|
|
67
|
-
|
|
68
|
-
if (isOffering) {
|
|
69
|
-
this.peerConnection.onnegotiationneeded = async () => {
|
|
70
|
-
if (this.peerConnection !== undefined) {
|
|
71
|
-
this.makingOffer = true
|
|
72
|
-
try {
|
|
73
|
-
await this.peerConnection.setLocalDescription()
|
|
74
|
-
} catch (err) {
|
|
75
|
-
logger.warn('Failed to set local description', { err })
|
|
76
|
-
}
|
|
77
|
-
if (this.peerConnection.localDescription !== null) {
|
|
78
|
-
this.emit('localDescription', this.peerConnection.localDescription?.sdp, this.peerConnection.localDescription?.type)
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
this.makingOffer = false
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const dataChannel = this.peerConnection.createDataChannel('streamrDataChannel')
|
|
85
|
-
this.setupDataChannel(dataChannel)
|
|
86
|
-
} else {
|
|
87
|
-
this.peerConnection.ondatachannel = (event) => {
|
|
88
|
-
this.setupDataChannel(event.channel)
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
public async setRemoteDescription(description: string, type: string): Promise<void> {
|
|
94
|
-
const offerCollision = (type.toLowerCase() === RtcDescription.OFFER) && (this.makingOffer || (this.peerConnection === undefined) ||
|
|
95
|
-
this.peerConnection.signalingState != 'stable')
|
|
96
|
-
|
|
97
|
-
const ignoreOffer = this.isOffering && offerCollision
|
|
98
|
-
if (ignoreOffer) {
|
|
99
|
-
return
|
|
100
|
-
}
|
|
101
|
-
try {
|
|
102
|
-
await this.peerConnection?.setRemoteDescription({ sdp: description, type: type.toLowerCase() as RTCSdpType })
|
|
103
|
-
clearTimeout(this.earlyTimeout)
|
|
104
|
-
} catch (err) {
|
|
105
|
-
logger.warn('Failed to set remote description', { err })
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if ((type.toLowerCase() === RtcDescription.OFFER) && (this.peerConnection !== undefined)) {
|
|
109
|
-
try {
|
|
110
|
-
await this.peerConnection.setLocalDescription()
|
|
111
|
-
} catch (err) {
|
|
112
|
-
logger.warn('Failed to set local description', { err })
|
|
113
|
-
}
|
|
114
|
-
if (this.peerConnection.localDescription !== null) {
|
|
115
|
-
this.emit('localDescription', this.peerConnection.localDescription.sdp, this.peerConnection.localDescription.type)
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
public addRemoteCandidate(candidate: string, mid: string): void {
|
|
121
|
-
this.peerConnection?.addIceCandidate({ candidate: candidate, sdpMid: mid })
|
|
122
|
-
.catch((err) => {
|
|
123
|
-
logger.warn('Failed to add ICE candidate', { err })
|
|
124
|
-
})
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
public isOpen(): boolean {
|
|
128
|
-
return this.lastState === 'connected'
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// IConnection implementation
|
|
132
|
-
|
|
133
|
-
public async close(gracefulLeave: boolean, reason?: string): Promise<void> {
|
|
134
|
-
this.doClose(gracefulLeave, reason)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
private doClose(gracefulLeave: boolean, reason?: string): void {
|
|
138
|
-
if (!this.closed) {
|
|
139
|
-
this.closed = true
|
|
140
|
-
this.lastState = 'closed'
|
|
141
|
-
clearTimeout(this.earlyTimeout)
|
|
142
|
-
|
|
143
|
-
this.stopListening()
|
|
144
|
-
this.emit('disconnected', gracefulLeave, undefined, reason)
|
|
145
|
-
|
|
146
|
-
this.removeAllListeners()
|
|
147
|
-
|
|
148
|
-
if (this.dataChannel !== undefined) {
|
|
149
|
-
try {
|
|
150
|
-
this.dataChannel.close()
|
|
151
|
-
} catch (err) {
|
|
152
|
-
logger.warn('Failed to close data channel', { err })
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
this.dataChannel = undefined
|
|
157
|
-
|
|
158
|
-
if (this.peerConnection !== undefined) {
|
|
159
|
-
try {
|
|
160
|
-
this.peerConnection.close()
|
|
161
|
-
} catch (err) {
|
|
162
|
-
logger.warn('Failed to close connection', { err })
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
this.peerConnection = undefined
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
public destroy(): void {
|
|
171
|
-
this.removeAllListeners()
|
|
172
|
-
this.doClose(false)
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
public send(data: Uint8Array): void {
|
|
176
|
-
if (this.lastState === 'connected') {
|
|
177
|
-
this.dataChannel?.send(data as Buffer)
|
|
178
|
-
} else {
|
|
179
|
-
logger.warn('Tried to send on a connection with last state ' + this.lastState)
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
private setupDataChannel(dataChannel: RTCDataChannel): void {
|
|
184
|
-
this.dataChannel = dataChannel
|
|
185
|
-
this.dataChannel.binaryType = 'arraybuffer'
|
|
186
|
-
|
|
187
|
-
dataChannel.onopen = () => {
|
|
188
|
-
logger.trace('dc.onOpen')
|
|
189
|
-
this.onDataChannelOpen()
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
dataChannel.onclose = () => {
|
|
193
|
-
logger.trace('dc.onClosed')
|
|
194
|
-
this.doClose(false)
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
dataChannel.onerror = (err) => {
|
|
198
|
-
logger.warn('Data channel error', { err })
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
dataChannel.onmessage = (msg) => {
|
|
202
|
-
logger.trace('dc.onmessage')
|
|
203
|
-
this.emit('data', new Uint8Array(msg.data))
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
private stopListening() {
|
|
208
|
-
if (this.dataChannel !== undefined) {
|
|
209
|
-
this.dataChannel.onopen = null
|
|
210
|
-
this.dataChannel.onclose = null
|
|
211
|
-
this.dataChannel.onerror = null
|
|
212
|
-
this.dataChannel.onbufferedamountlow = null
|
|
213
|
-
this.dataChannel.onmessage = null
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
if (this.peerConnection !== undefined) {
|
|
217
|
-
this.peerConnection.onconnectionstatechange = null
|
|
218
|
-
this.peerConnection.onicecandidate = null
|
|
219
|
-
this.peerConnection.onicegatheringstatechange = null
|
|
220
|
-
this.peerConnection.onnegotiationneeded = null
|
|
221
|
-
this.peerConnection.ondatachannel = null
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
private onDataChannelOpen(): void {
|
|
226
|
-
this.lastState = 'connected'
|
|
227
|
-
this.emit('connected')
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
private onStateChange(): void {
|
|
231
|
-
if (this.peerConnection!.connectionState === DisconnectedRtcPeerConnectionStateEnum.CLOSED
|
|
232
|
-
|| this.peerConnection!.connectionState === DisconnectedRtcPeerConnectionStateEnum.DISCONNECTED
|
|
233
|
-
|| this.peerConnection!.connectionState === DisconnectedRtcPeerConnectionStateEnum.FAILED
|
|
234
|
-
) {
|
|
235
|
-
this.doClose(false)
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
public setConnectionId(connectionId: ConnectionID): void {
|
|
240
|
-
this.connectionId = connectionId
|
|
241
|
-
}
|
|
242
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export interface WebrtcConnectionEvents {
|
|
2
|
-
localDescription: (description: string, type: string) => void
|
|
3
|
-
localCandidate: (candidate: string, mid: string) => void
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export enum RtcDescription {
|
|
7
|
-
OFFER = 'offer',
|
|
8
|
-
ANSWER = 'answer'
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface IWebrtcConnection {
|
|
12
|
-
start(isOffering: boolean): void
|
|
13
|
-
|
|
14
|
-
on(event: 'localDescription', listener: (description: string, type: string) => void): this
|
|
15
|
-
on(event: 'localCandidate', listener: (candidate: string, mid: string) => void): this
|
|
16
|
-
once(event: 'localDescription', listener: (description: string, type: string) => void): this
|
|
17
|
-
once(event: 'localCandidate', listener: (candidate: string, mid: string) => void): this
|
|
18
|
-
off(event: 'localDescription', listener: (description: string, type: string) => void): this
|
|
19
|
-
off(event: 'localCandidate', listener: (candidate: string, mid: string) => void): this
|
|
20
|
-
|
|
21
|
-
setRemoteDescription(description: string, type: string): Promise<void>
|
|
22
|
-
addRemoteCandidate(candidate: string, mid: string): void
|
|
23
|
-
isOpen(): boolean
|
|
24
|
-
}
|