implement router multicast, working on host heartbeat

This commit is contained in:
mykola2312 2025-01-12 04:12:54 +02:00
parent 0364776f81
commit c237ae0ba5
3 changed files with 54 additions and 1 deletions

View file

@ -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)
}

View file

@ -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

View file

@ -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
}