Module: Rubarb::OutgoingConnection

Includes:
RemoteCall
Included in:
OutgoingHandler
Defined in:
lib/rubarb/outgoing_connection.rb

Instance Method Summary collapse

Methods included from RemoteCall

#marshal_call, #unmarshal_call

Instance Method Details

#cancel_keep_aliveObject



27
28
29
# File 'lib/rubarb/outgoing_connection.rb', line 27

def cancel_keep_alive
  EventMachine::cancel_timer(@keep_alive_timer) if @keep_alive_timer
end

#receive_message(message) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rubarb/outgoing_connection.rb', line 7

def receive_message(message)
  id, *unmarshaled_message = *unmarshal_call(message)
  if unmarshaled_message.first.is_a?(Exception)
    call_errbacks(*unmarshaled_message)
  else
    if @callback[id]
      @callback[id].call(*unmarshaled_message)
      @callback.delete(id)
    end
  end
end

#remote_call(method, *args, &block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/rubarb/outgoing_connection.rb', line 19

def remote_call(method, *args, &block)
  reset_keep_alive
  id = @msg_id_generator.next
  @callback ||= {}
  @callback[id] = block
  send_message(marshal_call(id, method, *args))
end

#reset_keep_aliveObject



31
32
33
34
35
36
37
38
39
# File 'lib/rubarb/outgoing_connection.rb', line 31

def reset_keep_alive
  cancel_keep_alive
  return if @keep_alive_time.to_i == 0

  @keep_alive_timer = EventMachine::add_timer(@keep_alive_time) do
    send_message(marshal_call(""))
    reset_keep_alive
  end
end