2023-10-10 21:14:40 +02:00
|
|
|
// sync merges shared state between two nodes
|
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
|
|
|
"github.com/tim-beatham/wgmesh/pkg/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SyncServiceImpl struct {
|
2023-10-20 13:41:06 +02:00
|
|
|
rpc.UnimplementedSyncServiceServer
|
|
|
|
Server *ctrlserver.MeshCtrlServer
|
2023-10-10 21:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMesh: Gets a nodes local mesh configuration as a CRDT
|
|
|
|
func (s *SyncServiceImpl) GetConf(context context.Context, request *rpc.GetConfRequest) (*rpc.GetConfReply, error) {
|
2023-10-20 13:41:06 +02:00
|
|
|
mesh := s.Server.MeshManager.GetMesh(request.MeshId)
|
2023-10-10 21:14:40 +02:00
|
|
|
|
|
|
|
if mesh == nil {
|
|
|
|
return nil, errors.New("mesh does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
meshBytes := mesh.Save()
|
|
|
|
|
|
|
|
reply := rpc.GetConfReply{
|
|
|
|
Mesh: meshBytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &reply, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2023-10-20 13:41:06 +02:00
|
|
|
mesh := s.Server.MeshManager.GetMesh(request.MeshId)
|
2023-10-10 21:14:40 +02:00
|
|
|
|
|
|
|
if mesh == nil {
|
|
|
|
return nil, errors.New("mesh does not exist")
|
|
|
|
}
|
|
|
|
|
2023-10-20 13:41:06 +02:00
|
|
|
err := s.Server.MeshManager.UpdateMesh(request.MeshId, request.Changes)
|
2023-10-10 21:14:40 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rpc.SyncMeshReply{Success: true}, nil
|
|
|
|
}
|
|
|
|
|
2023-10-20 13:41:06 +02:00
|
|
|
func NewSyncService(server *ctrlserver.MeshCtrlServer) *SyncServiceImpl {
|
|
|
|
return &SyncServiceImpl{Server: server}
|
|
|
|
}
|