81-seperate-synchronisation

- More code comments
This commit is contained in:
Tim Beatham 2024-01-04 13:12:07 +00:00
parent 02dfd73e08
commit 59d8ae4334
6 changed files with 26 additions and 17 deletions

View File

@ -7,7 +7,9 @@ import (
"github.com/tim-beatham/smegmesh/pkg/lib"
)
// RouteManager: manager that leaks routes between meshes
type RouteManager interface {
// UpdateRoutes: leak all routes in each mesh
UpdateRoutes() error
}

View File

@ -17,20 +17,24 @@ type Querier interface {
Query(meshId string, queryParams string) ([]byte, error)
}
// JmesQuerier: queries the datstore in JMESPath syntax
type JmesQuerier struct {
manager mesh.MeshManager
}
// QueryError: query error if something went wrong
type QueryError struct {
msg string
}
// QuerRoute: represents a route in the query
type QueryRoute struct {
Destination string `json:"destination"`
HopCount int `json:"hopCount"`
Path string `json:"path"`
}
// QueryNode: represents a single node in the query
type QueryNode struct {
HostEndpoint string `json:"hostEndpoint"`
PublicKey string `json:"publicKey"`
@ -48,7 +52,7 @@ func (m *QueryError) Error() string {
return m.msg
}
// Query: queries the data
// Query: queries the the datastore at the given meshid
func (j *JmesQuerier) Query(meshId, queryParams string) ([]byte, error) {
mesh, ok := j.manager.GetMeshes()[meshId]
@ -74,6 +78,7 @@ func (j *JmesQuerier) Query(meshId, queryParams string) ([]byte, error) {
return bytes, err
}
// MeshNodeToQuerynode: convert the mesh node into a query abstraction
func MeshNodeToQueryNode(node mesh.MeshNode) *QueryNode {
queryNode := new(QueryNode)
queryNode.HostEndpoint = node.GetHostEndpoint()

View File

@ -14,10 +14,12 @@ import (
"github.com/tim-beatham/smegmesh/pkg/rpc"
)
// IpcHandler: represents a handler for ipc calls
type IpcHandler struct {
Server ctrlserver.CtrlServer
}
// getOverrideConfiguration: override any specific WireGuard configuration
func getOverrideConfiguration(args *ipc.WireGuardArgs) conf.WgConfiguration {
overrideConf := conf.WgConfiguration{}
@ -40,6 +42,7 @@ func getOverrideConfiguration(args *ipc.WireGuardArgs) conf.WgConfiguration {
return overrideConf
}
// CreateMesh: create a new mesh network
func (n *IpcHandler) CreateMesh(args *ipc.NewMeshArgs, reply *string) error {
overrideConf := getOverrideConfiguration(&args.WgArgs)
@ -66,6 +69,7 @@ func (n *IpcHandler) CreateMesh(args *ipc.NewMeshArgs, reply *string) error {
return err
}
// ListMeshes: list mesh networks
func (n *IpcHandler) ListMeshes(_ string, reply *ipc.ListMeshReply) error {
meshNames := make([]string, len(n.Server.GetMeshManager().GetMeshes()))
@ -79,6 +83,7 @@ func (n *IpcHandler) ListMeshes(_ string, reply *ipc.ListMeshReply) error {
return nil
}
// JoinMesh: join a mesh network
func (n *IpcHandler) JoinMesh(args *ipc.JoinMeshArgs, reply *string) error {
overrideConf := getOverrideConfiguration(&args.WgArgs)
@ -140,7 +145,7 @@ func (n *IpcHandler) JoinMesh(args *ipc.JoinMeshArgs, reply *string) error {
return nil
}
// LeaveMesh leaves a mesh network
// LeaveMesh: leaves a mesh network
func (n *IpcHandler) LeaveMesh(meshId string, reply *string) error {
err := n.Server.GetMeshManager().LeaveMesh(meshId)
@ -150,6 +155,7 @@ func (n *IpcHandler) LeaveMesh(meshId string, reply *string) error {
return err
}
// GetMesh: get a mesh network at the given meshid
func (n *IpcHandler) GetMesh(meshId string, reply *ipc.GetMeshReply) error {
theMesh := n.Server.GetMeshManager().GetMesh(meshId)
@ -181,6 +187,7 @@ func (n *IpcHandler) GetMesh(meshId string, reply *ipc.GetMeshReply) error {
return nil
}
// Query: perform a jmespath query
func (n *IpcHandler) Query(params ipc.QueryMesh, reply *string) error {
queryResponse, err := n.Server.GetQuerier().Query(params.MeshId, params.Query)
@ -192,6 +199,7 @@ func (n *IpcHandler) Query(params ipc.QueryMesh, reply *string) error {
return nil
}
// PutDescription: change your description in the mesh
func (n *IpcHandler) PutDescription(args ipc.PutDescriptionArgs, reply *string) error {
err := n.Server.GetMeshManager().SetDescription(args.MeshId, args.Description)
@ -203,6 +211,7 @@ func (n *IpcHandler) PutDescription(args ipc.PutDescriptionArgs, reply *string)
return nil
}
// PutAlias: put your aliasin the mesh
func (n *IpcHandler) PutAlias(args ipc.PutAliasArgs, reply *string) error {
if args.Alias == "" {
return fmt.Errorf("alias not provided")
@ -218,6 +227,7 @@ func (n *IpcHandler) PutAlias(args ipc.PutAliasArgs, reply *string) error {
return nil
}
// PutService: place a service in the mesh
func (n *IpcHandler) PutService(service ipc.PutServiceArgs, reply *string) error {
err := n.Server.GetMeshManager().SetService(service.MeshId, service.Service, service.Value)
@ -229,6 +239,7 @@ func (n *IpcHandler) PutService(service ipc.PutServiceArgs, reply *string) error
return nil
}
// DeleteService: withtract a service in the mesh
func (n *IpcHandler) DeleteService(service ipc.DeleteServiceArgs, reply *string) error {
err := n.Server.GetMeshManager().RemoveService(service.MeshId, service.Service)
@ -240,6 +251,7 @@ func (n *IpcHandler) DeleteService(service ipc.DeleteServiceArgs, reply *string)
return nil
}
// RobinIpcParams: parameters required to construct a new mesh network
type RobinIpcParams struct {
CtrlServer ctrlserver.CtrlServer
}

View File

@ -8,11 +8,13 @@ import (
"github.com/tim-beatham/smegmesh/pkg/rpc"
)
// WgRpc: represents a WireGuard rpc call
type WgRpc struct {
rpc.UnimplementedMeshCtrlServerServer
Server *ctrlserver.MeshCtrlServer
}
// GetMesh: serialise the mesh network into bytes
func (m *WgRpc) GetMesh(ctx context.Context, request *rpc.GetMeshRequest) (*rpc.GetMeshReply, error) {
mesh := m.Server.MeshManager.GetMesh(request.MeshId)

View File

@ -5,6 +5,7 @@ import (
"golang.org/x/sys/unix"
)
// RouteInstaller: install the routes to the given interface
type RouteInstaller interface {
InstallRoutes(devName string, routes ...lib.Route) error
}
@ -19,6 +20,8 @@ func (r *RouteInstallerImpl) InstallRoutes(devName string, routes ...lib.Route)
return err
}
defer rtnl.Close()
err = rtnl.DeleteRoutes(devName, unix.AF_INET6, routes...)
if err != nil {

View File

@ -1,15 +0,0 @@
package timer
import (
"github.com/tim-beatham/smegmesh/pkg/ctrlserver"
"github.com/tim-beatham/smegmesh/pkg/lib"
logging "github.com/tim-beatham/smegmesh/pkg/log"
)
func NewTimestampScheduler(ctrlServer *ctrlserver.MeshCtrlServer) lib.Timer {
timerFunc := func() error {
logging.Log.WriteInfof("Updated Timestamp")
return ctrlServer.MeshManager.UpdateTimeStamp()
}
return *lib.NewTimer(timerFunc, ctrlServer.Conf.HeartBeat)
}