« Core/Handler/CommandHandler/LoadLibraryHandler.pm
↑ Summary
Core/Transmitter/PerlTransmitterWrapper.pm »
| 1 | package Javonet::Sdk::Internal::RuntimeContext; | ||
| 2 | use strict; | ||
| 3 | use warnings FATAL => 'all'; | ||
| 4 | use Moose; | ||
| 5 | |||
| 6 | use aliased 'Javonet::Sdk::Core::PerlCommand' => 'PerlCommand'; | ||
| 7 | use aliased 'Javonet::Sdk::Internal::InvocationContext' => 'InvocationContext'; | ||
| 8 | use aliased 'Javonet::Core::Handler::PerlHandler' => 'PerlHandler'; | ||
| 9 | use aliased 'Javonet::Core::Interpreter::Interpreter' => 'Interpreter', qw(execute_); | ||
| 10 | use aliased 'Javonet::Core::Exception::ExceptionThrower' => 'ExceptionThrower'; | ||
| 11 | use aliased 'Javonet::Sdk::Core::TypesHandler' => 'TypesHandler'; | ||
| 12 | |||
| 13 | extends 'Javonet::Sdk::Internal::Abstract::AbstractRuntimeContext', | ||
| 14 | 'Javonet::Sdk::Internal::Abstract::AbstractTypeContext'; | ||
| 15 | |||
| 16 | our %memoryRuntimeContexts; | ||
| 17 | our %networkRuntimeContexts; | ||
| 18 | our %configRuntimeContexts; | ||
| 19 | |||
| 20 | #@override | ||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | |||
| 24 | my $self = { | ||
| 25 | runtime_name => shift, | ||
| 26 | connection_type => shift, | ||
| 27 | tcp_address => shift, | ||
| 28 | response_command => 0, | ||
| 29 | }; | ||
| 30 | bless $self, $class; | ||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub get_instance { | ||
| 35 | my $runtime_name = shift; | ||
| 36 | my $connection_type = shift; | ||
| 37 | my $tcp_address = shift; | ||
| 38 | my $path = shift; | ||
| 39 | |||
| 40 | if($connection_type eq Javonet::Sdk::Internal::ConnectionType::get_connection_type("InMemory")) { | ||
| 41 | if(exists $memoryRuntimeContexts{$runtime_name}) { | ||
| 42 | my $runtimeCtx = $memoryRuntimeContexts{$runtime_name}; | ||
| 43 | $runtimeCtx->{current_command} = undef(); | ||
| 44 | return $runtimeCtx; | ||
| 45 | } | ||
| 46 | else { | ||
| 47 | my $runtimeCtx = Javonet::Sdk::Internal::RuntimeContext->new($runtime_name, $connection_type, ''); | ||
| 48 | $memoryRuntimeContexts{$runtime_name} = $runtimeCtx; | ||
| 49 | return($runtimeCtx); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | if($connection_type eq Javonet::Sdk::Internal::ConnectionType::get_connection_type("Tcp")) { | ||
| 54 | my $networkRuntimeContextsKey = $runtime_name . $tcp_address; | ||
| 55 | if(exists $networkRuntimeContexts{$networkRuntimeContextsKey}) { | ||
| 56 | my $runtimeCtx = $networkRuntimeContexts{$networkRuntimeContextsKey}; | ||
| 57 | $runtimeCtx->{current_command} = undef(); | ||
| 58 | return $runtimeCtx; | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | my $runtimeCtx = Javonet::Sdk::Internal::RuntimeContext->new($runtime_name, $connection_type, $tcp_address); | ||
| 62 | $networkRuntimeContexts{$networkRuntimeContextsKey} = $runtimeCtx; | ||
| 63 | return($runtimeCtx); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | if($connection_type eq Javonet::Sdk::Internal::ConnectionType::get_connection_type("WithConfig")) { | ||
| 68 | my $configRuntimeContextsKey = $runtime_name . $path; | ||
| 69 | if(exists $configRuntimeContexts{$configRuntimeContextsKey}) { | ||
| 70 | my $runtimeCtx = $configRuntimeContexts{$configRuntimeContextsKey}; | ||
| 71 | $runtimeCtx->{current_command} = undef(); | ||
| 72 | return $runtimeCtx; | ||
| 73 | } | ||
| 74 | else { | ||
| 75 | my $runtimeCtx = Javonet::Sdk::Internal::RuntimeContext->new($runtime_name, $connection_type, $tcp_address); | ||
| 76 | $configRuntimeContexts{$configRuntimeContextsKey} = $runtimeCtx; | ||
| 77 | return($runtimeCtx); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | sub execute { | ||
| 84 | my $self = $_[0]; | ||
| 85 | my $command = shift; | ||
| 86 | my $connection_type = shift; | ||
| 87 | my $tcp_address = shift; | ||
| 88 | $self->{response_command} = Interpreter->execute_($command, $connection_type, $tcp_address); | ||
| 89 | if ($self->{response_command}->{command_type} == Javonet::Sdk::Core::PerlCommandType::get_command_type('Exception')) { | ||
| 90 | ExceptionThrower->throwException($self->{response_command}) | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | #@override | ||
| 96 | sub load_library { | ||
| 97 | my $self = shift; | ||
| 98 | my @load_library_parameters = @_; | ||
| 99 | |||
| 100 | my $command = PerlCommand->new( | ||
| 101 | runtime => $self->{runtime_name}, | ||
| 102 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('LoadLibrary'), | ||
| 103 | payload => \@load_library_parameters | ||
| 104 | ); | ||
| 105 | |||
| 106 | execute($self->build_command($command), $self->{connection_type}, $self->{tcp_address}); | ||
| 107 | return $self; | ||
| 108 | } | ||
| 109 | |||
| 110 | #@override | ||
| 111 | sub get_type { | ||
| 112 | my $self = shift; | ||
| 113 | my @arguments = @_; | ||
| 114 | |||
| 115 | my $command = PerlCommand->new( | ||
| 116 | runtime => $self->{runtime_name}, | ||
| 117 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('GetType'), | ||
| 118 | payload => \@arguments | ||
| 119 | ); | ||
| 120 | |||
| 121 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 122 | $self->{runtime_name}, | ||
| 123 | $self->{connection_type}, | ||
| 124 | $self->{tcp_address}, | ||
| 125 | $self->build_command($command), | ||
| 126 | 0 | ||
| 127 | ); | ||
| 128 | } | ||
| 129 | |||
| 130 | #@override | ||
| 131 | sub cast { | ||
| 132 | my $self = shift; | ||
| 133 | my @arguments = @_; | ||
| 134 | |||
| 135 | my $command = PerlCommand->new( | ||
| 136 | runtime => $self->{runtime_name}, | ||
| 137 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('Cast'), | ||
| 138 | payload => \@arguments | ||
| 139 | ); | ||
| 140 | |||
| 141 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 142 | $self->{runtime_name}, | ||
| 143 | $self->{connection_type}, | ||
| 144 | $self->{tcp_address}, | ||
| 145 | $self->build_command($command), | ||
| 146 | 0 | ||
| 147 | ); | ||
| 148 | } | ||
| 149 | |||
| 150 | #@override | ||
| 151 | sub get_enum_item { | ||
| 152 | my $self = shift; | ||
| 153 | my @arguments = @_; | ||
| 154 | |||
| 155 | my $command = PerlCommand->new( | ||
| 156 | runtime => $self->{runtime_name}, | ||
| 157 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('GetEnumItem'), | ||
| 158 | payload => \@arguments | ||
| 159 | ); | ||
| 160 | |||
| 161 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 162 | $self->{runtime_name}, | ||
| 163 | $self->{connection_type}, | ||
| 164 | $self->{tcp_address}, | ||
| 165 | $self->build_command($command), | ||
| 166 | 0 | ||
| 167 | ); | ||
| 168 | } | ||
| 169 | |||
| 170 | #@override | ||
| 171 | sub as_out { | ||
| 172 | my $self = shift; | ||
| 173 | my @arguments = @_; | ||
| 174 | |||
| 175 | my $command = PerlCommand->new( | ||
| 176 | runtime => $self->{runtime_name}, | ||
| 177 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('AsOut'), | ||
| 178 | payload => \@arguments | ||
| 179 | ); | ||
| 180 | |||
| 181 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 182 | $self->{runtime_name}, | ||
| 183 | $self->{connection_type}, | ||
| 184 | $self->{tcp_address}, | ||
| 185 | $self->build_command($command), | ||
| 186 | 0 | ||
| 187 | ); | ||
| 188 | } | ||
| 189 | |||
| 190 | #@override | ||
| 191 | sub as_ref { | ||
| 192 | my $self = shift; | ||
| 193 | my @arguments = @_; | ||
| 194 | |||
| 195 | my $command = PerlCommand->new( | ||
| 196 | runtime => $self->{runtime_name}, | ||
| 197 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('AsRef'), | ||
| 198 | payload => \@arguments | ||
| 199 | ); | ||
| 200 | |||
| 201 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 202 | $self->{runtime_name}, | ||
| 203 | $self->{connection_type}, | ||
| 204 | $self->{tcp_address}, | ||
| 205 | $self->build_command($command), | ||
| 206 | 0 | ||
| 207 | ); | ||
| 208 | } | ||
| 209 | |||
| 210 | #@override | ||
| 211 | sub invoke_global_function { | ||
| 212 | my $self = shift; | ||
| 213 | my @arguments = @_; | ||
| 214 | |||
| 215 | my $command = PerlCommand->new( | ||
| 216 | runtime => $self->{runtime_name}, | ||
| 217 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('InvokeGlobalFunction'), | ||
| 218 | payload => \@arguments | ||
| 219 | ); | ||
| 220 | |||
| 221 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 222 | $self->{runtime_name}, | ||
| 223 | $self->{connection_type}, | ||
| 224 | $self->{tcp_address}, | ||
| 225 | $self->build_command($command), | ||
| 226 | 0 | ||
| 227 | ); | ||
| 228 | } | ||
| 229 | |||
| 230 | sub build_command { | ||
| 231 | my ($self, $command) = @_; | ||
| 232 | my $payload_length = @{$command->{payload}}; | ||
| 233 | for (my $i = 0; $i < $payload_length; $i++) { | ||
| 234 | $command->{payload}[$i] = $self->encapsulate_payload_item($command->{payload}[$i]); | ||
| 235 | } | ||
| 236 | return $command->prepend_arg_to_payload($self->{current_command}); | ||
| 237 | } | ||
| 238 | |||
| 239 | sub encapsulate_payload_item { | ||
| 240 | my ($self, $payload_item) = @_; | ||
| 241 | |||
| 242 | if (!defined $payload_item) { | ||
| 243 | return PerlCommand->new( | ||
| 244 | runtime => $self->{runtime_name}, | ||
| 245 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('Value'), | ||
| 246 | payload => [] | ||
| 247 | ); | ||
| 248 | } | ||
| 249 | if (ref($payload_item) eq 'ARRAY') { | ||
| 250 | my $payload_length = @$payload_item; | ||
| 251 | for (my $i = 0; $i < $payload_length; $i++) { | ||
| 252 | $payload_item->[$i] = $self->encapsulate_payload_item($payload_item->[$i]); | ||
| 253 | } | ||
| 254 | return PerlCommand->new( | ||
| 255 | runtime => $self->{runtime_name}, | ||
| 256 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('Array'), | ||
| 257 | payload => $payload_item | ||
| 258 | ); | ||
| 259 | } | ||
| 260 | elsif ($payload_item->isa('Command')) { | ||
| 261 | my $payload_length = @{$payload_item->{payload}}; | ||
| 262 | for (my $i = 0; $i < $payload_length; $i++) { | ||
| 263 | $payload_item->{payload}[$i] = $self->encapsulate_payload_item($payload_item->{payload}[$i]); | ||
| 264 | } | ||
| 265 | return $payload_item; | ||
| 266 | } | ||
| 267 | elsif ($payload_item->isa('Javonet::Sdk::Internal::InvocationContext')) { | ||
| 268 | return $payload_item->get_current_command(); | ||
| 269 | } | ||
| 270 | elsif (TypesHandler->is_primitive_or_none($payload_item)) { | ||
| 271 | return PerlCommand->new( | ||
| 272 | runtime => $self->{runtime_name}, | ||
| 273 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('Value'), | ||
| 274 | payload => [ $payload_item ] | ||
| 275 | ); | ||
| 276 | } | ||
| 277 | else { | ||
| 278 | die "Unsupported payload item type: " . ref($payload_item) . " for payload item: $payload_item."; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | sub health_check { | ||
| 283 | my $self = shift; | ||
| 284 | |||
| 285 | my $command = PerlCommand->new( | ||
| 286 | runtime => $self->{runtime_name}, | ||
| 287 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('Value'), | ||
| 288 | payload => ['health_check'] | ||
| 289 | ); | ||
| 290 | |||
| 291 | execute($self->build_command($command), $self->{connection_type}, $self->{tcp_address}); | ||
| 292 | } | ||
| 293 | |||
| 294 | no Moose; | ||
| 295 | 1; |