// // rhctl // // Copyright (C) 2009-2016 Christian Pointner // // This file is part of rhctl. // // rhctl is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // any later version. // // rhctl is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with rhctl. If not, see . // package main import ( "github.com/olebedev/emitter" ) type CommandType uint8 const ( CmdStatus CommandType = iota CmdServer CmdSwitch ) func (c CommandType) String() string { switch c { case CmdStatus: return "status" case CmdServer: return "server" case CmdSwitch: return "switch" } return "unknown" } type Command struct { Type CommandType args []string } type SwitchControl struct { sw *AudioSwitch servers []*PlayoutServer Updates *emitter.Emitter Commands chan *Command } func (ctrl *SwitchControl) handleCommand(cmd *Command) { rhdl.Printf("Telnet: got command: %+v", cmd) switch cmd.Type { case CmdStatus: case CmdServer: case CmdSwitch: } } func handleServer(in <-chan ServerStatus, out chan<- ServerStatus) { for { update := <-in out <- update } } func (ctrl *SwitchControl) Run() { rhdl.Printf("SwitchCTRL: handler running...") serverUpdates := make(chan ServerStatus, 8) for _, srv := range ctrl.servers { go handleServer(srv.Updates, serverUpdates) } for { select { case update := <-ctrl.sw.Updates: rhdl.Printf("got update from switch: %+v", update) for _, srv := range ctrl.servers { srv.SwitchUpdates <- update } 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) } } } func SwitchControlInit(conf *Config, sw *AudioSwitch, servers []*PlayoutServer) (ctrl *SwitchControl) { ctrl = &SwitchControl{} ctrl.sw = sw ctrl.servers = servers ctrl.Updates = emitter.New(32) ctrl.Commands = make(chan *Command, 8) return }