Class: RPCPlus::Client

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO
Defined in:
lib/rpcplus/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rpcplus/client.rb', line 9

def initialize(host, port)
  @seq = 0
  @requests = {}

  @sock = Celluloid::IO::TCPSocket.new(host, port)
  @sock.write("CONNECT /_goRPC_ HTTP/1.0\r\nAccept: application/vnd.flynn.rpc-hijack+json\r\n\r\n")
  response = @sock.readline(/\r?\n/)
  raise 'invalid response' if !response.start_with?('HTTP/1.0 200')
  @sock.readline(/\r?\n/)
  async.read_responses
end

Instance Method Details

#request(method, arg, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rpcplus/client.rb', line 21

def request(method, arg, &block)
  req = { 'id' => @seq+=1, 'method' => method, 'params' => [arg] }
  future = Celluloid::Future.new
  @requests[req['id']] = { stream: block, future: future }
  Yajl::Encoder.encode(req, @sock)

  # block until stream is done, so that the block doesn't become invalid due
  # to https://github.com/celluloid/celluloid/pull/245
  future.value if block

  future
end