11import { PeerGroupAgentConfig , PeerInfo , PeerSource } from '../../agents/peer' ;
22import { Mesh , SyncMode , UsageToken } from '../../service/Mesh' ;
3+ import { SpawnCallback } from '../../agents/spawn' ;
4+ import { PeerGroupState } from '../../agents/peer/PeerGroupState' ;
5+ import { ObjectDiscoveryReply } from '../../agents/discovery' ;
6+ import { Endpoint } from '../../agents/network' ;
7+
8+ import { Identity , RSAKeyPair } from 'data/identity' ;
39import { Context , HashedObject , LiteralContext } from 'data/model' ;
410import { Hash } from 'data/model' ;
5- import { Endpoint } from 'mesh/agents/network' ;
11+
612import { AsyncStream } from 'util/streams' ;
7- import { ObjectDiscoveryReply } from 'mesh/agents/discovery' ;
13+
814import { RNGImpl } from 'crypto/random' ;
9- import { Identity , RSAKeyPair } from 'data/identity' ;
15+
1016import { Store } from 'storage/store' ;
1117import { LinkupAddress } from 'net/linkup' ;
12- import { SpawnCallback } from 'mesh/agents/spawn' ;
1318
1419/* Run a mesh remotely, and access it through a MeshProxy */
1520
@@ -25,7 +30,7 @@ import { SpawnCallback } from 'mesh/agents/spawn';
2530 *
2631 * Ah, the things we do for you, Hyper Hyper Space. */
2732
28- type MeshCommand = JoinPeerGroup | LeavePeerGroup |
33+ type MeshCommand = JoinPeerGroup | LeavePeerGroup | ForwardPeerGroupState |
2934 SyncObjectsWithPeerGroup | StopSyncObjectsWithPeerGroup |
3035 StartObjectBroadcast | StopObjectBroadcast |
3136 FindObjectByHash | FindObjectByHashSuffix | Shutdown |
@@ -50,6 +55,12 @@ type LeavePeerGroup = {
5055 usageToken : UsageToken
5156}
5257
58+ type ForwardPeerGroupState = {
59+ type : 'forward-peer-group-state' ,
60+ requestId : string ,
61+ peerGroupId : string
62+ }
63+
5364type SyncObjectsWithPeerGroup = {
5465 type : 'sync-objects-with-peer-group' ,
5566 peerGroupId : string ,
@@ -147,7 +158,7 @@ type ForwardGetPeerForEndpointReply = {
147158
148159type PeerInfoContext = { endpoint : Endpoint , identityHash : Hash , identity ?: LiteralContext } ;
149160
150- type CommandStreamedReply = LiteralObjectDiscoveryReply | DiscoveryEndReply | ObjectSpawnCallback ;
161+ type CommandStreamedReply = LiteralObjectDiscoveryReply | DiscoveryEndReply | ObjectSpawnCallback | PeerGroupStateReply ;
151162
152163type LiteralObjectDiscoveryReply = {
153164 type : 'object-discovery-reply'
@@ -173,6 +184,15 @@ type ObjectSpawnCallback = {
173184 senderEndpoint : string
174185}
175186
187+ type LiteralPeerInfo = { endpoint : Endpoint , identityHash : Hash , identity ?: LiteralContext } ;
188+
189+ type PeerGroupStateReply = {
190+ type : 'peer-group-state-reply' ,
191+ requestId : string ,
192+ local ?: LiteralPeerInfo ,
193+ remote ?: Array < LiteralPeerInfo >
194+ }
195+
176196
177197type PeerSourceRequest = GetPeersRequest | GetPeerForEndpointRequest ;
178198
@@ -199,6 +219,7 @@ class MeshHost {
199219 return ( type === 'join-peer-group' ||
200220 type === 'check-peer-group-usage' ||
201221 type === 'leave-peer-group' ||
222+ type === 'forward-peer-group-state' ||
202223 type === 'sync-objects-with-peer-group' ||
203224 type === 'stop-sync-objects-with-peer-group' ||
204225 type === 'start-object-broadcast' ||
@@ -220,7 +241,8 @@ class MeshHost {
220241
221242 return ( type === 'object-discovery-reply' ||
222243 type === 'object-discovery-end' ||
223- type === 'object-spawn-callback' ) ;
244+ type === 'object-spawn-callback' ||
245+ type === 'peer-group-state-reply' ) ;
224246 }
225247
226248 static isPeerSourceRequest ( msg : any ) : boolean {
@@ -275,6 +297,42 @@ class MeshHost {
275297 } else if ( command . type === 'leave-peer-group' ) {
276298 const leave = command as LeavePeerGroup ;
277299 this . mesh . leavePeerGroup ( leave . usageToken ) ;
300+
301+ } else if ( command . type === 'forward-peer-group-state' ) {
302+
303+ this . mesh . getPeerGroupState ( command . peerGroupId ) . then ( ( state : PeerGroupState | undefined ) => {
304+
305+ const reply : PeerGroupStateReply = {
306+ type : 'peer-group-state-reply' ,
307+ requestId : command . requestId ,
308+ }
309+
310+ if ( state !== undefined ) {
311+ const local : LiteralPeerInfo = { endpoint : state . local . endpoint , identityHash : state . local . identityHash } ;
312+
313+ if ( state . local . identity !== undefined ) {
314+ local . identity = state . local . identity . toLiteralContext ( ) ;
315+ }
316+
317+ reply . local = local ;
318+
319+ reply . remote = [ ] ;
320+
321+ for ( const peerInfo of state . remote . values ( ) ) {
322+ const lit : LiteralPeerInfo = { endpoint : peerInfo . endpoint , identityHash : peerInfo . identityHash } ;
323+
324+ if ( peerInfo . identity !== undefined ) {
325+ lit . identity = peerInfo . identity . toLiteralContext ( ) ;
326+ }
327+
328+ reply . remote . push ( lit ) ;
329+ }
330+ }
331+
332+ this . streamedReplyCb ( reply ) ;
333+
334+ } )
335+
278336 } else if ( command . type === 'sync-objects-with-peer-group' ) {
279337 const syncObjs = command as SyncObjectsWithPeerGroup ;
280338
@@ -570,11 +628,11 @@ class PeerSourceProxy implements PeerSource {
570628}
571629
572630export { MeshHost , MeshCommand ,
573- JoinPeerGroup , LeavePeerGroup ,
631+ JoinPeerGroup , LeavePeerGroup , ForwardPeerGroupState ,
574632 SyncObjectsWithPeerGroup , StopSyncObjectsWithPeerGroup ,
575633 StartObjectBroadcast , StopObjectBroadcast ,
576634 FindObjectByHash , FindObjectByHashSuffix , AddObjectSpawnCallback , SendObjectSpawnRequest ,
577635 Shutdown ,
578- CommandStreamedReply , LiteralObjectDiscoveryReply , DiscoveryEndReply , ObjectSpawnCallback ,
636+ CommandStreamedReply , LiteralObjectDiscoveryReply , DiscoveryEndReply , ObjectSpawnCallback , PeerGroupStateReply , LiteralPeerInfo ,
579637 ForwardGetPeersReply , ForwardGetPeerForEndpointReply ,
580638 PeerSourceRequest , GetPeersRequest , GetPeerForEndpointRequest , PeerInfoContext } ;
0 commit comments