« Sdk/Internal/RuntimeContext.pm
↑ Summary
Core/Handler/CommandHandler/InvokeGlobalFunctionHandler.pm »
| 1 | package Javonet::Core::Transmitter::PerlTransmitterWrapper; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use lib 'lib'; | ||
| 5 | use FFI::Platypus; | ||
| 6 | use Path::Tiny; | ||
| 7 | use File::Spec; | ||
| 8 | use Config; | ||
| 9 | use Cwd; | ||
| 10 | use POSIX qw(uname); | ||
| 11 | use Exporter qw(import); | ||
| 12 | our @EXPORT = qw(tw_initialize tw_send_command tw_activate tw_set_config_source tw_set_javonet_working_directory); | ||
| 13 | |||
| 14 | my $ffi; | ||
| 15 | |||
| 16 | my $send_command_native; | ||
| 17 | my $read_response_native; | ||
| 18 | my $activate_native; | ||
| 19 | my $get_native_error_native; | ||
| 20 | my $set_config_source_native; | ||
| 21 | my $set_working_directory_native; | ||
| 22 | |||
| 23 | |||
| 24 | sub tw_initialize { | ||
| 25 | my $osname = $^O; | ||
| 26 | { | ||
| 27 | $ffi = FFI::Platypus->new(api => 1); | ||
| 28 | use FFI::Platypus::DL qw(dlopen dlerror RTLD_PLATYPUS_DEFAULT); | ||
| 29 | my $dir = File::Spec->rel2abs(__FILE__); | ||
| 30 | my $current_dir = getcwd(); | ||
| 31 | my $perl_native_lib; | ||
| 32 | |||
| 33 | my $arch = do { | ||
| 34 | my $uname = (uname())[4]; | ||
| 35 | if ($uname =~ /64/) { | ||
| 36 | "X64"; | ||
| 37 | } elsif ($uname =~ /arm64|aarch64/) { | ||
| 38 | "ARM64"; | ||
| 39 | } elsif ($uname =~ /arm/) { | ||
| 40 | "ARM"; | ||
| 41 | } else { | ||
| 42 | "X86"; | ||
| 43 | } | ||
| 44 | }; | ||
| 45 | |||
| 46 | if ($osname eq "linux") { | ||
| 47 | $perl_native_lib = "Binaries/Native/Linux/$arch/libJavonetPerlRuntimeNative.so"; | ||
| 48 | } | ||
| 49 | elsif ($osname eq "darwin") { | ||
| 50 | $perl_native_lib = "Binaries/Native/MacOs/$arch/libJavonetPerlRuntimeNative.dylib"; | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | $perl_native_lib = "Binaries/Native/Windows/$arch/JavonetPerlRuntimeNative.dll"; | ||
| 54 | } | ||
| 55 | |||
| 56 | my $full_native_lib_path = "$current_dir/$perl_native_lib"; | ||
| 57 | my $error_message = "Native library not found at $full_native_lib_path\n" . | ||
| 58 | "Please Download and unzip Binaries at $current_dir directory folder using this link: https://download.javonet.com/binaries/Binaries.zip\n" . | ||
| 59 | "For more info please visit: https://www.javonet.com/guides/v2/getting-started/getting-started-perl"; | ||
| 60 | die $error_message unless -e $full_native_lib_path; | ||
| 61 | $ffi->lib($full_native_lib_path); | ||
| 62 | |||
| 63 | $send_command_native = $ffi->function('SendCommand' => [ 'uchar[]', 'int' ] => 'int'); | ||
| 64 | $read_response_native = $ffi->function('ReadResponse' => [ 'uchar[]', 'int' ] => 'int'); | ||
| 65 | $activate_native = $ffi->function('Activate' => ['string'] => 'int'); | ||
| 66 | $get_native_error_native = $ffi->function('GetNativeError' => [] => 'string'); | ||
| 67 | $set_config_source_native = $ffi->function('SetConfigSource' => ['string'] => 'int'); | ||
| 68 | $set_working_directory_native = $ffi->function('SetWorkingDirectory' => ['string'] => 'int'); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | sub tw_send_command { | ||
| 73 | my ($self, $message_ref) = @_; | ||
| 74 | my @message_array = @$message_ref; | ||
| 75 | my $response_array_len = $send_command_native->(\@message_array, scalar @message_array); | ||
| 76 | |||
| 77 | if ($response_array_len > 0) { | ||
| 78 | my @response_array = (1 .. $response_array_len); | ||
| 79 | $response_array[0] = $message_array[0]; | ||
| 80 | $read_response_native->(\@response_array, $response_array_len); | ||
| 81 | return \@response_array; | ||
| 82 | } | ||
| 83 | elsif ($response_array_len == 0) { | ||
| 84 | my $error_message = "Response is empty"; | ||
| 85 | die "$error_message"; | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | my $error_message = $get_native_error_native->(); | ||
| 89 | die "Javonet native error code: $response_array_len. $error_message"; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub tw_activate { | ||
| 94 | my ($self, $licenseKey) = @_; | ||
| 95 | tw_initialize(); | ||
| 96 | my $activation_result = $activate_native->($licenseKey); | ||
| 97 | if ($activation_result < 0) { | ||
| 98 | my $error_message = $get_native_error_native->(); | ||
| 99 | die "Javonet activation result: $activation_result. Native error message: $error_message"; | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | return $activation_result; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | sub tw_set_config_source { | ||
| 107 | my ($self, $config_path) = @_; | ||
| 108 | tw_initialize(); | ||
| 109 | my $set_config_result = $set_config_source_native->($config_path); | ||
| 110 | if ($set_config_result < 0) { | ||
| 111 | my $error_message = $get_native_error_native->(); | ||
| 112 | die "Javonet set config source result: $set_config_result. Native error message: $error_message"; | ||
| 113 | } | ||
| 114 | else { | ||
| 115 | return $set_config_result; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | sub tw_set_javonet_working_directory { | ||
| 120 | my ($self, $working_directory) = @_; | ||
| 121 | tw_initialize(); | ||
| 122 | my $set_working_directory_result = $set_working_directory_native->($working_directory); | ||
| 123 | if ($set_working_directory_result < 0) { | ||
| 124 | my $error_message = $get_native_error_native->(); | ||
| 125 | die "Javonet set working directory result: $set_working_directory_result. Native error message: $error_message"; | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | return $set_working_directory_result; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | 1; |