@dxos/network-manager 0.1.20 → 0.1.21
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/lib/browser/index.mjs +279 -116
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +288 -116
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node/index.cjs +274 -107
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +283 -107
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/types/src/network-manager.d.ts +6 -0
- package/dist/types/src/network-manager.d.ts.map +1 -1
- package/dist/types/src/swarm/peer.d.ts.map +1 -1
- package/dist/types/src/swarm/swarm.d.ts +4 -2
- package/dist/types/src/swarm/swarm.d.ts.map +1 -1
- package/dist/types/src/testing/test-builder.d.ts +2 -0
- package/dist/types/src/testing/test-builder.d.ts.map +1 -1
- package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
- package/dist/types/src/transport/webrtc-transport-proxy.d.ts +2 -1
- package/dist/types/src/transport/webrtc-transport-proxy.d.ts.map +1 -1
- package/dist/types/src/transport/webrtc-transport.d.ts +1 -0
- package/dist/types/src/transport/webrtc-transport.d.ts.map +1 -1
- package/package.json +16 -16
- package/src/network-manager.ts +44 -3
- package/src/signal/integration.test.ts +2 -0
- package/src/signal/message-router.test.ts +1 -1
- package/src/swarm/peer.ts +2 -1
- package/src/swarm/swarm.test.ts +1 -1
- package/src/swarm/swarm.ts +86 -62
- package/src/testing/test-builder.ts +9 -0
- package/src/tests/basic-test-suite.ts +31 -4
- package/src/tests/webrtc-transport.test.ts +3 -0
- package/src/transport/webrtc-transport-proxy.test.ts +2 -1
- package/src/transport/webrtc-transport-proxy.ts +9 -3
- package/src/transport/webrtc-transport.ts +6 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Event } from '@dxos/async';
|
|
2
2
|
import { PublicKey } from '@dxos/keys';
|
|
3
3
|
import { SignalManager } from '@dxos/messaging';
|
|
4
|
+
import { ConnectionState } from '@dxos/protocols/proto/dxos/client/services';
|
|
4
5
|
import { ConnectionLog } from './connection-log';
|
|
5
6
|
import { Swarm, SwarmMapper } from './swarm';
|
|
6
7
|
import { Topology } from './topology';
|
|
@@ -50,14 +51,18 @@ export declare class NetworkManager {
|
|
|
50
51
|
private readonly _signalManager;
|
|
51
52
|
private readonly _messenger;
|
|
52
53
|
private readonly _signalConnection;
|
|
54
|
+
private _connectionState;
|
|
55
|
+
readonly connectionStateChanged: Event<ConnectionState>;
|
|
53
56
|
private readonly _connectionLog?;
|
|
54
57
|
readonly topicsUpdated: Event<void>;
|
|
55
58
|
constructor({ transportFactory, signalManager, log }: NetworkManagerOptions);
|
|
56
59
|
get connectionLog(): ConnectionLog | undefined;
|
|
57
60
|
get signalManager(): SignalManager;
|
|
61
|
+
get connectionState(): ConnectionState;
|
|
58
62
|
get topics(): PublicKey[];
|
|
59
63
|
getSwarmMap(topic: PublicKey): SwarmMapper | undefined;
|
|
60
64
|
getSwarm(topic: PublicKey): Swarm | undefined;
|
|
65
|
+
open(): Promise<void>;
|
|
61
66
|
close(): Promise<void>;
|
|
62
67
|
/**
|
|
63
68
|
* Join the swarm.
|
|
@@ -67,5 +72,6 @@ export declare class NetworkManager {
|
|
|
67
72
|
* Close the connection.
|
|
68
73
|
*/
|
|
69
74
|
leaveSwarm(topic: PublicKey): Promise<void>;
|
|
75
|
+
setConnectionState(state: ConnectionState): Promise<void>;
|
|
70
76
|
}
|
|
71
77
|
//# sourceMappingURL=network-manager.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"network-manager.d.ts","sourceRoot":"","sources":["../../../src/network-manager.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,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,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC;IA2CrD;;OAEG;IACG,kBAAkB;IAsCxB;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAmDnB,eAAe;IAcf,QAAQ,CAAC,OAAO,EAAE,aAAa;IAS/B,OAAO;CAMd"}
|
|
@@ -40,11 +40,13 @@ export declare class Swarm {
|
|
|
40
40
|
get topic(): Topic;
|
|
41
41
|
destroy(): Promise<void>;
|
|
42
42
|
setTopology(topology: Topology): Promise<void>;
|
|
43
|
-
private _getOrCreatePeer;
|
|
44
|
-
private _destroyPeer;
|
|
45
43
|
onSwarmEvent(swarmEvent: SwarmEvent): void;
|
|
46
44
|
onOffer(message: OfferMessage): Promise<Answer>;
|
|
47
45
|
onSignal(message: SignalMessage): Promise<void>;
|
|
46
|
+
goOffline(): Promise<void>;
|
|
47
|
+
goOnline(): Promise<void>;
|
|
48
|
+
private _getOrCreatePeer;
|
|
49
|
+
private _destroyPeer;
|
|
48
50
|
private _getSwarmController;
|
|
49
51
|
/**
|
|
50
52
|
* Creates a connection then sends message over signal network.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"swarm.d.ts","sourceRoot":"","sources":["../../../../src/swarm/swarm.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,wCAAwC,CAAC;AACpE,OAAO,EAAE,MAAM,EAAE,MAAM,uCAAuC,CAAC;AAG/D,OAAO,EAAiB,YAAY,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AACvE,OAAO,EAAmB,QAAQ,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MA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AAM,cAAqB;gBAMjB,MAAM,EAAE,SAAS,EACjB,UAAU,EAAE,SAAS,EAC9B,SAAS,EAAE,QAAQ,EACV,iBAAiB,EAAE,oBAAoB,EACvC,UAAU,EAAE,SAAS,EACrB,iBAAiB,EAAE,gBAAgB,EACnC,MAAM,EAAE,MAAM,GAAG,SAAS;IAqB7C,IAAI,WAAW,iBAId;IAED,IACI,SAAS,cAEZ;IAED;;OAEG;IACH,IAAI,KAAK,IAAI,MAAM,GAAG,SAAS,CAE9B;IAED,IACI,KAAK,IAAI,KAAK,CAEjB;IAGK,OAAO;IAQP,WAAW,CAAC,QAAQ,EAAE,QAAQ;IAiBpC,YAAY,CAAC,UAAU,EAAE,UAAU;IA6B7B,OAAO,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC;IAwB/C,QAAQ,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAkB/C,SAAS;IAUT,QAAQ;IASd,OAAO,CAAC,gBAAgB;YA8CV,YAAY;IAM1B,OAAO,CAAC,mBAAmB;IAuC3B;;OAEG;YACW,mBAAmB;YAwBnB,gBAAgB;CAQ/B"}
|
|
@@ -33,6 +33,8 @@ export declare class TestPeer {
|
|
|
33
33
|
close(): Promise<void>;
|
|
34
34
|
getSwarm(topic: PublicKey): TestSwarmConnection;
|
|
35
35
|
createSwarm(topic: PublicKey): TestSwarmConnection;
|
|
36
|
+
goOffline(): Promise<void>;
|
|
37
|
+
goOnline(): Promise<void>;
|
|
36
38
|
}
|
|
37
39
|
export declare class TestSwarmConnection {
|
|
38
40
|
readonly peer: TestPeer;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-builder.d.ts","sourceRoot":"","sources":["../../../../src/testing/test-builder.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,mBAAmB,EAA8B,sBAAsB,EAAE,MAAM,iBAAiB,CAA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AAC;gBAEN,IAAI,EAAE,QAAQ,EAAW,KAAK,EAAE,SAAS;IAQxD,IAAI,CAAC,QAAQ,yBAA+B;IAW5C,KAAK;CAIZ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"basic-test-suite.d.ts","sourceRoot":"","sources":["../../../../src/tests/basic-test-suite.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAMzC;;GAEG;AACH,eAAO,MAAM,cAAc,gBAAiB,WAAW,
|
|
1
|
+
{"version":3,"file":"basic-test-suite.d.ts","sourceRoot":"","sources":["../../../../src/tests/basic-test-suite.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAMzC;;GAEG;AACH,eAAO,MAAM,cAAc,gBAAiB,WAAW,6BAuItD,CAAC"}
|
|
@@ -12,12 +12,13 @@ export declare type WebRTCTransportProxyParams = {
|
|
|
12
12
|
};
|
|
13
13
|
export declare class WebRTCTransportProxy implements Transport {
|
|
14
14
|
private readonly _params;
|
|
15
|
+
private readonly _proxyId;
|
|
16
|
+
private readonly _ctx;
|
|
15
17
|
readonly closed: Event<void>;
|
|
16
18
|
private _closed;
|
|
17
19
|
readonly connected: Event<void>;
|
|
18
20
|
readonly errors: ErrorStream;
|
|
19
21
|
private _serviceStream;
|
|
20
|
-
private readonly _proxyId;
|
|
21
22
|
constructor(_params: WebRTCTransportProxyParams);
|
|
22
23
|
private _handleConnection;
|
|
23
24
|
private _handleData;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webrtc-transport-proxy.d.ts","sourceRoot":"","sources":["../../../../src/transport/webrtc-transport-proxy.ts"],"names":[],"mappings":";AAMA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"webrtc-transport-proxy.d.ts","sourceRoot":"","sources":["../../../../src/transport/webrtc-transport-proxy.ts"],"names":[],"mappings":";AAMA,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,EAAE,gBAAgB,GAAG,SAAS;CAatD"}
|
|
@@ -15,6 +15,7 @@ export declare type WebRTCTransportParams = {
|
|
|
15
15
|
export declare class WebRTCTransport implements Transport {
|
|
16
16
|
private readonly params;
|
|
17
17
|
private readonly _peer;
|
|
18
|
+
private _closed;
|
|
18
19
|
readonly closed: Event<void>;
|
|
19
20
|
readonly connected: Event<void>;
|
|
20
21
|
readonly errors: ErrorStream;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webrtc-transport.d.ts","sourceRoot":"","sources":["../../../../src/transport/webrtc-transport.ts"],"names":[],"mappings":";AAOA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,WAAW,EAAS,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,MAAM,EAAE,MAAM,uCAAuC,CAAC;AAE/D,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG1D,oBAAY,qBAAqB,GAAG;IAClC,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC,eAAe,CAAC;IAC/B,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/C,CAAC;AAEF;;GAEG;AACH,qBAAa,eAAgB,YAAW,SAAS;
|
|
1
|
+
{"version":3,"file":"webrtc-transport.d.ts","sourceRoot":"","sources":["../../../../src/transport/webrtc-transport.ts"],"names":[],"mappings":";AAOA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,WAAW,EAAS,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,MAAM,EAAE,MAAM,uCAAuC,CAAC;AAE/D,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG1D,oBAAY,qBAAqB,GAAG;IAClC,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC,eAAe,CAAC;IAC/B,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/C,CAAC;AAEF;;GAEG;AACH,qBAAa,eAAgB,YAAW,SAAS;IAQnC,OAAO,CAAC,QAAQ,CAAC,MAAM;IAPnC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAa;IACnC,OAAO,CAAC,OAAO,CAAS;IAExB,QAAQ,CAAC,MAAM,cAAe;IAC9B,QAAQ,CAAC,SAAS,cAAe;IACjC,QAAQ,CAAC,MAAM,cAAqB;gBAEP,MAAM,EAAE,qBAAqB;IA+BpD,OAAO;IASb,MAAM,CAAC,MAAM,EAAE,MAAM;YASP,kBAAkB;CAIjC;AAGD,eAAO,MAAM,4BAA4B,kBAAmB,GAAG,KAAG,gBAMhE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/network-manager",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.21",
|
|
4
4
|
"description": "Network Manager",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -25,20 +25,20 @@
|
|
|
25
25
|
"simple-peer": "9.11.0",
|
|
26
26
|
"ws": "^7.4.4",
|
|
27
27
|
"xor-distance": "^2.0.0",
|
|
28
|
-
"@dxos/async": "0.1.
|
|
29
|
-
"@dxos/codec-protobuf": "0.1.
|
|
30
|
-
"@dxos/context": "0.1.
|
|
31
|
-
"@dxos/credentials": "0.1.
|
|
32
|
-
"@dxos/crypto": "0.1.
|
|
33
|
-
"@dxos/debug": "0.1.
|
|
34
|
-
"@dxos/keys": "0.1.
|
|
35
|
-
"@dxos/log": "0.1.
|
|
36
|
-
"@dxos/messaging": "0.1.
|
|
37
|
-
"@dxos/node-std": "0.1.
|
|
38
|
-
"@dxos/protocols": "0.1.
|
|
39
|
-
"@dxos/rpc": "0.1.
|
|
40
|
-
"@dxos/teleport": "0.1.
|
|
41
|
-
"@dxos/util": "0.1.
|
|
28
|
+
"@dxos/async": "0.1.21",
|
|
29
|
+
"@dxos/codec-protobuf": "0.1.21",
|
|
30
|
+
"@dxos/context": "0.1.21",
|
|
31
|
+
"@dxos/credentials": "0.1.21",
|
|
32
|
+
"@dxos/crypto": "0.1.21",
|
|
33
|
+
"@dxos/debug": "0.1.21",
|
|
34
|
+
"@dxos/keys": "0.1.21",
|
|
35
|
+
"@dxos/log": "0.1.21",
|
|
36
|
+
"@dxos/messaging": "0.1.21",
|
|
37
|
+
"@dxos/node-std": "0.1.21",
|
|
38
|
+
"@dxos/protocols": "0.1.21",
|
|
39
|
+
"@dxos/rpc": "0.1.21",
|
|
40
|
+
"@dxos/teleport": "0.1.21",
|
|
41
|
+
"@dxos/util": "0.1.21"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@swc-node/register": "^1.5.4",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"earljs": "~0.1.10",
|
|
49
49
|
"fast-check": "~3.3.0",
|
|
50
50
|
"typescript": "^4.8.4",
|
|
51
|
-
"@dxos/signal": "0.1.
|
|
51
|
+
"@dxos/signal": "0.1.21"
|
|
52
52
|
},
|
|
53
53
|
"publishConfig": {
|
|
54
54
|
"access": "public"
|
package/src/network-manager.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { Event } from '@dxos/async';
|
|
|
8
8
|
import { PublicKey } from '@dxos/keys';
|
|
9
9
|
import { log } from '@dxos/log';
|
|
10
10
|
import { Messenger, SignalManager } from '@dxos/messaging';
|
|
11
|
+
import { ConnectionState } from '@dxos/protocols/proto/dxos/client/services';
|
|
11
12
|
import { ComplexMap } from '@dxos/util';
|
|
12
13
|
|
|
13
14
|
import { ConnectionLog } from './connection-log';
|
|
@@ -71,6 +72,10 @@ export class NetworkManager {
|
|
|
71
72
|
private readonly _signalManager: SignalManager;
|
|
72
73
|
private readonly _messenger: Messenger;
|
|
73
74
|
private readonly _signalConnection: SignalConnection;
|
|
75
|
+
|
|
76
|
+
private _connectionState = ConnectionState.ONLINE;
|
|
77
|
+
public readonly connectionStateChanged = new Event<ConnectionState>();
|
|
78
|
+
|
|
74
79
|
private readonly _connectionLog?: ConnectionLog;
|
|
75
80
|
|
|
76
81
|
public readonly topicsUpdated = new Event<void>();
|
|
@@ -103,6 +108,10 @@ export class NetworkManager {
|
|
|
103
108
|
return this._signalManager;
|
|
104
109
|
}
|
|
105
110
|
|
|
111
|
+
get connectionState() {
|
|
112
|
+
return this._connectionState;
|
|
113
|
+
}
|
|
114
|
+
|
|
106
115
|
// TODO(burdon): Reconcile with "discovery_key".
|
|
107
116
|
get topics() {
|
|
108
117
|
return Array.from(this._swarms.keys());
|
|
@@ -116,6 +125,11 @@ export class NetworkManager {
|
|
|
116
125
|
return this._swarms.get(topic);
|
|
117
126
|
}
|
|
118
127
|
|
|
128
|
+
async open() {
|
|
129
|
+
await this._messenger.open();
|
|
130
|
+
await this._signalManager.open();
|
|
131
|
+
}
|
|
132
|
+
|
|
119
133
|
async close() {
|
|
120
134
|
for (const topic of this._swarms.keys()) {
|
|
121
135
|
await this.leaveSwarm(topic).catch((err) => {
|
|
@@ -123,13 +137,13 @@ export class NetworkManager {
|
|
|
123
137
|
});
|
|
124
138
|
}
|
|
125
139
|
|
|
126
|
-
await this.
|
|
140
|
+
await this._messenger.close();
|
|
141
|
+
await this._signalManager.close();
|
|
127
142
|
}
|
|
128
143
|
|
|
129
144
|
/**
|
|
130
145
|
* Join the swarm.
|
|
131
146
|
*/
|
|
132
|
-
// TODO(burdon): Join/Open? Swarm abstraction?
|
|
133
147
|
async joinSwarm({
|
|
134
148
|
topic,
|
|
135
149
|
peerId,
|
|
@@ -169,7 +183,7 @@ export class NetworkManager {
|
|
|
169
183
|
*/
|
|
170
184
|
async leaveSwarm(topic: PublicKey) {
|
|
171
185
|
if (!this._swarms.has(topic)) {
|
|
172
|
-
log.warn('swarm not open', { topic: PublicKey.from(topic) });
|
|
186
|
+
log.warn('swarm not open', { topic: PublicKey.from(topic).truncate() });
|
|
173
187
|
return;
|
|
174
188
|
}
|
|
175
189
|
|
|
@@ -189,4 +203,31 @@ export class NetworkManager {
|
|
|
189
203
|
await this.topicsUpdated.emit();
|
|
190
204
|
log('left', { topic: PublicKey.from(topic), count: this._swarms.size });
|
|
191
205
|
}
|
|
206
|
+
|
|
207
|
+
async setConnectionState(state: ConnectionState) {
|
|
208
|
+
if (state === this._connectionState) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
switch (state) {
|
|
213
|
+
case ConnectionState.OFFLINE: {
|
|
214
|
+
this._connectionState = state;
|
|
215
|
+
// go offline
|
|
216
|
+
await this._messenger.close();
|
|
217
|
+
await this._signalManager.close();
|
|
218
|
+
await Promise.all([...this._swarms.values()].map((swarm) => swarm.goOffline()));
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
case ConnectionState.ONLINE: {
|
|
222
|
+
this._connectionState = state;
|
|
223
|
+
// go online
|
|
224
|
+
this._messenger.open();
|
|
225
|
+
await this._signalManager.open();
|
|
226
|
+
await Promise.all([...this._swarms.values()].map((swarm) => swarm.goOnline()));
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
this.connectionStateChanged.emit(this._connectionState);
|
|
232
|
+
}
|
|
192
233
|
}
|
|
@@ -65,6 +65,8 @@ describe('Signal Integration Test', () => {
|
|
|
65
65
|
|
|
66
66
|
await peerNetworking1.signalManager.join({ topic, peerId: peer1 });
|
|
67
67
|
await peerNetworking2.signalManager.join({ topic, peerId: peer2 });
|
|
68
|
+
await peerNetworking1.signalManager.subscribeMessages(peer1);
|
|
69
|
+
await peerNetworking2.signalManager.subscribeMessages(peer2);
|
|
68
70
|
|
|
69
71
|
await promise1;
|
|
70
72
|
await promise2;
|
|
@@ -47,7 +47,7 @@ describe('MessageRouter', () => {
|
|
|
47
47
|
}) => {
|
|
48
48
|
const peerId = PublicKey.random();
|
|
49
49
|
const signalManager = new WebsocketSignalManager([signalApiUrl]);
|
|
50
|
-
afterTest(() => signalManager.
|
|
50
|
+
afterTest(() => signalManager.close());
|
|
51
51
|
|
|
52
52
|
const messenger = new Messenger({ signalManager });
|
|
53
53
|
await messenger.listen({
|
package/src/swarm/peer.ts
CHANGED
|
@@ -80,7 +80,8 @@ export class Peer {
|
|
|
80
80
|
if (this.connection || this.initiating) {
|
|
81
81
|
// Peer with the highest Id closes its connection, and accepts remote peer's offer.
|
|
82
82
|
if (remoteId.toHex() < this.localPeerId.toHex()) {
|
|
83
|
-
|
|
83
|
+
// TODO(burdon): Too verbose.
|
|
84
|
+
log("closing local connection and accepting remote peer's offer", {
|
|
84
85
|
id: this.id,
|
|
85
86
|
topic: this.topic,
|
|
86
87
|
peerId: this.localPeerId
|
package/src/swarm/swarm.test.ts
CHANGED
|
@@ -23,7 +23,7 @@ describe('Swarm', () => {
|
|
|
23
23
|
|
|
24
24
|
beforeEach(() => {
|
|
25
25
|
signalManager = new MemorySignalManager(context);
|
|
26
|
-
afterTest(() => signalManager.
|
|
26
|
+
afterTest(() => signalManager.close());
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
const setupSwarm = ({ topic, peerId }: { topic: PublicKey; peerId: PublicKey }) => {
|
package/src/swarm/swarm.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import assert from 'node:assert';
|
|
6
6
|
|
|
7
|
-
import { Event, scheduleTask, sleep } from '@dxos/async';
|
|
7
|
+
import { Event, scheduleTask, sleep, synchronized } from '@dxos/async';
|
|
8
8
|
import { Context } from '@dxos/context';
|
|
9
9
|
import { ErrorStream } from '@dxos/debug';
|
|
10
10
|
import { PublicKey } from '@dxos/keys';
|
|
@@ -19,7 +19,7 @@ import { SwarmController, Topology } from '../topology';
|
|
|
19
19
|
import { TransportFactory } from '../transport';
|
|
20
20
|
import { Topic } from '../types';
|
|
21
21
|
import { WireProtocolProvider } from '../wire-protocol';
|
|
22
|
-
import { Connection } from './connection';
|
|
22
|
+
import { Connection, ConnectionState } from './connection';
|
|
23
23
|
import { Peer } from './peer';
|
|
24
24
|
|
|
25
25
|
const INITIATION_DELAY = 100;
|
|
@@ -129,6 +129,7 @@ export class Swarm {
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
async setTopology(topology: Topology) {
|
|
132
|
+
assert(!this._ctx.disposed, 'Swarm is offline');
|
|
132
133
|
if (topology === this._topology) {
|
|
133
134
|
return;
|
|
134
135
|
}
|
|
@@ -143,66 +144,15 @@ export class Swarm {
|
|
|
143
144
|
this._topology.update();
|
|
144
145
|
}
|
|
145
146
|
|
|
146
|
-
|
|
147
|
-
let peer = this._peers.get(peerId);
|
|
148
|
-
if (!peer) {
|
|
149
|
-
peer = new Peer(
|
|
150
|
-
peerId,
|
|
151
|
-
this._topic,
|
|
152
|
-
this._ownPeerId,
|
|
153
|
-
this._swarmMessenger,
|
|
154
|
-
this._protocolProvider,
|
|
155
|
-
this._transportFactory,
|
|
156
|
-
{
|
|
157
|
-
onInitiated: (connection) => {
|
|
158
|
-
this.connectionAdded.emit(connection);
|
|
159
|
-
},
|
|
160
|
-
onConnected: () => {
|
|
161
|
-
this.connected.emit(peerId);
|
|
162
|
-
},
|
|
163
|
-
onDisconnected: async () => {
|
|
164
|
-
if (!peer!.advertizing) {
|
|
165
|
-
await this._destroyPeer(peer!.id);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
this.disconnected.emit(peerId);
|
|
169
|
-
this._topology.update();
|
|
170
|
-
},
|
|
171
|
-
onRejected: () => {
|
|
172
|
-
// If the peer rejected our connection remove it from the set of candidates.
|
|
173
|
-
// TODO(dmaretskyi): Set flag instead.
|
|
174
|
-
if (this._peers.has(peerId)) {
|
|
175
|
-
void this._destroyPeer(peerId);
|
|
176
|
-
}
|
|
177
|
-
},
|
|
178
|
-
onAccepted: () => {
|
|
179
|
-
this._topology.update();
|
|
180
|
-
},
|
|
181
|
-
onOffer: (remoteId) => {
|
|
182
|
-
return this._topology.onOffer(remoteId);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
);
|
|
186
|
-
this._peers.set(peerId, peer);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
return peer;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
private async _destroyPeer(peerId: PublicKey) {
|
|
193
|
-
assert(this._peers.has(peerId));
|
|
194
|
-
await this._peers.get(peerId)!.destroy();
|
|
195
|
-
this._peers.delete(peerId);
|
|
196
|
-
}
|
|
197
|
-
|
|
147
|
+
@synchronized
|
|
198
148
|
onSwarmEvent(swarmEvent: SwarmEvent) {
|
|
199
149
|
log('swarm event', { swarmEvent }); // TODO(burdon): Stringify.
|
|
200
|
-
if (this._ctx.disposed) {
|
|
201
|
-
log.warn('ignored for destroyed swarm');
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
150
|
|
|
205
151
|
if (swarmEvent.peerAvailable) {
|
|
152
|
+
if (this._ctx.disposed) {
|
|
153
|
+
log.warn('ignored for offline swarm');
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
206
156
|
const peerId = PublicKey.from(swarmEvent.peerAvailable.peer);
|
|
207
157
|
log('new peer', { peerId });
|
|
208
158
|
if (!peerId.equals(this._ownPeerId)) {
|
|
@@ -213,8 +163,9 @@ export class Swarm {
|
|
|
213
163
|
const peer = this._peers.get(PublicKey.from(swarmEvent.peerLeft.peer));
|
|
214
164
|
if (peer) {
|
|
215
165
|
peer.advertizing = false;
|
|
216
|
-
if (!peer.connection) {
|
|
217
|
-
|
|
166
|
+
if (!peer.connection || peer.connection.state !== ConnectionState.CONNECTED) {
|
|
167
|
+
// Destroy peer only if there is no p2p-connection established
|
|
168
|
+
void this._destroyPeer(peer.id).catch((err) => log.catch(err));
|
|
218
169
|
}
|
|
219
170
|
}
|
|
220
171
|
}
|
|
@@ -222,10 +173,11 @@ export class Swarm {
|
|
|
222
173
|
this._topology.update();
|
|
223
174
|
}
|
|
224
175
|
|
|
176
|
+
@synchronized
|
|
225
177
|
async onOffer(message: OfferMessage): Promise<Answer> {
|
|
226
178
|
log('offer', { message });
|
|
227
179
|
if (this._ctx.disposed) {
|
|
228
|
-
log.info('ignored for
|
|
180
|
+
log.info('ignored for offline swarm');
|
|
229
181
|
return { accept: false };
|
|
230
182
|
}
|
|
231
183
|
|
|
@@ -249,7 +201,7 @@ export class Swarm {
|
|
|
249
201
|
async onSignal(message: SignalMessage): Promise<void> {
|
|
250
202
|
log('signal', { message });
|
|
251
203
|
if (this._ctx.disposed) {
|
|
252
|
-
log.info('ignored for
|
|
204
|
+
log.info('ignored for offline swarm');
|
|
253
205
|
return;
|
|
254
206
|
}
|
|
255
207
|
assert(
|
|
@@ -263,6 +215,78 @@ export class Swarm {
|
|
|
263
215
|
await peer.onSignal(message);
|
|
264
216
|
}
|
|
265
217
|
|
|
218
|
+
// For debug purposes
|
|
219
|
+
async goOffline() {
|
|
220
|
+
await this._ctx.dispose();
|
|
221
|
+
[...this._peers.values()].forEach((peer) => {
|
|
222
|
+
peer.advertizing = false;
|
|
223
|
+
});
|
|
224
|
+
await Promise.all([...this._peers.keys()].map((peerId) => this._closeConnection(peerId)));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// For debug purposes
|
|
228
|
+
@synchronized
|
|
229
|
+
async goOnline() {
|
|
230
|
+
this._ctx = new Context();
|
|
231
|
+
|
|
232
|
+
[...this._peers.values()].forEach((peer) => {
|
|
233
|
+
peer.advertizing = true;
|
|
234
|
+
});
|
|
235
|
+
this._topology.update();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private _getOrCreatePeer(peerId: PublicKey): Peer {
|
|
239
|
+
let peer = this._peers.get(peerId);
|
|
240
|
+
if (!peer) {
|
|
241
|
+
peer = new Peer(
|
|
242
|
+
peerId,
|
|
243
|
+
this._topic,
|
|
244
|
+
this._ownPeerId,
|
|
245
|
+
this._swarmMessenger,
|
|
246
|
+
this._protocolProvider,
|
|
247
|
+
this._transportFactory,
|
|
248
|
+
{
|
|
249
|
+
onInitiated: (connection) => {
|
|
250
|
+
this.connectionAdded.emit(connection);
|
|
251
|
+
},
|
|
252
|
+
onConnected: () => {
|
|
253
|
+
this.connected.emit(peerId);
|
|
254
|
+
},
|
|
255
|
+
onDisconnected: async () => {
|
|
256
|
+
if (!peer!.advertizing) {
|
|
257
|
+
await this._destroyPeer(peer!.id);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
this.disconnected.emit(peerId);
|
|
261
|
+
this._topology.update();
|
|
262
|
+
},
|
|
263
|
+
onRejected: () => {
|
|
264
|
+
// If the peer rejected our connection remove it from the set of candidates.
|
|
265
|
+
// TODO(dmaretskyi): Set flag instead.
|
|
266
|
+
if (this._peers.has(peerId)) {
|
|
267
|
+
void this._destroyPeer(peerId);
|
|
268
|
+
}
|
|
269
|
+
},
|
|
270
|
+
onAccepted: () => {
|
|
271
|
+
this._topology.update();
|
|
272
|
+
},
|
|
273
|
+
onOffer: (remoteId) => {
|
|
274
|
+
return this._topology.onOffer(remoteId);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
);
|
|
278
|
+
this._peers.set(peerId, peer);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return peer;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
private async _destroyPeer(peerId: PublicKey) {
|
|
285
|
+
assert(this._peers.has(peerId));
|
|
286
|
+
await this._peers.get(peerId)!.destroy();
|
|
287
|
+
this._peers.delete(peerId);
|
|
288
|
+
}
|
|
289
|
+
|
|
266
290
|
private _getSwarmController(): SwarmController {
|
|
267
291
|
return {
|
|
268
292
|
getState: () => ({
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { PublicKey } from '@dxos/keys';
|
|
6
6
|
import { MemorySignalManager, MemorySignalManagerContext, WebsocketSignalManager } from '@dxos/messaging';
|
|
7
7
|
import { schema } from '@dxos/protocols';
|
|
8
|
+
import { ConnectionState } from '@dxos/protocols/proto/dxos/client/services';
|
|
8
9
|
import { createLinkedPorts, createProtoRpcPeer, ProtoRpcPeer } from '@dxos/rpc';
|
|
9
10
|
import { ComplexMap } from '@dxos/util';
|
|
10
11
|
|
|
@@ -147,6 +148,14 @@ export class TestPeer {
|
|
|
147
148
|
this._swarms.set(topic, swarm);
|
|
148
149
|
return swarm;
|
|
149
150
|
}
|
|
151
|
+
|
|
152
|
+
async goOffline() {
|
|
153
|
+
await this._networkManager.setConnectionState(ConnectionState.OFFLINE);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async goOnline() {
|
|
157
|
+
await this._networkManager.setConnectionState(ConnectionState.ONLINE);
|
|
158
|
+
}
|
|
150
159
|
}
|
|
151
160
|
|
|
152
161
|
// TODO(burdon): Reconcile with new Swarm concept.
|
|
@@ -6,7 +6,7 @@ import { expect } from 'chai';
|
|
|
6
6
|
|
|
7
7
|
import { PublicKey } from '@dxos/keys';
|
|
8
8
|
import { log } from '@dxos/log';
|
|
9
|
-
import { test } from '@dxos/test';
|
|
9
|
+
import { afterTest, test } from '@dxos/test';
|
|
10
10
|
import { range } from '@dxos/util';
|
|
11
11
|
|
|
12
12
|
import { TestBuilder } from '../testing';
|
|
@@ -19,7 +19,7 @@ import { exchangeMessages, joinSwarm, leaveSwarm, openAndCloseAfterTest } from '
|
|
|
19
19
|
* Basic swarm tests.
|
|
20
20
|
*/
|
|
21
21
|
export const basicTestSuite = (testBuilder: TestBuilder, runTests = true) => {
|
|
22
|
-
if (runTests) {
|
|
22
|
+
if (!runTests) {
|
|
23
23
|
return;
|
|
24
24
|
}
|
|
25
25
|
|
|
@@ -37,7 +37,9 @@ export const basicTestSuite = (testBuilder: TestBuilder, runTests = true) => {
|
|
|
37
37
|
// TODO(burdon): Test with more peers (configure and test messaging).
|
|
38
38
|
test('joins swarm with star topology', async () => {
|
|
39
39
|
const peer1 = testBuilder.createPeer();
|
|
40
|
+
afterTest(() => peer1.close());
|
|
40
41
|
const peer2 = testBuilder.createPeer();
|
|
42
|
+
afterTest(() => peer2.close());
|
|
41
43
|
await openAndCloseAfterTest([peer1, peer2]);
|
|
42
44
|
|
|
43
45
|
const topic = PublicKey.random();
|
|
@@ -85,11 +87,12 @@ export const basicTestSuite = (testBuilder: TestBuilder, runTests = true) => {
|
|
|
85
87
|
expect(topics).to.have.length(numSwarms);
|
|
86
88
|
});
|
|
87
89
|
|
|
88
|
-
test('joins multiple swarms concurrently', async () => {
|
|
90
|
+
test.skip('joins multiple swarms concurrently', async () => {
|
|
89
91
|
const createSwarm = async () => {
|
|
90
92
|
const topicA = PublicKey.random();
|
|
91
93
|
const peer1a = testBuilder.createPeer();
|
|
92
94
|
const peer2a = testBuilder.createPeer();
|
|
95
|
+
await openAndCloseAfterTest([peer1a, peer2a]);
|
|
93
96
|
|
|
94
97
|
const swarm1a = await peer1a.createSwarm(topicA).join();
|
|
95
98
|
const swarm2a = await peer2a.createSwarm(topicA).join();
|
|
@@ -106,7 +109,30 @@ export const basicTestSuite = (testBuilder: TestBuilder, runTests = true) => {
|
|
|
106
109
|
]);
|
|
107
110
|
});
|
|
108
111
|
|
|
109
|
-
test('
|
|
112
|
+
test('going offline and back online', async () => {
|
|
113
|
+
const peer1 = testBuilder.createPeer();
|
|
114
|
+
const peer2 = testBuilder.createPeer();
|
|
115
|
+
await openAndCloseAfterTest([peer1, peer2]);
|
|
116
|
+
|
|
117
|
+
const topic1 = PublicKey.random();
|
|
118
|
+
|
|
119
|
+
const [swarm1, swarm2] = await joinSwarm([peer1, peer2], topic1);
|
|
120
|
+
await exchangeMessages(swarm1, swarm2);
|
|
121
|
+
|
|
122
|
+
//
|
|
123
|
+
// Going offline and back online
|
|
124
|
+
//
|
|
125
|
+
const peerLeft = peer2._networkManager.signalManager.swarmEvent.waitFor((event) => !!event.swarmEvent.peerLeft);
|
|
126
|
+
await peer1.goOffline();
|
|
127
|
+
await peerLeft;
|
|
128
|
+
await peer1.goOnline();
|
|
129
|
+
|
|
130
|
+
await exchangeMessages(swarm1, swarm2);
|
|
131
|
+
await leaveSwarm([peer1, peer2], topic1);
|
|
132
|
+
}).timeout(2_000);
|
|
133
|
+
|
|
134
|
+
// TODO(mykola): broken.
|
|
135
|
+
test.skip('many peers and connections', async () => {
|
|
110
136
|
const numTopics = 5;
|
|
111
137
|
const peersPerTopic = 5;
|
|
112
138
|
|
|
@@ -117,6 +143,7 @@ export const basicTestSuite = (testBuilder: TestBuilder, runTests = true) => {
|
|
|
117
143
|
await Promise.all(
|
|
118
144
|
range(peersPerTopic).map(async () => {
|
|
119
145
|
const peer = testBuilder.createPeer();
|
|
146
|
+
await openAndCloseAfterTest([peer]);
|
|
120
147
|
const swarm = await peer.createSwarm(topic).join();
|
|
121
148
|
await swarm.protocol.connected.waitForCondition(
|
|
122
149
|
() => swarm.protocol.connections.size === peersPerTopic - 1
|
|
@@ -10,7 +10,10 @@ describe('WebRTC transport', () => {
|
|
|
10
10
|
basicTestSuite(testBuilder);
|
|
11
11
|
}).timeout(10_000);
|
|
12
12
|
|
|
13
|
+
/*
|
|
14
|
+
// TODO(burdon): Flaky.
|
|
13
15
|
describe('WebRTC transport proxy', () => {
|
|
14
16
|
const testBuilder = new TestBuilder({ signalHosts: [TEST_SIGNAL_URL], bridge: true });
|
|
15
17
|
basicTestSuite(testBuilder);
|
|
16
18
|
}).timeout(10_000);
|
|
19
|
+
*/
|