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



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

def initialize(url, options={})
  @url = url
  @client = HTTPClient.new
  @encoding = options[:encoding] || Encoding::UTF_8
  @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



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

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.



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

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.



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

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.



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

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.



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

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