Class: MobME::Infrastructure::Queue::ZeroMQ::ConnectionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/mobme/infrastructure/queue/zeromq/connection_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#requestObject

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, message)
  request = message.map(&:copy_out_string).join
  message.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_messageObject



16
17
18
19
20
21
# File 'lib/mobme/infrastructure/queue/zeromq/connection_handler.rb', line 16

def receive_message
  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_message(*messages)
  @send_messages_buffer = messages
  EM.next_tick { @connection.register_writable }

  @client_fiber = Fiber.current
  Fiber.yield 
end