Class: Cosmos::ServerProxy

Inherits:
Object show all
Defined in:
lib/cosmos/script/script.rb

Overview

Provides a proxy to the JsonDRbObject which communicates with the API server

Instance Method Summary collapse

Constructor Details

#initializeServerProxy

Create a JsonDRbObject connection to the API server



201
202
203
204
205
206
207
# File 'lib/cosmos/script/script.rb', line 201

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.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/cosmos/script/script.rb', line 211

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
  case method_name
  when :shutdown
    @json_drb.shutdown
  when :request
    @json_drb.request(*method_params, **kw_params, scope: $cosmos_scope)
  else
    if $disconnect
      result = nil
      # If :disconnect is there delete it and return the value
      # If it is not there, delete returns nil
      disconnect = kw_params.delete(: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 =~ /get_\w*|list_\w*|^tlm|limits_enabled|subscribe/
        result = @json_drb.method_missing(method_name, *method_params, **kw_params, scope: $cosmos_scope)
      end
      # If they overrode the return value using the disconnect keyword then return that
      return disconnect ? disconnect : result
    else
      @json_drb.method_missing(method_name, *method_params, **kw_params, scope: $cosmos_scope)
    end
  end
end

Instance Method Details

#generate_authObject

generate the auth object



192
193
194
195
196
197
198
# File 'lib/cosmos/script/script.rb', line 192

def generate_auth
  if ENV['COSMOS_API_USER'].nil?
    return CosmosAuthentication.new()
  else
    return CosmosKeycloakAuthentication.new(ENV['COSMOS_KEYCLOAK_URL'])
  end
end

#generate_timeoutObject

pull cosmos-cmd-tlm-api timeout from environment variables



186
187
188
189
# File 'lib/cosmos/script/script.rb', line 186

def generate_timeout
  timeout = ENV['COSMOS_API_TIMEOUT'] || '1.0'
  return timeout.to_f
end

#generate_urlObject

pull cosmos-cmd-tlm-api url from environment variables



177
178
179
180
181
182
183
# File 'lib/cosmos/script/script.rb', line 177

def generate_url
  schema = ENV['COSMOS_API_SCHEMA'] || 'http'
  hostname = ENV['COSMOS_API_HOSTNAME'] || (ENV['COSMOS_DEVEL'] ? '127.0.0.1' : 'cosmos-cmd-tlm-api')
  port = ENV['COSMOS_API_PORT'] || '2901'
  port = port.to_i
  return "#{schema}://#{hostname}:#{port}"
end