Class: MobME::Infrastructure::Queue::ZeroMQ::ConnectionHandler
- Inherits:
-
Object
- Object
- MobME::Infrastructure::Queue::ZeroMQ::ConnectionHandler
- Defined in:
- lib/mobme/infrastructure/queue/zeromq/connection_handler.rb
Instance Attribute Summary collapse
-
#request ⇒ Object
Returns the value of attribute request.
Instance Method Summary collapse
-
#initialize(connection, identity = "") ⇒ ConnectionHandler
constructor
A new instance of ConnectionHandler.
- #on_readable(connection, message) ⇒ Object
- #on_writable(connection) ⇒ Object
- #receive_message ⇒ Object
- #send_message(*messages) ⇒ Object
Constructor Details
#initialize(connection, identity = "") ⇒ ConnectionHandler
Returns a new instance of ConnectionHandler.
6 7 8 9 10 11 12 13 14 |
# File 'lib/mobme/infrastructure/queue/zeromq/connection_handler.rb', line 6 def initialize(connection, identity = "") @connection = connection @connection.handler = self @connection.identity = "#{identity ? "#{identity}:" : ""}#{@client_fiber.object_id}" @connection.notify_readable = false @connection.notify_writable = false @send_messages_buffer = nil end |
Instance Attribute Details
#request ⇒ Object
Returns the value of attribute request.
4 5 6 |
# File 'lib/mobme/infrastructure/queue/zeromq/connection_handler.rb', line 4 def request @request end |
Instance Method Details
#on_readable(connection, message) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/mobme/infrastructure/queue/zeromq/connection_handler.rb', line 31 def on_readable(connection, ) request = .map(&:copy_out_string).join .each { |part| part.close } @connection.notify_readable = false @connection.notify_writable = false @client_fiber.resume(request) end |
#on_writable(connection) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/mobme/infrastructure/queue/zeromq/connection_handler.rb', line 41 def on_writable(connection) return_value = connection.send_msg *@send_messages_buffer @connection.notify_readable = false @connection.notify_writable = false @client_fiber.resume(return_value) end |
#receive_message ⇒ Object
16 17 18 19 20 21 |
# File 'lib/mobme/infrastructure/queue/zeromq/connection_handler.rb', line 16 def EM.next_tick { @connection.register_readable } @client_fiber = Fiber.current Fiber.yield end |
#send_message(*messages) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/mobme/infrastructure/queue/zeromq/connection_handler.rb', line 23 def (*) @send_messages_buffer = EM.next_tick { @connection.register_writable } @client_fiber = Fiber.current Fiber.yield end |