@streamr/dht 100.2.5-beta.0 → 101.0.0-beta.0
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/package.json +7 -7
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionLockRpcLocal.js +8 -8
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.js +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +4 -6
- package/dist/src/connection/ConnectionManager.js +128 -103
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +15 -14
- package/dist/src/connection/ConnectorFacade.js +70 -52
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +9 -2
- package/dist/src/connection/Handshaker.js +117 -27
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +13 -38
- package/dist/src/connection/ManagedConnection.js +31 -252
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/OutputBuffer.d.ts +9 -0
- package/dist/src/connection/OutputBuffer.js +26 -0
- package/dist/src/connection/OutputBuffer.js.map +1 -0
- package/dist/src/connection/PendingConnection.d.ts +19 -0
- package/dist/src/connection/PendingConnection.js +59 -0
- package/dist/src/connection/PendingConnection.js.map +1 -0
- package/dist/src/connection/connectivityChecker.js +3 -3
- package/dist/src/connection/connectivityChecker.js.map +1 -1
- package/dist/src/connection/connectivityRequestHandler.js +2 -2
- package/dist/src/connection/connectivityRequestHandler.js.map +1 -1
- package/dist/src/connection/simulator/Simulator.d.ts +1 -3
- package/dist/src/connection/simulator/Simulator.js +1 -4
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +1 -2
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.d.ts +3 -3
- package/dist/src/connection/simulator/SimulatorConnector.js +28 -21
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.d.ts +1 -6
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +3 -20
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +11 -6
- package/dist/src/connection/webrtc/WebrtcConnector.js +57 -42
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +8 -10
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +21 -44
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/AbstractWebsocketClientConnection.js +8 -2
- package/dist/src/connection/websocket/AbstractWebsocketClientConnection.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +3 -3
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +8 -8
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
- package/dist/src/connection/websocket/NodeWebsocketClientConnection.js +1 -1
- package/dist/src/connection/websocket/NodeWebsocketClientConnection.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketClientConnector.d.ts +26 -0
- package/dist/src/connection/websocket/WebsocketClientConnector.js +86 -0
- package/dist/src/connection/websocket/WebsocketClientConnector.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.d.ts +19 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.js +23 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcRemote.d.ts +5 -0
- package/dist/src/connection/websocket/{WebsocketConnectorRpcRemote.js → WebsocketClientConnectorRpcRemote.js} +4 -4
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcRemote.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketServer.d.ts +8 -5
- package/dist/src/connection/websocket/WebsocketServer.js +11 -11
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/connection/websocket/{WebsocketConnector.d.ts → WebsocketServerConnector.d.ts} +16 -21
- package/dist/src/connection/websocket/{WebsocketConnector.js → WebsocketServerConnector.js} +112 -160
- package/dist/src/connection/websocket/WebsocketServerConnector.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +4 -4
- package/dist/src/dht/DhtNode.js +85 -84
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +3 -3
- package/dist/src/dht/DhtNodeRpcLocal.js +9 -9
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +3 -3
- package/dist/src/dht/ExternalApiRpcLocal.js +5 -5
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.js +2 -2
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +4 -4
- package/dist/src/dht/PeerManager.js +22 -22
- package/dist/src/dht/PeerManager.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +3 -3
- package/dist/src/dht/contact/SortedContactList.js +9 -9
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +3 -3
- package/dist/src/dht/discovery/DiscoverySession.js +21 -21
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +3 -3
- package/dist/src/dht/discovery/PeerDiscovery.js +46 -44
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/discovery/RingDiscoverySession.d.ts +3 -3
- package/dist/src/dht/discovery/RingDiscoverySession.js +19 -19
- package/dist/src/dht/discovery/RingDiscoverySession.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +33 -33
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js +8 -8
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +4 -4
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +24 -24
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +4 -4
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +5 -5
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +3 -3
- package/dist/src/dht/routing/Router.js +20 -20
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.d.ts +3 -3
- package/dist/src/dht/routing/RouterRpcLocal.js +16 -16
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -3
- package/dist/src/dht/routing/RoutingSession.js +24 -24
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +3 -3
- package/dist/src/dht/store/StoreManager.js +25 -25
- package/dist/src/dht/store/StoreManager.js.map +1 -1
- package/dist/src/dht/store/StoreRpcLocal.d.ts +3 -3
- package/dist/src/dht/store/StoreRpcLocal.js +12 -12
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/exports.d.ts +3 -0
- package/dist/src/exports.js +5 -1
- package/dist/src/exports.js.map +1 -1
- package/dist/src/proto/google/protobuf/any.d.ts +5 -8
- package/dist/src/proto/google/protobuf/any.js.map +1 -1
- package/dist/src/proto/google/protobuf/empty.d.ts +1 -0
- package/dist/src/proto/google/protobuf/empty.js.map +1 -1
- package/dist/src/proto/google/protobuf/timestamp.d.ts +1 -10
- package/dist/src/proto/google/protobuf/timestamp.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +4 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +8 -8
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +3 -3
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +4 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +2 -2
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.d.ts +2 -2
- package/dist/src/transport/ListeningRpcCommunicator.js +2 -2
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -2
- package/dist/src/transport/RoutingRpcCommunicator.js +2 -2
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/package.json +7 -7
- package/protos/DhtRpc.proto +1 -1
- package/src/connection/ConnectionLockRpcLocal.ts +9 -9
- package/src/connection/ConnectionLockRpcRemote.ts +1 -1
- package/src/connection/ConnectionManager.ts +153 -111
- package/src/connection/ConnectorFacade.ts +84 -61
- package/src/connection/Handshaker.ts +131 -27
- package/src/connection/ManagedConnection.ts +41 -304
- package/src/connection/OutputBuffer.ts +28 -0
- package/src/connection/PendingConnection.ts +68 -0
- package/src/connection/connectivityChecker.ts +2 -2
- package/src/connection/connectivityRequestHandler.ts +1 -1
- package/src/connection/simulator/Simulator.ts +1 -5
- package/src/connection/simulator/SimulatorConnection.ts +1 -2
- package/src/connection/simulator/SimulatorConnector.ts +34 -33
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -6
- package/src/connection/webrtc/NodeWebrtcConnection.ts +3 -24
- package/src/connection/webrtc/WebrtcConnector.ts +73 -62
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +26 -56
- package/src/connection/websocket/AbstractWebsocketClientConnection.ts +8 -2
- package/src/connection/websocket/AutoCertifierClientFacade.ts +11 -11
- package/src/connection/websocket/NodeWebsocketClientConnection.ts +1 -1
- package/src/connection/websocket/WebsocketClientConnector.ts +119 -0
- package/src/connection/websocket/WebsocketClientConnectorRpcLocal.ts +39 -0
- package/src/connection/websocket/{WebsocketConnectorRpcRemote.ts → WebsocketClientConnectorRpcRemote.ts} +2 -2
- package/src/connection/websocket/WebsocketServer.ts +18 -14
- package/src/connection/websocket/{WebsocketConnector.ts → WebsocketServerConnector.ts} +128 -205
- package/src/dht/DhtNode.ts +90 -89
- package/src/dht/DhtNodeRpcLocal.ts +11 -11
- package/src/dht/ExternalApiRpcLocal.ts +6 -6
- package/src/dht/ExternalApiRpcRemote.ts +2 -2
- package/src/dht/PeerManager.ts +24 -24
- package/src/dht/contact/SortedContactList.ts +10 -10
- package/src/dht/discovery/DiscoverySession.ts +24 -24
- package/src/dht/discovery/PeerDiscovery.ts +47 -45
- package/src/dht/discovery/RingDiscoverySession.ts +23 -23
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +36 -36
- package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +9 -9
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +25 -25
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +7 -7
- package/src/dht/routing/Router.ts +21 -21
- package/src/dht/routing/RouterRpcLocal.ts +17 -17
- package/src/dht/routing/RoutingSession.ts +26 -26
- package/src/dht/store/StoreManager.ts +27 -27
- package/src/dht/store/StoreRpcLocal.ts +13 -13
- package/src/exports.ts +3 -0
- package/src/proto/google/protobuf/any.ts +6 -9
- package/src/proto/google/protobuf/empty.ts +2 -1
- package/src/proto/google/protobuf/timestamp.ts +2 -11
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +9 -9
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +3 -3
- package/src/proto/packages/dht/protos/DhtRpc.ts +4 -4
- package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +1 -1
- package/src/transport/ListeningRpcCommunicator.ts +3 -3
- package/src/transport/RoutingRpcCommunicator.ts +3 -3
- package/test/end-to-end/Layer0Webrtc.test.ts +0 -10
- package/test/integration/ConnectionManager.test.ts +3 -2
- package/test/integration/GeoIpConnectivityChecking.test.ts +1 -1
- package/test/integration/SimultaneousConnections.test.ts +2 -2
- package/test/integration/WebrtcConnectionManagement.test.ts +2 -10
- package/test/integration/{WebsocketConnectorRpc.test.ts → WebsocketClientConnectorRpc.test.ts} +9 -9
- package/test/integration/WebsocketConnectionManagement.test.ts +11 -29
- package/test/unit/ConnectionManager.test.ts +64 -0
- package/test/unit/DiscoverySession.test.ts +1 -1
- package/test/unit/Handshaker.test.ts +169 -0
- package/test/unit/ManagedConnection.test.ts +58 -0
- package/test/unit/PendingConnection.test.ts +57 -0
- package/test/unit/WebrtcConnector.test.ts +56 -0
- package/test/unit/{WebsocketConnector.test.ts → WebsocketClientConnector.test.ts} +56 -11
- package/test/unit/WebsocketServerConnector.test.ts +102 -0
- package/test/utils/FakeConnectorFacade.ts +41 -0
- package/test/utils/mock/MockConnection.ts +26 -0
- package/test/utils/utils.ts +2 -2
- package/dist/src/connection/IConnectionSource.d.ts +0 -4
- package/dist/src/connection/IConnectionSource.js +0 -3
- package/dist/src/connection/IConnectionSource.js.map +0 -1
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.d.ts +0 -7
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.js +0 -20
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnector.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +0 -19
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +0 -23
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +0 -5
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +0 -1
- package/src/connection/IConnectionSource.ts +0 -6
- package/src/connection/webrtc/ManagedWebrtcConnection.ts +0 -27
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +0 -39
|
@@ -9,7 +9,7 @@ import { RingId, RingIdRaw, getLeftDistance, getRingIdFromPeerDescriptor, getRin
|
|
|
9
9
|
|
|
10
10
|
const logger = new Logger(module)
|
|
11
11
|
|
|
12
|
-
interface
|
|
12
|
+
interface RingDiscoverySessionOptions {
|
|
13
13
|
targetId: RingIdRaw
|
|
14
14
|
parallelism: number
|
|
15
15
|
noProgressLimit: number
|
|
@@ -25,33 +25,33 @@ export class RingDiscoverySession {
|
|
|
25
25
|
private noProgressCounter = 0
|
|
26
26
|
private ongoingRequests: Set<DhtAddress> = new Set()
|
|
27
27
|
private doneGate = new Gate(false)
|
|
28
|
-
private readonly
|
|
28
|
+
private readonly options: RingDiscoverySessionOptions
|
|
29
29
|
private numContactedPeers = 0
|
|
30
30
|
private targetIdAsRingId: RingId
|
|
31
31
|
|
|
32
|
-
constructor(
|
|
33
|
-
this.
|
|
34
|
-
this.targetIdAsRingId = getRingIdFromRaw(this.
|
|
32
|
+
constructor(options: RingDiscoverySessionOptions) {
|
|
33
|
+
this.options = options
|
|
34
|
+
this.targetIdAsRingId = getRingIdFromRaw(this.options.targetId)
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
private addContacts(contacts: PeerDescriptor[]): void {
|
|
38
|
-
if (this.
|
|
38
|
+
if (this.options.abortSignal.aborted || this.doneGate.isOpen()) {
|
|
39
39
|
return
|
|
40
40
|
}
|
|
41
41
|
for (const contact of contacts) {
|
|
42
|
-
this.
|
|
42
|
+
this.options.peerManager.addContact(contact)
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
private async fetchClosestContactsFromRemote(contact: DhtNodeRpcRemote): Promise<RingContacts> {
|
|
47
|
-
if (this.
|
|
47
|
+
if (this.options.abortSignal.aborted || this.doneGate.isOpen()) {
|
|
48
48
|
return { left: [], right: [] }
|
|
49
49
|
}
|
|
50
50
|
logger.trace(`Getting closest ring peers from contact: ${getNodeIdFromPeerDescriptor(contact.getPeerDescriptor())}`)
|
|
51
51
|
this.numContactedPeers++
|
|
52
|
-
this.
|
|
53
|
-
const returnedContacts = await contact.getClosestRingPeers(this.
|
|
54
|
-
this.
|
|
52
|
+
this.options.contactedPeers.add(contact.getNodeId())
|
|
53
|
+
const returnedContacts = await contact.getClosestRingPeers(this.options.targetId)
|
|
54
|
+
this.options.peerManager.setContactActive(contact.getNodeId())
|
|
55
55
|
return returnedContacts
|
|
56
56
|
}
|
|
57
57
|
|
|
@@ -60,7 +60,7 @@ export class RingDiscoverySession {
|
|
|
60
60
|
return
|
|
61
61
|
}
|
|
62
62
|
this.ongoingRequests.delete(nodeId)
|
|
63
|
-
const oldClosestContacts = this.
|
|
63
|
+
const oldClosestContacts = this.options.peerManager.getClosestRingContactsTo(this.options.targetId, 1)
|
|
64
64
|
const oldClosestLeftDistance = getLeftDistance(
|
|
65
65
|
this.targetIdAsRingId,
|
|
66
66
|
getRingIdFromPeerDescriptor(oldClosestContacts.left[0].getPeerDescriptor())
|
|
@@ -70,7 +70,7 @@ export class RingDiscoverySession {
|
|
|
70
70
|
getRingIdFromPeerDescriptor(oldClosestContacts.right[0].getPeerDescriptor())
|
|
71
71
|
)
|
|
72
72
|
this.addContacts(contacts.left.concat(contacts.right))
|
|
73
|
-
const newClosestContacts = this.
|
|
73
|
+
const newClosestContacts = this.options.peerManager.getClosestRingContactsTo(this.options.targetId, 1)
|
|
74
74
|
const newClosestLeftDistance = getLeftDistance(this.targetIdAsRingId,
|
|
75
75
|
getRingIdFromPeerDescriptor(newClosestContacts.left[0].getPeerDescriptor()))
|
|
76
76
|
const newClosestRightDistance = getLeftDistance(this.targetIdAsRingId,
|
|
@@ -85,20 +85,20 @@ export class RingDiscoverySession {
|
|
|
85
85
|
return
|
|
86
86
|
}
|
|
87
87
|
this.ongoingRequests.delete(peer.getNodeId())
|
|
88
|
-
this.
|
|
88
|
+
this.options.peerManager.removeContact(peer.getNodeId())
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
private findMoreContacts(): void {
|
|
92
|
-
if (this.
|
|
92
|
+
if (this.options.abortSignal.aborted || this.doneGate.isOpen()) {
|
|
93
93
|
return
|
|
94
94
|
}
|
|
95
|
-
const uncontacted = this.
|
|
96
|
-
this.
|
|
97
|
-
this.
|
|
98
|
-
this.
|
|
95
|
+
const uncontacted = this.options.peerManager.getClosestRingContactsTo(
|
|
96
|
+
this.options.targetId,
|
|
97
|
+
this.options.parallelism,
|
|
98
|
+
this.options.contactedPeers
|
|
99
99
|
)
|
|
100
100
|
if ((uncontacted.left.length === 0 && uncontacted.right.length === 0)
|
|
101
|
-
|| this.noProgressCounter >= this.
|
|
101
|
+
|| this.noProgressCounter >= this.options.noProgressLimit) {
|
|
102
102
|
this.doneGate.open()
|
|
103
103
|
return
|
|
104
104
|
}
|
|
@@ -122,7 +122,7 @@ export class RingDiscoverySession {
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
for (const nextPeer of merged) {
|
|
125
|
-
if (this.ongoingRequests.size >= this.
|
|
125
|
+
if (this.ongoingRequests.size >= this.options.parallelism) {
|
|
126
126
|
break
|
|
127
127
|
}
|
|
128
128
|
this.ongoingRequests.add(nextPeer.getNodeId())
|
|
@@ -137,12 +137,12 @@ export class RingDiscoverySession {
|
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
public async findClosestNodes(timeout: number): Promise<void> {
|
|
140
|
-
if (this.
|
|
140
|
+
if (this.options.peerManager.getNearbyContactCount(this.options.contactedPeers) === 0) {
|
|
141
141
|
return
|
|
142
142
|
}
|
|
143
143
|
setImmediate(() => {
|
|
144
144
|
this.findMoreContacts()
|
|
145
145
|
})
|
|
146
|
-
await withTimeout(this.doneGate.waitUntilOpen(), timeout, 'discovery session timed out', this.
|
|
146
|
+
await withTimeout(this.doneGate.waitUntilOpen(), timeout, 'discovery session timed out', this.options.abortSignal)
|
|
147
147
|
}
|
|
148
148
|
}
|
|
@@ -27,7 +27,7 @@ import { DhtAddress, areEqualPeerDescriptors, getDhtAddressFromRaw, getNodeIdFro
|
|
|
27
27
|
import { getDistance } from '../PeerManager'
|
|
28
28
|
import { ConnectionsView } from '../../exports'
|
|
29
29
|
|
|
30
|
-
interface
|
|
30
|
+
interface RecursiveOperationManagerOptions {
|
|
31
31
|
rpcCommunicator: RoutingRpcCommunicator
|
|
32
32
|
sessionTransport: ITransport
|
|
33
33
|
router: Router
|
|
@@ -47,21 +47,21 @@ export class RecursiveOperationManager {
|
|
|
47
47
|
|
|
48
48
|
private ongoingSessions: Map<string, RecursiveOperationSession> = new Map()
|
|
49
49
|
private stopped = false
|
|
50
|
-
private readonly
|
|
50
|
+
private readonly options: RecursiveOperationManagerOptions
|
|
51
51
|
|
|
52
|
-
constructor(
|
|
53
|
-
this.
|
|
52
|
+
constructor(options: RecursiveOperationManagerOptions) {
|
|
53
|
+
this.options = options
|
|
54
54
|
this.registerLocalRpcMethods()
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
private registerLocalRpcMethods() {
|
|
58
58
|
const rpcLocal = new RecursiveOperationRpcLocal({
|
|
59
59
|
doRouteRequest: (routedMessage: RouteMessageWrapper) => this.doRouteRequest(routedMessage),
|
|
60
|
-
addContact: (contact: PeerDescriptor) => this.
|
|
61
|
-
isMostLikelyDuplicate: (requestId: string) => this.
|
|
62
|
-
addToDuplicateDetector: (requestId: string) => this.
|
|
60
|
+
addContact: (contact: PeerDescriptor) => this.options.addContact(contact),
|
|
61
|
+
isMostLikelyDuplicate: (requestId: string) => this.options.router.isMostLikelyDuplicate(requestId),
|
|
62
|
+
addToDuplicateDetector: (requestId: string) => this.options.router.addToDuplicateDetector(requestId)
|
|
63
63
|
})
|
|
64
|
-
this.
|
|
64
|
+
this.options.rpcCommunicator.registerRpcMethod(
|
|
65
65
|
RouteMessageWrapper,
|
|
66
66
|
RouteMessageAck,
|
|
67
67
|
'routeRequest',
|
|
@@ -85,23 +85,23 @@ export class RecursiveOperationManager {
|
|
|
85
85
|
return { closestNodes: [] }
|
|
86
86
|
}
|
|
87
87
|
const session = new RecursiveOperationSession({
|
|
88
|
-
transport: this.
|
|
88
|
+
transport: this.options.sessionTransport,
|
|
89
89
|
targetId,
|
|
90
|
-
localPeerDescriptor: this.
|
|
91
|
-
// TODO use
|
|
92
|
-
waitedRoutingPathCompletions: this.
|
|
90
|
+
localPeerDescriptor: this.options.localPeerDescriptor,
|
|
91
|
+
// TODO use options option or named constant?
|
|
92
|
+
waitedRoutingPathCompletions: this.options.connectionsView.getConnectionCount() > 1 ? 2 : 1,
|
|
93
93
|
operation,
|
|
94
94
|
// TODO would it make sense to give excludedPeer as one of the fields RecursiveOperationSession?
|
|
95
95
|
doRouteRequest: (routedMessage: RouteMessageWrapper) => {
|
|
96
96
|
return this.doRouteRequest(routedMessage, excludedPeer)
|
|
97
97
|
}
|
|
98
98
|
})
|
|
99
|
-
if (this.
|
|
100
|
-
const dataEntries = Array.from(this.
|
|
99
|
+
if (this.options.connectionsView.getConnectionCount() === 0) {
|
|
100
|
+
const dataEntries = Array.from(this.options.localDataStore.values(targetId))
|
|
101
101
|
session.onResponseReceived(
|
|
102
|
-
getNodeIdFromPeerDescriptor(this.
|
|
103
|
-
[this.
|
|
104
|
-
[this.
|
|
102
|
+
getNodeIdFromPeerDescriptor(this.options.localPeerDescriptor),
|
|
103
|
+
[this.options.localPeerDescriptor],
|
|
104
|
+
[this.options.localPeerDescriptor],
|
|
105
105
|
dataEntries,
|
|
106
106
|
true
|
|
107
107
|
)
|
|
@@ -111,27 +111,27 @@ export class RecursiveOperationManager {
|
|
|
111
111
|
if (waitForCompletion === true) {
|
|
112
112
|
try {
|
|
113
113
|
await runAndWaitForEvents3<RecursiveOperationSessionEvents>(
|
|
114
|
-
[() => session.start(this.
|
|
114
|
+
[() => session.start(this.options.serviceId)],
|
|
115
115
|
[[session, 'completed']],
|
|
116
|
-
// TODO use
|
|
116
|
+
// TODO use options option or named constant?
|
|
117
117
|
15000
|
|
118
118
|
)
|
|
119
119
|
} catch (err) {
|
|
120
120
|
logger.debug('start failed', { err })
|
|
121
121
|
}
|
|
122
122
|
} else {
|
|
123
|
-
session.start(this.
|
|
123
|
+
session.start(this.options.serviceId)
|
|
124
124
|
// Wait for delete operation to be sent out by the router
|
|
125
125
|
// TODO: Add a feature to wait for the router to pass the message?
|
|
126
126
|
await wait(50)
|
|
127
127
|
}
|
|
128
128
|
if (operation === RecursiveOperation.FETCH_DATA) {
|
|
129
|
-
const dataEntries = Array.from(this.
|
|
129
|
+
const dataEntries = Array.from(this.options.localDataStore.values(targetId))
|
|
130
130
|
if (dataEntries.length > 0) {
|
|
131
|
-
this.sendResponse([this.
|
|
131
|
+
this.sendResponse([this.options.localPeerDescriptor], this.options.localPeerDescriptor, session.getId(), [], dataEntries, true)
|
|
132
132
|
}
|
|
133
133
|
} else if (operation === RecursiveOperation.DELETE_DATA) {
|
|
134
|
-
this.
|
|
134
|
+
this.options.localDataStore.markAsDeleted(targetId, getNodeIdFromPeerDescriptor(this.options.localPeerDescriptor))
|
|
135
135
|
}
|
|
136
136
|
this.ongoingSessions.delete(session.getId())
|
|
137
137
|
session.stop()
|
|
@@ -146,25 +146,25 @@ export class RecursiveOperationManager {
|
|
|
146
146
|
dataEntries: DataEntry[],
|
|
147
147
|
noCloserNodesFound: boolean = false
|
|
148
148
|
): void {
|
|
149
|
-
const isOwnNode = areEqualPeerDescriptors(this.
|
|
149
|
+
const isOwnNode = areEqualPeerDescriptors(this.options.localPeerDescriptor, targetPeerDescriptor)
|
|
150
150
|
if (isOwnNode && this.ongoingSessions.has(serviceId)) {
|
|
151
151
|
this.ongoingSessions.get(serviceId)!
|
|
152
152
|
.onResponseReceived(
|
|
153
|
-
getNodeIdFromPeerDescriptor(this.
|
|
153
|
+
getNodeIdFromPeerDescriptor(this.options.localPeerDescriptor),
|
|
154
154
|
routingPath,
|
|
155
155
|
closestConnectedNodes,
|
|
156
156
|
dataEntries,
|
|
157
157
|
noCloserNodesFound
|
|
158
158
|
)
|
|
159
159
|
} else {
|
|
160
|
-
// TODO use
|
|
161
|
-
const remoteCommunicator = new ListeningRpcCommunicator(serviceId, this.
|
|
160
|
+
// TODO use options option or named constant?
|
|
161
|
+
const remoteCommunicator = new ListeningRpcCommunicator(serviceId, this.options.sessionTransport, { rpcRequestTimeout: 15000 })
|
|
162
162
|
const rpcRemote = new RecursiveOperationSessionRpcRemote(
|
|
163
|
-
this.
|
|
163
|
+
this.options.localPeerDescriptor,
|
|
164
164
|
targetPeerDescriptor,
|
|
165
165
|
remoteCommunicator,
|
|
166
166
|
RecursiveOperationSessionRpcClient,
|
|
167
|
-
// TODO use
|
|
167
|
+
// TODO use options option or named constant?
|
|
168
168
|
10000
|
|
169
169
|
)
|
|
170
170
|
rpcRemote.sendResponse(routingPath, closestConnectedNodes, dataEntries, noCloserNodesFound)
|
|
@@ -178,15 +178,15 @@ export class RecursiveOperationManager {
|
|
|
178
178
|
}
|
|
179
179
|
const targetId = getDhtAddressFromRaw(routedMessage.target)
|
|
180
180
|
const request = (routedMessage.message!.body as { recursiveOperationRequest: RecursiveOperationRequest }).recursiveOperationRequest
|
|
181
|
-
// TODO use
|
|
181
|
+
// TODO use options option or named constant?
|
|
182
182
|
const closestConnectedNodes = this.getClosestConnectedNodes(targetId, 5)
|
|
183
183
|
const dataEntries = (request.operation === RecursiveOperation.FETCH_DATA)
|
|
184
|
-
? Array.from(this.
|
|
184
|
+
? Array.from(this.options.localDataStore.values(targetId))
|
|
185
185
|
: []
|
|
186
186
|
if (request.operation === RecursiveOperation.DELETE_DATA) {
|
|
187
|
-
this.
|
|
187
|
+
this.options.localDataStore.markAsDeleted(targetId, getNodeIdFromPeerDescriptor(routedMessage.sourcePeer!))
|
|
188
188
|
}
|
|
189
|
-
if (areEqualBinaries(this.
|
|
189
|
+
if (areEqualBinaries(this.options.localPeerDescriptor.nodeId, routedMessage.target)) {
|
|
190
190
|
// TODO this is also very similar case to what we do at line 255, could simplify the code paths?
|
|
191
191
|
this.sendResponse(
|
|
192
192
|
routedMessage.routingPath,
|
|
@@ -198,7 +198,7 @@ export class RecursiveOperationManager {
|
|
|
198
198
|
)
|
|
199
199
|
return createRouteMessageAck(routedMessage)
|
|
200
200
|
} else {
|
|
201
|
-
const ack = this.
|
|
201
|
+
const ack = this.options.router.doRouteMessage(routedMessage, RoutingMode.RECURSIVE, excludedPeer)
|
|
202
202
|
if ((ack.error === undefined) || (ack.error === RouteMessageError.NO_TARGETS)) {
|
|
203
203
|
const noCloserContactsFound = (ack.error === RouteMessageError.NO_TARGETS) ||
|
|
204
204
|
(
|
|
@@ -220,7 +220,7 @@ export class RecursiveOperationManager {
|
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
private getClosestConnectedNodes(referenceId: DhtAddress, limit: number): PeerDescriptor[] {
|
|
223
|
-
const connectedNodes = this.
|
|
223
|
+
const connectedNodes = this.options.connectionsView.getConnections().map((c) => this.options.createDhtNodeRpcRemote(c))
|
|
224
224
|
const sorted = new SortedContactList<DhtNodeRpcRemote>({
|
|
225
225
|
referenceId,
|
|
226
226
|
maxSize: limit,
|
|
@@ -233,7 +233,7 @@ export class RecursiveOperationManager {
|
|
|
233
233
|
private isPeerCloserToIdThanSelf(peer: PeerDescriptor, nodeIdOrDataKey: DhtAddress): boolean {
|
|
234
234
|
const nodeIdOrDataKeyRaw = getRawFromDhtAddress(nodeIdOrDataKey)
|
|
235
235
|
const distance1 = getDistance(peer.nodeId, nodeIdOrDataKeyRaw)
|
|
236
|
-
const distance2 = getDistance(this.
|
|
236
|
+
const distance2 = getDistance(this.options.localPeerDescriptor.nodeId, nodeIdOrDataKeyRaw)
|
|
237
237
|
return distance1 < distance2
|
|
238
238
|
}
|
|
239
239
|
|
|
@@ -7,7 +7,7 @@ import { getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
|
7
7
|
|
|
8
8
|
const logger = new Logger(module)
|
|
9
9
|
|
|
10
|
-
interface
|
|
10
|
+
interface RecursiveOperationRpcLocalOptions {
|
|
11
11
|
doRouteRequest: (routedMessage: RouteMessageWrapper) => RouteMessageAck
|
|
12
12
|
addContact: (contact: PeerDescriptor, setActive?: boolean) => void
|
|
13
13
|
isMostLikelyDuplicate: (requestId: string) => boolean
|
|
@@ -16,19 +16,19 @@ interface RecursiveOperationRpcLocalConfig {
|
|
|
16
16
|
|
|
17
17
|
export class RecursiveOperationRpcLocal implements IRecursiveOperationRpc {
|
|
18
18
|
|
|
19
|
-
private readonly
|
|
19
|
+
private readonly options: RecursiveOperationRpcLocalOptions
|
|
20
20
|
|
|
21
|
-
constructor(
|
|
22
|
-
this.
|
|
21
|
+
constructor(options: RecursiveOperationRpcLocalOptions) {
|
|
22
|
+
this.options = options
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
async routeRequest(routedMessage: RouteMessageWrapper): Promise<RouteMessageAck> {
|
|
26
|
-
if (this.
|
|
26
|
+
if (this.options.isMostLikelyDuplicate(routedMessage.requestId)) {
|
|
27
27
|
return createRouteMessageAck(routedMessage, RouteMessageError.DUPLICATE)
|
|
28
28
|
}
|
|
29
|
-
const
|
|
30
|
-
logger.trace(`Received routeRequest call from ${
|
|
31
|
-
this.
|
|
32
|
-
return this.
|
|
29
|
+
const remoteNodeId = getNodeIdFromPeerDescriptor(getPreviousPeer(routedMessage) ?? routedMessage.sourcePeer!)
|
|
30
|
+
logger.trace(`Received routeRequest call from ${remoteNodeId}`)
|
|
31
|
+
this.options.addToDuplicateDetector(routedMessage.requestId)
|
|
32
|
+
return this.options.doRouteRequest(routedMessage)
|
|
33
33
|
}
|
|
34
34
|
}
|
|
@@ -24,7 +24,7 @@ export interface RecursiveOperationSessionEvents {
|
|
|
24
24
|
completed: () => void
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export interface
|
|
27
|
+
export interface RecursiveOperationSessionOptions {
|
|
28
28
|
transport: ITransport
|
|
29
29
|
targetId: DhtAddress
|
|
30
30
|
localPeerDescriptor: PeerDescriptor
|
|
@@ -45,18 +45,18 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
45
45
|
private completionEventEmitted = false
|
|
46
46
|
private noCloserNodesReceivedCounter = 0
|
|
47
47
|
private readonly noCloserNodesReceivedFrom: Set<DhtAddress> = new Set()
|
|
48
|
-
private readonly
|
|
48
|
+
private readonly options: RecursiveOperationSessionOptions
|
|
49
49
|
|
|
50
|
-
constructor(
|
|
50
|
+
constructor(options: RecursiveOperationSessionOptions) {
|
|
51
51
|
super()
|
|
52
|
-
this.
|
|
52
|
+
this.options = options
|
|
53
53
|
this.results = new SortedContactList({
|
|
54
|
-
referenceId:
|
|
55
|
-
maxSize: 10, // TODO use
|
|
54
|
+
referenceId: options.targetId,
|
|
55
|
+
maxSize: 10, // TODO use options option or named constant?
|
|
56
56
|
allowToContainReferenceId: true
|
|
57
57
|
})
|
|
58
|
-
this.rpcCommunicator = new ListeningRpcCommunicator(this.id,
|
|
59
|
-
rpcRequestTimeout: 15000 // TODO use
|
|
58
|
+
this.rpcCommunicator = new ListeningRpcCommunicator(this.id, options.transport, {
|
|
59
|
+
rpcRequestTimeout: 15000 // TODO use options option or named constant?
|
|
60
60
|
})
|
|
61
61
|
this.registerLocalRpcMethods()
|
|
62
62
|
}
|
|
@@ -64,13 +64,13 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
64
64
|
private registerLocalRpcMethods() {
|
|
65
65
|
const rpcLocal = new RecursiveOperationSessionRpcLocal({
|
|
66
66
|
onResponseReceived: (
|
|
67
|
-
|
|
67
|
+
remoteNodeId: DhtAddress,
|
|
68
68
|
routingPath: PeerDescriptor[],
|
|
69
69
|
closestConnectedNodes: PeerDescriptor[],
|
|
70
70
|
dataEntries: DataEntry[],
|
|
71
71
|
noCloserNodesFound: boolean
|
|
72
72
|
) => {
|
|
73
|
-
this.onResponseReceived(
|
|
73
|
+
this.onResponseReceived(remoteNodeId, routingPath, closestConnectedNodes, dataEntries, noCloserNodesFound)
|
|
74
74
|
}
|
|
75
75
|
})
|
|
76
76
|
this.rpcCommunicator.registerRpcNotification(RecursiveOperationResponse, 'sendResponse',
|
|
@@ -79,13 +79,13 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
79
79
|
|
|
80
80
|
public start(serviceId: ServiceID): void {
|
|
81
81
|
const routeMessage = this.wrapRequest(serviceId)
|
|
82
|
-
this.
|
|
82
|
+
this.options.doRouteRequest(routeMessage)
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
private wrapRequest(serviceId: ServiceID): RouteMessageWrapper {
|
|
86
86
|
const request: RecursiveOperationRequest = {
|
|
87
87
|
sessionId: this.getId(),
|
|
88
|
-
operation: this.
|
|
88
|
+
operation: this.options.operation
|
|
89
89
|
}
|
|
90
90
|
const msg: Message = {
|
|
91
91
|
messageId: v4(),
|
|
@@ -98,8 +98,8 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
98
98
|
const routeMessage: RouteMessageWrapper = {
|
|
99
99
|
message: msg,
|
|
100
100
|
requestId: v4(),
|
|
101
|
-
target: getRawFromDhtAddress(this.
|
|
102
|
-
sourcePeer: this.
|
|
101
|
+
target: getRawFromDhtAddress(this.options.targetId),
|
|
102
|
+
sourcePeer: this.options.localPeerDescriptor,
|
|
103
103
|
reachableThrough: [],
|
|
104
104
|
routingPath: [],
|
|
105
105
|
parallelRootNodeIds: []
|
|
@@ -113,10 +113,10 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
113
113
|
unreportedHops.delete(id)
|
|
114
114
|
})
|
|
115
115
|
if (this.noCloserNodesReceivedCounter >= 1 && unreportedHops.size === 0) {
|
|
116
|
-
if (this.
|
|
117
|
-
&& (this.hasNonStaleData() || this.noCloserNodesReceivedCounter >= this.
|
|
116
|
+
if (this.options.operation === RecursiveOperation.FETCH_DATA
|
|
117
|
+
&& (this.hasNonStaleData() || this.noCloserNodesReceivedCounter >= this.options.waitedRoutingPathCompletions)) {
|
|
118
118
|
return true
|
|
119
|
-
} else if (this.
|
|
119
|
+
} else if (this.options.operation === RecursiveOperation.FETCH_DATA) {
|
|
120
120
|
return false
|
|
121
121
|
}
|
|
122
122
|
return true
|
|
@@ -129,7 +129,7 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
public onResponseReceived(
|
|
132
|
-
|
|
132
|
+
remoteNodeId: DhtAddress,
|
|
133
133
|
routingPath: PeerDescriptor[],
|
|
134
134
|
closestConnectedNodes: PeerDescriptor[],
|
|
135
135
|
dataEntries: DataEntry[],
|
|
@@ -143,13 +143,13 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
143
143
|
this.results.addContact(new Contact(descriptor))
|
|
144
144
|
})
|
|
145
145
|
this.processFoundData(dataEntries)
|
|
146
|
-
if (noCloserNodesFound || this.noCloserNodesReceivedFrom.has(
|
|
147
|
-
this.onNoCloserPeersFound(
|
|
146
|
+
if (noCloserNodesFound || this.noCloserNodesReceivedFrom.has(remoteNodeId)) {
|
|
147
|
+
this.onNoCloserPeersFound(remoteNodeId)
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
private addKnownHops(routingPath: PeerDescriptor[]) {
|
|
152
|
-
const localNodeId = getNodeIdFromPeerDescriptor(this.
|
|
152
|
+
const localNodeId = getNodeIdFromPeerDescriptor(this.options.localPeerDescriptor)
|
|
153
153
|
routingPath.forEach((desc) => {
|
|
154
154
|
const newNodeId = getNodeIdFromPeerDescriptor(desc)
|
|
155
155
|
if (localNodeId !== newNodeId) {
|
|
@@ -159,7 +159,7 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
private setHopAsReported(desc: PeerDescriptor) {
|
|
162
|
-
const localNodeId = getNodeIdFromPeerDescriptor(this.
|
|
162
|
+
const localNodeId = getNodeIdFromPeerDescriptor(this.options.localPeerDescriptor)
|
|
163
163
|
const newNodeId = getNodeIdFromPeerDescriptor(desc)
|
|
164
164
|
if (localNodeId !== newNodeId) {
|
|
165
165
|
this.reportedHops.add(newNodeId)
|
|
@@ -187,9 +187,9 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
187
187
|
})
|
|
188
188
|
}
|
|
189
189
|
|
|
190
|
-
private onNoCloserPeersFound(
|
|
190
|
+
private onNoCloserPeersFound(remoteNodeId: DhtAddress): void {
|
|
191
191
|
this.noCloserNodesReceivedCounter += 1
|
|
192
|
-
this.noCloserNodesReceivedFrom.add(
|
|
192
|
+
this.noCloserNodesReceivedFrom.add(remoteNodeId)
|
|
193
193
|
if (this.isCompleted()) {
|
|
194
194
|
this.emit('completed')
|
|
195
195
|
this.completionEventEmitted = true
|
|
@@ -203,7 +203,7 @@ export class RecursiveOperationSession extends EventEmitter<RecursiveOperationSe
|
|
|
203
203
|
this.emit('completed')
|
|
204
204
|
this.completionEventEmitted = true
|
|
205
205
|
}
|
|
206
|
-
}, 4000) // TODO use
|
|
206
|
+
}, 4000) // TODO use options option or named constant?
|
|
207
207
|
}
|
|
208
208
|
}
|
|
209
209
|
|
|
@@ -8,9 +8,9 @@ import { DhtAddress, getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
|
8
8
|
|
|
9
9
|
const logger = new Logger(module)
|
|
10
10
|
|
|
11
|
-
interface
|
|
11
|
+
interface RecursiveOperationSessionRpcLocalOptions {
|
|
12
12
|
onResponseReceived: (
|
|
13
|
-
|
|
13
|
+
remoteNodeId: DhtAddress,
|
|
14
14
|
routingPath: PeerDescriptor[],
|
|
15
15
|
nodes: PeerDescriptor[],
|
|
16
16
|
dataEntries: DataEntry[],
|
|
@@ -20,16 +20,16 @@ interface RecursiveOperationSessionRpcLocalConfig {
|
|
|
20
20
|
|
|
21
21
|
export class RecursiveOperationSessionRpcLocal implements IRecursiveOperationSessionRpc {
|
|
22
22
|
|
|
23
|
-
private readonly
|
|
23
|
+
private readonly options: RecursiveOperationSessionRpcLocalOptions
|
|
24
24
|
|
|
25
|
-
constructor(
|
|
26
|
-
this.
|
|
25
|
+
constructor(options: RecursiveOperationSessionRpcLocalOptions) {
|
|
26
|
+
this.options = options
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
async sendResponse(report: RecursiveOperationResponse, context: ServerCallContext): Promise<Empty> {
|
|
30
|
-
const
|
|
30
|
+
const remoteNodeId = getNodeIdFromPeerDescriptor((context as DhtCallContext).incomingSourceDescriptor!)
|
|
31
31
|
logger.trace('RecursiveOperationResponse arrived: ' + JSON.stringify(report))
|
|
32
|
-
this.
|
|
32
|
+
this.options.onResponseReceived(remoteNodeId, report.routingPath, report.closestConnectedNodes, report.dataEntries, report.noCloserNodesFound)
|
|
33
33
|
return {}
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -8,7 +8,7 @@ import { RouterRpcLocal, createRouteMessageAck } from './RouterRpcLocal'
|
|
|
8
8
|
import { DhtAddress, areEqualPeerDescriptors, getDhtAddressFromRaw, getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
9
9
|
import { RoutingTablesCache } from './RoutingTablesCache'
|
|
10
10
|
|
|
11
|
-
export interface
|
|
11
|
+
export interface RouterOptions {
|
|
12
12
|
rpcCommunicator: RoutingRpcCommunicator
|
|
13
13
|
localPeerDescriptor: PeerDescriptor
|
|
14
14
|
handleMessage: (message: Message) => void
|
|
@@ -27,13 +27,13 @@ export class Router {
|
|
|
27
27
|
private readonly forwardingTable: Map<DhtAddress, ForwardingTableEntry> = new Map()
|
|
28
28
|
private readonly routingTablesCache = new RoutingTablesCache()
|
|
29
29
|
private ongoingRoutingSessions: Map<string, RoutingSession> = new Map()
|
|
30
|
-
// TODO use
|
|
30
|
+
// TODO use options option or named constant?
|
|
31
31
|
private readonly duplicateRequestDetector: DuplicateDetector = new DuplicateDetector(10000)
|
|
32
32
|
private stopped = false
|
|
33
|
-
private readonly
|
|
33
|
+
private readonly options: RouterOptions
|
|
34
34
|
|
|
35
|
-
constructor(
|
|
36
|
-
this.
|
|
35
|
+
constructor(options: RouterOptions) {
|
|
36
|
+
this.options = options
|
|
37
37
|
this.registerLocalRpcMethods()
|
|
38
38
|
}
|
|
39
39
|
|
|
@@ -42,10 +42,10 @@ export class Router {
|
|
|
42
42
|
doRouteMessage: (routedMessage: RouteMessageWrapper, mode?: RoutingMode) => this.doRouteMessage(routedMessage, mode),
|
|
43
43
|
setForwardingEntries: (routedMessage: RouteMessageWrapper) => this.setForwardingEntries(routedMessage),
|
|
44
44
|
duplicateRequestDetector: this.duplicateRequestDetector,
|
|
45
|
-
localPeerDescriptor: this.
|
|
46
|
-
handleMessage: this.
|
|
45
|
+
localPeerDescriptor: this.options.localPeerDescriptor,
|
|
46
|
+
handleMessage: this.options.handleMessage
|
|
47
47
|
})
|
|
48
|
-
this.
|
|
48
|
+
this.options.rpcCommunicator.registerRpcMethod(
|
|
49
49
|
RouteMessageWrapper,
|
|
50
50
|
RouteMessageAck,
|
|
51
51
|
'routeMessage',
|
|
@@ -56,7 +56,7 @@ export class Router {
|
|
|
56
56
|
return rpcLocal.routeMessage(routedMessage)
|
|
57
57
|
}
|
|
58
58
|
)
|
|
59
|
-
this.
|
|
59
|
+
this.options.rpcCommunicator.registerRpcMethod(
|
|
60
60
|
RouteMessageWrapper,
|
|
61
61
|
RouteMessageAck,
|
|
62
62
|
'forwardMessage',
|
|
@@ -71,7 +71,7 @@ export class Router {
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
public send(msg: Message, reachableThrough: PeerDescriptor[]): void {
|
|
74
|
-
msg.sourceDescriptor = this.
|
|
74
|
+
msg.sourceDescriptor = this.options.localPeerDescriptor
|
|
75
75
|
const targetPeerDescriptor = msg.targetDescriptor!
|
|
76
76
|
const forwardingEntry = this.forwardingTable.get(getNodeIdFromPeerDescriptor(targetPeerDescriptor))
|
|
77
77
|
if (forwardingEntry && forwardingEntry.peerDescriptors.length > 0) {
|
|
@@ -79,7 +79,7 @@ export class Router {
|
|
|
79
79
|
message: msg,
|
|
80
80
|
requestId: v4(),
|
|
81
81
|
target: forwardingEntry.peerDescriptors[0].nodeId,
|
|
82
|
-
sourcePeer: this.
|
|
82
|
+
sourcePeer: this.options.localPeerDescriptor,
|
|
83
83
|
reachableThrough,
|
|
84
84
|
routingPath: [],
|
|
85
85
|
parallelRootNodeIds: []
|
|
@@ -95,7 +95,7 @@ export class Router {
|
|
|
95
95
|
message: msg,
|
|
96
96
|
requestId: v4(),
|
|
97
97
|
target: targetPeerDescriptor.nodeId,
|
|
98
|
-
sourcePeer: this.
|
|
98
|
+
sourcePeer: this.options.localPeerDescriptor,
|
|
99
99
|
reachableThrough,
|
|
100
100
|
routingPath: [],
|
|
101
101
|
parallelRootNodeIds: []
|
|
@@ -132,7 +132,7 @@ export class Router {
|
|
|
132
132
|
})
|
|
133
133
|
setImmediate(async () => {
|
|
134
134
|
try {
|
|
135
|
-
// TODO use
|
|
135
|
+
// TODO use options option or named constant?
|
|
136
136
|
await withTimeout(eventReceived, 10000)
|
|
137
137
|
logger.trace('raceEvents ended from routingSession: ' + session.sessionId)
|
|
138
138
|
} catch (e) {
|
|
@@ -158,15 +158,15 @@ export class Router {
|
|
|
158
158
|
excludedNodeIds.add(nodeId as DhtAddress)
|
|
159
159
|
})
|
|
160
160
|
return new RoutingSession({
|
|
161
|
-
rpcCommunicator: this.
|
|
162
|
-
localPeerDescriptor: this.
|
|
161
|
+
rpcCommunicator: this.options.rpcCommunicator,
|
|
162
|
+
localPeerDescriptor: this.options.localPeerDescriptor,
|
|
163
163
|
routedMessage,
|
|
164
|
-
// TODO use
|
|
165
|
-
parallelism: areEqualPeerDescriptors(this.
|
|
164
|
+
// TODO use options option or named constant?
|
|
165
|
+
parallelism: areEqualPeerDescriptors(this.options.localPeerDescriptor, routedMessage.sourcePeer!) ? 2 : 1,
|
|
166
166
|
mode,
|
|
167
167
|
excludedNodeIds,
|
|
168
168
|
routingTablesCache: this.routingTablesCache,
|
|
169
|
-
getConnections: this.
|
|
169
|
+
getConnections: this.options.getConnections
|
|
170
170
|
})
|
|
171
171
|
}
|
|
172
172
|
|
|
@@ -187,7 +187,7 @@ export class Router {
|
|
|
187
187
|
}
|
|
188
188
|
|
|
189
189
|
onNodeConnected(peerDescriptor: PeerDescriptor): void {
|
|
190
|
-
const remote = new RoutingRemoteContact(peerDescriptor, this.
|
|
190
|
+
const remote = new RoutingRemoteContact(peerDescriptor, this.options.localPeerDescriptor, this.options.rpcCommunicator)
|
|
191
191
|
this.routingTablesCache.onNodeConnected(remote)
|
|
192
192
|
}
|
|
193
193
|
|
|
@@ -215,7 +215,7 @@ export class Router {
|
|
|
215
215
|
|
|
216
216
|
private setForwardingEntries(routedMessage: RouteMessageWrapper): void {
|
|
217
217
|
const reachableThroughWithoutSelf = routedMessage.reachableThrough.filter((peer) => {
|
|
218
|
-
return !areEqualPeerDescriptors(peer, this.
|
|
218
|
+
return !areEqualPeerDescriptors(peer, this.options.localPeerDescriptor)
|
|
219
219
|
})
|
|
220
220
|
|
|
221
221
|
if (reachableThroughWithoutSelf.length > 0) {
|
|
@@ -229,7 +229,7 @@ export class Router {
|
|
|
229
229
|
peerDescriptors: reachableThroughWithoutSelf,
|
|
230
230
|
timeout: setTimeout(() => {
|
|
231
231
|
this.forwardingTable.delete(sourceNodeId)
|
|
232
|
-
}, 10000) // TODO use
|
|
232
|
+
}, 10000) // TODO use options option or named constant?
|
|
233
233
|
}
|
|
234
234
|
this.forwardingTable.set(sourceNodeId, forwardingEntry)
|
|
235
235
|
}
|