File Coverage

blib/lib/Reflex/POE/Session.pm
Criterion Covered Total %
statement 12 24 50.0
branch 0 4 0.0
condition n/a
subroutine 4 7 57.1
pod 0 3 0.0
total 16 38 42.1


line stmt bran cond sub pod time code
1             package Reflex::POE::Session;
2             # vim: ts=2 sw=2 noexpandtab
3             $Reflex::POE::Session::VERSION = '0.100';
4 8     8   32 use Moose;
  8         13  
  8         64  
5             extends 'Reflex::Base';
6              
7 8     8   36910 use Scalar::Util qw(weaken);
  8         14  
  8         367  
8 8     8   34 use POE::Session; # for ARG0
  8         9  
  8         50  
9 8     8   3879 use Reflex::Event::POE;
  8         23  
  8         2223  
10              
11             my %session_id_to_object;
12              
13             has sid => (
14             isa => 'Str',
15             is => 'ro',
16             );
17              
18             sub BUILD {
19 0     0 0   my $self = shift;
20              
21 0           $session_id_to_object{$self->sid()}{$self} = $self;
22 0           weaken $session_id_to_object{$self->sid()}{$self};
23             }
24              
25             sub DEMOLISH {
26 0     0 0   my $self = shift;
27 0           delete $session_id_to_object{$self->sid()}{$self};
28             delete $session_id_to_object{$self->sid()} unless (
29 0 0         keys %{$session_id_to_object{$self->sid()}}
  0            
30             );
31             }
32              
33             sub deliver {
34 0     0 0   my ($class, $sender_id, $event_name, $args) = @_;
35              
36             # Not a session anyone is interested in.
37 0 0         return unless exists $session_id_to_object{$sender_id};
38              
39 0           foreach my $self (values %{$session_id_to_object{$sender_id}}) {
  0            
40 0           $self->emit(
41             -name => $event_name,
42             -type => 'Reflex::Event::POE',
43             args => [ @$args ],
44             );
45             }
46             }
47              
48             __PACKAGE__->meta->make_immutable;
49              
50             1;
51              
52             __END__
53              
54             =pod
55              
56             =encoding UTF-8
57              
58             =for :stopwords Rocco Caputo
59              
60             =head1 NAME
61              
62             Reflex::POE::Session - Watch events from a POE::Session object.
63              
64             =head1 VERSION
65              
66             This document describes version 0.100, released on April 02, 2017.
67              
68             =head1 SYNOPSIS
69              
70             This sample usage is not a complete program. The rest of the program
71             exists in eg-13-irc-bot.pl, in the tarball's eg directory.
72              
73             sub BUILD {
74             my $self = shift;
75              
76             $self->component(
77             POE::Component::IRC->spawn(
78             nick => "reflex_$$",
79             ircname => "Reflex Test Bot",
80             server => "10.0.0.25",
81             ) || die "Drat: $!"
82             );
83              
84             $self->poco_watcher(
85             Reflex::POE::Session->new(
86             sid => $self->component()->session_id(),
87             )
88             );
89              
90             $self->run_within_session(
91             sub {
92             $self->component()->yield(register => "all");
93             $self->component()->yield(connect => {});
94             }
95             )
96             }
97              
98             =head1 DESCRIPTION
99              
100             Reflex::POE::Session allows a Reflex::Base object to receive events
101             from a specific POE::Session instance, identified by the session's ID.
102              
103             Authors are encouraged to encapsulate POE sessions within Reflex
104             objects. Most users should not need use Reflex::POE::Session (or
105             other Reflex::POE helpers) directly.
106              
107             =head2 Public Attributes
108              
109             =head3 sid
110              
111             The "sid" must contain the ID of the POE::Session to be watched. This
112             is in fact how Reflex::POE::Session knows which session to watch. See
113             L<POE> for more information about session IDs.
114              
115             =head2 Public Events
116              
117             Reflex::POE::Session will emit() events on behalf of the watched
118             POE::Session. If the session posts "irc_001", then
119             Reflex::POE::Session will emit "irc_001", and so on.
120              
121             Reflex::POE::Session's "args" parameter will contain all of the POE
122             event's paramters, from ARG0 through the end of the parameter list.
123             They will be mapped to Reflex paramters "0" through the last index.
124              
125             Assume that this POE post() call invokes this Reflex callback() via
126             Refex::POE::Session:
127              
128             $kernel->post( event => qw(one one two three five) );
129              
130             ...;
131              
132             sub callback {
133             my ($self, $event) = @_;
134             print "$_\n" foreach $event->args_list();
135             }
136              
137             The callback will print five lines:
138              
139             one
140             one
141             two
142             three
143             five
144              
145             =for Pod::Coverage BUILD DEMOLISH deliver
146              
147             =head1 CAVEATS
148              
149             Reflex::POE::Session will take note of every event sent by the
150             session, although it won't try to deliver ones that haven't been
151             registered with the callback object. However, the act of filtering
152             these events out is more overhead than simply not registering interest
153             in the first place. A later version will be more optimal.
154              
155             Reflex::POE::Wheel provides a way to map parameters to symbolic names.
156             Reflex::POE::Session may also provide a similar mechanism in the
157             future, obsoleting the parameter numbers.
158              
159             =head1 SEE ALSO
160              
161             Please see those modules/websites for more information related to this module.
162              
163             =over 4
164              
165             =item *
166              
167             L<Reflex|Reflex>
168              
169             =item *
170              
171             L<Moose::Manual::Concepts>
172              
173             =item *
174              
175             L<Reflex>
176              
177             =item *
178              
179             L<Reflex::POE::Event>
180              
181             =item *
182              
183             L<Reflex::POE::Postback>
184              
185             =item *
186              
187             L<Reflex::POE::Wheel::Run>
188              
189             =item *
190              
191             L<Reflex::POE::Wheel>
192              
193             =item *
194              
195             L<Reflex/ACKNOWLEDGEMENTS>
196              
197             =item *
198              
199             L<Reflex/ASSISTANCE>
200              
201             =item *
202              
203             L<Reflex/AUTHORS>
204              
205             =item *
206              
207             L<Reflex/BUGS>
208              
209             =item *
210              
211             L<Reflex/BUGS>
212              
213             =item *
214              
215             L<Reflex/CONTRIBUTORS>
216              
217             =item *
218              
219             L<Reflex/COPYRIGHT>
220              
221             =item *
222              
223             L<Reflex/LICENSE>
224              
225             =item *
226              
227             L<Reflex/TODO>
228              
229             =back
230              
231             =head1 BUGS AND LIMITATIONS
232              
233             You can make new bug reports, and view existing ones, through the
234             web interface at L<http://rt.cpan.org/Public/Dist/Display.html?Name=Reflex>.
235              
236             =head1 AUTHOR
237              
238             Rocco Caputo <rcaputo@cpan.org>
239              
240             =head1 COPYRIGHT AND LICENSE
241              
242             This software is copyright (c) 2017 by Rocco Caputo.
243              
244             This is free software; you can redistribute it and/or modify it under
245             the same terms as the Perl 5 programming language system itself.
246              
247             =head1 AVAILABILITY
248              
249             The latest version of this module is available from the Comprehensive Perl
250             Archive Network (CPAN). Visit L<http://www.perl.com/CPAN/> to find a CPAN
251             site near you, or see L<https://metacpan.org/module/Reflex/>.
252              
253             =head1 DISCLAIMER OF WARRANTY
254              
255             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
256             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
257             WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
258             PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
259             EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
260             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
261             PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
262             SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME
263             THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
264              
265             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
266             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
267             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
268             TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
269             CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
270             SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
271             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
272             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
273             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
274             DAMAGES.
275              
276             =cut