11import { PeerGroupAgentConfig , PeerInfo , PeerSource } from '../../agents/peer' ;
2- import { Mesh , SyncMode , UsageToken } from '../../service/Mesh' ;
2+ import { CannotInferPeerGroup , Mesh , SyncMode , UsageToken } from '../../service/Mesh' ;
33import { SpawnCallback } from '../../agents/spawn' ;
44import { PeerGroupState } from '../../agents/peer/PeerGroupState' ;
55import { ObjectDiscoveryReply } from '../../agents/discovery' ;
66import { Endpoint } from '../../agents/network' ;
77
88import { Identity , RSAKeyPair } from 'data/identity' ;
9- import { Context , HashedObject , LiteralContext } from 'data/model' ;
9+ import { Context , HashedObject , LiteralContext , MutableObject } from 'data/model' ;
1010import { Hash } from 'data/model' ;
1111
1212import { AsyncStream } from 'util/streams' ;
@@ -15,6 +15,7 @@ import { RNGImpl } from 'crypto/random';
1515
1616import { Store } from 'storage/store' ;
1717import { LinkupAddress } from 'net/linkup' ;
18+ import { SyncState } from 'mesh/agents/state' ;
1819
1920/* Run a mesh remotely, and access it through a MeshProxy */
2021
@@ -33,9 +34,11 @@ import { LinkupAddress } from 'net/linkup';
3334type MeshCommand = JoinPeerGroup | LeavePeerGroup | ForwardPeerGroupState |
3435 SyncObjectsWithPeerGroup | StopSyncObjectsWithPeerGroup |
3536 StartObjectBroadcast | StopObjectBroadcast |
36- FindObjectByHash | FindObjectByHashSuffix | Shutdown |
37+ FindObjectByHash | FindObjectByHashSuffix |
3738 ForwardGetPeersReply | ForwardGetPeerForEndpointReply |
38- AddObjectSpawnCallback | SendObjectSpawnRequest ;
39+ AddObjectSpawnCallback | SendObjectSpawnRequest |
40+ ForwardSyncState |
41+ Shutdown ;
3942
4043type JoinPeerGroup = {
4144 type : 'join-peer-group' ;
@@ -137,6 +140,13 @@ type SendObjectSpawnRequest = {
137140 spawnId : string
138141}
139142
143+ type ForwardSyncState = {
144+ type : 'forward-sync-state' ,
145+ requestId : string ,
146+ mutLiteralContext : LiteralContext ,
147+ peerGroupId ?: string
148+ }
149+
140150type Shutdown = {
141151 type : 'shutdown'
142152}
@@ -157,7 +167,7 @@ type ForwardGetPeerForEndpointReply = {
157167
158168type PeerInfoContext = { endpoint : Endpoint , identityHash : Hash , identity ?: LiteralContext } ;
159169
160- type CommandStreamedReply = LiteralObjectDiscoveryReply | DiscoveryEndReply | ObjectSpawnCallback | PeerGroupStateReply ;
170+ type CommandStreamedReply = LiteralObjectDiscoveryReply | DiscoveryEndReply | ObjectSpawnCallback | PeerGroupStateReply | SyncStateReply ;
161171
162172type LiteralObjectDiscoveryReply = {
163173 type : 'object-discovery-reply'
@@ -192,6 +202,13 @@ type PeerGroupStateReply = {
192202 remote ?: Array < LiteralPeerInfo >
193203}
194204
205+ type SyncStateReply = {
206+ type : 'sync-state-reply' ,
207+ requestId : string ,
208+ state ?: SyncState
209+ error ?: string ,
210+ errorType ?: 'infer-peer-group'
211+ }
195212
196213type PeerSourceRequest = GetPeersRequest | GetPeerForEndpointRequest ;
197214
@@ -231,7 +248,8 @@ class MeshHost {
231248 type === 'forward-get-peers-reply' ||
232249 type === 'forward-get-peer-for-endpoint-reply' ||
233250 type === 'add-object-spawn-callback' ||
234- type === 'send-object-spawn-callback'
251+ type === 'send-object-spawn-callback' ||
252+ type === 'forward-sync-state'
235253 ) ;
236254 }
237255
@@ -330,8 +348,34 @@ class MeshHost {
330348
331349 this . streamedReplyCb ( reply ) ;
332350
333- } )
351+ } ) ;
352+
353+ } else if ( command . type === 'forward-sync-state' ) {
354+
355+ const mut = HashedObject . fromLiteralContext ( command . mutLiteralContext ) as MutableObject ;
356+
357+ const reply : SyncStateReply = {
358+ type : 'sync-state-reply' ,
359+ requestId : command . requestId ,
360+ }
361+
362+ this . mesh . getSyncState ( mut , command . peerGroupId )
363+ . then ( ( state : SyncState | undefined ) => {
364+
365+ reply . state = state ;
366+
367+ this . streamedReplyCb ( reply ) ;
368+
369+ } ) . catch ( ( reason : any ) => {
370+ if ( reason instanceof CannotInferPeerGroup ) {
371+ reply . errorType = 'infer-peer-group' ;
372+ }
373+
374+ reply . error = reason ;
334375
376+ this . streamedReplyCb ( reply ) ;
377+ } ) ;
378+
335379 } else if ( command . type === 'sync-objects-with-peer-group' ) {
336380 const syncObjs = command as SyncObjectsWithPeerGroup ;
337381
@@ -631,6 +675,7 @@ export { MeshHost, MeshCommand,
631675 SyncObjectsWithPeerGroup , StopSyncObjectsWithPeerGroup ,
632676 StartObjectBroadcast , StopObjectBroadcast ,
633677 FindObjectByHash , FindObjectByHashSuffix , AddObjectSpawnCallback , SendObjectSpawnRequest ,
678+ ForwardSyncState ,
634679 Shutdown ,
635680 CommandStreamedReply , LiteralObjectDiscoveryReply , DiscoveryEndReply , ObjectSpawnCallback , PeerGroupStateReply , LiteralPeerInfo ,
636681 ForwardGetPeersReply , ForwardGetPeerForEndpointReply ,
0 commit comments