From df0caeadd4412bc25573665cad3e6e49551a7163 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Tue, 19 Apr 2016 02:41:28 +0200
Subject: enforce uniqueness in confi switch port assignment


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) {
-- 
cgit v0.10.2