Class: InvocationContext

Inherits:
AbstractInvocationContext show all
Includes:
Enumerable
Defined in:
lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb

Instance Method Summary collapse

Methods inherited from AbstractInvocationContext

#set_generic_type

Constructor Details

#initialize(runtime_name, connection_type, tcp_ip_address, command, is_executed = false) ⇒ InvocationContext

Returns a new instance of InvocationContext.



9
10
11
12
13
14
15
16
17
18
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 9

def initialize(runtime_name, connection_type, tcp_ip_address, command, is_executed = false)
  @is_executed = is_executed
  @runtime_name = runtime_name
  @connection_type = connection_type
  @tcp_ip_address = tcp_ip_address
  @current_command = command
  @response_command = nil
  @interpreter = Interpreter.new
  ObjectSpace.define_finalizer(self, self.finalize(@current_command, @is_executed, @interpreter, @connection_type, @tcp_ip_address))
end

Instance Method Details

#[](i) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 37

def [](i)
  if @current_command.command_type != CommandType::REFERENCE
    raise "Object is not iterable"
  else
    invocation_context_iterator = InvocationContextIterator.new(self)
    return invocation_context_iterator[i]
  end
end

#[]=(i, value) ⇒ Object



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

def []=(i, value)
  if @current_command.command_type != CommandType::REFERENCE
    raise "Object is not iterable"
  else
    invocation_context_iterator = InvocationContextIterator.new(self)
    return invocation_context_iterator[i] = value
  end
end

#build_command(command) ⇒ Object



147
148
149
150
151
152
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 147

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

#create_instance(*args) ⇒ Object



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

def create_instance(*args)
  local_command = Command.new(@runtime_name, CommandType::CREATE_CLASS_INSTANCE, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end

#current_commandObject



176
177
178
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 176

def current_command
  @current_command
end

#encapsulate_payload_item(payload_item) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 154

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 55

def execute
  @response_command = @interpreter.execute(@current_command, @connection_type, @tcp_ip_address)

  if @response_command.command_type == CommandType::EXCEPTION
    raise ExceptionThrower.throw_exception(@response_command)
  end

  if @response_command.command_type == CommandType::CREATE_CLASS_INSTANCE
    @current_command = @response_command
    @is_executed = true
    return self
  end

  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, @response_command, true)
end

#finalize(command, is_executed, interpreter, connection_type, tcp_ip_address) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 20

def finalize(command, is_executed, interpreter, connection_type, tcp_ip_address)
  proc do
    if command.command_type == CommandType::REFERENCE && is_executed == true
      destructCommand = Command.new(@runtime_name, CommandType::DESTRUCT_REFERENCE, command.payload)
      interpreter.execute(destructCommand, connection_type, tcp_ip_address)
    end
  end
end

#get_index(*args) ⇒ Object



106
107
108
109
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 106

def get_index(*args)
  local_command = Command.new(@runtime_name, CommandType::ARRAY_GET_ITEM, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end

#get_instance_field(*args) ⇒ Object



91
92
93
94
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 91

def get_instance_field(*args)
  local_command = Command.new(@runtime_name, CommandType::GET_INSTANCE_FIELD, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end

#get_rank(*args) ⇒ Object



116
117
118
119
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 116

def get_rank(*args)
  local_command = Command.new(@runtime_name, CommandType::ARRAY_GET_RANK, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end

#get_size(*args) ⇒ Object



111
112
113
114
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 111

def get_size(*args)
  local_command = Command.new(@runtime_name, CommandType::ARRAY_GET_SIZE, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end

#get_static_field(*args) ⇒ Object



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

def get_static_field(*args)
  local_command = Command.new(@runtime_name, CommandType::GET_STATIC_FIELD, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end

#get_valueObject



143
144
145
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 143

def get_value
  return @current_command.payload[0]
end

#invoke_generic_method(*args) ⇒ Object



131
132
133
134
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 131

def invoke_generic_method(*args)
  local_command = Command.new(@runtime_name, CommandType::INVOKE_GENERIC_METHOD, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end

#invoke_generic_static_method(*args) ⇒ Object



126
127
128
129
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 126

def invoke_generic_static_method(*args)
  local_command = Command.new(@runtime_name, CommandType::INVOKE_GENERIC_STATIC_METHOD, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end

#invoke_instance_method(*args) ⇒ Object



71
72
73
74
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 71

def invoke_instance_method(*args)
  local_command = Command.new(@runtime_name, CommandType::INVOKE_INSTANCE_METHOD, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end

#invoke_static_method(*args) ⇒ Object



101
102
103
104
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 101

def invoke_static_method(*args)
  local_command = Command.new(@runtime_name, CommandType::INVOKE_STATIC_METHOD, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end

#iteratorObject



29
30
31
32
33
34
35
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 29

def iterator
  if @current_command.command_type != CommandType::REFERENCE
    raise "Object is not iterable"
  else
    return InvocationContextIterator.new(self)
  end
end

#response_commandObject



180
181
182
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 180

def response_command
  @response_command
end

#retrieve_array(*args) ⇒ Object



136
137
138
139
140
141
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 136

def retrieve_array(*args)
  local_command = Command.new(@runtime_name, CommandType::RETRIEVE_ARRAY, [*args])
  localInvCtx = InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
  localInvCtx.execute
  return localInvCtx.response_command.payload
end

#set_index(*args) ⇒ Object



121
122
123
124
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 121

def set_index(*args)
  local_command = Command.new(@runtime_name, CommandType::ARRAY_SET_ITEM, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end

#set_instance_field(*args) ⇒ Object



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

def set_instance_field(*args)
  local_command = Command.new(@runtime_name, CommandType::SET_INSTANCE_FIELD, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end

#set_static_field(*args) ⇒ Object



86
87
88
89
# File 'lib/javonet-ruby-sdk/sdk/internal/invocation_context.rb', line 86

def set_static_field(*args)
  local_command = Command.new(@runtime_name, CommandType::SET_STATIC_FIELD, [*args])
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command))
end