summaryrefslogtreecommitdiff
path: root/src/rhctl
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2016-03-26 19:57:22 (GMT)
committerChristian Pointner <equinox@spreadspace.org>2016-03-26 19:57:22 (GMT)
commitaf3c3e7cf792eb21e5fe9c96237723561644b7c4 (patch)
tree9ccef4c38060db5088019e2ee375bd35f508909b /src/rhctl
parentf27fa7e80e6052ce927b064bf9292dd94a480f39 (diff)
disconnecting serial devices is now properly handled
Diffstat (limited to 'src/rhctl')
-rw-r--r--src/rhctl/audio_switch.go14
-rw-r--r--src/rhctl/main.go4
-rw-r--r--src/rhctl/playout_server.go10
-rw-r--r--src/rhctl/serial_port.go49
4 files changed, 52 insertions, 25 deletions
diff --git a/src/rhctl/audio_switch.go b/src/rhctl/audio_switch.go
index 1368da8..5fc9fac 100644
--- a/src/rhctl/audio_switch.go
+++ b/src/rhctl/audio_switch.go
@@ -23,7 +23,6 @@ package main
import (
"fmt"
- "time"
)
type AudioSwitch struct {
@@ -32,15 +31,22 @@ type AudioSwitch struct {
func (sw *AudioSwitch) Run() {
rhdl.Printf("running audio switch")
- sw.port.tx <- "hello switch"
- time.Sleep(10 * time.Second)
+ stop := make(chan bool)
+ sw.port.Run(stop)
+
+ select {
+ case <-stop:
+ return
+ case data := <-sw.port.rx:
+ rhl.Printf("got data from audio switch: %q", data)
+ }
}
func SwitchInit(conf *Config) (sw *AudioSwitch, err error) {
sw = &AudioSwitch{}
- if sw.port, err = SerialOpenAndHandle(conf.Audioswitch.Device, conf.Audioswitch.Baudrate, ""); err != nil {
+ if sw.port, err = SerialOpen(conf.Audioswitch.Device, conf.Audioswitch.Baudrate, ""); err != nil {
err = fmt.Errorf("Error opening switch port: %s", err)
return
}
diff --git a/src/rhctl/main.go b/src/rhctl/main.go
index e6164a0..23a7f13 100644
--- a/src/rhctl/main.go
+++ b/src/rhctl/main.go
@@ -92,6 +92,10 @@ func main() {
}
servers = append(servers, server)
}
+ if len(servers) <= 0 {
+ rhl.Printf("Error: there is no playout server configured...")
+ return
+ }
// running essential parts
stop := make(chan bool)
diff --git a/src/rhctl/playout_server.go b/src/rhctl/playout_server.go
index 5491b23..276c462 100644
--- a/src/rhctl/playout_server.go
+++ b/src/rhctl/playout_server.go
@@ -43,10 +43,10 @@ type PlayoutServer struct {
func (srv *PlayoutServer) Run() {
rhdl.Printf("running playout server: %s", srv.name)
- srv.control.tx <- fmt.Sprintf("hello %s", srv.name)
- srv.heartbeat.tx <- fmt.Sprintf("beat %s", srv.name)
- time.Sleep(5 * time.Second)
+ for {
+ time.Sleep(time.Second)
+ }
}
func ServerInit(name string, conf *Config) (srv *PlayoutServer, err error) {
@@ -55,11 +55,11 @@ func ServerInit(name string, conf *Config) (srv *PlayoutServer, err error) {
srv.health = Dead
srv.channel = "music"
- if srv.control, err = SerialOpenAndHandle(conf.Servers[name].ControlDevice, conf.Servers[name].ControlBaudrate, "\r\n"); err != nil {
+ if srv.control, err = SerialOpen(conf.Servers[name].ControlDevice, conf.Servers[name].ControlBaudrate, "\r\n"); err != nil {
err = fmt.Errorf("Error opening control port(%s): %s", srv.name, err)
return
}
- if srv.heartbeat, err = SerialOpenAndHandle(conf.Servers[name].HeartbeatDevice, conf.Servers[name].HeartbeatBaudrate, "\r\n"); err != nil {
+ if srv.heartbeat, err = SerialOpen(conf.Servers[name].HeartbeatDevice, conf.Servers[name].HeartbeatBaudrate, "\r\n"); err != nil {
err = fmt.Errorf("Error opening control port(%s): %s", srv.name, err)
return
}
diff --git a/src/rhctl/serial_port.go b/src/rhctl/serial_port.go
index c7804a6..e809484 100644
--- a/src/rhctl/serial_port.go
+++ b/src/rhctl/serial_port.go
@@ -127,44 +127,61 @@ func (b *Baudrate) UnmarshalTOML(data []byte) error {
}
type SerialPort struct {
- port *sio.Port
- rx <-chan string
- tx chan<- string
+ port *sio.Port
+ rx <-chan string
+ tx chan<- string
+ newline string
}
-func serialReader(c chan<- string, port *sio.Port) {
+func serialReader(c chan<- string, port *sio.Port, stop chan<- bool) {
+ defer func() {
+ port.Close()
+ stop <- true
+ }()
+
scanner := bufio.NewScanner(port)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
- if err := scanner.Err(); err != nil {
- panic(err.Error())
- }
data := scanner.Text()
if len(data) == 0 {
continue
}
c <- string(data)
}
+ if err := scanner.Err(); err != nil {
+ rhl.Printf("device(%s): read error: %s", port.LocalAddr(), err)
+ panic(err.Error())
+ } else {
+ rhl.Printf("device(%s): closed", port.LocalAddr())
+ }
}
-func serialWriter(c <-chan string, port *sio.Port, newline string) {
+func serialWriter(c <-chan string, port *sio.Port, newline string, stop chan<- bool) {
+ defer func() {
+ port.Close()
+ stop <- true
+ }()
+
for data := range c {
port.Write([]byte(data + newline))
}
- port.Close()
}
-func SerialOpenAndHandle(device string, rate Baudrate, newline string) (port *SerialPort, err error) {
- port = &SerialPort{}
- if port.port, err = sio.Open(device, rate.code); err != nil {
- return
- }
+func (port *SerialPort) Run(stop chan<- bool) (err error) {
tx := make(chan string, 10)
rx := make(chan string, 20)
- go serialReader(rx, port.port)
- go serialWriter(tx, port.port, newline)
+ go serialReader(rx, port.port, stop)
+ go serialWriter(tx, port.port, port.newline, stop)
port.rx = rx
port.tx = tx
return
}
+
+func SerialOpen(device string, rate Baudrate, newline string) (port *SerialPort, err error) {
+ port = &SerialPort{newline: newline}
+ if port.port, err = sio.Open(device, rate.code); err != nil {
+ return
+ }
+ return
+}