From fb6188fb495e8c2acd0a069566938436740784d0 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 21 Apr 2016 02:21:05 +0200 Subject: make reconcile simpler - needs testing... diff --git a/src/rhctl/conf.go b/src/rhctl/conf.go index bf68fec..3cfaeac 100644 --- a/src/rhctl/conf.go +++ b/src/rhctl/conf.go @@ -40,13 +40,21 @@ type ConfigSwitchInputs []struct { func (p ConfigSwitchInputs) Len() int { return len(p) } func (p ConfigSwitchInputs) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p ConfigSwitchInputs) Less(i, j int) bool { return p[i].Number < p[j].Number } -func (p ConfigSwitchInputs) Get(server, channel string) SwitchInputNum { +func (p ConfigSwitchInputs) GetNumber(server, channel string) (SwitchInputNum, error) { for _, i := range p { if i.Server == server && i.Channel == channel { - return i.Number + return i.Number, nil } } - return SwitchInputNum(0) + return SwitchInputNum(0), errors.New("no such server/channel") +} +func (p ConfigSwitchInputs) GetServerAndChannel(in SwitchInputNum) (string, string, error) { + for _, i := range p { + if i.Number == in { + return i.Server, i.Channel, nil + } + } + return "", "", errors.New("no such input") } type Config struct { diff --git a/src/rhctl/switch_control.go b/src/rhctl/switch_control.go index a7b5a58..ec77b2e 100644 --- a/src/rhctl/switch_control.go +++ b/src/rhctl/switch_control.go @@ -165,17 +165,12 @@ func (ctrl *SwitchControl) getSwitchServerAssignment() (swsrv, swch string, err return "", "", errors.New("no or more than one input is active") } - for _, in := range ctrl.sw.Inputs { - if in.Number == activeInput { - return in.Server, in.Channel, nil - } - } - return "", "", errors.New("unknown input") + return ctrl.sw.Inputs.GetServerAndChannel(activeInput) } func (ctrl *SwitchControl) selectServer(server string) bool { - newIn := ctrl.sw.Inputs.Get(server, ctrl.state.Server[server].Channel) - if newIn == SwitchInputNum(0) { + newIn, err := ctrl.sw.Inputs.GetNumber(server, ctrl.state.Server[server].Channel) + if err != nil { rhdl.Printf("SwitchCTRL: no audio input configured for server/channel: '%s/%s'", server, ctrl.state.Server[server].Channel) return false } @@ -207,79 +202,57 @@ func (ctrl *SwitchControl) selectNextValidServer() { } func (ctrl *SwitchControl) reconcile(requestedServer string) (result bool) { + // TODO: requested server is ignored in any case... fix this if !ctrl.state.Settled { return } + if len(ctrl.servers) < 1 { + ctrl.state.Mood = MoodHappy + rhdl.Printf("SwitchCTRL: there are no servers configured -> now in mood: %s", ctrl.state.Mood) + return + } + if ctrl.state.Mood == MoodAwakening && requestedServer != "" { + rhl.Printf("SwitchCTRL: ignoreing preferred server requests while waking-up") + return + } rhdl.Printf("SwitchCTRL: reconciling state... (requested server: '%s')", requestedServer) - switch ctrl.state.Mood { - case MoodAwakening: - if len(ctrl.servers) < 1 { - ctrl.state.Mood = MoodHappy - rhdl.Printf("SwitchCTRL: there are no servers configured -> now in mood: %s", ctrl.state.Mood) - return - } - - if requestedServer != "" { - rhl.Printf("SwitchCTRL: ignoreing preferred server requests while waking-up") - return - } - - swsrv, swch, err := ctrl.getSwitchServerAssignment() - if err != nil { - ctrl.state.Mood = MoodSad - rhl.Printf("SwitchCTRL: just woken up... switch input assignments are ambigious or outdated -> now in mood: %s", ctrl.state.Mood) - return - } + swsrv, swch, err := ctrl.getSwitchServerAssignment() + if err != nil { + ctrl.state.Mood = MoodSad + rhl.Printf("SwitchCTRL: switch input assignments are ambigious -> now in mood: %s", ctrl.state.Mood) + return + } + rhl.Printf("SwitchCTRL: switch is set to server/channel: '%s/%s'", swsrv, swch) - rhl.Printf("SwitchCTRL: just woken up... switch is set to server '%s' channel '%s'", swsrv, swch) + s, exists := ctrl.state.Server[swsrv] + if !exists || s.Health != ServerAlive || s.Channel == "" { + rhl.Printf("SwitchCTRL: server '%s' is unknown, dead or has invalid channel!", swsrv) + ctrl.selectNextValidServer() + return + } + ctrl.state.ActiveServer = swsrv - s, exists := ctrl.state.Server[swsrv] - if !exists || s.Health != ServerAlive || s.Channel == "" { - rhl.Printf("SwitchCTRL: server '%s' is unknown, dead or has invalid channel!", swsrv) + if s.Channel != swch { + // TODO: during normal operation switching server channels is expected and shouldn't lead to mood nervous?!?!? + ctrl.state.Mood = MoodNervous + rhl.Printf("SwitchCTRL: switch and server channel mismatch: '%s' != '%s' -> now in mood: %s", swch, s.Channel, ctrl.state.Mood) + if !ctrl.selectServer(ctrl.state.ActiveServer) { ctrl.selectNextValidServer() - return - } - - ctrl.state.ActiveServer = swsrv - - if s.Channel != swch { - ctrl.state.Mood = MoodNervous - rhl.Printf("SwitchCTRL: switch and server channel mismatch: '%s' != '%s' -> now in mood: %s", swch, s.Channel, ctrl.state.Mood) - if !ctrl.selectServer(ctrl.state.ActiveServer) { - ctrl.selectNextValidServer() - } - return } - - for _, s := range ctrl.state.Server { - if s.Health != ServerAlive { - ctrl.state.Mood = MoodNervous - rhl.Printf("SwitchCTRL: at least one configured server is dead -> now in mood: %s", ctrl.state.Mood) - return - } - } - ctrl.state.Mood = MoodHappy - rhl.Printf("SwitchCTRL: all servers are alive -> now in mood: %s", ctrl.state.Mood) return + } - default: - if len(ctrl.servers) < 1 { - ctrl.state.Mood = MoodHappy - rhdl.Printf("SwitchCTRL: there are no servers configured -> now in mood: %s", ctrl.state.Mood) + for _, s := range ctrl.state.Server { + if s.Health != ServerAlive { + ctrl.state.Mood = MoodNervous + rhl.Printf("SwitchCTRL: at least one configured server is dead -> now in mood: %s", ctrl.state.Mood) return } - - rhl.Printf("SwitchCTRL: error handling of mood %s is not implemented", ctrl.state.Mood) } - - // TODO: change active server if it fails or on request - // return true if requested server got selected - - // TODO: change switch output mappings if servers change channels or fail - - // TODO: set mood dependent on overall-state + ctrl.state.Mood = MoodHappy + rhl.Printf("SwitchCTRL: all servers are alive -> now in mood: %s", ctrl.state.Mood) return } -- cgit v0.10.2