From bd15749c24fab1a7ce6d5d452f92f842a337577d Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Tue, 31 Dec 2024 19:24:42 +0200 Subject: [PATCH] preparing config for lux routing --- crypto/lux_key.go | 5 +++++ host/lux_host.go | 57 ++++++++++++++++++++++++++++++++++++++--------- proto/lux_type.go | 13 +++++++++++ 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/crypto/lux_key.go b/crypto/lux_key.go index b6b63ad..f88232e 100644 --- a/crypto/lux_key.go +++ b/crypto/lux_key.go @@ -130,12 +130,17 @@ func (ks *LuxKeyStore) Put(key LuxKey) error { return nil } +func (ks *LuxKeyStore) Count() int { + return len(ks.keys) +} + func (ks *LuxKeyStore) Keys() []LuxKey { values := make([]LuxKey, len(ks.keys)) i := 0 for _, value := range ks.keys { values[i] = value + i += 1 } return values diff --git a/host/lux_host.go b/host/lux_host.go index 2d22db6..bd1bb41 100644 --- a/host/lux_host.go +++ b/host/lux_host.go @@ -9,43 +9,80 @@ import ( "gopkg.in/ini.v1" ) +type luxConfigRoute struct { + Id string `ini:"id"` + Channel string `ini:"channel"` + Address string `ini:"address"` +} + type luxConfig struct { Host struct { - Name string `ini:"name"` - Nodes string `ini:"nodes"` + Name string `ini:"name"` } `ini:"host"` KeyStore struct { Path string `ini:"path"` } `ini:"keystore"` + + Routes []luxConfigRoute } var config luxConfig func LuxHostEntry(configPath string) error { - ini, err := ini.Load(configPath) + ini, err := ini.LoadSources(ini.LoadOptions{ + AllowNonUniqueSections: true, + }, configPath) if err != nil { return err } if err = ini.MapTo(&config); err != nil { return err } + + // parse routes + { + sections, err := ini.SectionsByName("route") + if err != nil { + log.Fatal(err) + } + + config.Routes = make([]luxConfigRoute, 0) + for _, section := range sections { + route := luxConfigRoute{} + if err = section.MapTo(&route); err != nil { + log.Fatal(err) + } + + config.Routes = append(config.Routes, route) + } + } fmt.Println(config) ks := crypto.NewLuxKeyStore(config.KeyStore.Path) - key, err := crypto.NewLuxKey(proto.LuxTypeHost) - if err != nil { - log.Fatal(err) - } if err := ks.Load(); err != nil { log.Fatal(err) } - if err := ks.Put(key); err != nil { - log.Fatal(err) + + var hostA, hostB crypto.LuxKey + if ks.Count() < 2 { + hostA, _ = crypto.NewLuxKey(proto.LuxTypeHost) + hostB, _ = crypto.NewLuxKey(proto.LuxTypeHost) + + if err := ks.Put(hostA); err != nil { + log.Fatal(err) + } + if err := ks.Put(hostB); err != nil { + log.Fatal(err) + } + } else { + keys := ks.Keys() + hostA, hostB = keys[0], keys[1] } - fmt.Println(key.String()) + fmt.Println(hostA.String()) + fmt.Println(hostB.String()) return nil } diff --git a/proto/lux_type.go b/proto/lux_type.go index 20b9de0..ffcde02 100644 --- a/proto/lux_type.go +++ b/proto/lux_type.go @@ -1,5 +1,7 @@ package proto +import "errors" + type LuxType uint const ( @@ -9,6 +11,17 @@ const ( const LUX_PROTO_TYPE_SIZE = 2 +func LuxTypeFromString(name string) (LuxType, error) { + switch name { + case "host": + return LuxTypeHost, nil + case "node": + return LuxTypeNode, nil + default: + return LuxTypeHost, errors.New("unknown lux type") + } +} + func (luxType *LuxType) Read(rd *LuxBuffer) error { if val, err := rd.ReadUint16(); err != nil { return err