summaryrefslogtreecommitdiff
path: root/src/rhctl/switch_control.go
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2016-04-02 23:13:25 (GMT)
committerChristian Pointner <equinox@helsinki.at>2016-04-02 23:13:25 (GMT)
commit2f34a5e62285a3c5334c270c6111e43fc12e8dbf (patch)
tree8931b4d9fa0262f3ccc61d6b59d28a7ebd2a34dd /src/rhctl/switch_control.go
parentdf49dff4c6f96d1c2e032cc93e7b6bdb57f11833 (diff)
refactoring of server, switch states
Diffstat (limited to 'src/rhctl/switch_control.go')
-rw-r--r--src/rhctl/switch_control.go90
1 files changed, 79 insertions, 11 deletions
diff --git a/src/rhctl/switch_control.go b/src/rhctl/switch_control.go
index f52e9a2..91cc161 100644
--- a/src/rhctl/switch_control.go
+++ b/src/rhctl/switch_control.go
@@ -27,18 +27,67 @@ import (
"github.com/btittelbach/pubsub"
)
+type Mood uint
+
+const (
+ MoodAwakening Mood = iota
+ MoodSad
+ MoodNervous
+ MoodHappy
+)
+
+func (m Mood) String() string {
+ switch m {
+ case MoodAwakening:
+ return "awakening"
+ case MoodSad:
+ return "sad"
+ case MoodNervous:
+ return "nervous"
+ case MoodHappy:
+ return "happy"
+ }
+ return "unknown"
+}
+
+type State struct {
+ Mood Mood
+ Switch SwitchState
+ ActiveServer string
+ Server map[string]ServerState
+}
+
+func (ctrl *SwitchControl) updateOverallState(update interface{}) {
+ switch update.(type) {
+ case SwitchState:
+ // s := update.(SwitchState)
+ rhdl.Printf("SwitchCTRL overall-state: got switch update")
+ // TODO: update ctrl.state
+ // TODO: publish new "state"
+ case ServerState:
+ s := update.(ServerState)
+ rhdl.Printf("SwitchCTRL overall-state: got server update from '%s'", s.Name)
+ // TODO: update ctrl.state
+ // TODO: publish new "state"
+ default:
+ rhl.Printf("SwitchCTRL overall-state: got unknown state-update of type %T", update)
+ return
+ }
+ rhdl.Printf("SwitchCTRL overall-state: %+v", ctrl.state)
+}
+
type CommandType uint8
const (
- CmdStatus CommandType = iota
+ CmdState CommandType = iota
CmdServer
CmdSwitch
)
func (c CommandType) String() string {
switch c {
- case CmdStatus:
- return "status"
+ case CmdState:
+ return "state"
case CmdServer:
return "server"
case CmdSwitch:
@@ -56,13 +105,14 @@ type Command struct {
type SwitchControl struct {
sw *AudioSwitch
servers []*PlayoutServer
+ state State
Updates *pubsub.PubSub
Commands chan *Command
}
func (ctrl *SwitchControl) handleCommand(cmd *Command) {
switch cmd.Type {
- case CmdStatus:
+ case CmdState:
case CmdServer:
case CmdSwitch:
if len(cmd.Args) == 0 {
@@ -79,19 +129,29 @@ func (ctrl *SwitchControl) handleCommand(cmd *Command) {
}
}
-func handleServer(in <-chan ServerStatus, out chan<- ServerStatus) {
+func handleServer(in <-chan ServerState, out chan<- ServerState) {
for {
update := <-in
out <- update
}
}
+func (ctrl *SwitchControl) reconcile() {
+ rhdl.Printf("SwitchCTRL: reconciling state...")
+ // TODO: set mood dependent on overall-state
+ // if mood changes publish new "state"
+
+ // TODO: send out commands to switch, server to get missing infos
+
+ // TODO: change switch output mappings if servers change channels or fail
+}
+
func (ctrl *SwitchControl) Run() {
rhdl.Printf("SwitchCTRL: handler running...")
- serverUpdates := make(chan ServerStatus, 8)
+ serverStateChanges := make(chan ServerState, 8)
for _, srv := range ctrl.servers {
- go handleServer(srv.Updates, serverUpdates)
+ go handleServer(srv.StateChanges, serverStateChanges)
}
for {
@@ -102,10 +162,16 @@ func (ctrl *SwitchControl) Run() {
srv.SwitchUpdates <- update
}
ctrl.Updates.Pub(update, "switch:"+update.Type.String())
- case status := <-serverUpdates:
- rhdl.Printf("got server status update: %+v", status)
- ctrl.Updates.Pub(status, "server:status")
- // TODO: recalculate overall status and send out commands to switch
+ case state := <-ctrl.sw.StateChanges:
+ rhdl.Printf("got switch state update: %+v", state)
+ ctrl.Updates.Pub(state, "switch:state")
+ ctrl.updateOverallState(state)
+ ctrl.reconcile()
+ case state := <-serverStateChanges:
+ rhdl.Printf("got server state update: %+v", state)
+ ctrl.Updates.Pub(state, "server:state")
+ ctrl.updateOverallState(state)
+ ctrl.reconcile()
case cmd := <-ctrl.Commands:
ctrl.handleCommand(cmd)
}
@@ -116,6 +182,8 @@ func SwitchControlInit(conf *Config, sw *AudioSwitch, servers []*PlayoutServer)
ctrl = &SwitchControl{}
ctrl.sw = sw
ctrl.servers = servers
+ ctrl.state.Mood = MoodAwakening
+ ctrl.state.Server = make(map[string]ServerState)
ctrl.Updates = pubsub.NewNonBlocking(32)
ctrl.Commands = make(chan *Command, 8)
return