Class: OpenC3::JsonDRbObject
- Inherits:
-
JsonApiObject
- Object
- JsonApiObject
- OpenC3::JsonDRbObject
- 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
Instance Method Summary collapse
-
#initialize(url: ENV['OPENC3_API_URL'], timeout: 1.0, authentication: nil) ⇒ JsonDRbObject
constructor
A new instance of JsonDRbObject.
-
#method_missing(method_name, *method_params, **keyword_params) ⇒ Object
Forwards all method calls to the remote service.
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.
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.
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 |