Class: OpenC3::JsonApiObject

Inherits:
Object
  • Object
show all
Defined in:
lib/openc3/io/json_api_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 JsonApiObject can be used to call server methods directly:

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

Direct Known Subclasses

JsonDRbObject

Constant Summary collapse

USER_AGENT =
'OpenC3 / v7 (ruby/openc3/lib/io/json_api_object)'.freeze
DEFAULT_READ_TIMEOUT_S =

Time limit for the first response byte from the server. This must be an explicit number rather than nil, see connect() for why. Requests can legitimately block for a long time, e.g. cmd() with a large timeout waiting on an interface ack, so this is effectively "wait forever" while still bounded so a wedged connection eventually releases the thread.

86400

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, timeout: 1.0, authentication: nil, read_timeout: nil) ⇒ JsonApiObject

Returns a new instance of JsonApiObject.

Parameters:

  • url (String) —

    The url of openc3-cosmos-cmd-tlm-api http://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

  • read_timeout (Float) (defaults to: nil) —

    The time to wait for the first response byte from the server. Defaults to the OPENC3_API_READ_TIMEOUT environment variable or DEFAULT_READ_TIMEOUT_S.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/openc3/io/json_api_object.rb', line 68

def initialize(url:, timeout: 1.0, authentication: nil, read_timeout: nil)
  @http = nil
  @mutex = Mutex.new
  @request_data = ""
  @response_data = ""
  @url = url
  @log = [nil, nil, nil]
  @authentication = authentication.nil? ? generate_auth() : authentication
  @timeout = timeout
  @read_timeout = (read_timeout || ENV.fetch('OPENC3_API_READ_TIMEOUT', DEFAULT_READ_TIMEOUT_S)).to_f
  @shutdown = false
  # JsonDRb.debug = true # Enable for debugging
end

Instance Attribute Details

#request_data ⇒ Object (readonly)

Returns the value of attribute request_data.



51
52
53
# File 'lib/openc3/io/json_api_object.rb', line 51

def request_data
  @request_data
end

#response_data ⇒ Object (readonly)

Returns the value of attribute response_data.



52
53
54
# File 'lib/openc3/io/json_api_object.rb', line 52

def response_data
  @response_data
end

Instance Method Details

#disconnect ⇒ Object

Disconnects from http server



113
114
115
116
# File 'lib/openc3/io/json_api_object.rb', line 113

def disconnect
  @http.close if @http
  @http = nil
end

#generate_auth ⇒ Object

generate the auth object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/openc3/io/json_api_object.rb', line 83

def generate_auth
  if ENV['OPENC3_API_TOKEN'].nil? and ENV['OPENC3_API_USER'].nil?
    if ENV['OPENC3_API_PASSWORD']
      return OpenC3Authentication.new()
    else
      return nil
    end
  else
    return OpenC3KeycloakAuthentication.new(ENV['OPENC3_KEYCLOAK_URL'])
  end
end

#request(*method_params, **keyword_params) ⇒ Object

Forwards all method calls to the remote service.

Parameters:

  • 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.

Raises:



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/openc3/io/json_api_object.rb', line 100

def request(*method_params, **keyword_params)
  raise JsonApiError, "Shutdown" if @shutdown
  method = method_params[0]
  endpoint = method_params[1]
  @mutex.synchronize do
    kwargs = _generate_kwargs(keyword_params)
    @log = [nil, nil, nil]
    connect() if !@http
    return _send_request(method: method, endpoint: endpoint, kwargs: kwargs)
  end
end

#shutdown ⇒ Object

Permanently disconnects from the http server



119
120
121
122
# File 'lib/openc3/io/json_api_object.rb', line 119

def shutdown
  @shutdown = true
  disconnect()
end