Class: Cosmos::JsonDRbObject

Inherits:
JsonApiObject show all
Defined in:
lib/cosmos/io/json_drb_object.rb

Overview

Used to forward all method calls to the remote server object. Before using this class ensure the remote service has been started in the server class:

json = JsonDrb.new
json.start_service('127.0.0.1', 7777, self)

Now the JsonDRbObject can be used to call server methods directly:

server = JsonDRbObject('http://cosmos-cmd-tlm-api:2901', 1.0)
server.cmd(*args)

Constant Summary collapse

USER_AGENT =
'Cosmos / v5 (ruby/cosmos/lib/io/json_drb_object)'

Instance Attribute Summary

Attributes inherited from JsonApiObject

#request_data, #response_data

Instance Method Summary collapse

Methods inherited from JsonApiObject

#disconnect, #request, #shutdown

Constructor Details

#initialize(url: ENV['COSMOS_API_URL'], timeout: 1.0, authentication: nil) ⇒ JsonDRbObject

Returns a new instance of JsonDRbObject.

Parameters:

  • url (String) (defaults to: ENV['COSMOS_API_URL'])

    The url of cosmos-cmd-tlm-api cosmos-cmd-tlm-api:2901

  • timeout (Float) (defaults to: 1.0)

    The time to wait before disconnecting 1.0

  • authentication (CosmosAuthentication) (defaults to: nil)

    The authentication object if nill initialize will generate



45
46
47
48
# File 'lib/cosmos/io/json_drb_object.rb', line 45

def initialize(url: ENV['COSMOS_API_URL'], timeout: 1.0, authentication: nil)
  super(url: url, timeout: timeout, authentication: authentication)
  @uri = URI("#{url}/cosmos-api/api")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *method_params, **keyword_params) ⇒ Object

Forwards all method calls to the remote service.

Parameters:

  • method_name (Symbol)

    Name of the method to call

  • method_params (Array)

    Array of parameters to pass to the method

  • keyword_params (Hash<Symbol, Variable>)

    Hash of keyword parameters

Returns:

  • The result of the method call. If the method raises an exception the same exception is also raised. If something goes wrong with the protocol a JsonDRbError exception is raised.

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cosmos/io/json_drb_object.rb', line 58

def method_missing(method_name, *method_params, **keyword_params)
  raise JsonDRbError, "Shutdown" if @shutdown
  @mutex.synchronize do
    for attempt in 1..3
      @log = [nil, nil, nil]
      connect() if !@http
      json_rpc_request = JsonRpcRequest.new(method_name, method_params, keyword_params, @id)
      data = json_rpc_request.to_json(:allow_nan => true)
      response_body = make_request(data: data)
      if !response_body or response_body.to_s.length < 1
        disconnect()
      else
        response = JsonRpcResponse.from_json(response_body)
        return handle_response(response: response)
      end
    end
    error = "#{attempt} no response from server: #{@log[0]} ::: #{@log[1]} ::: #{@log[2]}"
    raise JsonDRbError, error
  end
end