File Coverage

blib/lib/POE/Component/MessageQueue/Message.pm
Criterion Covered Total %
statement 28 30 93.3
branch 5 10 50.0
condition 6 14 42.8
subroutine 7 7 100.0
pod 0 4 0.0
total 46 65 70.7


line stmt bran cond sub pod time code
1             #
2             # Copyright 2007-2010 David Snopek <dsnopek@gmail.com>
3             #
4             # This program is free software: you can redistribute it and/or modify
5             # it under the terms of the GNU General Public License as published by
6             # the Free Software Foundation, either version 2 of the License, or
7             # (at your option) any later version.
8             #
9             # This program is distributed in the hope that it will be useful,
10             # but WITHOUT ANY WARRANTY; without even the implied warranty of
11             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12             # GNU General Public License for more details.
13             #
14             # You should have received a copy of the GNU General Public License
15             # along with this program. If not, see <http://www.gnu.org/licenses/>.
16             #
17              
18             package POE::Component::MessageQueue::Message;
19 13     13   186773 use Moose;
  13         798523  
  13         96  
20 13     13   86274 use Net::Stomp::Frame;
  13         9522  
  13         157  
21              
22             has id => (
23             is => 'ro',
24             isa => 'Str',
25             required => 1,
26             );
27              
28             has destination => (
29             is => 'ro',
30             isa => 'Str',
31             required => 1,
32             );
33              
34             has body => (
35             is => 'rw',
36             clearer => 'delete_body',
37             );
38              
39             has persistent => (
40             is => 'ro',
41             isa => 'Bool',
42             required => 1,
43             );
44              
45             has expire_at => (
46             is => 'rw',
47             isa => 'Num',
48             predicate => 'has_expiration',
49             );
50              
51             has 'deliver_at' => (
52             is => 'rw',
53             isa => 'Num',
54             predicate => 'has_delay',
55             clearer => 'clear_delay',
56             );
57              
58             has claimant => (
59             is => 'rw',
60             isa => 'Maybe[Int]',
61             writer => 'claim',
62             predicate => 'claimed',
63             clearer => 'disown',
64             );
65              
66             has 'size' => (
67             is => 'ro',
68             isa => 'Num',
69             lazy => 1,
70             default => sub {
71             my $self = shift;
72 13     13   3314 use bytes;
  13         67  
  13         101  
73             return bytes::length($self->body);
74             }
75             );
76              
77             my $order = 0;
78             my $last_time = 0;
79              
80             has 'timestamp' => (
81             is => 'ro',
82             isa => 'Num',
83             default => sub {
84             my $time = time;
85             $order = 0 if $time != $last_time;
86             $last_time = $time;
87             return "$time." . sprintf('%05d', $order++);
88             },
89             );
90              
91             __PACKAGE__->meta->make_immutable();
92              
93             sub equals
94             {
95 1414     1414 0 7439 my ($self, $other) = @_;
96             # This is a dirty hack, rewriting to use get_attribute_list would be preferred
97             #foreach my $ameta (values %{__PACKAGE__->meta->_attribute_map})
98 1414         3859 foreach my $name (__PACKAGE__->meta->get_attribute_list())
99             {
100 12726         64694 my $ameta = __PACKAGE__->meta->get_attribute($name);
101 12726         278572 my $reader = $ameta->get_read_method;
102 12726         441270 my ($one, $two) = ($self->$reader, $other->$reader);
103 12726 50 66     35550 next if (!defined $one) && (!defined $two);
104 8484 50 33     32141 return 0 unless (defined $one) && (defined $two);
105 8484 50       23371 return 0 unless ($one eq $two);
106             }
107 1414         4341 return 1;
108             }
109              
110             sub clone
111             {
112 2101     2101 0 3523739 my $self = $_[0];
113 2101         9036 return $self->meta->clone_object($self);
114             }
115              
116             sub from_stomp_frame {
117 150     150 0 444 my ($class, $frame) = @_;
118 150   50     447 my $persistent = lc($frame->headers->{persistent} || '');
119             my $msg = $class->new(
120             id => $frame->headers->{'message-id'},
121             destination => $frame->headers->{destination},
122 150   33     1053 persistent => ($persistent eq 'true') || ($persistent eq '1'),
123             body => $frame->body,
124             );
125 150 50 33     4883 if (!$msg->persistent and my $after = $frame->headers->{'expire-after'}) {
126 0         0 $msg->expire_at(time + $after);
127             }
128 150 50       525 if (my $after = $frame->headers->{'deliver-after'}) {
129 0         0 $msg->deliver_at(time + $after);
130             }
131 150         1090 return $msg;
132             }
133              
134             sub create_stomp_frame
135             {
136 131     131 0 381 my $self = shift;
137              
138 131         4224 return Net::Stomp::Frame->new({
139             command => 'MESSAGE',
140             headers => {
141             'destination' => $self->destination,
142             'message-id' => $self->id,
143             'content-length' => $self->size,
144             },
145             body => $self->body,
146             });
147             }
148              
149             1;
150