Class: RuntimeContext
- Inherits:
-
AbstractRuntimeContext
- Object
- AbstractRuntimeContext
- RuntimeContext
- Defined in:
- lib/hypertube-ruby-sdk/sdk/runtime_context.rb
Overview
Represents a single context which allows interaction with a selected technology. Refers to a single instance of the called runtime within a particular target OS process. This can be either the local currently running process (inMemory) or a particular remote process identified by the IP Address and PORT of the target RuntimeBridge instance. Multiple Runtime Contexts can be initialized within one process. Calling the same technology on inMemory communication channel will return the existing instance of runtime context. Calling the same technology on TCP channel but on different nodes will result in unique Runtime Contexts. Within the runtime context, any number of libraries can be loaded and any objects from the target technology can be interacted with, as they are aware of each other due to sharing the same memory space and same runtime instance.
Constant Summary collapse
- @@memory_runtime_contexts =
{}
- @@network_runtime_contexts =
{}
- @@ws_runtime_contexts =
{}
Instance Attribute Summary collapse
-
#connection_data ⇒ Object
readonly
Returns the value of attribute connection_data.
-
#current_command ⇒ Object
accessor get method.
-
#is_stateless ⇒ Object
accessor get method.
-
#runtime_name ⇒ Object
readonly
Returns the value of attribute runtime_name.
Class Method Summary collapse
- .get_instance(runtime_name, connection_type, connection_data) ⇒ Object
-
.initialize_runtime_context(config) ⇒ RuntimeContext
Initializes RuntimeContext based on the Config object and loads modules.
Instance Method Summary collapse
-
#as_out(*args) ⇒ InvocationContext
Creates a reference type argument that can be passed to a method with an out parameter modifier.
-
#as_ref(*args) ⇒ InvocationContext
Creates a reference type argument that can be passed to a method with a ref parameter modifier.
- #build_command(command) ⇒ Object
- #build_invocation_context(command) ⇒ Object
-
#cast(*args) ⇒ InvocationContext
Casts the provided value to a specific type.
- #encapsulate_payload_item(payload_item) ⇒ Object
-
#execute ⇒ Object
Executes the current command.
-
#get_enum_item(*args) ⇒ InvocationContext
Retrieves a specific item from an enum type.
-
#get_global_field(field_name) ⇒ InvocationContext
Retrieves the value of a global field from the called runtime.
-
#get_type(*args) ⇒ InvocationContext
Retrieves a reference to a specific type.
-
#initialize(runtime_name, connection_type, connection_data) ⇒ RuntimeContext
constructor
accessor set method.
-
#invoke_global_function(function_name, *args) ⇒ InvocationContext
Invokes a function from the called runtime.
-
#load_library(*args) ⇒ RuntimeContext
Adds a reference to a library.
Constructor Details
#initialize(runtime_name, connection_type, connection_data) ⇒ RuntimeContext
accessor set method
30 31 32 33 34 35 36 37 38 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 30 def initialize(runtime_name, connection_type, connection_data) @runtime_name = runtime_name @connection_type = connection_type @connection_data = connection_data @current_command = nil @is_stateless = false SdkMessageHelper.('SdkMessage', RuntimeNameHandler.get_name(@runtime_name).capitalize + ' initialized') end |
Instance Attribute Details
#connection_data ⇒ Object (readonly)
Returns the value of attribute connection_data.
26 27 28 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 26 def connection_data @connection_data end |
#current_command ⇒ Object
accessor get method
25 26 27 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 25 def current_command @current_command end |
#is_stateless ⇒ Object
accessor get method
25 26 27 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 25 def is_stateless @is_stateless end |
#runtime_name ⇒ Object (readonly)
Returns the value of attribute runtime_name.
26 27 28 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 26 def runtime_name @runtime_name end |
Class Method Details
.get_instance(runtime_name, connection_type, connection_data) ⇒ Object
40 41 42 43 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 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 40 def self.get_instance(runtime_name, connection_type, connection_data) rtm_ctx = case connection_type when ConnectionType::IN_MEMORY if @@memory_runtime_contexts.has_key?(runtime_name) @@memory_runtime_contexts[runtime_name] else runtime_ctx = RuntimeContext.new(runtime_name, connection_type, connection_data) @@memory_runtime_contexts[runtime_name] = runtime_ctx runtime_ctx end when ConnectionType::TCP key = runtime_name.to_s + ':' + connection_data.to_s if @@network_runtime_contexts.has_key?(key) @@network_runtime_contexts[key] else runtime_ctx = RuntimeContext.new(runtime_name, connection_type, connection_data) @@network_runtime_contexts[key] = runtime_ctx runtime_ctx end when ConnectionType::WEB_SOCKET key = runtime_name.to_s + ':' + connection_data.to_s if @@ws_runtime_contexts.has_key?(key) @@ws_runtime_contexts[key] else runtime_ctx = RuntimeContext.new(runtime_name, connection_type, connection_data) @@ws_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) ⇒ RuntimeContext
Initializes RuntimeContext based on the Config object and loads modules.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 78 def self.initialize_runtime_context(config) raise 'Config cannot be null.' if config.nil? connection_data = config.connection_data runtime = config.runtime connection_type = if connection_data.nil? ConnectionType::IN_MEMORY elsif connection_data.is_a?(TcpConnectionData) ConnectionType::TCP elsif connection_data.is_a?(WsConnectionData) ConnectionType::WEB_SOCKET else raise ArgumentError, 'Unknown connection type.' end rtm_ctx = get_instance(runtime, connection_type, connection_data) rtm_ctx.is_stateless = config.is_stateless if connection_data.nil? 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.(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) ⇒ 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.
205 206 207 208 209 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 205 def as_out(*args) local_command = Command.new(@runtime_name, CommandType::AS_OUT, [*args]) @current_command = nil build_invocation_context(local_command) end |
#as_ref(*args) ⇒ 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.
191 192 193 194 195 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 191 def as_ref(*args) local_command = Command.new(@runtime_name, CommandType::AS_REF, [*args]) @current_command = nil build_invocation_context(local_command) end |
#build_command(command) ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 241 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
236 237 238 239 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 236 def build_invocation_context(command) InvocationContext.new(@runtime_name, @connection_type, @connection_data, build_command(command), false, @is_stateless) end |
#cast(*args) ⇒ 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
164 165 166 167 168 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 164 def cast(*args) local_command = Command.new(@runtime_name, CommandType::CAST, [*args]) @current_command = nil build_invocation_context(local_command) end |
#encapsulate_payload_item(payload_item) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 250 def encapsulate_payload_item(payload_item) if payload_item.is_a? Command if payload_item.command_type == CommandType::VALUE || payload_item.command_type == 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? InvocationContext payload_item.current_command elsif payload_item.is_a? Array copied_array = payload_item.map { |item| encapsulate_payload_item(item) } Command.new(@runtime_name, CommandType::ARRAY, copied_array) elsif TypesHandler.primitive_or_none?(payload_item) Command.new(@runtime_name, 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 |
#execute ⇒ Object
Executes the current command. The initial state of 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 RuntimeContext. Each invocation triggers the creation of a new 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.
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 124 def execute @response_command = Interpreter.execute(@current_command, @connection_type, @connection_data) @current_command = nil return unless @response_command.command_type == CommandType::EXCEPTION exception = ExceptionThrower.throw_exception(@response_command) SdkMessageHelper.('SdkException', exception.) raise exception end |
#get_enum_item(*args) ⇒ 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.
177 178 179 180 181 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 177 def get_enum_item(*args) local_command = Command.new(@runtime_name, CommandType::GET_ENUM_ITEM, [*args]) @current_command = nil build_invocation_context(local_command) end |
#get_global_field(field_name) ⇒ 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.
230 231 232 233 234 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 230 def get_global_field(field_name) local_command = Command.new(@runtime_name, CommandType::GET_GLOBAL_FIELD, [field_name]) @current_command = nil build_invocation_context(local_command) end |
#get_type(*args) ⇒ 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.
152 153 154 155 156 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 152 def get_type(*args) local_command = Command.new(@runtime_name, CommandType::GET_TYPE, [*args]) @current_command = nil build_invocation_context(local_command) end |
#invoke_global_function(function_name, *args) ⇒ 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.
218 219 220 221 222 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 218 def invoke_global_function(function_name, *args) local_command = Command.new(@runtime_name, CommandType::INVOKE_GLOBAL_FUNCTION, [function_name, *args]) @current_command = nil build_invocation_context(local_command) end |
#load_library(*args) ⇒ RuntimeContext
Adds a reference to a library. 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.
142 143 144 145 146 147 |
# File 'lib/hypertube-ruby-sdk/sdk/runtime_context.rb', line 142 def load_library(*args) local_command = Command.new(@runtime_name, CommandType::LOAD_LIBRARY, [*args]) @current_command = build_command(local_command) execute self end |