File Coverage

blib/lib/POE/Component/MessageQueue/Client.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


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 1     1   2698 use Moose;
  0            
  0            
20             use POE::Component::MessageQueue::Subscription;
21             use POE::Kernel;
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             my ($self, $destination, $client_ack) = @_;
54              
55             my $subscription = POE::Component::MessageQueue::Subscription->new(
56             destination => $destination,
57             client => $self,
58             client_ack => $client_ack,
59             );
60              
61             $self->set_subscription($destination->name => $subscription);
62             $destination->set_subscription($self->id => $subscription);
63             return $subscription;
64             }
65              
66             sub unsubscribe
67             {
68             my ($self, $destination) = @_;
69              
70             $self->delete_subscription($destination->name);
71             $destination->delete_subscription($self->id);
72             $destination->notify(unsubscribe => {
73             destination => $destination,
74             client => $self,
75             });
76             }
77              
78             sub send_frame
79             {
80             my ($self, $frame) = @_;
81             my ($session, $socket);
82              
83             return 0 unless ($session = $poe_kernel->alias_resolve($self->id));
84             return 0 unless ($socket = $session->get_heap()->{client});
85              
86             $socket->put($frame);
87             return 1;
88             }
89              
90             sub connect
91             {
92             my ($self, $login, $passcode) = @_;
93              
94             $self->login($login);
95             $self->passcode($passcode);
96             $self->connected(1);
97              
98             my $id = $self->id;
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             my $self = shift;
110             $poe_kernel->post($self->id, "shutdown");
111             }
112              
113             1;
114