File Coverage

blib/lib/Event/RPC/Message/Negotiate.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 4 100.0
condition 6 6 100.0
subroutine 10 10 100.0
pod 0 6 0.0
total 51 57 89.4


line stmt bran cond sub pod time code
1             # $Id: Message.pm,v 1.9 2014-01-28 15:40:10 joern Exp $
2              
3             #-----------------------------------------------------------------------
4             # Copyright (C) 2002-2015 by Jörn Reder .
5             # All Rights Reserved. See file COPYRIGHT for details.
6             #
7             # This module is part of Event::RPC, which is free software; you can
8             # redistribute it and/or modify it under the same terms as Perl itself.
9             #-----------------------------------------------------------------------
10              
11             package Event::RPC::Message::Negotiate;
12              
13 24     24   133 use base Event::RPC::Message;
  24         53  
  24         14217  
14              
15 24     24   178 use Carp;
  24         35  
  24         1504  
16 24     24   142 use strict;
  24         37  
  24         497  
17 24     24   125 use utf8;
  24         37  
  24         129  
18              
19             my %MESSAGE_FORMATS = (
20             "SERL" => "Event::RPC::Message::Sereal",
21             "CBOR" => "Event::RPC::Message::CBOR",
22             "JSON" => "Event::RPC::Message::JSON",
23             "STOR" => "Event::RPC::Message::Storable",
24             );
25              
26             my @MESSAGE_FORMAT_ORDER = qw( SERL CBOR JSON STOR );
27              
28 107     107 0 893 sub known_message_formats { \%MESSAGE_FORMATS }
29 43     43 0 178365 sub message_format_order { \@MESSAGE_FORMAT_ORDER }
30              
31             my $STORABLE_FALLBACK_OK = 0;
32 11     11 0 96 sub get_storable_fallback_ok { $STORABLE_FALLBACK_OK }
33 84     84 0 438 sub set_storable_fallback_ok { $STORABLE_FALLBACK_OK = $_[1] }
34              
35             sub encode_message {
36 138     138 0 343 my $self = shift;
37 138         290 my ($decoded) = @_;
38              
39 138   100     1718 my $ok = $decoded->{ok} || "";
40 138   100     1325 my $msg = $decoded->{msg} || "";
41 138   100     903 my $cmd = $decoded->{cmd} || "";
42              
43 138         1737 s,/\d/,,g for ( $ok, $msg, $cmd );
44              
45 138         1317 return "/0/E:R:M:N/1/$ok/2/$msg/3/$cmd/0/";
46             }
47              
48             sub decode_message {
49 139     139 0 343 my $self = shift;
50 139         919 my ($encoded) = @_;
51              
52 139 100       5340 return { ok => $1, msg => $2, cmd => $3 }
53             if $encoded =~ m,^/0/E:R:M:N/1/(.*?)/2/(.*?)/3/(.*?)/0/$,;
54              
55             #-- An old client or server may not know our message
56             #-- format negotiation protocol, so we provide a Storable
57             #-- fallback mechanism, if we're allowed to do so.
58 11 100       154 if ( $self->get_storable_fallback_ok ) {
59 10         235 require Event::RPC::Message::Storable;
60 10         190 bless $self, "Event::RPC::Message::Storable";
61 10         127 return $self->decode_message($encoded);
62             }
63              
64             #-- No Storable fallback allowed. We bail out!
65 1         41 die "Error on message format negotitation and no fallback to Storable allowed\n";
66             }
67              
68             1;
69              
70             __END__