summaryrefslogtreecommitdiff
path: root/src/rhctl/playout_server.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/rhctl/playout_server.go')
-rw-r--r--src/rhctl/playout_server.go74
1 files changed, 35 insertions, 39 deletions
diff --git a/src/rhctl/playout_server.go b/src/rhctl/playout_server.go
index fbcfeff..5fc1dfd 100644
--- a/src/rhctl/playout_server.go
+++ b/src/rhctl/playout_server.go
@@ -53,8 +53,7 @@ type ServerState struct {
type PlayoutServer struct {
name string
- control *SerialPort
- heartbeat *SerialPort
+ device *SerialPort
hbtimeout time.Duration
hbtimer *time.Timer
hbthreshold uint
@@ -65,9 +64,33 @@ type PlayoutServer struct {
SwitchUpdates chan SwitchUpdate
}
-func (srv *PlayoutServer) handleControl(data string) {
- rhdl.Printf("Server(%s): got control message: %q", srv.name, data)
+func (srv *PlayoutServer) handleHeartbeat() {
+ rhdl.Printf("Server(%s): got heartbeat message", srv.name)
+ srv.hbtimer.Reset(srv.hbtimeout)
+ if (srv.hbcnt + 1) < srv.hbthreshold {
+ srv.hbcnt++
+ return
+ }
+ old := srv.state.Health
+ srv.state.Health = ServerAlive
+ srv.state.Changed = time.Now()
+ if old != srv.state.Health {
+ srv.StateChanges <- srv.state
+ rhl.Printf("Server(%s): is back from the dead!", srv.name)
+ if srv.state.Channel == "" {
+ srv.device.tx <- "channel ?"
+ }
+ }
+}
+
+func (srv *PlayoutServer) handleMessage(data string) {
+ rhdl.Printf("Server(%s): got message: %q", srv.name, data)
+
+ if data == "hb" {
+ srv.handleHeartbeat()
+ return
+ }
if strings.HasPrefix(data, "channel ") {
if len(data) <= 8 {
rhl.Printf("Server(%s) sent empty channel name", srv.name)
@@ -86,27 +109,7 @@ func (srv *PlayoutServer) handleControl(data string) {
// TODO: send command to swichctrl: &Command{Type: CmdServer, Args: data[9:], Response: nil}
return
}
- rhl.Printf("Server(%s): ignoring unknown control message: %q", srv.name, data)
-}
-
-func (srv *PlayoutServer) handleHeartbeat(data string) {
- rhdl.Printf("Server(%s): got heartbeat message: %q", srv.name, data)
- srv.hbtimer.Reset(srv.hbtimeout)
- if (srv.hbcnt + 1) < srv.hbthreshold {
- srv.hbcnt++
- return
- }
-
- old := srv.state.Health
- srv.state.Health = ServerAlive
- srv.state.Changed = time.Now()
- if old != srv.state.Health {
- srv.StateChanges <- srv.state
- rhl.Printf("Server(%s): is back from the dead!", srv.name)
- if srv.state.Channel == "" {
- srv.control.tx <- "channel ?"
- }
- }
+ rhl.Printf("Server(%s): ignoring unknown message: %q", srv.name, data)
}
func (srv *PlayoutServer) handleHBTimeout() {
@@ -119,8 +122,7 @@ func (srv *PlayoutServer) handleHBTimeout() {
func (srv *PlayoutServer) Run() {
stop := make(chan bool)
- srv.control.Run(stop)
- srv.heartbeat.Run(stop)
+ srv.device.Run(stop)
srv.hbtimer = time.NewTimer(srv.hbtimeout)
srv.hbcnt = 0
@@ -129,20 +131,18 @@ func (srv *PlayoutServer) Run() {
select {
case <-stop:
return
- case data := <-srv.control.rx:
- srv.handleControl(data)
- case data := <-srv.heartbeat.rx:
- srv.handleHeartbeat(data)
+ case data := <-srv.device.rx:
+ srv.handleMessage(data)
case <-srv.hbtimer.C:
srv.handleHBTimeout()
case <-srv.UpdateRequest:
if srv.state.Health == ServerDead {
srv.StateChanges <- srv.state
} else {
- srv.control.tx <- "channel ?"
+ srv.device.tx <- "channel ?"
}
case update := <-srv.SwitchUpdates:
- srv.control.tx <- update.Data
+ srv.device.tx <- update.Data
}
}
}
@@ -165,12 +165,8 @@ func ServerInit(name string, conf *Config) (srv *PlayoutServer, err error) {
srv.UpdateRequest = make(chan bool, 8)
srv.SwitchUpdates = make(chan SwitchUpdate, 32)
- if srv.control, err = SerialOpen(conf.Servers[name].ControlDevice, conf.Servers[name].ControlBaudrate, "\n"); err != nil {
- err = fmt.Errorf("Server(%s): error opening control port: %s", srv.name, err)
- return
- }
- if srv.heartbeat, err = SerialOpen(conf.Servers[name].HeartbeatDevice, conf.Servers[name].HeartbeatBaudrate, "\n"); err != nil {
- err = fmt.Errorf("Server(%s): error opening heartbeat port: %s", srv.name, err)
+ if srv.device, err = SerialOpen(conf.Servers[name].Device, conf.Servers[name].Baudrate, "\n"); err != nil {
+ err = fmt.Errorf("Server(%s): error opening serial port: %s", srv.name, err)
return
}