diff options
| -rw-r--r-- | client/client.go | 2 | ||||
| -rw-r--r-- | client/fsm.go | 6 | ||||
| -rw-r--r-- | internal/fsm/fsm.go | 43 |
3 files changed, 20 insertions, 31 deletions
diff --git a/client/client.go b/client/client.go index a64b4a4..e5a39c0 100644 --- a/client/client.go +++ b/client/client.go @@ -145,7 +145,7 @@ func New(name string, version string, directory string, stateCallback func(FSMSt http.RegisterAgent(userAgentName(name), version) // Initialize the FSM - c.FSM = newFSM(stateCallback, directory) + c.FSM = newFSM(stateCallback) // Debug only if given c.Debug = debug diff --git a/client/fsm.go b/client/fsm.go index 3c2ed80..c8858f9 100644 --- a/client/fsm.go +++ b/client/fsm.go @@ -91,7 +91,6 @@ func GetStateName(s FSMStateID) string { func newFSM( callback func(FSMStateID, FSMStateID, interface{}) bool, - directory string, ) fsm.FSM { states := FSMStates{ StateDeregistered: FSMState{ @@ -168,9 +167,8 @@ func newFSM( }, }, } - returnedFSM := fsm.FSM{} - returnedFSM.Init(StateMain, states, callback, directory, GetStateName) - return returnedFSM + + return fsm.NewFSM(StateMain, states, callback, GetStateName) } // SetState sets the state for the client FSM to `state` diff --git a/internal/fsm/fsm.go b/internal/fsm/fsm.go index 950f7cf..5985aa5 100644 --- a/internal/fsm/fsm.go +++ b/internal/fsm/fsm.go @@ -3,11 +3,19 @@ package fsm import "fmt" +// State represents a single node in the graph. +type State struct { + // Transitions indicates which out arrows this node has + Transitions []Transition +} + type ( // StateID represents the Identifier of the state. StateID int8 // StateIDSlice represents the list of state identifiers. StateIDSlice []StateID + // States is the map from state identifier to the state itself + States map[StateID]State ) // Len is defined here such that we can sort the slice @@ -33,15 +41,6 @@ type Transition struct { Description string } -// States is the map from state identifier to the state itself -type States map[StateID]State - -// State represents a single node in the graph. -type State struct { - // Transitions indicates which out arrows this node has - Transitions []Transition -} - // FSM represents the total graph. type FSM struct { // States is the map from state ID to states @@ -57,9 +56,6 @@ type FSM struct { // It takes the old state, the new state and the data and returns if this is handled by the client StateCallback func(StateID, StateID, interface{}) bool - // Directory represents the path where the state graph is stored - Directory string - // GetStateName gets the name of a state as a string GetStateName func(StateID) string @@ -67,20 +63,15 @@ type FSM struct { initial StateID } -// Init initializes the state machine and sets it to the given current state. -func (fsm *FSM) Init( - current StateID, - states States, - callback func(StateID, StateID, interface{}) bool, - directory string, - nameGen func(StateID) string, -) { - fsm.States = states - fsm.Current = current - fsm.StateCallback = callback - fsm.Directory = directory - fsm.GetStateName = nameGen - fsm.initial = current +// NewFSM creates a new finite state machine +func NewFSM(current StateID, states States, callback func(StateID, StateID, interface{}) bool, nameGen func(StateID) string) FSM { + return FSM{ + States: states, + Current: current, + StateCallback: callback, + GetStateName: nameGen, + initial: current, + } } // InState returns whether or not the state machine is in the given 'check' state. |
