Class: OpenC3::ServerProxy
Overview
Provides a proxy to the JsonDRbObject which communicates with the API server
Instance Method Summary collapse
-
#generate_auth ⇒ Object
generate the auth object.
-
#generate_timeout ⇒ Object
pull openc3-cosmos-cmd-tlm-api timeout from environment variables.
-
#generate_url ⇒ Object
pull openc3-cosmos-cmd-tlm-api url from environment variables.
-
#initialize ⇒ ServerProxy
constructor
Create a JsonDRbObject connection to the API server.
-
#method_missing(method_name, *method_params, **kw_params) ⇒ Object
Ruby method which captures any method calls on this object.
Constructor Details
#initialize ⇒ ServerProxy
Create a JsonDRbObject connection to the API server
275 276 277 278 279 280 281 |
# File 'lib/openc3/script/script.rb', line 275 def initialize @json_drb = JsonDRbObject.new( url: generate_url(), timeout: generate_timeout(), authentication: generate_auth() ) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *method_params, **kw_params) ⇒ Object
Ruby method which captures any method calls on this object. This allows us to proxy the methods to the API server through the JsonDRbObject.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/openc3/script/script.rb', line 285 def method_missing(method_name, *method_params, **kw_params) # Must call shutdown and disconnect on the JsonDRbObject itself # to avoid it being sent to the API kw_params[:scope] = $openc3_scope unless kw_params[:scope] case method_name when :shutdown @json_drb.shutdown when :request @json_drb.request(*method_params, **kw_params) else # If :disconnect is there delete it and return the value # If it is not there, delete returns nil disconnect = kw_params.delete(:disconnect) if $disconnect return disconnect if disconnect # The only commands allowed through in disconnect mode are read-only # Thus we allow the get, list, tlm and limits_enabled and subscribe methods if method_name =~ /\w*_get$|^get_\w*|\w*_list$|^list_\w*|^tlm|^limits_enabled$|^subscribe$/ return @json_drb.method_missing(method_name, *method_params, **kw_params) else return nil end else @json_drb.method_missing(method_name, *method_params, **kw_params) end end end |
Instance Method Details
#generate_auth ⇒ Object
generate the auth object
262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/openc3/script/script.rb', line 262 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 |
#generate_timeout ⇒ Object
pull openc3-cosmos-cmd-tlm-api timeout from environment variables
256 257 258 259 |
# File 'lib/openc3/script/script.rb', line 256 def generate_timeout timeout = ENV['OPENC3_API_TIMEOUT'] || '1.0' return timeout.to_f end |
#generate_url ⇒ Object
pull openc3-cosmos-cmd-tlm-api url from environment variables
247 248 249 250 251 252 253 |
# File 'lib/openc3/script/script.rb', line 247 def generate_url schema = ENV['OPENC3_API_SCHEMA'] || 'http' hostname = ENV['OPENC3_API_HOSTNAME'] || (ENV['OPENC3_DEVEL'] ? '127.0.0.1' : 'openc3-cosmos-cmd-tlm-api') port = ENV['OPENC3_API_PORT'] || '2901' port = port.to_i return "#{schema}://#{hostname}:#{port}" end |