forked from extern/smegmesh
Bidirectional syncing
This commit is contained in:
@@ -4,8 +4,11 @@ package sync
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
crdt "github.com/tim-beatham/wgmesh/pkg/automerge"
|
||||
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
||||
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
||||
"github.com/tim-beatham/wgmesh/pkg/rpc"
|
||||
)
|
||||
|
||||
@@ -32,22 +35,60 @@ func (s *SyncServiceImpl) GetConf(context context.Context, request *rpc.GetConfR
|
||||
}
|
||||
|
||||
// Sync: Pings a node and syncs the mesh configuration with the other node
|
||||
func (s *SyncServiceImpl) SyncMesh(conext context.Context, request *rpc.SyncMeshRequest) (*rpc.SyncMeshReply, error) {
|
||||
mesh := s.Server.MeshManager.GetMesh(request.MeshId)
|
||||
// SyncMesh: syncs the two streams changes
|
||||
func (s *SyncServiceImpl) SyncMesh(stream rpc.SyncService_SyncMeshServer) error {
|
||||
var meshId = ""
|
||||
var syncer *crdt.AutomergeSync = nil
|
||||
|
||||
if mesh == nil {
|
||||
return nil, errors.New("mesh does not exist")
|
||||
for {
|
||||
logging.InfoLog.Println("Received Attempt")
|
||||
in, err := stream.Recv()
|
||||
logging.InfoLog.Println("Received Worked")
|
||||
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(meshId) == 0 {
|
||||
meshId = in.MeshId
|
||||
|
||||
mesh := s.Server.MeshManager.GetMesh(meshId)
|
||||
|
||||
if mesh == nil {
|
||||
return errors.New("mesh does not exist")
|
||||
}
|
||||
|
||||
syncer = mesh.GetSyncer()
|
||||
} else if meshId != in.MeshId {
|
||||
return errors.New("Differing MeshIDs")
|
||||
}
|
||||
|
||||
if syncer == nil {
|
||||
return errors.New("Syncer should not be nil")
|
||||
}
|
||||
|
||||
msg, moreMessages := syncer.GenerateMessage()
|
||||
|
||||
if err = stream.Send(&rpc.SyncMeshReply{Success: true, Changes: msg}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(in.Changes) != 0 {
|
||||
if err = syncer.RecvMessage(in.Changes); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !moreMessages || err == io.EOF {
|
||||
logging.InfoLog.Println("SYNC Completed")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
err := s.Server.MeshManager.UpdateMesh(request.MeshId, request.Changes)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &rpc.SyncMeshReply{Success: true}, nil
|
||||
}
|
||||
|
||||
func NewSyncService(server *ctrlserver.MeshCtrlServer) *SyncServiceImpl {
|
||||
return &SyncServiceImpl{Server: server}
|
||||
}
|
||||
|
Reference in New Issue
Block a user