From f27fa7e80e6052ce927b064bf9292dd94a480f39 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 26 Mar 2016 20:23:34 +0100 Subject: add timeout for audioswtich commands to config file diff --git a/sample-config.toml b/sample-config.toml index 9afeff2..e5f87df 100644 --- a/sample-config.toml +++ b/sample-config.toml @@ -1,6 +1,7 @@ [audioswitch] dev = "/dev/ttyUSB0" baud = 9600 +timeout = "500ms" [servers] diff --git a/src/rhctl/conf.go b/src/rhctl/conf.go index acd5cd1..7a1aea0 100644 --- a/src/rhctl/conf.go +++ b/src/rhctl/conf.go @@ -23,6 +23,8 @@ package main import ( "os" + "strings" + "time" "github.com/naoina/toml" ) @@ -31,6 +33,7 @@ type Config struct { Audioswitch struct { Device string `toml:"dev"` Baudrate Baudrate `toml:"baud"` + Timeout Duration `toml:"timeout"` } Servers map[string]struct { @@ -47,6 +50,16 @@ type Config struct { } } +type Duration struct { + time.Duration +} + +func (d *Duration) UnmarshalTOML(data []byte) (err error) { + ds := strings.TrimRight(strings.TrimLeft(string(data), "\""), "\"") + d.Duration, err = time.ParseDuration(ds) + return +} + func ReadConfig(configfile string) (conf *Config, err error) { conf = &Config{} @@ -60,5 +73,10 @@ func ReadConfig(configfile string) (conf *Config, err error) { if err = decoder.Decode(conf); err != nil { return } + + if conf.Audioswitch.Timeout.Duration <= time.Duration(0) { + conf.Audioswitch.Timeout.Duration = time.Second + } + return } diff --git a/src/rhctl/main.go b/src/rhctl/main.go index dff1758..e6164a0 100644 --- a/src/rhctl/main.go +++ b/src/rhctl/main.go @@ -77,8 +77,6 @@ func main() { } rhl.Printf("just started...") - stop := make(chan bool) - sw, err := SwitchInit(conf) if err != nil { rhl.Println("error initializing audio switch: ", err) @@ -95,6 +93,9 @@ func main() { servers = append(servers, server) } + // running essential parts + stop := make(chan bool) + go func() { rhl.Printf("starting audioswitch handler") sw.Run() -- cgit v0.10.2