Emitter.pm

filter: Statementstmt 100% 32 / 32 Branchbran 50.0% 3 / 6 Conditioncond 45.4% 5 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 76.9% 50 / 65 CC 8 SCAR 12.1
CC
8
Cov
82%
send
5.9
_find_listener
3.1
send_args
2.0
CRAP 3.4
↑ Summary Listener.pm »
1 package BeamX::Peer::Emitter;
2  
3 # ABSTRACT: Beam::Emitter with peer-to-peer messaging
4  
5 2 ▶use Types::Standard ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 with 'Beam::Emitter';
10  
11 2 our $VERSION = '0.003';
12  
13 2 ▶sub _find_listener {
Subroutine _find_listener: called CC=3 SCAR=11.3
14  
15 2 my ( $self, $peer, $name ) = @_;
16  
17 2 ▶ return if !defined $peer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $peer 0 2 2
18  
19 2 ▶ return ( grep { $_->has_peer && $_->peer == $peer }
Condition66%2/3
$_->has_peer && $_->peer == $peer
!ll&&!rl&&r
202
MC/DC50%1/2
$_->has_peer && $_->peer == $peer
$_->has_peer $_->peer == $peer
Truth table66%2/3
A && B
A= $_->has_peerB= $_->peer == $peer
ABresult
0X0
100
111
20 $self->listeners( $name ) )[0];
21 }
22  
23 #pod =method subscribe
24 #pod
25 #pod # subscribe as Beam::Emitter does
26 #pod $emitter->subscribe( $event_name, $subref, [, %args] );
27 #pod
28 #pod Subscribe to the named event from C<$emitter>. C<$subref>
29 #pod will be called when the event is emitted.
30 #pod
31 #pod By default, the emitter tracks the listener with an object of class
32 #pod L<BeamX::Peer::Listener>. C<%args> is used to pass arguments
33 #pod to its constructor.
34 #pod
35 #pod To enable C<$emitter> to send the event directly to a C<$peer> via
36 #pod the L<send> method, specify the peer with the C<peer> key in C<%args>.
37 #pod
38 #pod $emitter->subscribe( $event_name, $subref, peer => $peer, %args );
39 #pod
40 #pod To use a different listener class, subclass B<BeamX::Peer::Emitter>
41 #pod and pass its name via the C<class> key in C<%args>.
42 #pod
43 #pod $emitter->subscribe( $event_name, $subref, class => MyListener, %args );
44 #pod
45 #pod =cut
46  
47 2 ▶around subscribe => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
48  
49 2 my $orig = shift;
50  
51 2 splice( @_, 3, 0, class => 'BeamX::Peer::Listener', );
52  
53 2 &$orig;
54 2 };
55  
56 #pod =method send
57 #pod
58 #pod $emitter->send( $peer, $event_name [, %args] );
59 #pod
60 #pod Send the named event to the specified peer. C<%args> is a list of
61 #pod name, value pairs to pass to the L<Beam::Event> constructor; use the
62 #pod C<class> key to specify an alternate event class.
63 #pod
64 #pod =cut
65  
66 1 ▶sub send {
Subroutine send: called CC=5 SCAR=17.8
Pod documented
67  
68 1 my ( $self, $peer, $name, %args ) = @_;
69  
70 1 ▶ my $listener = $self->_find_listener( $peer, $name )
Branch: 1 branch
BranchTrueFalseTotal
my $listener = $self->_find_listener($peer, $name) or return 1 0 1
71 or return;
72  
73 1 ▶ my $class = delete $args{class} || "Beam::Event";
Condition50%1/2
delete $args{'class'} || 'Beam::Event'
l!l
01
MC/DC0%0/1
delete $args{'class'} || 'Beam::Event'
delete $args{'class'}
Truth table50%1/2
A
A= delete $args{'class'}
Aresult
11
00
74  
75 1 ▶ $args{emitter} ||= $self;
Condition33%1/3
$args{"emitter"} ||= $self
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{"emitter"} ||= $self
$args{"emitter"} $self
Truth table33%1/3
A ||= B
A= $args{"emitter"}B= $self
ABresult
1X1
011
000
76 1 ▶ $args{name} ||= $name;
Condition33%1/3
$args{"name"} ||= $name
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{"name"} ||= $name
$args{"name"} $name
Truth table33%1/3
A ||= B
A= $args{"name"}B= $name
ABresult
1X1
011
000
77  
78 1 my $event = $class->new( %args );
79 1 $listener->callback->( $event );
80 1 return $event;
81 }
82  
83 #pod =method send_args
84 #pod
85 #pod $emitter->send_args( $peer, $event_name, @args] );
86 #pod
87 #pod Send the named event to the specified peer. C<@args> will be passed
88 #pod to the subscribed callback.
89 #pod
90 #pod =cut
91  
92 1 ▶sub send_args {
Subroutine send_args: called CC=2 SCAR=7.0
Pod documented
93  
94 1 my ( $self, $peer, $name, @args ) = @_;
95  
96 1 ▶ my $listener = $self->_find_listener( $peer, $name )
Branch: 1 branch
BranchTrueFalseTotal
my $listener = $self->_find_listener($peer, $name) or return 1 0 1
97 or return;
98  
99 1 $listener->callback->( @args );
100 1 return;
101 }
102  
103  
104 2 1;
105  
106 #
107 # This file is part of BeamX-Peer-Emitter
108 #
109 # This software is Copyright (c) 2016 by the Smithsonian Astrophysical Observatory.
110 #
111 # This is free software, licensed under:
112 #
113 # The GNU General Public License, Version 3, June 2007
114 #
115  
116 =pod
117  
118 =encoding UTF-8
119  
120 =head1 NAME
121  
122 BeamX::Peer::Emitter - Beam::Emitter with peer-to-peer messaging
123  
124 =head1 VERSION
125  
126 version 0.003
127  
128 =head1 SYNOPSIS
129  
130 use 5.10.0;
131 use Safe::Isa;
132
133 sub fmt_msg {
134 $_[0]->$_isa( 'Beam::Event' )
135 ? sprintf( "received event '%s' from node %s", $_[0]->name, $_[0]->emitter->id )
136 : join( ' ', @_ );
137 }
138
139
140 package Node {
141
142 use Safe::Isa;
143 use Moo;
144 with 'BeamX::Peer::Emitter';
145
146 has id => (
147 is => 'ro',
148 required => 1,
149 );
150
151 sub recv {
152
153 my $self = shift;
154
155 say $self->id, ': ', &::fmt_msg;
156 }
157
158 }
159
160 my $n1 = Node->new( id => 'N1' );
161 my $n2 = Node->new( id => 'N2' );
162
163
164 # standard Beam::Emitter event
165 $n1->subscribe( 'alert', sub { say 'non-peer: ', &fmt_msg } );
166
167 # explicit peer event
168 $n1->subscribe( 'alert', sub { $n2->recv( @_ ) }, peer => $n2 );
169
170 say "Broadcast Event object:";
171 $n1->emit( 'alert' );
172
173 say "\nSend Event object directly to \$n2";
174 $n1->send( $n2, 'alert' );
175
176 say "\nBroadcast arbitrary args";
177 $n1->emit_args( 'alert', q[Server's Down!] );
178
179 say "\nSend arbitrary args directly to \$n2";
180 $n1->send_args( $n2, 'alert', q[Let's get coffee!] );
181  
182 Results in:
183  
184 Broadcast Event object:
185 non-peer: received event 'alert' from node N1
186 N2: received event 'alert' from node N1
187  
188 Send Event object directly to $n2
189 N2: received event 'alert' from node N1
190  
191 Broadcast arbitrary args
192 non-peer: Server's Down!
193 N2: Server's Down!
194  
195 Send arbitrary args directly to $n2
196 N2: Let's get coffee!
197  
198 =head1 DESCRIPTION
199  
200 B<BeamX::Peer::Emitter> is a role (based upon L<Beam::Emitter>) which
201 adds the ability to notify individual subscribers (peers) of
202 events to L<Beam::Emitter>'s publish/subscribe capabilities.
203  
204 =head1 METHODS
205  
206 =head2 subscribe
207  
208 # subscribe as Beam::Emitter does
209 $emitter->subscribe( $event_name, $subref, [, %args] );
210  
211 Subscribe to the named event from C<$emitter>. C<$subref>
212 will be called when the event is emitted.
213  
214 By default, the emitter tracks the listener with an object of class
215 L<BeamX::Peer::Listener>. C<%args> is used to pass arguments
216 to its constructor.
217  
218 To enable C<$emitter> to send the event directly to a C<$peer> via
219 the L<send> method, specify the peer with the C<peer> key in C<%args>.
220  
221 $emitter->subscribe( $event_name, $subref, peer => $peer, %args );
222  
223 To use a different listener class, subclass B<BeamX::Peer::Emitter>
224 and pass its name via the C<class> key in C<%args>.
225  
226 $emitter->subscribe( $event_name, $subref, class => MyListener, %args );
227  
228 =head2 send
229  
230 $emitter->send( $peer, $event_name [, %args] );
231  
232 Send the named event to the specified peer. C<%args> is a list of
233 name, value pairs to pass to the L<Beam::Event> constructor; use the
234 C<class> key to specify an alternate event class.
235  
236 =head2 send_args
237  
238 $emitter->send_args( $peer, $event_name, @args] );
239  
240 Send the named event to the specified peer. C<@args> will be passed
241 to the subscribed callback.
242  
243 =head1 SEE ALSO
244  
245 L<Beam::Emitter>
246  
247 =head1 AUTHOR
248  
249 Diab Jerius <djerius@cpan.org>
250  
251 =head1 COPYRIGHT AND LICENSE
252  
253 This software is Copyright (c) 2016 by the Smithsonian Astrophysical Observatory.
254  
255 This is free software, licensed under:
256  
257 The GNU General Public License, Version 3, June 2007
258  
259 =cut
260  
261 __END__
↑ Summary Listener.pm »