Skip to content
Snippets Groups Projects
fsm_idle.go 1005 B
Newer Older
Oliver Herms's avatar
Oliver Herms committed
package server

Oliver Herms's avatar
Oliver Herms committed
import (
	"time"
)

Oliver Herms's avatar
Oliver Herms committed
type idleState struct {
Oliver Herms's avatar
Oliver Herms committed
	fsm            *FSM
Oliver Herms's avatar
Oliver Herms committed
	newStateReason string
}

Oliver Herms's avatar
Oliver Herms committed
func newIdleState(fsm *FSM) *idleState {
Oliver Herms's avatar
Oliver Herms committed
	return &idleState{
		fsm: fsm,
	}
}

Oliver Herms's avatar
Oliver Herms committed
func (s idleState) run() (state, string) {
	if s.fsm.peer.reconnectInterval != 0 {
		time.Sleep(s.fsm.peer.reconnectInterval)
		go s.fsm.activate()
	}
Oliver Herms's avatar
Oliver Herms committed
	for {
Oliver Herms's avatar
Oliver Herms committed
		event := <-s.fsm.eventCh
		switch event {
Oliver Herms's avatar
Oliver Herms committed
		case ManualStart:
Oliver Herms's avatar
Oliver Herms committed
			return s.manualStart()
Oliver Herms's avatar
Oliver Herms committed
		case AutomaticStart:
Oliver Herms's avatar
Oliver Herms committed
			return s.automaticStart()
		case Cease:
			return newCeaseState(), "Cease"
Oliver Herms's avatar
Oliver Herms committed
		default:
			continue
		}
	}
}

Oliver Herms's avatar
Oliver Herms committed
func (s *idleState) manualStart() (state, string) {
Oliver Herms's avatar
Oliver Herms committed
	s.newStateReason = "Received ManualStart event"
Oliver Herms's avatar
Oliver Herms committed
	return s.start()
Oliver Herms's avatar
Oliver Herms committed
}

Oliver Herms's avatar
Oliver Herms committed
func (s *idleState) automaticStart() (state, string) {
Oliver Herms's avatar
Oliver Herms committed
	s.newStateReason = "Received AutomaticStart event"
Oliver Herms's avatar
Oliver Herms committed
	return s.start()
Oliver Herms's avatar
Oliver Herms committed
}

Oliver Herms's avatar
Oliver Herms committed
func (s *idleState) start() (state, string) {
Oliver Herms's avatar
Oliver Herms committed
	s.fsm.resetConnectRetryCounter()
	s.fsm.startConnectRetryTimer()
Oliver Herms's avatar
Oliver Herms committed
	go s.fsm.tcpConnect()

	return newConnectState(s.fsm), s.newStateReason
Oliver Herms's avatar
Oliver Herms committed
}