preparing config for lux routing
This commit is contained in:
parent
3172def440
commit
bd15749c24
3 changed files with 65 additions and 10 deletions
|
|
@ -130,12 +130,17 @@ func (ks *LuxKeyStore) Put(key LuxKey) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ks *LuxKeyStore) Count() int {
|
||||||
|
return len(ks.keys)
|
||||||
|
}
|
||||||
|
|
||||||
func (ks *LuxKeyStore) Keys() []LuxKey {
|
func (ks *LuxKeyStore) Keys() []LuxKey {
|
||||||
values := make([]LuxKey, len(ks.keys))
|
values := make([]LuxKey, len(ks.keys))
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
for _, value := range ks.keys {
|
for _, value := range ks.keys {
|
||||||
values[i] = value
|
values[i] = value
|
||||||
|
i += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return values
|
return values
|
||||||
|
|
|
||||||
|
|
@ -9,43 +9,80 @@ import (
|
||||||
"gopkg.in/ini.v1"
|
"gopkg.in/ini.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type luxConfigRoute struct {
|
||||||
|
Id string `ini:"id"`
|
||||||
|
Channel string `ini:"channel"`
|
||||||
|
Address string `ini:"address"`
|
||||||
|
}
|
||||||
|
|
||||||
type luxConfig struct {
|
type luxConfig struct {
|
||||||
Host struct {
|
Host struct {
|
||||||
Name string `ini:"name"`
|
Name string `ini:"name"`
|
||||||
Nodes string `ini:"nodes"`
|
|
||||||
} `ini:"host"`
|
} `ini:"host"`
|
||||||
|
|
||||||
KeyStore struct {
|
KeyStore struct {
|
||||||
Path string `ini:"path"`
|
Path string `ini:"path"`
|
||||||
} `ini:"keystore"`
|
} `ini:"keystore"`
|
||||||
|
|
||||||
|
Routes []luxConfigRoute
|
||||||
}
|
}
|
||||||
|
|
||||||
var config luxConfig
|
var config luxConfig
|
||||||
|
|
||||||
func LuxHostEntry(configPath string) error {
|
func LuxHostEntry(configPath string) error {
|
||||||
ini, err := ini.Load(configPath)
|
ini, err := ini.LoadSources(ini.LoadOptions{
|
||||||
|
AllowNonUniqueSections: true,
|
||||||
|
}, configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = ini.MapTo(&config); err != nil {
|
if err = ini.MapTo(&config); err != nil {
|
||||||
return err
|
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)
|
fmt.Println(config)
|
||||||
|
|
||||||
ks := crypto.NewLuxKeyStore(config.KeyStore.Path)
|
ks := crypto.NewLuxKeyStore(config.KeyStore.Path)
|
||||||
key, err := crypto.NewLuxKey(proto.LuxTypeHost)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ks.Load(); err != nil {
|
if err := ks.Load(); err != nil {
|
||||||
log.Fatal(err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package proto
|
package proto
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
type LuxType uint
|
type LuxType uint
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -9,6 +11,17 @@ const (
|
||||||
|
|
||||||
const LUX_PROTO_TYPE_SIZE = 2
|
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 {
|
func (luxType *LuxType) Read(rd *LuxBuffer) error {
|
||||||
if val, err := rd.ReadUint16(); err != nil {
|
if val, err := rd.ReadUint16(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue