Class: EventState::ObjectMachine

Inherits:
Machine
  • Object
show all
Includes:
EventMachine::P::ObjectProtocol
Defined in:
lib/event_state/object_machine.rb

Overview

Base class for a machine in which the messages are ruby objects. See the README for examples.

Instance Attribute Summary

Attributes inherited from Machine

#state

Instance Method Summary collapse

Methods inherited from Machine

#add_state_timer, on_enter, on_exit, on_recv, on_send, on_unbind, #post_init, print_state_machine_dot, protocol, reverse_protocol, state, timeout, #transition_on_recv, #transition_on_send, transitions, #unbind

Instance Method Details

#message_name(message) ⇒ Object

Return the class of the message as the message name. You can override this method to provide your own mapping from messages to names.

Parameters:

  • message (Object)

Returns:

  • (Object)

    must be hashable and comparable by value; for example, a symbol, string, number or class makes a good message name



18
19
20
# File 'lib/event_state/object_machine.rb', line 18

def message_name message
  message.class
end

#receive_object(message) ⇒ nil

Called by EventMachine (actually, the ObjectProtocol) when a message is received; makes the appropriate transition.

Note: if you want to receive non-messages as well, you should override this method in your subclass, and call super only when a message is received.

Parameters:

  • message (Object)

    received

Returns:

  • (nil)


34
35
36
# File 'lib/event_state/object_machine.rb', line 34

def receive_object message
  transition_on_recv message_name(message), message
end

#send_message(message) ⇒ nil

Call ObjectProtocol‘s send_object on the message and make the transition.

Parameters:

  • message (Object)

    to be sent

Returns:

  • (nil)


46
47
48
49
50
51
# File 'lib/event_state/object_machine.rb', line 46

def send_message message
  raise "not on the reactor thread" unless EM.reactor_thread?
  transition_on_send message_name(message), message do |msg|
    send_object msg
  end
end