Module: Arpie::EventMachine::ArpieProtocol

Defined in:
lib/arpie/em.rb

Overview

A EventMachine protocol implementing a simple protocol chain handler. To use, simply include it in your EM Connection moduleas you would any other EM protocol.

The module expects a list of protocols given along with the module initializer:

EM::start_server host, port, ArpieProtocol,
  Arpie::MarshalProtocol.new, Arpie::SizedProtocol.new

To receive messages, override receive(message), which will be called once for each message decoded with the given protocols.

To send messages back over the same connection, simply call send(message).

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#chainObject (readonly)

Returns the value of attribute chain.



19
20
21
# File 'lib/arpie/em.rb', line 19

def chain
  @chain
end

Instance Method Details

#initialize(*protocols) ⇒ Object



21
22
23
# File 'lib/arpie/em.rb', line 21

def initialize *protocols
  @chain = Arpie::ProtocolChain.new(*protocols)
end

#receive(message) ⇒ Object

Receive a message. Override this in your implemention.



26
27
# File 'lib/arpie/em.rb', line 26

def receive message
end

#receive_data(data) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/arpie/em.rb', line 29

def receive_data data
  begin
    for msg in @chain.from(data)
      receive msg
    end
  rescue Arpie::EIncomplete
    nil
  end
end

#send(message) ⇒ Object

Send a message, encoding it with the given protocols.



41
42
43
44
45
# File 'lib/arpie/em.rb', line 41

def send message
  for msg in @chain.to(message)
    send_data(msg)
  end
end