mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-08-18 17:08:15 +02:00
Timer in go that syncs with random nodes in the mesh every
given time interval.
This commit is contained in:
@@ -6,19 +6,37 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
||||
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
||||
"github.com/tim-beatham/wgmesh/pkg/rpc"
|
||||
)
|
||||
|
||||
// SyncRequester: coordinates the syncing of meshes
|
||||
type SyncRequester interface {
|
||||
GetMesh(meshId string) error
|
||||
SyncMesh(meshid string) error
|
||||
GetMesh(meshId string, endPoint string) error
|
||||
SyncMesh(meshid string, endPoint string) error
|
||||
}
|
||||
|
||||
type SyncRequesterImpl struct {
|
||||
server *ctrlserver.MeshCtrlServer
|
||||
}
|
||||
|
||||
func (s *SyncRequesterImpl) Authenticate(meshId, endpoint string) error {
|
||||
|
||||
peerConnection, err := s.server.ConnectionManager.AddConnection(endpoint)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = peerConnection.Authenticate(meshId)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// GetMesh: Retrieves the local state of the mesh at the endpoint
|
||||
func (s *SyncRequesterImpl) GetMesh(meshId string, endPoint string) error {
|
||||
peerConnection, err := s.server.ConnectionManager.GetConnection(endPoint)
|
||||
@@ -60,7 +78,11 @@ func (s *SyncRequesterImpl) GetMesh(meshId string, endPoint string) error {
|
||||
}
|
||||
|
||||
// SyncMesh: Proactively send a sync request to the other mesh
|
||||
func (s *SyncRequesterImpl) SyncMesh(meshId string, endpoint string) error {
|
||||
func (s *SyncRequesterImpl) SyncMesh(meshId, endpoint string) error {
|
||||
if !s.server.ConnectionManager.HasConnection(endpoint) {
|
||||
s.Authenticate(meshId, endpoint)
|
||||
}
|
||||
|
||||
peerConnection, err := s.server.ConnectionManager.GetConnection(endpoint)
|
||||
|
||||
if err != nil {
|
||||
@@ -107,5 +129,10 @@ func (s *SyncRequesterImpl) SyncMesh(meshId string, endpoint string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
logging.InfoLog.Printf("Synced with node: %s meshId: %s\n", endpoint, meshId)
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewSyncRequester(s *ctrlserver.MeshCtrlServer) SyncRequester {
|
||||
return &SyncRequesterImpl{server: s}
|
||||
}
|
||||
|
Reference in New Issue
Block a user