| 1 | package Javonet::Core::Protocol::CommandSerializer; | ||
| 2 | use strict; | ||
| 3 | use warnings FATAL => 'all'; | ||
| 4 | |||
| 5 | use lib 'lib'; | ||
| 6 | use aliased 'Javonet::Sdk::Core::PerlCommand' => 'PerlCommand'; | ||
| 7 | use aliased 'Javonet::Sdk::Core::RuntimeLib' => 'RuntimeLib', qw(get_runtime); | ||
| 8 | use aliased 'Javonet::Sdk::Core::Type' => 'Type', qw(get_type); | ||
| 9 | use aliased 'Javonet::Core::Protocol::TypeSerializer' => 'TypeSerializer'; | ||
| 10 | use aliased 'Javonet::Sdk::Core::TypesHandler' => 'TypesHandler'; | ||
| 11 | use aliased 'Javonet::Core::Handler::ReferencesCache' => 'ReferencesCache'; | ||
| 12 | use aliased 'Javonet::Sdk::Core::PerlCommandType' => 'PerlCommandType'; | ||
| 13 | use Javonet::Sdk::Internal::ConnectionType; | ||
| 14 | |||
| 15 | use Exporter qw(import); | ||
| 16 | our @EXPORT = qw(serialize serialize_connection_data); | ||
| 17 | |||
| 18 | sub serialize { | ||
| 19 | my ($class, $root_command, $connection_type, $tcp_address, $runtimeVersion) = @_; | ||
| 20 | $runtimeVersion = 0 unless defined $runtimeVersion; | ||
| 21 | |||
| 22 | # Local buffer instead of global | ||
| 23 | my @byte_buffer = (); | ||
| 24 | |||
| 25 | # Helper to insert data into buffer | ||
| 26 | my $insert_into_buffer = sub { | ||
| 27 | my (@data) = @_; | ||
| 28 | push @byte_buffer, @data; | ||
| 29 | }; | ||
| 30 | |||
| 31 | # Write runtime and version | ||
| 32 | $insert_into_buffer->($root_command->{runtime}, $runtimeVersion); | ||
| 33 | |||
| 34 | # Write connection data | ||
| 35 | my @connection_data = $class->serialize_connection_data($connection_type, $tcp_address); | ||
| 36 | $insert_into_buffer->(@connection_data); | ||
| 37 | |||
| 38 | # Write runtime name and command type header for Perl runtime | ||
| 39 | $insert_into_buffer->( | ||
| 40 | Javonet::Sdk::Core::RuntimeLib::get_runtime('Perl'), | ||
| 41 | $root_command->{command_type} | ||
| 42 | ); | ||
| 43 | |||
| 44 | # Serialize payload recursively | ||
| 45 | $class->serialize_recursively($root_command, $insert_into_buffer); | ||
| 46 | |||
| 47 | return @byte_buffer; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub serialize_connection_data { | ||
| 51 | my ($class, $connection_type, $tcp_address) = @_; | ||
| 52 | if (defined $connection_type && | ||
| 53 | Javonet::Sdk::Internal::ConnectionType::get_connection_type('Tcp') eq $connection_type) { | ||
| 54 | my @tcp_array = split(':', $tcp_address); | ||
| 55 | my @tcp_ip = split('\\.', $tcp_array[0]); | ||
| 56 | my @bytes_port = unpack "C*", pack "v", $tcp_array[1]; | ||
| 57 | return (Javonet::Sdk::Internal::ConnectionType::get_connection_type('Tcp'), @tcp_ip, @bytes_port); | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | return (Javonet::Sdk::Internal::ConnectionType::get_connection_type('InMemory'), 0, 0, 0, 0, 0, 0); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | # Recursively serialize command payload - matches C# SerializeRecursively | ||
| 65 | sub serialize_recursively { | ||
| 66 | my ($class, $command, $insert_into_buffer) = @_; | ||
| 67 | |||
| 68 | my $payload_ref = $command->{payload}; | ||
| 69 | my @payload = @$payload_ref; | ||
| 70 | |||
| 71 | foreach my $item (@payload) { | ||
| 72 | if (defined $item && ref $item eq 'Javonet::Sdk::Core::PerlCommand') { | ||
| 73 | # Item is a Command - serialize it and recurse | ||
| 74 | my @command_bytes = TypeSerializer->serializeCommand($item); | ||
| 75 | $insert_into_buffer->(@command_bytes); | ||
| 76 | $class->serialize_recursively($item, $insert_into_buffer); | ||
| 77 | } | ||
| 78 | elsif (TypesHandler->is_primitive_or_none($item)) { | ||
| 79 | # Item is primitive or null | ||
| 80 | my @primitive_bytes = TypeSerializer->serializePrimitive($item); | ||
| 81 | $insert_into_buffer->(@primitive_bytes); | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | # Item is a reference - cache it and create a Reference command | ||
| 85 | my $reference_cache = ReferencesCache->new(); | ||
| 86 | my $guid = $reference_cache->cache_reference($item); | ||
| 87 | my $reference_command = PerlCommand->new( | ||
| 88 | runtime => Javonet::Sdk::Core::RuntimeLib::get_runtime('Perl'), | ||
| 89 | command_type => PerlCommandType->get_command_type('Reference'), | ||
| 90 | payload => [$guid] | ||
| 91 | ); | ||
| 92 | my @command_bytes = TypeSerializer->serializeCommand($reference_command); | ||
| 93 | $insert_into_buffer->(@command_bytes); | ||
| 94 | $class->serialize_recursively($reference_command, $insert_into_buffer); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; |