From c237ae0ba516cd2e96a7e5b3dd8e8efc666babd3 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sun, 12 Jan 2025 04:12:54 +0200 Subject: [PATCH] implement router multicast, working on host heartbeat --- host/lux_host.go | 33 ++++++++++++++++++++++++++++++++- net/lux_packet.go | 3 +++ net/lux_router.go | 19 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/host/lux_host.go b/host/lux_host.go index 108554a..1b644eb 100644 --- a/host/lux_host.go +++ b/host/lux_host.go @@ -3,6 +3,7 @@ package host import ( "lux/crypto" "lux/net" + "lux/proto" ) // interface to get data for options. may be blocking @@ -33,5 +34,35 @@ func (host *LuxHost) AddOptionProvider(provider LuxOptionProvider) { } func (host *LuxHost) CaptureState() (LuxState, error) { - return LuxState{}, nil + state := LuxState{ + Hostname: host.hostname, + Options: make(map[LuxOptionType]LuxOption), + } + + for _, provider := range host.providers { + opt, err := provider.Provide() + if err != nil { + return state, err + } + + state.Options[opt.Type()] = opt + } + + return state, nil +} + +func (host *LuxHost) Heartbeat() error { + state, err := host.CaptureState() + if err != nil { + return err + } + + packet := net.LuxPacket{ + Target: host.router.GetThisKey().Id, + Type: 0x6969, // FIXME + Buffer: proto.NewLuxBuffer(), + } + + state.Write(&packet.Buffer) + return host.router.Multicast(packet) } diff --git a/net/lux_packet.go b/net/lux_packet.go index 9cabb0c..a5b7389 100644 --- a/net/lux_packet.go +++ b/net/lux_packet.go @@ -17,6 +17,9 @@ const LUX_PROTO_PACKET_HDRLEN = proto.LUX_PROTO_ID_SIZE + 2 // Buffer contains LuxBuffer with stripped headers // NOTE: Packets are always padded, so payloads may be // bigger than expected and contain trailing bytes + +// Target (ID) is rather confusing. What it actually describes is +// associated key with route. type LuxPacket struct { Target proto.LuxID Type uint diff --git a/net/lux_router.go b/net/lux_router.go index 35c299e..fed3383 100644 --- a/net/lux_router.go +++ b/net/lux_router.go @@ -282,5 +282,24 @@ func (r *LuxRouter) Send(packet LuxPacket) error { return err } + // TODO: close route if it fails? return route.Associated.Send(dgram) } + +func (r *LuxRouter) Multicast(packet LuxPacket) error { + for _, route := range r.routes { + if bytes.Equal(route.Key.Id.UUID[:], packet.Target.UUID[:]) { + dgram, err := EncryptLuxPacket(packet, route.Key, route.Destination) + if err != nil { + return err + } + + if err = route.Associated.Send(dgram); err != nil { + // TODO: close route if it fails? + return err + } + } + } + + return nil +}