Class: MessagePack::RPCOverHTTP::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/msgpack/rpc_over_http/client.rb

Overview

Cliet for MessagePack-RPC over HTTP.

Constant Summary collapse

HEADER =
{"Content-Type" => 'application/x-msgpack'}

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
# File 'lib/msgpack/rpc_over_http/client.rb', line 14

def initialize(url, options={})
  @url = url
  @client = HTTPClient.new
  @reqtable = {}
  @seqid = 0
end

Instance Method Details

#call(method, *args) ⇒ Object

call-seq:

call(symbol, *args) -> result of remote method

Calls remote method. This method is same as call_async(method, *args).value



31
32
33
# File 'lib/msgpack/rpc_over_http/client.rb', line 31

def call(method, *args)
  return send_request(method, args)
end

#call_async(method, *args) ⇒ Object

call-seq:

call_async(symbol, *args) -> Celluloid::Future

Calls remote method asynchronously. This method is non-blocking and returns Future.



40
41
42
43
# File 'lib/msgpack/rpc_over_http/client.rb', line 40

def call_async(method, *args)
  require 'celluloid'
  return Celluloid::Future.new{ send_request(method, args) }
end

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

call-seq:

callback(symbol, *args) {|res, err| } -> Celluloid::Future

Calls remote method asynchronously. The callback method is called with Future when the result is reached. ‘err’ is assigned a instance of RemoteError or child if res is nil.



51
52
53
54
55
56
57
58
59
60
# File 'lib/msgpack/rpc_over_http/client.rb', line 51

def callback(method, *args, &block)
  require 'celluloid'
  return Celluloid::Future.new do
    begin
      block.call(send_request(method, args))
    rescue RemoteError => ex
      block.call(nil, ex)
    end
  end
end

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

call-seq:

stream(symbol, *args) {|chunk| }

Calls remote method with streaming. Remote method have to return a chunked response.



67
68
69
70
71
72
73
74
75
76
# File 'lib/msgpack/rpc_over_http/client.rb', line 67

def stream(method, *args, &block)
  data = create_request_body(method, args)
  @client.post_content(@url, :body => data, :header => HEADER) do |chunk|
    begin
      block.call(get_result(chunk))
    rescue RemoteError => ex
      block.call(nil, ex)
    end
  end
end

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

call-seq:

stream_async(symbol, *args) {|chunk| } -> Celluloid::Future

Calls remote method asynchronously with streaming.



82
83
84
85
86
87
# File 'lib/msgpack/rpc_over_http/client.rb', line 82

def stream_async(method, *args, &block)
  require 'celluloid'
  return Celluloid::Future.new do
    stream(method, *args, &block)
  end
end