implement lux node state serialization

This commit is contained in:
mykola2312 2025-01-18 13:35:22 +02:00
parent 0f3b59fda6
commit ca2dd6da1a

View file

@ -13,6 +13,32 @@ type LuxHostState struct {
Increment uint32 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 { func (state *LuxHostState) String() string {
return fmt.Sprintf("%s hostname %s opts %d increment %d", state.HostId.String(), return fmt.Sprintf("%s hostname %s opts %d increment %d", state.HostId.String(),
state.State.Hostname, len(state.State.Options), state.Increment) state.State.Hostname, len(state.State.Options), state.Increment)
@ -89,3 +115,37 @@ func (ns *LuxNodeState) Generation() uint64 {
func (ns *LuxNodeState) IssueNewGeneration() { func (ns *LuxNodeState) IssueNewGeneration() {
ns.generation = rand.Uint64() 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)
}