Class: FSM

Inherits:
Object
  • Object
show all
Defined in:
lib/netutils/fsm.rb

Direct Known Subclasses

Parser

Defined Under Namespace

Classes: State

Constant Summary collapse

FSM_S_INIT =
0

Instance Method Summary collapse

Constructor Details

#initializeFSM

Returns a new instance of FSM.



12
13
14
15
16
17
# File 'lib/netutils/fsm.rb', line 12

def initialize
	@state = FSM_S_INIT
	@fsm = Array.new
	@name2index = Hash.new
	@index2name = Array.new
end

Instance Method Details

#add(name, cb) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/netutils/fsm.rb', line 19

def add(name, cb)
	if @name2index.key?(name)
		raise(ArgumentError, "duplicated key \"#{name}\"")
	end
	@fsm.push(State.new(cb))
	idx = @fsm.length - 1
	@name2index[name] = idx
	@index2name[idx]  = name
end

#cbObject



29
30
31
# File 'lib/netutils/fsm.rb', line 29

def cb
	return @fsm[@state].cb
end

#changeto(name) ⇒ Object



33
34
35
36
37
38
# File 'lib/netutils/fsm.rb', line 33

def changeto(name)
	if ! @name2index.key?(name)
		raise(ArgumentError, "unnown state: \"#{name}\"")
	end
	@state = @name2index[name]
end

#state_nameObject



40
41
42
# File 'lib/netutils/fsm.rb', line 40

def state_name
	return @index2name[@state]
end