Class: Hypertube::Sdk::RuntimeContext

Inherits:
Internal::AbstractRuntimeContext show all
Defined in:
lib/hypertube-ruby-sdk/sdk/runtime_context.rb

Constant Summary collapse

@@memory_runtime_contexts =
{}
@@network_runtime_contexts =
{}
@@ws_runtime_contexts =
{}
@@http2_runtime_contexts =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime_name, connection_data) ⇒ RuntimeContext

accessor set method



35
36
37
38
39
40
41
42
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 35

def initialize(runtime_name, connection_data)
  @runtime_name = runtime_name
  @connection_data = connection_data
  @current_command = nil
  @is_stateless = false
  Hypertube::Sdk::Tools::SdkMessageHelper.send_message_to_app_insights('SdkMessage',
                                                Hypertube::Utils::RuntimeNameHandler.get_name(@runtime_name).capitalize + ' initialized')
end

Instance Attribute Details

#connection_dataObject

accessor get method



30
31
32
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 30

def connection_data
  @connection_data
end

#current_commandObject

accessor get method



30
31
32
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 30

def current_command
  @current_command
end

#is_statelessObject

accessor get method



30
31
32
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 30

def is_stateless
  @is_stateless
end

#runtime_nameObject (readonly)

Returns the value of attribute runtime_name.



31
32
33
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 31

def runtime_name
  @runtime_name
end

Class Method Details

.build_connection_cache_key(runtime_name, connection_data) ⇒ Object



88
89
90
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 88

def self.build_connection_cache_key(runtime_name, connection_data)
  runtime_name.to_s + '|' + connection_data.to_canonical_key
end

.get_instance(runtime_name, connection_data) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 44

def self.get_instance(runtime_name, connection_data)
  rtm_ctx = case connection_data.connection_type
            when Hypertube::Utils::ConnectionType::IN_MEMORY
              if @@memory_runtime_contexts.has_key?(runtime_name)
                @@memory_runtime_contexts[runtime_name]
              else
                runtime_ctx = Hypertube::Sdk::RuntimeContext.new(runtime_name, connection_data)
                @@memory_runtime_contexts[runtime_name] = runtime_ctx
                runtime_ctx
              end
            when Hypertube::Utils::ConnectionType::TCP
              key = build_connection_cache_key(runtime_name, connection_data)
              if @@network_runtime_contexts.has_key?(key)
                @@network_runtime_contexts[key]
              else
                runtime_ctx = Hypertube::Sdk::RuntimeContext.new(runtime_name, connection_data)
                @@network_runtime_contexts[key] = runtime_ctx
                runtime_ctx
              end
            when Hypertube::Utils::ConnectionType::WEB_SOCKET
              key = build_connection_cache_key(runtime_name, connection_data)
              if @@ws_runtime_contexts.has_key?(key)
                @@ws_runtime_contexts[key]
              else
                runtime_ctx = Hypertube::Sdk::RuntimeContext.new(runtime_name, connection_data)
                @@ws_runtime_contexts[key] = runtime_ctx
                runtime_ctx
              end
            when Hypertube::Utils::ConnectionType::HTTP2
              key = build_connection_cache_key(runtime_name, connection_data)
              if @@http2_runtime_contexts.has_key?(key)
                @@http2_runtime_contexts[key]
              else
                runtime_ctx = Hypertube::Sdk::RuntimeContext.new(runtime_name, connection_data)
                @@http2_runtime_contexts[key] = runtime_ctx
                runtime_ctx
              end
            else
              raise ArgumentError, 'Unknown connection type.'
            end
  rtm_ctx.current_command = nil
  rtm_ctx
end

.initialize_runtime_context(config) ⇒ Hypertube::Sdk::RuntimeContext

Initializes Hypertube::Sdk::RuntimeContext based on the Hypertube::Sdk::Configuration::Config object and loads modules.

Parameters:

Returns:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 95

def self.initialize_runtime_context(config)
  raise 'Hypertube::Sdk::Configuration::Config cannot be null.' if config.nil?

  connection_data = config.connection_data
  runtime = config.runtime
  connection_data = Hypertube::Utils::InMemoryConnectionData.new if connection_data.nil?

  rtm_ctx = get_instance(runtime, connection_data)
  rtm_ctx.is_stateless = config.is_stateless

  if connection_data.connection_type == Hypertube::Utils::ConnectionType::IN_MEMORY
    modules = config.modules
    if modules && !modules.strip.empty?
      modules.split(',').each do |module_path|
        trimmed = module_path.strip
        next if trimmed.empty?

        begin
          full_path = File.realpath(File.expand_path(trimmed))
          rtm_ctx.load_library(full_path)
        rescue StandardError
          raise "Error resolving path for module: #{trimmed}"
        end
      end
    end
  end

  rtm_ctx
end

Instance Method Details

#as_out(*args) ⇒ Hypertube::Sdk::InvocationContext

Creates a reference type argument that can be passed to a method with an out parameter modifier. This method is used when working with methods from the called runtime that require arguments to be passed by reference. The arguments include the value and optionally the type of the reference. The type must be retrieved from the called runtime using the getType method. After creating the reference, it can be used as an argument when invoking methods.

Parameters:

  • args (Array)

    The value and optionally the type of the reference.

Returns:

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/methods-arguments/passing-arguments-by-reference-with-out-keyword article on Hypertube Guides}


212
213
214
215
216
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 212

def as_out(*args)
  local_command = Hypertube::Utils::Command.new(@runtime_name, Hypertube::Utils::CommandType::AS_OUT, [*args])
  @current_command = nil
  build_invocation_context(local_command)
end

#as_ref(*args) ⇒ Hypertube::Sdk::InvocationContext

Creates a reference type argument that can be passed to a method with a ref parameter modifier. This method is used when working with methods from the called runtime that require arguments to be passed by reference. The arguments include the value and optionally the type of the reference. The type must be retrieved from the called runtime using the getType method. After creating the reference, it can be used as an argument when invoking methods.

Parameters:

  • args (Array)

    The value and optionally the type of the reference.

Returns:

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/methods-arguments/passing-arguments-by-reference-with-ref-keyword article on Hypertube Guides}


198
199
200
201
202
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 198

def as_ref(*args)
  local_command = Hypertube::Utils::Command.new(@runtime_name, Hypertube::Utils::CommandType::AS_REF, [*args])
  @current_command = nil
  build_invocation_context(local_command)
end

#build_command(command) ⇒ Object



248
249
250
251
252
253
254
255
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 248

def build_command(command)
  return @current_command if command.nil? || command.payload.nil?

  command.payload.each_index do |i|
    command.payload[i] = encapsulate_payload_item(command.payload[i])
  end
  command.prepend_arg_to_payload(@current_command)
end

#build_invocation_context(command) ⇒ Object



243
244
245
246
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 243

def build_invocation_context(command)
  Hypertube::Sdk::InvocationContext.new(@runtime_name, @connection_type, @connection_data, build_command(command), false,
                        @is_stateless)
end

#cast(*args) ⇒ Hypertube::Sdk::InvocationContext

Casts the provided value to a specific type. This method is used when invoking methods that require specific types of arguments. The arguments include the target type and the value to be cast. The target type must be retrieved from the called runtime using the getType method. After casting the value, it can be used as an argument when invoking methods. #see Refer to this article on Hypertube Guides

Parameters:

  • args (Array)

    The target type and the value to be cast.

Returns:



171
172
173
174
175
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 171

def cast(*args)
  local_command = Hypertube::Utils::Command.new(@runtime_name, Hypertube::Utils::CommandType::CAST, [*args])
  @current_command = nil
  build_invocation_context(local_command)
end

#encapsulate_payload_item(payload_item) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 257

def encapsulate_payload_item(payload_item)
  if payload_item.is_a? Hypertube::Utils::Command
    if payload_item.command_type == Hypertube::Utils::CommandType::VALUE || payload_item.command_type == Hypertube::Utils::CommandType::ARRAY
      return payload_item
    end

    payload_item.payload.each_index do |i|
      payload_item.payload[i] = encapsulate_payload_item(payload_item.payload[i])
    end
    payload_item

  elsif payload_item.is_a? Hypertube::Sdk::InvocationContext
    payload_item.current_command

  elsif payload_item.is_a? Array
    copied_array = payload_item.map { |item| encapsulate_payload_item(item) }
    Hypertube::Utils::Command.new(@runtime_name, Hypertube::Utils::CommandType::ARRAY, copied_array)
  elsif Hypertube::Utils::TypesHandler.primitive_or_none?(payload_item)
    Hypertube::Utils::Command.new(@runtime_name, Hypertube::Utils::CommandType::VALUE, payload_item.nil? ? [nil] : [*payload_item])
  else
    raise TypeError, "Unsupported payload item type: #{payload_item.class} for payload item: #{payload_item}."
  end
end

#executeObject

Executes the current command. The initial state of Hypertube::Sdk::RuntimeContext is non-materialized, wrapping either a single command or a chain of recursively nested commands. Commands become nested through each invocation of methods on Hypertube::Sdk::RuntimeContext. Each invocation triggers the creation of a new Hypertube::Sdk::RuntimeContext instance wrapping the current command with a new parent command. The developer can decide at any moment of the materialization for the context, taking full control of the chunks of the expression being transferred and processed on the target runtime.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/foundations/execute-method article on Hypertube Guides}


131
132
133
134
135
136
137
138
139
140
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 131

def execute
  @response_command = Hypertube::Core::Interpreter::Interpreter.execute(@current_command, @connection_data)
  @current_command = nil

  return unless @response_command.command_type == Hypertube::Utils::CommandType::EXCEPTION

  exception = Hypertube::Utils::Exceptions::ExceptionThrower.throw_exception(@response_command)
  Hypertube::Sdk::Tools::SdkMessageHelper.send_message_to_app_insights('SdkException', exception.message)
  raise exception
end

#get_enum_item(*args) ⇒ Hypertube::Sdk::InvocationContext

Retrieves a specific item from an enum type. This method is used when working with enums from the called runtime. The arguments include the enum type and the name of the item. The enum type must be retrieved from the called runtime using the getType method. After retrieving the item, it can be used as an argument when invoking methods.

Parameters:

  • args (Array)

    The enum type and the name of the item.

Returns:

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/enums/using-enum-type article on Hypertube Guides}


184
185
186
187
188
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 184

def get_enum_item(*args)
  local_command = Hypertube::Utils::Command.new(@runtime_name, Hypertube::Utils::CommandType::GET_ENUM_ITEM, [*args])
  @current_command = nil
  build_invocation_context(local_command)
end

#get_global_field(field_name) ⇒ Hypertube::Sdk::InvocationContext

Retrieves the value of a global field from the called runtime. The argument includes the global field name. After retrieving the field, the result can be used for further operations.

Parameters:

  • field_name (String)

    The name of the global field to retrieve.

Returns:

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/fields-and-properties/getting-values-for-global-fields article on Hypertube Guides}


237
238
239
240
241
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 237

def get_global_field(field_name)
  local_command = Hypertube::Utils::Command.new(@runtime_name, Hypertube::Utils::CommandType::GET_GLOBAL_FIELD, [field_name])
  @current_command = nil
  build_invocation_context(local_command)
end

#get_type(*args) ⇒ Hypertube::Sdk::InvocationContext

Retrieves a reference to a specific type. The type can be a class, interface or enum. The type can be retrieved from any referenced library.

Parameters:

  • args (Array)

    The full name of the type.

Returns:



159
160
161
162
163
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 159

def get_type(*args)
  local_command = Hypertube::Utils::Command.new(@runtime_name, Hypertube::Utils::CommandType::GET_TYPE, [*args])
  @current_command = nil
  build_invocation_context(local_command)
end

#invoke_global_function(function_name, *args) ⇒ Hypertube::Sdk::InvocationContext

Invokes a function from the called runtime. This method is used when working with functions from the called runtime. The arguments include the function name and the arguments to be passed to the function. After invoking the function, the result can be used for further operations.

Parameters:

  • function_name (String)

    The name of the function to invoke.

  • args (Array)

    The arguments to be passed to the function.

Returns:

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/functions/invoking-functions article on Hypertube Guides}


225
226
227
228
229
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 225

def invoke_global_function(function_name, *args)
  local_command = Hypertube::Utils::Command.new(@runtime_name, Hypertube::Utils::CommandType::INVOKE_GLOBAL_FUNCTION, [function_name, *args])
  @current_command = nil
  build_invocation_context(local_command)
end

#load_library(*args) ⇒ Hypertube::Sdk::RuntimeContext

Adds a reference to a library. Hypertube::Sdk::RuntimeBridge allows you to reference and use modules or packages written in various languages. This method allows you to use any library from all supported technologies. The necessary libraries need to be referenced. The argument is a relative or full path to the library. If the library has dependencies on other libraries, the latter needs to be added first. After referencing the library, any objects stored in this package can be used. Use static classes, create instances, call methods, use fields and properties, and much more.

Parameters:

  • args (Array)

    The relative or full path to the library.

Returns:

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/getting-started/adding-references-to-libraries article on Hypertube Guides}


149
150
151
152
153
154
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 149

def load_library(*args)
  local_command = Hypertube::Utils::Command.new(@runtime_name, Hypertube::Utils::CommandType::LOAD_LIBRARY, [*args])
  @current_command = build_command(local_command)
  execute
  self
end