From 53d0b44c0e1e51ab70dfa75cc94087359fc90b49 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 26 Mar 2016 23:50:49 +0100 Subject: parsing server channel command works diff --git a/src/rhctl/main.go b/src/rhctl/main.go index 23a7f13..4638293 100644 --- a/src/rhctl/main.go +++ b/src/rhctl/main.go @@ -116,6 +116,11 @@ func main() { }(server) } + // TODO: remove this after testing server interface + for { + rhl.Printf("new server state: %+v", <-servers[0].Updates) + } + <-stop rhl.Printf("at least one essential part has stopped - bringing down the whole process") } diff --git a/src/rhctl/playout_server.go b/src/rhctl/playout_server.go index 276c462..506279b 100644 --- a/src/rhctl/playout_server.go +++ b/src/rhctl/playout_server.go @@ -23,7 +23,7 @@ package main import ( "fmt" - "time" + "strings" ) type ServerHealth bool @@ -33,27 +33,62 @@ const ( Alive = true ) +type ServerStatus struct { + health ServerHealth + channel string +} + type PlayoutServer struct { name string control *SerialPort heartbeat *SerialPort - health ServerHealth - channel string + status ServerStatus + Updates chan ServerStatus +} + +func (srv *PlayoutServer) handleControl(data string) { + rhdl.Printf("Server(%s): got control message: %q", srv.name, data) + + if strings.HasPrefix(data, "channel ") { + if len(data) <= 8 { + rhl.Printf("Server(%s) sent empty channel name", srv.name) + return + } + srv.status.channel = data[8:] + srv.Updates <- srv.status + return + } + rhl.Printf("Warning: ignoring unknown control message from '%s': %q", srv.name, data) +} + +func (srv *PlayoutServer) handleHeartbeat(data string) { + rhdl.Printf("Server(%s): got heartbeat message: %q", srv.name, data) } func (srv *PlayoutServer) Run() { - rhdl.Printf("running playout server: %s", srv.name) + stop := make(chan bool) + srv.control.Run(stop) + srv.heartbeat.Run(stop) + rhdl.Printf("Server(%s) handler running...", srv.name) for { - time.Sleep(time.Second) + select { + case <-stop: + return + case data := <-srv.control.rx: + srv.handleControl(data) + case data := <-srv.heartbeat.rx: + srv.handleHeartbeat(data) + } } } func ServerInit(name string, conf *Config) (srv *PlayoutServer, err error) { srv = &PlayoutServer{} srv.name = name - srv.health = Dead - srv.channel = "music" + srv.status.health = Dead + srv.status.channel = "" + srv.Updates = make(chan ServerStatus, 8) 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) -- cgit v0.10.2