File Coverage

blib/lib/POE/Component/MessageQueue/Client.pm
Criterion Covered Total %
statement 32 32 100.0
branch 2 4 50.0
condition n/a
subroutine 8 8 100.0
pod 0 5 0.0
total 42 49 85.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::Client;
19 13     13   119914 use Moose;
  13         1180719  
  13         105  
20 13     13   95552 use POE::Component::MessageQueue::Subscription;
  13         59  
  13         694  
21 13     13   2918 use POE::Kernel;
  13         133600  
  13         143  
22              
23             has subscriptions => (
24             is => 'ro',
25             isa => 'HashRef[POE::Component::MessageQueue::Subscription]',
26             default => sub { {} },
27             traits => ['Hash'],
28             handles => {
29             'set_subscription' => 'set',
30             'get_subscription' => 'get',
31             'all_subscriptions' => 'values',
32             'delete_subscription' => 'delete',
33             },
34             );
35              
36             has id => (
37             is => 'ro',
38             required => 1,
39             );
40              
41             has connected => (
42             is => 'rw',
43             default => 0,
44             );
45              
46             has login => (is => 'rw');
47             has passcode => (is => 'rw');
48              
49             __PACKAGE__->meta->make_immutable();
50              
51             sub subscribe
52             {
53 6     6 0 25 my ($self, $destination, $client_ack) = @_;
54              
55 6         291 my $subscription = POE::Component::MessageQueue::Subscription->new(
56             destination => $destination,
57             client => $self,
58             client_ack => $client_ack,
59             );
60              
61 6         196 $self->set_subscription($destination->name => $subscription);
62 6         237 $destination->set_subscription($self->id => $subscription);
63 6         39 return $subscription;
64             }
65              
66             sub unsubscribe
67             {
68 6     6 0 20 my ($self, $destination) = @_;
69              
70 6         203 $self->delete_subscription($destination->name);
71 6         170 $destination->delete_subscription($self->id);
72 6         75 $destination->notify(unsubscribe => {
73             destination => $destination,
74             client => $self,
75             });
76             }
77              
78             sub send_frame
79             {
80 365     365 0 10975 my ($self, $frame) = @_;
81 365         758 my ($session, $socket);
82              
83 365 50       12030 return 0 unless ($session = $poe_kernel->alias_resolve($self->id));
84 365 50       12888 return 0 unless ($socket = $session->get_heap()->{client});
85              
86 365         3492 $socket->put($frame);
87 365         47033 return 1;
88             }
89              
90             sub connect
91             {
92 11     11 0 67 my ($self, $login, $passcode) = @_;
93              
94 11         380 $self->login($login);
95 11         349 $self->passcode($passcode);
96 11         363 $self->connected(1);
97              
98 11         298 my $id = $self->id;
99 11         99 $self->send_frame(Net::Stomp::Frame->new({
100             command => "CONNECTED",
101             headers => {
102             session => "client-$id",
103             },
104             }));
105             }
106              
107             sub shutdown
108             {
109 11     11 0 84 my $self = shift;
110 11         369 $poe_kernel->post($self->id, "shutdown");
111             }
112              
113             1;
114