Parent

Files

RawJsonRpc::RawClientJsonRpc

Implements the base class for all Clients. The class get all methode calls generate the json data from it

Every client must implement get_response and send_request.

Examples

send request

client.request("run", [1,2,3])                  # calls: run params: 1 2 3
ret = client.request("attack", "alpha", 1, 3.0) # calls: attack
                                                # params "alpha", 1, 3.0
                                                # returns data into ret

send request shortcut

client.run([1,2,3])
ret = client.attack("alpha", 1, 3.0)

send notification (no return value)

client.notification("beat", [:alpha, 123], "fast")

Public Instance Methods

get_response() click to toggle source

returns the server response must be implmented

# File lib/rawjsonrpc/client.rb, line 52
def get_response
  raise NotImplementedError.new("method not overriden")
end
method_missing(name, *args) click to toggle source

redirect calls

# File lib/rawjsonrpc/client.rb, line 36
def method_missing(name, *args)
  if args.empty?
    request(name)
  else
    request(name, *args)
  end
end
notification(function_name, *para) click to toggle source

Sends a notification to server. A Notifications don’t return any value

# File lib/rawjsonrpc/client.rb, line 65
def notification(function_name, *para)
  para = {'method' => function_name, 'params' => para, 'id' => nil}
    .to_json
  send_request para
  nil
end
request(function_name, *para) click to toggle source

Sends a request to server return a value or raise an RpcException if server sends an error.

# File lib/rawjsonrpc/client.rb, line 73
def request(function_name, *para)
  id_count = id
  para = {'method' => function_name, 'params' =>  para, 'id' => id_count}
    .to_json
  send_request(para)
  result = get_response
  if result.empty? or result == nil
    raise(RpcError.new("empty respons"))
  end
  response = JSON.load(result)
  if response["error"] != nil
    raise(RpcError.new(response["error"]))
  elsif response["id"] != id_count
    raise(RpcError.new("server send wrong data."))
  end
  response["result"]
end
send_request(para) click to toggle source

Send the request to server must be implmented. takes json string.

# File lib/rawjsonrpc/client.rb, line 58
def send_request para
  raise NotImplementedError.new("method not overriden")
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.