File Coverage

blib/lib/XAS/Lib/POE/PubSub.pm
Criterion Covered Total %
statement 6 24 25.0
branch n/a
condition n/a
subroutine 2 6 33.3
pod 4 4 100.0
total 12 34 35.2


line stmt bran cond sub pod time code
1             package XAS::Lib::POE::PubSub;
2              
3             our $VERSION = '0.01';
4              
5 1     1   3 use POE;
  1         2  
  1         4  
6              
7             use XAS::Class
8 1         5 debug => 0,
9             version => $VERSION,
10             base => 'XAS::Singleton',
11             utils => ':validation',
12 1     1   217 ;
  1         5  
13              
14             #use Data::Dumper;
15              
16             # ----------------------------------------------------------------------
17             # Public Methods
18             # ----------------------------------------------------------------------
19              
20             sub subscribe {
21 0     0 1   my $self = shift;
22 0           my ($session, $channel) = validate_params(\@_, [
23             1,
24             { optional => 1, default => 'default' }
25             ]);
26              
27 0           $self->{'registry'}->{$channel}->{$session} = $session;
28              
29             }
30              
31             sub unsubscribe {
32 0     0 1   my $self = shift;
33 0           my ($session, $channel) = validate_params(\@_, [
34             1,
35             { optional => 1, default => 'default' }
36             ]);
37              
38 0           delete $self->{'registry'}->{$channel}->{$session};
39              
40             }
41              
42             sub publish {
43 0     0 1   my $self = shift;
44 0           my $p = validate_params(\@_, {
45             -event => 1,
46             -args => { optional => 1, default => undef },
47             -channel => { optional => 1, default => 'default' },
48             });
49              
50 0           my $event = $p->{'event'};
51 0           my $channel = $p->{'channel'};
52 0           my $args = $p->{'args'};
53              
54 0           foreach my $session (keys %{$self->{'registry'}->{$channel}}) {
  0            
55              
56 0           $poe_kernel->post($session, $event, $args);
57              
58             }
59              
60             }
61              
62             # ----------------------------------------------------------------------
63             # Private Methods
64             # ----------------------------------------------------------------------
65              
66             sub init {
67 0     0 1   my $class = shift;
68              
69 0           my $self = $class->SUPER::init(@_);
70              
71 0           $self->{'registry'} = {};
72              
73 0           return $self;
74              
75             }
76              
77             1;
78              
79             __END__
80              
81             =head1 NAME
82              
83             XAS::Lib::POE::PubSub - A publish/subscribe class for POE sessions
84              
85             =head1 SYNOPSIS
86              
87             use POE;
88             use XAS::Lib::POE::PubSub;
89              
90             my $pubsub = XAS::Lib::POE::PubSub->new();
91              
92             $pubsub->subscribe('session', 'channel');
93             $pubusb->publish(
94             -event => 'event',
95             -channel => 'channel',
96             );
97              
98             =head1 DESCRIPTION
99              
100             This is a very simple channel based publish/subscribe framework for POE. It
101             is implemented as a singleton. It allows you to publish events to all
102             interested sessions.
103              
104             =head1 METHODS
105              
106             =head2 subscribe($session, $channel);
107              
108             This method allows you to subscribe to the specified channel.
109              
110             =over 4
111              
112             =item B<$session>
113              
114             The session name.
115              
116             =item B<$channel>
117              
118             The optional channel to subscribe too. Defaults to 'default'.
119              
120             =back
121              
122             =head2 unsubscribe($session, $channel);
123              
124             This method allows you to unsubscribe from the specified channel.
125              
126             =over 4
127              
128             =item B<$session>
129              
130             The session name.
131              
132             =item B<$channel>
133              
134             The optional channel to unsubscribe from. Defaults to 'default'.
135              
136             =back
137              
138             =head2 publish
139              
140             This method allows you to publish an event to a specified channel. Additional
141             arguuments can be supplied. It takes the following parameters:
142              
143             =over 4
144              
145             =item B<-event>
146              
147             The event to send.
148              
149             =item B<-channel>
150              
151             Optional channel. Defaults to 'default'.
152              
153             =item B<-args>
154              
155             Optional additional arguments to send with the event. The context of the
156             arguments is determined by the method that is handling the event. For example:
157              
158             -args => ['this', 'is', 'neat']
159             -args => { this => 'is neat' }
160             -args => 'this is neat'
161              
162             Are all valid arguments.
163              
164             =back
165              
166             =head1 SEE ALSO
167              
168             =over 4
169              
170             =item L<XAS|XAS>
171              
172             =back
173              
174             =head1 AUTHOR
175              
176             Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
177              
178             =head1 COPYRIGHT AND LICENSE
179              
180             Copyright (C) 2014 Kevin L. Esteb
181              
182             This is free software; you can redistribute it and/or modify it under
183             the terms of the Artistic License 2.0. For details, see the full text
184             of the license at http://www.perlfoundation.org/artistic_license_2_0.
185              
186             =cut