Devel/IPerl/Message/ZMQ.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 99 CC 3 SCAR 12.0
CC
3
Cov
0%
message_from_zmq_blobs
6.0
zmq_blobs_from_message
6.0
messages_from_zmq_blobs
2.0
CRAP 3.3
1 package Devel::IPerl::Message::ZMQ;
2 $Devel::IPerl::Message::ZMQ::VERSION = '0.012';
3 use strict;
4 use warnings;
5 use Moo;
6 use JSON::MaybeXS;
7 use namespace::autoclean;
8 use Digest::SHA qw(hmac_sha256_hex);
9  
10 extends qw(Devel::IPerl::Message);
11  
12 use constant DELIMITER => '<IDS|MSG>';
13  
14 has zmq_uuids => ( is => 'rw', default => sub { [] } );
15 has shared_key => ( is => 'rw', predicate => 1 ); # has_shared_key
16  
17 # reads in message from ZMQ wire protocol
18 # see spec: <http://ipython.org/ipython-doc/dev/development/messaging.html#the-wire-protocol>
19 sub message_from_zmq_blobs {
20 my ($self, $blobs, %opt) = @_;
21  
22 # Read in all identifiers
23 my @uuids = ();
24 while (1) {
25 if ($blobs->[0] eq DELIMITER) {
26 shift @$blobs;
27 last;
28 } else {
29 push @uuids, $blobs->[0];
30 shift @$blobs;
31 }
32 }
33  
34 my $hmac_signature = $blobs->[0]; # b'baddad42', # HMAC signature
35 my $header = $blobs->[1]; # b'{header}', # serialized header dict
36 my $parent_header = $blobs->[2]; # b'{parent_header}', # serialized parent header dict
37 my $metadata = $blobs->[3]; # b'{metadata}', # serialized metadata dict
38 my $content = $blobs->[4]; # b'{content}, # serialized content dict
39  
40 # b'blob', # extra raw data buffer(s)
41 my $number_of_blobs = @$blobs;
42 my @blobs_rest = ( @$blobs[5..$number_of_blobs-1] );
43  
44 # TODO check the signature
45 $self->new(
46 zmq_uuids => \@uuids,
47 header => decode_json($header),
48 parent_header => decode_json($parent_header),
49 metadata => decode_json($metadata),
50 content => decode_json($content),
51 blobs => [ @blobs_rest ],
52 %opt,
53 );
54 }
55  
56 sub messages_from_zmq_blobs {
57 my ($self, $blobs, %opt) = @_;
58  
59 # UUID is at the start of the blobs
60 my $uuid = $blobs->[0];
61  
62 # each messages starts with the UUID
63 my @message_starts = grep { $blobs->[$_] eq $uuid } 0..@$blobs-1;
64 # each message ends right before the next one starts
65 my @message_ends = map { $message_starts[$_] - 1 } 1..@message_starts-1;
66 # last message goes to the very end
67 push @message_ends, @$blobs - 1;
68  
69 my @messages;
70 for my $idx (0..@message_starts-1) {
71 my $message_blobs = [ @{$blobs}[ $message_starts[$idx]..$message_ends[$idx] ] ];
72 push @messages, $self->message_from_zmq_blobs( $message_blobs, %opt );
73 }
74 @messages;
75 }
76  
77 sub zmq_blobs_from_message {
78 my ($self) = @_;
79  
80 my $serialized;
81 my @blob_order = qw( header parent_header metadata content );
82 $serialized->{$_} = encode_json( $self->$_ ) for @blob_order;
83  
84 # implement HMAC signature
85 my $hmac_signature;
86 if( $self->has_shared_key ) {
87 my $data = "";
88 $data .= $serialized->{$_} for @blob_order;
89 $hmac_signature = hmac_sha256_hex( $data, $self->shared_key );
90 } else {
91 # if auth is disabled, signature is empty string
92 $hmac_signature = '';
93 }
94  
95 [
96 @{$self->zmq_uuids},
97 DELIMITER,
98 $hmac_signature,
99 @$serialized{@blob_order},
100 ( map { encode_json($_) } @{ $self->blobs } ),
101 ];
102 }
103  
104 around new_reply_to => sub {
105 my $orig = shift;
106 my $ret = $orig->(@_);
107 $ret->shared_key( $ret->reply_to->shared_key )
108 if $ret->reply_to->has_shared_key;
109 $ret->zmq_uuids( $ret->reply_to->zmq_uuids );
110 $ret;
111 };
112  
113 1;
114  
115 __END__