summaryrefslogtreecommitdiff
path: root/cmd/dolmetschctld/statemachine.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/dolmetschctld/statemachine.go')
-rw-r--r--cmd/dolmetschctld/statemachine.go115
1 files changed, 35 insertions, 80 deletions
diff --git a/cmd/dolmetschctld/statemachine.go b/cmd/dolmetschctld/statemachine.go
index 2d8013f..a779604 100644
--- a/cmd/dolmetschctld/statemachine.go
+++ b/cmd/dolmetschctld/statemachine.go
@@ -29,54 +29,9 @@ import (
"time"
"spreadspace.org/dolmetschctl/pkg/mixer"
+ "spreadspace.org/dolmetschctl/pkg/types"
)
-type State int
-
-const (
- StateNew = iota
- StateSettling
- StateSettled
-)
-
-func (s State) String() string {
- switch s {
- case StateNew:
- return "new"
- case StateSettling:
- return "settling"
- case StateSettled:
- return "settled"
- default:
- return "unknown"
- }
-}
-
-func (s State) MarshalText() (data []byte, err error) {
- data = []byte(s.String())
- return
-}
-
-type Language string
-
-func (l Language) String() string {
- if l == "" {
- return "none"
- }
- return string(l)
-}
-
-func (s Language) MarshalText() (data []byte, err error) {
- data = []byte(s.String())
- return
-}
-
-type FullState struct {
- state State
- ratio float32
- language Language
-}
-
type MixerChannelState struct {
level mixer.FaderLevel
mute mixer.Mute
@@ -94,16 +49,16 @@ type MixerChannels struct {
}
type setLanguageReq struct {
- lang Language
+ lang types.Language
resCh chan error
}
type getLanguageReq struct {
- resCh chan Language
+ resCh chan types.Language
}
type getLanguagesReq struct {
- resCh chan []Language
+ resCh chan []types.Language
}
type setOriginal2InterpreterRatioReq struct {
@@ -116,7 +71,7 @@ type getOriginal2InterpreterRatioReq struct {
}
type getStateRes struct {
- FullState
+ types.FullState
}
type getStateReq struct {
@@ -124,7 +79,7 @@ type getStateReq struct {
}
type subscribeStateReq struct {
- ch chan<- FullState
+ ch chan<- types.FullState
resCh chan error
}
@@ -142,12 +97,12 @@ type StateMachine struct {
exitedCh chan struct{}
mixerEventCh chan mixer.Event
- languages map[Language]*MixerChannels
- channel2lang map[mixer.Channel]Language
+ languages map[types.Language]*MixerChannels
+ channel2lang map[mixer.Channel]types.Language
- state State
+ state types.State
original2InterpreterRatio float32
- language Language
+ language types.Language
stateSubscribers list.List
}
@@ -157,13 +112,13 @@ func (sm *StateMachine) publishState() {
for sub := sm.stateSubscribers.Front(); sub != nil; sub = next {
next = sub.Next()
- ch, ok := (sub.Value).(chan<- FullState)
+ ch, ok := (sub.Value).(chan<- types.FullState)
if !ok {
panic(fmt.Sprintf("statemachine: subscriber list element value has wrong type: %T", sub.Value))
}
select {
- case ch <- FullState{sm.state, sm.original2InterpreterRatio, sm.language}:
+ case ch <- types.FullState{sm.state, sm.original2InterpreterRatio, sm.language}:
default:
// subscriber is not responding...
// log.Printf("statemachine: removing subscriber '%v', because it is not responding", ch)
@@ -173,7 +128,7 @@ func (sm *StateMachine) publishState() {
}
}
-func (sm *StateMachine) subscribeState(out chan<- FullState) error {
+func (sm *StateMachine) subscribeState(out chan<- types.FullState) error {
// log.Printf("statemachine: %v subscribed to state changes", out)
sm.stateSubscribers.PushBack(out)
return nil
@@ -222,10 +177,10 @@ func (sm *StateMachine) initMixer() {
mcs.interpreter.target.level = mixer.FaderLevelOff
}
sm.language = ""
- sm.state = StateSettled
+ sm.state = types.StateSettled
}
-func (sm *StateMachine) getLanguages() (langs []Language) {
+func (sm *StateMachine) getLanguages() (langs []types.Language) {
langs = append(langs, "none")
for lang, _ := range sm.languages {
langs = append(langs, lang)
@@ -234,7 +189,7 @@ func (sm *StateMachine) getLanguages() (langs []Language) {
}
// the "current language" is what is currently spoken on stage
-func (sm *StateMachine) setLanguage(l Language) error {
+func (sm *StateMachine) setLanguage(l types.Language) error {
if l == "none" {
l = ""
}
@@ -283,18 +238,18 @@ func (sm *StateMachine) reconcile(ticker bool) {
}
}
- if sm.state != StateSettled && !ticker {
+ if sm.state != types.StateSettled && !ticker {
return
}
- sm.state = StateSettled
+ sm.state = types.StateSettled
for _, mcs := range sm.languages {
if mcs.original.target.level != mcs.original.current.level {
sm.mixer.SetLevel(mcs.original.num, calcNextLevel(mcs.original.target.level, mcs.original.current.level))
- sm.state = StateSettling
+ sm.state = types.StateSettling
}
if mcs.interpreter.target.level != mcs.interpreter.current.level {
sm.mixer.SetLevel(mcs.interpreter.num, calcNextLevel(mcs.interpreter.target.level, mcs.interpreter.current.level))
- sm.state = StateSettling
+ sm.state = types.StateSettling
}
}
}
@@ -311,7 +266,7 @@ func (sm *StateMachine) run() {
select {
case <-t.C:
- if sm.state == StateSettling {
+ if sm.state == types.StateSettling {
sm.reconcile(true)
}
case req := <-sm.setLanguageCh:
@@ -327,7 +282,7 @@ func (sm *StateMachine) run() {
case req := <-sm.getOriginal2InterpreterRatioCh:
req.resCh <- sm.original2InterpreterRatio
case req := <-sm.getStateCh:
- req.resCh <- getStateRes{FullState{sm.state, sm.original2InterpreterRatio, sm.language}}
+ req.resCh <- getStateRes{types.FullState{sm.state, sm.original2InterpreterRatio, sm.language}}
case req := <-sm.subscribeStateCh:
req.resCh <- sm.subscribeState(req.ch)
case ev := <-sm.mixerEventCh:
@@ -356,17 +311,17 @@ func NewStateMachine(m *mixer.Mixer) (*StateMachine, error) {
sm.exitedCh = make(chan struct{})
sm.mixerEventCh = make(chan mixer.Event, 1000)
- sm.languages = make(map[Language]*MixerChannels)
- sm.channel2lang = make(map[mixer.Channel]Language)
+ sm.languages = make(map[types.Language]*MixerChannels)
+ sm.channel2lang = make(map[mixer.Channel]types.Language)
- sm.state = StateNew
+ sm.state = types.StateNew
sm.language = ""
return sm, nil
}
// TODO: currently we can only deal with 2 languages...
-func (sm *StateMachine) AddLanguage(name Language, original, interpreter mixer.Channel) error {
- if sm.state != StateNew {
+func (sm *StateMachine) AddLanguage(name types.Language, original, interpreter mixer.Channel) error {
+ if sm.state != types.StateNew {
return fmt.Errorf("adding languages is only allowed during startup")
}
if name == "none" {
@@ -400,20 +355,20 @@ func (sm *StateMachine) Start() {
go sm.run()
}
-func (sm *StateMachine) SetLanguage(l Language) error {
+func (sm *StateMachine) SetLanguage(l types.Language) error {
resCh := make(chan error)
sm.setLanguageCh <- setLanguageReq{l, resCh}
return <-resCh
}
-func (sm *StateMachine) GetLanguage() Language {
- resCh := make(chan Language)
+func (sm *StateMachine) GetLanguage() types.Language {
+ resCh := make(chan types.Language)
sm.getLanguageCh <- getLanguageReq{resCh}
return <-resCh
}
-func (sm *StateMachine) GetLanguages() []Language {
- resCh := make(chan []Language)
+func (sm *StateMachine) GetLanguages() []types.Language {
+ resCh := make(chan []types.Language)
sm.getLanguagesCh <- getLanguagesReq{resCh}
return <-resCh
}
@@ -430,14 +385,14 @@ func (sm *StateMachine) GetOriginal2InterpreterRatio() float32 {
return <-resCh
}
-func (sm *StateMachine) GetState() (State, float32, Language) {
+func (sm *StateMachine) GetState() (types.State, float32, types.Language) {
resCh := make(chan getStateRes)
sm.getStateCh <- getStateReq{resCh}
res := <-resCh
- return res.state, res.ratio, res.language
+ return res.State, res.Ratio, res.Language
}
-func (sm *StateMachine) SubscribeState(out chan<- FullState) error {
+func (sm *StateMachine) SubscribeState(out chan<- types.FullState) error {
resCh := make(chan error)
sm.subscribeStateCh <- subscribeStateReq{out, resCh}
return <-resCh