Class: OpenC3::JsonDRbObject

Inherits:
JsonApiObject show all
Defined in:
lib/openc3/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://openc3-cosmos-cmd-tlm-api:2901', 1.0)
server.cmd(*args)

Constant Summary collapse

USER_AGENT =
'OpenC3 / v5 (ruby/openc3/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, #generate_auth, #request, #shutdown

Constructor Details

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

Returns a new instance of JsonDRbObject.

Parameters:

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

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

  • timeout (Float) (defaults to: 1.0)

    The time to wait before disconnecting 1.0

  • authentication (OpenC3Authentication) (defaults to: nil)

    The authentication object if nill initialize will generate



46
47
48
49
# File 'lib/openc3/io/json_drb_object.rb', line 46

def initialize(url: ENV['OPENC3_API_URL'], timeout: 1.0, authentication: nil)
  super(url: url, timeout: timeout, authentication: authentication)
  @uri = URI("#{url}/openc3-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:



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

def method_missing(method_name, *method_params, **keyword_params)
  raise JsonDRbError, "Shutdown" if @shutdown
  @mutex.synchronize do
    @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)
    token = keyword_params[:token]
    response_body = make_request(data: data, token: token)
    if !response_body or response_body.to_s.length < 1
      disconnect()
    else
      response = JsonRpcResponse.from_json(response_body)
      return handle_response(response: response)
    end
    error = "No response from server: #{@log[0]} ::: #{@log[1]} ::: #{@log[2]}"
    raise JsonDRbError, error
  end
end