Class: RuntimeContext

Inherits:
AbstractRuntimeContext show all
Defined in:
lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb

Constant Summary collapse

@@memory_runtime_contexts =
Hash.new
@@network_runtime_contexts =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractRuntimeContext

#get_module

Constructor Details

#initialize(runtime_name, connection_type, tcp_address) ⇒ RuntimeContext

Returns a new instance of RuntimeContext.



16
17
18
19
20
21
22
# File 'lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb', line 16

def initialize(runtime_name, connection_type, tcp_address)
  @runtime_name = runtime_name
  @connection_type = connection_type
  @tcp_ip_address = tcp_address
  @current_command = nil
  @interpreter = Interpreter.new
end

Instance Attribute Details

#current_commandObject

accessor get method



11
12
13
# File 'lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb', line 11

def current_command
  @current_command
end

Class Method Details

.get_instance(runtime_name, connection_type, tcp_address) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb', line 24

def self.get_instance(runtime_name, connection_type, tcp_address)
  if (ConnectionType::TCP == connection_type) && tcp_address == nil
    if @@network_runtime_contexts.has_key?(tcp_address)
      runtime_ctx = @@network_runtime_contexts[tcp_address]
      runtime_ctx.current_command = nil
      return runtime_ctx
    else
      runtime_ctx = RuntimeContext.new(runtime_name, connection_type, tcp_address)
      @@network_runtime_contexts[:tcp_address] = runtime_ctx
      return runtime_ctx
    end
  else
    if (@@memory_runtime_contexts.has_key?(runtime_name))
      runtime_ctx = @@network_runtime_contexts[runtime_name]
      runtime_ctx.current_command = nil
      return runtime_ctx
    else
      runtime_ctx = RuntimeContext.new(runtime_name, connection_type, nil)
      @@memory_runtime_contexts[:tcp_address] = runtime_ctx
      return runtime_ctx
    end
  end
end

Instance Method Details

#as_out(*args) ⇒ Object



87
88
89
90
91
# File 'lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb', line 87

def as_out(*args)
  local_command = Command.new(@runtime_name, CommandType::AS_OUT, [*args])
  @current_command = nil
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), false)
end

#as_ref(*args) ⇒ Object



81
82
83
84
85
# File 'lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb', line 81

def as_ref(*args)
  local_command = Command.new(@runtime_name, CommandType::AS_REF, [*args])
  @current_command = nil
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), false)
end

#build_command(command) ⇒ Object



94
95
96
97
98
99
# File 'lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb', line 94

def build_command(command)
  command.payload.each_index {|i|
    command.payload[i] = encapsulate_payload_item(command.payload[i])
  }
  return command.prepend_arg_to_payload(@current_command)
end

#cast(*args) ⇒ Object



69
70
71
72
73
# File 'lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb', line 69

def cast(*args)
  local_command = Command.new(@runtime_name, CommandType::CAST, [*args])
  @current_command = nil
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), false)
end

#encapsulate_payload_item(payload_item) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb', line 101

def encapsulate_payload_item(payload_item)
  if payload_item.is_a? Command
    payload_item.payload.each_index { |i|
      payload_item.payload[i] = encapsulate_payload_item(payload_item.payload[i])
    }
    return payload_item

  elsif payload_item.is_a? InvocationContext
    return payload_item.current_command

  elsif payload_item.is_a? Array
    payload_item.each_index { |i|
      payload_item[i] = encapsulate_payload_item(payload_item[i])
    }
    return Command.new(@runtime_name, CommandType::ARRAY, [*payload_item])

  else
    return Command.new(@runtime_name, CommandType::VALUE, [*payload_item])

  end
end

#executeObject



48
49
50
51
52
53
54
# File 'lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb', line 48

def execute
  @response_command = @interpreter.execute(@current_command, @connection_type, @tcp_ip_address)
  @current_command = nil
  if @response_command.command_type == CommandType::EXCEPTION
    raise ExceptionThrower.throw_exception(@response_command)
  end
end

#get_enum_item(*args) ⇒ Object



75
76
77
78
79
# File 'lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb', line 75

def get_enum_item(*args)
  local_command = Command.new(@runtime_name, CommandType::GET_ENUM_ITEM, [*args])
  @current_command = nil
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), false)
end

#get_type(*args) ⇒ Object



63
64
65
66
67
# File 'lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb', line 63

def get_type(*args)
  local_command = Command.new(@runtime_name, CommandType::GET_TYPE, [*args])
  @current_command = nil
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), false)
end

#load_library(*args) ⇒ Object



56
57
58
59
60
61
# File 'lib/javonet-ruby-sdk/sdk/internal/runtime_context.rb', line 56

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