From a8345a1ac7105a4393ab189e45c5ef0aead7577e Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 30 Mar 2016 02:06:52 +0200 Subject: add support for listening to status messages diff --git a/Makefile b/Makefile index 34a8e2e..c394c02 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ EXECUTEABLE := rhctl LIBS := "github.com/schleibinger/sio" \ "github.com/naoina/toml" \ + "github.com/olebedev/emitter" \ "github.com/spreadspace/telgo" .PHONY: getlibs updatelibs vet format build clean distclean diff --git a/src/rhctl/audio_switch.go b/src/rhctl/audio_switch.go index 4f1ef31..764a231 100644 --- a/src/rhctl/audio_switch.go +++ b/src/rhctl/audio_switch.go @@ -33,6 +33,16 @@ const ( SwitchError ) +func (c SwitchResult) String() string { + switch c { + case SwitchOK: + return "OK" + case SwitchError: + return "error" + } + return "unknown" +} + type SwitchResponse struct { Result SwitchResult Message string @@ -46,13 +56,29 @@ type SwitchCommand struct { type SwitchUpdateType uint8 const ( - SwitchStatus SwitchUpdateType = iota + SwitchAudio SwitchUpdateType = iota SwitchGPI SwitchOC SwitchRelay SwitchSilence ) +func (c SwitchUpdateType) String() string { + switch c { + case SwitchAudio: + return "audio" + case SwitchGPI: + return "gpi" + case SwitchOC: + return "oc" + case SwitchRelay: + return "relay" + case SwitchSilence: + return "silence" + } + return "unknown" +} + type SwitchUpdate struct { Type SwitchUpdateType Data string @@ -92,7 +118,7 @@ func (sw *AudioSwitch) handleData(data string) { rhl.Printf("Audioswitch: ignoring unexpected response: %q", data) } case "S0L": - sw.Updates <- SwitchUpdate{SwitchStatus, data} + sw.Updates <- SwitchUpdate{SwitchAudio, data} case "S0P": sw.Updates <- SwitchUpdate{SwitchGPI, data} case "S0O": diff --git a/src/rhctl/switch_control.go b/src/rhctl/switch_control.go index b48ac07..82faec8 100644 --- a/src/rhctl/switch_control.go +++ b/src/rhctl/switch_control.go @@ -21,11 +21,14 @@ package main +import ( + "github.com/olebedev/emitter" +) + type CommandType uint8 const ( CmdStatus CommandType = iota - CmdListen CmdServer CmdSwitch ) @@ -34,8 +37,6 @@ func (c CommandType) String() string { switch c { case CmdStatus: return "status" - case CmdListen: - return "listen" case CmdServer: return "server" case CmdSwitch: @@ -52,6 +53,7 @@ type Command struct { type SwitchControl struct { sw *AudioSwitch servers []*PlayoutServer + Updates *emitter.Emitter Commands chan *Command } @@ -59,7 +61,6 @@ func (ctrl *SwitchControl) handleCommand(cmd *Command) { rhdl.Printf("Telnet: got command: %+v", cmd) switch cmd.Type { case CmdStatus: - case CmdListen: case CmdServer: case CmdSwitch: } @@ -87,9 +88,10 @@ func (ctrl *SwitchControl) Run() { for _, srv := range ctrl.servers { srv.SwitchUpdates <- update } - // TODO: send out to all clients who are interested in this + ctrl.Updates.Emit("switch:"+update.Type.String(), update) case status := <-serverUpdates: rhdl.Printf("got server status update: %+v", status) + ctrl.Updates.Emit("server:status", status) // TODO: recalculate overall status and send out commands to switch case cmd := <-ctrl.Commands: ctrl.handleCommand(cmd) @@ -101,6 +103,7 @@ func SwitchControlInit(conf *Config, sw *AudioSwitch, servers []*PlayoutServer) ctrl = &SwitchControl{} ctrl.sw = sw ctrl.servers = servers + ctrl.Updates = emitter.New(32) ctrl.Commands = make(chan *Command, 8) return } diff --git a/src/rhctl/telnet.go b/src/rhctl/telnet.go index 8d73846..a07a820 100644 --- a/src/rhctl/telnet.go +++ b/src/rhctl/telnet.go @@ -22,6 +22,7 @@ package main import ( + "github.com/olebedev/emitter" "github.com/spreadspace/telgo" ) @@ -35,9 +36,44 @@ func telnetStatus(c *telgo.Client, args []string, ctrl *SwitchControl) bool { return false } +func telnetListener(c *telgo.Client, ch <-chan emitter.Event) { + rhdl.Println("started telnetListener goroutine") + for { + event, ok := <-ch + if !ok { + return + } + c.Sayln("got event: %+v", event) // we need a way to find out that the client has stopped working + } + rhdl.Println("stopped telnetListener goroutine") +} + func telnetListen(c *telgo.Client, args []string, ctrl *SwitchControl) bool { - ctrl.Commands <- &Command{Type: CmdListen} - // TODO: implement this + var ch <-chan emitter.Event + if len(args) <= 1 { + ch = ctrl.Updates.On("*") + } else { + switch args[1] { + case "status": + ch = ctrl.Updates.On("status") + case "server": + ch = ctrl.Updates.On("server:status") + case "audio": + fallthrough + case "gpi": + fallthrough + case "oc": + fallthrough + case "relay": + fallthrough + case "silence": + ch = ctrl.Updates.On("switch:" + args[1]) + default: + c.Sayln("unknown message type") + return false + } + } + go telnetListener(c, ch) return false } @@ -72,7 +108,9 @@ func telnetHelp(c *telgo.Client, args []string) bool { case "listen": c.Sayln("usage: listen [ ]") c.Sayln(" subscribe to messages of type . The following types are allowed:") - c.Sayln(" - status audio input/output mapping changes") + c.Sayln(" - status overall status changes") + c.Sayln(" - server status/health of the playout server") + c.Sayln(" - audio audio input/output mapping changes") c.Sayln(" - gpi general purpose input status messages") c.Sayln(" - oc open-collector status messages") c.Sayln(" - relay relay status messages") -- cgit v0.10.2