// // 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 ( "fmt" "strings" ) type ServerHealth bool const ( Dead ServerHealth = false Alive = true ) type ServerStatus struct { health ServerHealth channel string } type PlayoutServer struct { name string control *SerialPort heartbeat *SerialPort 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() { stop := make(chan bool) srv.control.Run(stop) srv.heartbeat.Run(stop) rhdl.Printf("Server(%s) handler running...", srv.name) for { 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.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) return } 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 } return }