Class: Rcom::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/rcom/rpc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Response

Returns a new instance of Response.



31
32
33
34
35
# File 'lib/rcom/rpc.rb', line 31

def initialize(params)
  @node = params[:node]
  @channel = params[:channel]
  @server = params[:server]
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



29
30
31
# File 'lib/rcom/rpc.rb', line 29

def channel
  @channel
end

#nodeObject (readonly)

Returns the value of attribute node.



29
30
31
# File 'lib/rcom/rpc.rb', line 29

def node
  @node
end

#serverObject (readonly)

Returns the value of attribute server.



29
30
31
# File 'lib/rcom/rpc.rb', line 29

def server
  @server
end

Instance Method Details

#send_method(method, args) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/rcom/rpc.rb', line 60

def send_method(method, args)
  begin
    server.send(method, args)
  rescue
    return nil
  end
end

#serveObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rcom/rpc.rb', line 37

def serve
  begin
    loop do
      ch, request = node.brpop(channel)

      message = MessagePack.unpack(
        request,
        symbolize_keys: true
      )
      response = send_method(
        message[:method],
        message[:args]
      )

      node.rpush(message[:id], response.to_msgpack)
    end
  rescue
    sleep 1
    retry
  rescue Interrupt => _
  end
end