From ca2dd6da1a9375a4aae711d5cc18429ef34c394f Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sat, 18 Jan 2025 13:35:22 +0200 Subject: [PATCH] implement lux node state serialization --- node/lux_node_state.go | 60 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/node/lux_node_state.go b/node/lux_node_state.go index 40e1a24..deb1e23 100644 --- a/node/lux_node_state.go +++ b/node/lux_node_state.go @@ -13,6 +13,32 @@ type LuxHostState struct { Increment uint32 } +func (state *LuxHostState) Read(rd *proto.LuxBuffer) error { + err := state.HostId.Read(rd) + if err != nil { + return err + } + + err = state.State.Read(rd) + if err != nil { + return err + } + + inc, err := rd.ReadUint32() + if err != nil { + return err + } + + state.Increment = inc + return nil +} + +func (state *LuxHostState) Write(wd *proto.LuxBuffer) { + state.HostId.Write(wd) + state.State.Write(wd) + wd.WriteUint32(state.Increment) +} + func (state *LuxHostState) String() string { return fmt.Sprintf("%s hostname %s opts %d increment %d", state.HostId.String(), state.State.Hostname, len(state.State.Options), state.Increment) @@ -89,3 +115,37 @@ func (ns *LuxNodeState) Generation() uint64 { func (ns *LuxNodeState) IssueNewGeneration() { ns.generation = rand.Uint64() } + +func (ns *LuxNodeState) Read(rd *proto.LuxBuffer) error { + // read hosts states + hostNum, err := rd.ReadUint16() + if err != nil { + return err + } + + for i := 0; i < int(hostNum); i++ { + state := LuxHostState{} + if err := state.Read(rd); err != nil { + return err + } + + // add state to hosts map + ns.hosts[state.HostId] = &state + } + + gen, err := rd.ReadUint64() + if err != nil { + return err + } + + ns.generation = gen + return nil +} + +func (ns *LuxNodeState) Write(wd *proto.LuxBuffer) { + wd.WriteUint16(uint16(len(ns.hosts))) + for _, state := range ns.hosts { + state.Write(wd) + } + wd.WriteUint64(ns.generation) +}