diff options
author | Christian Pointner <equinox@helsinki.at> | 2016-04-19 00:41:28 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2016-04-19 00:41:28 (GMT) |
commit | df0caeadd4412bc25573665cad3e6e49551a7163 (patch) | |
tree | 2ac52f9f830780d20be86ea417dee8e836441c4e | |
parent | 80e141f45e0d017816e7fe41ff4aba6c72f3965e (diff) |
enforce uniqueness in confi switch port assignment
-rw-r--r-- | sample-config.toml | 8 | ||||
-rw-r--r-- | src/rhctl/conf.go | 34 | ||||
-rw-r--r-- | src/rhctl/switch_control.go | 1 |
3 files changed, 29 insertions, 14 deletions
diff --git a/sample-config.toml b/sample-config.toml index e31db07..33403a4 100644 --- a/sample-config.toml +++ b/sample-config.toml @@ -3,10 +3,10 @@ dev = "/dev/ttyUSB0" baud = 19200 timeout = "500ms" unit = 0 -ports = [ { server = "master", channel = "main", number = 1 }, - { server = "master", channel = "music", number = 2 }, - { server = "standby", channel = "main", number = 3 }, - { server = "standby", channel = "music", number = 4 } ] +ports = [ { number = 1, server = "master", channel = "main" }, + { number = 2, server = "master", channel = "music" }, + { number = 3, server = "standby", channel = "main" }, + { number = 4, server = "standby", channel = "music" } ] [servers] diff --git a/src/rhctl/conf.go b/src/rhctl/conf.go index 77ab174..bbf546c 100644 --- a/src/rhctl/conf.go +++ b/src/rhctl/conf.go @@ -22,24 +22,32 @@ package main import ( + "errors" "os" + "sort" "strings" "time" "github.com/naoina/toml" ) +type ConfigSwitchPorts []struct { + Number SwitchInputNum `toml:"number"` + Server string `toml:"server"` + Channel string `toml:"channel"` +} + +func (p ConfigSwitchPorts) Len() int { return len(p) } +func (p ConfigSwitchPorts) Swap(i, j int) { p[i], p[j] = p[j], p[i] } +func (p ConfigSwitchPorts) Less(i, j int) bool { return p[i].Number < p[j].Number } + type Config struct { Audioswitch struct { - Device string `toml:"dev"` - Baudrate Baudrate `toml:"baud"` - Timeout Duration `toml:"timeout"` - Unit SwitchUnitID `toml:"unit"` - Ports []struct { - Server string `toml:"server"` - Channel string `toml:"channel"` - Number SwitchInputNum `toml:"number"` - } + Device string `toml:"dev"` + Baudrate Baudrate `toml:"baud"` + Timeout Duration `toml:"timeout"` + Unit SwitchUnitID `toml:"unit"` + Ports ConfigSwitchPorts `toml:"ports"` } Servers map[string]struct { @@ -84,5 +92,13 @@ func ReadConfig(configfile string) (conf *Config, err error) { if err = decoder.Decode(conf); err != nil { return } + + sort.Sort(conf.Audioswitch.Ports) + for i := 1; i < len(conf.Audioswitch.Ports); i++ { + if conf.Audioswitch.Ports[i].Number == conf.Audioswitch.Ports[i-1].Number { + return nil, errors.New("audioswitch port assignments must be unique") + } + } + return } diff --git a/src/rhctl/switch_control.go b/src/rhctl/switch_control.go index 822c044..425278e 100644 --- a/src/rhctl/switch_control.go +++ b/src/rhctl/switch_control.go @@ -148,7 +148,6 @@ func (ctrl *SwitchControl) checkMissingOrStaleStates(maxAge time.Duration) { server.UpdateRequest <- true } } - } func (ctrl *SwitchControl) reconcile(requestedServer string) (result bool) { |