| 1 | # ABSTRACT: Peer-to-Peer Publish/Subscribe Network of Objects | ||||||||||||||||||||||||||||||||
| 2 | package Net::Object::Peer; | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use 5.10.0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 18 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 18 | our @CARP_NOT = qw( Beam::Emitter ); | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 18 | ▶ | use List::Util qw[ all first uniqstr ]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 18 | ▶ | use Scalar::Util qw[ refaddr weaken ]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 18 | ▶ | use Data::OptList qw[ mkopt ]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 18 | ▶ | use Safe::Isa; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 18 | ▶ | use Ref::Util qw[ is_arrayref ]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 18 | ▶ | use Types::Standard ':all'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 18 | ▶ | use Sub::Quote; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 18 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 18 | ▶ | use MooX::ProtectedAttributes; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 18 | our $VERSION = '0.06'; # TRIAL | |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 18 | ▶ | use Net::Object::Peer::Event; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 25 | 18 | ▶ | use Net::Object::Peer::Types qw[ -all ]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 26 | 18 | ▶ | use Net::Object::Peer::UnsubscribeEvent; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 27 | 18 | ▶ | use Net::Object::Peer::Listener; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | 18 | ▶ | use Net::Object::Peer::Emitter; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 29 | 18 | ▶ | use Net::Object::Peer::RefAddr; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 30 | 18 | ▶ | use Net::Object::Peer::Subscriptions; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 18 | ▶ | use Sub::QuoteX::Utils qw[ quote_subs ]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | #pod =begin pod_coverage | ||||||||||||||||||||||||||||||||
| 35 | #pod | ||||||||||||||||||||||||||||||||
| 36 | #pod =head3 UnsubscribeEvent | ||||||||||||||||||||||||||||||||
| 37 | #pod | ||||||||||||||||||||||||||||||||
| 38 | #pod =head3 Subscription | ||||||||||||||||||||||||||||||||
| 39 | #pod | ||||||||||||||||||||||||||||||||
| 40 | #pod =head3 Listener | ||||||||||||||||||||||||||||||||
| 41 | #pod | ||||||||||||||||||||||||||||||||
| 42 | #pod =head3 Emitter | ||||||||||||||||||||||||||||||||
| 43 | #pod | ||||||||||||||||||||||||||||||||
| 44 | #pod =end pod_coverage | ||||||||||||||||||||||||||||||||
| 45 | #pod | ||||||||||||||||||||||||||||||||
| 46 | #pod =cut | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 18 | ▶ | use constant UNSUBSCRIBED => 'unsubscribed'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 49 | 18 | ▶ | use constant UnsubscribeEvent => __PACKAGE__ . '::UnsubscribeEvent'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 50 | 18 | ▶ | use constant Emitter => __PACKAGE__ . '::Emitter'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 18 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | has _subscriptions => ( | ||||||||||||||||||||||||||||||||
| 56 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 57 | init_args => undef, | ||||||||||||||||||||||||||||||||
| 58 | isa => InstanceOf ['Net::Object::Peer::Subscriptions'], | ||||||||||||||||||||||||||||||||
| 59 | 105 | ▶ | default => sub { Net::Object::Peer::Subscriptions->new }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 60 | 18 | ); | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | protected_has _emitter => ( | ||||||||||||||||||||||||||||||||
| 63 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 64 | init_arg => undef, | ||||||||||||||||||||||||||||||||
| 65 | 107 | ▶ | default => sub { Net::Object::Peer::Emitter->new }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 66 | 18 | handles => [qw< emit_args send_args >], | |||||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | #pod =attr event_handler_prefix | ||||||||||||||||||||||||||||||||
| 70 | #pod | ||||||||||||||||||||||||||||||||
| 71 | #pod The string which prefixes default event handler method names. See | ||||||||||||||||||||||||||||||||
| 72 | #pod L</subscribe>. It will by default be initialized to the return value | ||||||||||||||||||||||||||||||||
| 73 | #pod of the L</default_event_handler_prefix> method. It may be specified | ||||||||||||||||||||||||||||||||
| 74 | #pod during object construction. | ||||||||||||||||||||||||||||||||
| 75 | #pod | ||||||||||||||||||||||||||||||||
| 76 | #pod For example, the default handler method name for an event named | ||||||||||||||||||||||||||||||||
| 77 | #pod C<changed> would be C<_cb_changed>. The class must provide that | ||||||||||||||||||||||||||||||||
| 78 | #pod method. See L</subscribe> for more information. | ||||||||||||||||||||||||||||||||
| 79 | #pod | ||||||||||||||||||||||||||||||||
| 80 | #pod =cut | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | has event_handler_prefix => ( | ||||||||||||||||||||||||||||||||
| 83 | is => 'lazy', | ||||||||||||||||||||||||||||||||
| 84 | isa => Str, | ||||||||||||||||||||||||||||||||
| 85 | 13 | ▶ | builder => sub { $_[0]->default_event_handler_prefix }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_build_event_handler_prefix: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 86 | 18 | ); | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 18 | has _events_arr => ( | |||||||||||||||||||||||||||||||
| 91 | is => 'rwp', | ||||||||||||||||||||||||||||||||
| 92 | isa => ArrayRef [Identifier], | ||||||||||||||||||||||||||||||||
| 93 | coerce => qsub q{ 'ARRAY' eq ref $_[0] ? [ @{$_[0]} ] : [ $_[0] ]; }, | ||||||||||||||||||||||||||||||||
| 94 | default => qsub q{ ['detach', 'unsubscribed', $_[0]->default_events ]; }, | ||||||||||||||||||||||||||||||||
| 95 | init_arg => 'events', | ||||||||||||||||||||||||||||||||
| 96 | trigger => 1 | ||||||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 18 | has _events_hash => ( | |||||||||||||||||||||||||||||||
| 100 | is => 'lazy', | ||||||||||||||||||||||||||||||||
| 101 | init_args => undef, | ||||||||||||||||||||||||||||||||
| 102 | clearer => 1, | ||||||||||||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 4 | ▶ | sub _trigger__events_arr { | ||||||||||||||||||||||||||||||
|
Subroutine
_trigger__events_arr: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | # force rebuild of _events attribute | ||||||||||||||||||||||||||||||||
| 110 | 4 | $self->_clear_events_hash; | |||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | # _events gives us a unique list which includes the standard ones. | ||||||||||||||||||||||||||||||||
| 113 | # we know that we own the arrayref in $self->events because of the | ||||||||||||||||||||||||||||||||
| 114 | # its coercion. | ||||||||||||||||||||||||||||||||
| 115 | 4 | @{ $self->_events_arr } = keys %{ $self->_events_hash }; | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 104 | ▶ | sub _build__events_hash { | ||||||||||||||||||||||||||||||
|
Subroutine
_build__events_hash: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 104 | my %events = ( detach => 1, unsubscribed => 1 ); | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 104 | my $events_arr = $_[0]->_events_arr; | |||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | # just need to populate the keys | ||||||||||||||||||||||||||||||||
| 125 | 104 | ▶ | @events{ @{ $events_arr } } = undef | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | if $events_arr; | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 104 | \%events; | |||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | #pod =method emits_events | ||||||||||||||||||||||||||||||||
| 132 | #pod | ||||||||||||||||||||||||||||||||
| 133 | #pod $bool = $obj->emits_events( @event_names ); | ||||||||||||||||||||||||||||||||
| 134 | #pod | ||||||||||||||||||||||||||||||||
| 135 | #pod Returns true if the object emits I<all> of the named events | ||||||||||||||||||||||||||||||||
| 136 | #pod | ||||||||||||||||||||||||||||||||
| 137 | #pod =cut | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 373 | ▶ | sub emits_events { | ||||||||||||||||||||||||||||||
|
Subroutine
emits_events: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 373 | my $self = shift; | |||||||||||||||||||||||||||||||
| 142 | 373 | my $events = $self->_events_hash; | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 373 | ▶ | return all { exists $events->{$_} } @_; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | #pod =method default_events | ||||||||||||||||||||||||||||||||
| 148 | #pod | ||||||||||||||||||||||||||||||||
| 149 | #pod @events = $class->default_events; | ||||||||||||||||||||||||||||||||
| 150 | #pod | ||||||||||||||||||||||||||||||||
| 151 | #pod Returns a list of events which this class will emit, excluding the C<detach> and C<unsubscribed> events. | ||||||||||||||||||||||||||||||||
| 152 | #pod The default implementation returns an empty list. A per object event list may be specified via | ||||||||||||||||||||||||||||||||
| 153 | #pod the L</events> attribute or the C<events> option to the L<constructor|/new>. | ||||||||||||||||||||||||||||||||
| 154 | #pod | ||||||||||||||||||||||||||||||||
| 155 | #pod =cut | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | 64 | ▶ | sub default_events { } | ||||||||||||||||||||||||||||||
|
Subroutine
default_events: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | #pod =attr addr | ||||||||||||||||||||||||||||||||
| 162 | #pod | ||||||||||||||||||||||||||||||||
| 163 | #pod A L<Net::Object::Peer::RefAddr> object providing a unique identity for this emitter. | ||||||||||||||||||||||||||||||||
| 164 | #pod | ||||||||||||||||||||||||||||||||
| 165 | #pod =cut | ||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | 18 | has addr => ( | |||||||||||||||||||||||||||||||
| 168 | is => 'rwp', | ||||||||||||||||||||||||||||||||
| 169 | isa => InstanceOf ['Net::Object::Peer::RefAddr'], | ||||||||||||||||||||||||||||||||
| 170 | init_arg => undef, | ||||||||||||||||||||||||||||||||
| 171 | predicate => 1, | ||||||||||||||||||||||||||||||||
| 172 | ); | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | #pod =begin pod_coverage | ||||||||||||||||||||||||||||||||
| 175 | #pod | ||||||||||||||||||||||||||||||||
| 176 | #pod =head4 has_addr | ||||||||||||||||||||||||||||||||
| 177 | #pod | ||||||||||||||||||||||||||||||||
| 178 | #pod =head4 BUILD | ||||||||||||||||||||||||||||||||
| 179 | #pod | ||||||||||||||||||||||||||||||||
| 180 | #pod =end pod_coverage | ||||||||||||||||||||||||||||||||
| 181 | #pod | ||||||||||||||||||||||||||||||||
| 182 | #pod =cut | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 105 | ▶ | sub BUILD { } | ||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | 105 | ▶ | before BUILD => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | 105 | my $self = shift; | |||||||||||||||||||||||||||||||
| 189 | 105 | $self->_set_addr( Net::Object::Peer::RefAddr->new( $self ) ); | |||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | 18 | }; | |||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | #pod =method new | ||||||||||||||||||||||||||||||||
| 195 | #pod | ||||||||||||||||||||||||||||||||
| 196 | #pod $obj = Net::Object::Peer->new( %args | \%args ); | ||||||||||||||||||||||||||||||||
| 197 | #pod | ||||||||||||||||||||||||||||||||
| 198 | #pod Construct a new object. The following arguments are available: | ||||||||||||||||||||||||||||||||
| 199 | #pod | ||||||||||||||||||||||||||||||||
| 200 | #pod =over | ||||||||||||||||||||||||||||||||
| 201 | #pod | ||||||||||||||||||||||||||||||||
| 202 | #pod =item event_handler_prefix => I<string> | ||||||||||||||||||||||||||||||||
| 203 | #pod | ||||||||||||||||||||||||||||||||
| 204 | #pod The string which prefixes default event handler method names. See L</event_handler_prefix> | ||||||||||||||||||||||||||||||||
| 205 | #pod | ||||||||||||||||||||||||||||||||
| 206 | #pod =item events => I<string> | I<arrayref> | ||||||||||||||||||||||||||||||||
| 207 | #pod | ||||||||||||||||||||||||||||||||
| 208 | #pod The name(s) of the event(s) this object will emit (don't include the | ||||||||||||||||||||||||||||||||
| 209 | #pod C<unsubscribed> and C<detach> events). May be a single string or an | ||||||||||||||||||||||||||||||||
| 210 | #pod arrayref. If not specified, the list of events will be initialized via the | ||||||||||||||||||||||||||||||||
| 211 | #pod L</default_events> class method. | ||||||||||||||||||||||||||||||||
| 212 | #pod | ||||||||||||||||||||||||||||||||
| 213 | #pod =back | ||||||||||||||||||||||||||||||||
| 214 | #pod | ||||||||||||||||||||||||||||||||
| 215 | #pod =cut | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | #pod =method default_event_handler_prefix | ||||||||||||||||||||||||||||||||
| 218 | #pod | ||||||||||||||||||||||||||||||||
| 219 | #pod This class method returns the prefix for the default event handler method names. | ||||||||||||||||||||||||||||||||
| 220 | #pod It defaults to returning C<_cb_>. | ||||||||||||||||||||||||||||||||
| 221 | #pod | ||||||||||||||||||||||||||||||||
| 222 | #pod =cut | ||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | 34 | ▶ | sub default_event_handler_prefix { '_cb_' } | ||||||||||||||||||||||||||||||
|
Subroutine
default_event_handler_prefix: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | #pod =method build_sub | ||||||||||||||||||||||||||||||||
| 227 | #pod | ||||||||||||||||||||||||||||||||
| 228 | #pod $coderef = $self->build_sub( $emitter, @tuple ); | ||||||||||||||||||||||||||||||||
| 229 | #pod | ||||||||||||||||||||||||||||||||
| 230 | #pod C<build_sub> is the method responsible for creating and | ||||||||||||||||||||||||||||||||
| 231 | #pod compiling the code for an event handler. It is invoked | ||||||||||||||||||||||||||||||||
| 232 | #pod from the L</subscribe()> method, with the following parameters | ||||||||||||||||||||||||||||||||
| 233 | #pod | ||||||||||||||||||||||||||||||||
| 234 | #pod =over | ||||||||||||||||||||||||||||||||
| 235 | #pod | ||||||||||||||||||||||||||||||||
| 236 | #pod =item C<$emitter> | ||||||||||||||||||||||||||||||||
| 237 | #pod | ||||||||||||||||||||||||||||||||
| 238 | #pod The emitter object. | ||||||||||||||||||||||||||||||||
| 239 | #pod | ||||||||||||||||||||||||||||||||
| 240 | #pod =item C<@tuple> | ||||||||||||||||||||||||||||||||
| 241 | #pod | ||||||||||||||||||||||||||||||||
| 242 | #pod the tuple passed to L</subscribe> for this event. | ||||||||||||||||||||||||||||||||
| 243 | #pod | ||||||||||||||||||||||||||||||||
| 244 | #pod =back | ||||||||||||||||||||||||||||||||
| 245 | #pod | ||||||||||||||||||||||||||||||||
| 246 | #pod The default implementation will return a L<Sub::Quote::quote_sub|Sub::Quote/quote_sub> | ||||||||||||||||||||||||||||||||
| 247 | #pod generated code reference for method calls and code specified as a | ||||||||||||||||||||||||||||||||
| 248 | #pod string. See L<Net::Object::Peer::Cookbook/Loop Detecton> for an | ||||||||||||||||||||||||||||||||
| 249 | #pod example of using this attribute to inline additional code in the event | ||||||||||||||||||||||||||||||||
| 250 | #pod handler. | ||||||||||||||||||||||||||||||||
| 251 | #pod | ||||||||||||||||||||||||||||||||
| 252 | #pod | ||||||||||||||||||||||||||||||||
| 253 | #pod | ||||||||||||||||||||||||||||||||
| 254 | #pod =cut | ||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | 59 | ▶ | sub build_sub { | ||||||||||||||||||||||||||||||
|
Subroutine
build_sub: called CC=6 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | 59 | my ( $self, $emitter, $name, $arg ) = @_; | |||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||
| 260 | 59 | return do { | |||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | 59 | ▶ | if ( !defined $arg ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | 53 | quote_subs( [ $self, $self->event_handler_prefix . $name ] ); | |||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||
| 267 | elsif ( 'HASH' eq ref $arg ) { | ||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | 2 | my %arg = %$arg; | |||||||||||||||||||||||||||||||
| 270 | 2 | delete $arg{name}; | |||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | 2 | ▶ | if ( defined $arg{method} ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | 2 | quote_subs( [ $self, delete $arg{method}, %arg ] ); | |||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||
| 277 | elsif ( defined $arg->{code} ) { | ||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||
| 279 | 0 | quote_subs( [ delete $arg{code}, %arg ] ); | |||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | else { | ||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | 0 | croak( __PACKAGE__ | |||||||||||||||||||||||||||||||
| 285 | . "::build_sub: can't figure out what to do with \%arg" ); | ||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | elsif ( 'CODE' eq ref $arg ) { | ||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | 4 | $arg; | |||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||
| 294 | else { | ||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||
| 296 | 0 | croak( __PACKAGE__ . "::build_sub: illegal value for \$arg" ); | |||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||
| 298 | }; | ||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | #pod =method subscribe | ||||||||||||||||||||||||||||||||
| 303 | #pod | ||||||||||||||||||||||||||||||||
| 304 | #pod $self->subscribe( $peer, @event_tuple [, @event_tuple, ... ] ); | ||||||||||||||||||||||||||||||||
| 305 | #pod | ||||||||||||||||||||||||||||||||
| 306 | #pod Subscribe to one or more events sent by C<$peer>, which must consume | ||||||||||||||||||||||||||||||||
| 307 | #pod the L<Net::Object::Peer> role. If C<$peer> additionally consumes the | ||||||||||||||||||||||||||||||||
| 308 | #pod L<Net::Object::Peer::Ephemeral> role, a strong reference to C<$peer> | ||||||||||||||||||||||||||||||||
| 309 | #pod is stored. (See L<Net::Object::Peer::Cookbook/Translation/Proxy Nodes>.) | ||||||||||||||||||||||||||||||||
| 310 | #pod An attempt to subscribe to an event the peer does not produce will throw | ||||||||||||||||||||||||||||||||
| 311 | #pod an exception. | ||||||||||||||||||||||||||||||||
| 312 | #pod | ||||||||||||||||||||||||||||||||
| 313 | #pod The event name and the action to be performed when the event is | ||||||||||||||||||||||||||||||||
| 314 | #pod emitted are specified by a tuple with the following forms: | ||||||||||||||||||||||||||||||||
| 315 | #pod | ||||||||||||||||||||||||||||||||
| 316 | #pod =over | ||||||||||||||||||||||||||||||||
| 317 | #pod | ||||||||||||||||||||||||||||||||
| 318 | #pod =item C<< $event_name >> | ||||||||||||||||||||||||||||||||
| 319 | #pod | ||||||||||||||||||||||||||||||||
| 320 | #pod the event handler will invoke the C<${prefix}${event_name}> method on C<$self>, | ||||||||||||||||||||||||||||||||
| 321 | #pod where C<$prefix> is the L<event_handler_prefix attribute|/event_handler_prefix>. | ||||||||||||||||||||||||||||||||
| 322 | #pod | ||||||||||||||||||||||||||||||||
| 323 | #pod =item C<< $event_name => { method => $method_name } >> | ||||||||||||||||||||||||||||||||
| 324 | #pod | ||||||||||||||||||||||||||||||||
| 325 | #pod The event handler will invoke the C<$method_name> method on C<$self>. | ||||||||||||||||||||||||||||||||
| 326 | #pod | ||||||||||||||||||||||||||||||||
| 327 | #pod =item C<< $event_name => CODEREF >> | ||||||||||||||||||||||||||||||||
| 328 | #pod | ||||||||||||||||||||||||||||||||
| 329 | #pod The passed code reference is called. | ||||||||||||||||||||||||||||||||
| 330 | #pod | ||||||||||||||||||||||||||||||||
| 331 | #pod =item C<< $event_name => { code => $code, capture => \%capture } >> | ||||||||||||||||||||||||||||||||
| 332 | #pod | ||||||||||||||||||||||||||||||||
| 333 | #pod C<$code> is a string containing code to be run by the event handler. | ||||||||||||||||||||||||||||||||
| 334 | #pod C<%capture> is a hash containing variable captures. See the | ||||||||||||||||||||||||||||||||
| 335 | #pod documentation for "\%captures" in L<Sub::Quote/quote_sub> for more | ||||||||||||||||||||||||||||||||
| 336 | #pod information. | ||||||||||||||||||||||||||||||||
| 337 | #pod | ||||||||||||||||||||||||||||||||
| 338 | #pod =back | ||||||||||||||||||||||||||||||||
| 339 | #pod | ||||||||||||||||||||||||||||||||
| 340 | #pod If C<$peer> provides a C<_notify_subscribed> method, that will be invoked as | ||||||||||||||||||||||||||||||||
| 341 | #pod | ||||||||||||||||||||||||||||||||
| 342 | #pod $peer->_notify_subscribed( $self, $event_name, ... ); | ||||||||||||||||||||||||||||||||
| 343 | #pod | ||||||||||||||||||||||||||||||||
| 344 | #pod for each subscription. | ||||||||||||||||||||||||||||||||
| 345 | #pod | ||||||||||||||||||||||||||||||||
| 346 | #pod =cut | ||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | 49 | ▶ | sub subscribe { | ||||||||||||||||||||||||||||||
|
Subroutine
subscribe: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||
| 350 | 49 | my $self = shift; | |||||||||||||||||||||||||||||||
| 351 | 49 | my $peer = shift; | |||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||
| 353 | 49 | my $subscriptions = $self->_subscriptions; | |||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||
| 355 | 49 | weaken $self; | |||||||||||||||||||||||||||||||
| 356 | 49 | weaken $peer; | |||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||
| 358 | 49 | my $notify_subscribed = $peer->can( '_notify_subscribed' ); | |||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||
| 360 | 49 | my $args = Data::OptList::mkopt( | |||||||||||||||||||||||||||||||
| 361 | \@_, | ||||||||||||||||||||||||||||||||
| 362 | { | ||||||||||||||||||||||||||||||||
| 363 | moniker => 'events', | ||||||||||||||||||||||||||||||||
| 364 | require_unique => 1, | ||||||||||||||||||||||||||||||||
| 365 | must_be => [ 'CODE', 'SCALAR', 'HASH' ], | ||||||||||||||||||||||||||||||||
| 366 | } ); | ||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||
| 368 | # don't register anything until we've parsed the input list of | ||||||||||||||||||||||||||||||||
| 369 | # event names and possible subs in order to make this as atomic as | ||||||||||||||||||||||||||||||||
| 370 | # possible. | ||||||||||||||||||||||||||||||||
| 371 | 49 | my @register; | |||||||||||||||||||||||||||||||
| 372 | 49 | for my $opt ( @$args ) { | |||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | 60 | my ( $name, $arg ) = @$opt; | |||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||
| 376 | 60 | ▶ | croak( "\$name must be a string\n" ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 377 | if ref $name; | ||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||
| 379 | 60 | ▶ | croak( qq[\$peer does not emit event "$name" ] ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 380 | unless $peer->emits_events( $name ); | ||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | 59 | push @register, [ $name, $self->build_sub( $peer, $name, $arg ) ]; | |||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||
| 385 | 47 | for my $event ( @register ) { | |||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||
| 388 | 58 | my ( $name, $sub ) = @$event; | |||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||
| 390 | 58 | $self->_subscriptions->remove( | |||||||||||||||||||||||||||||||
| 391 | name => $name, | ||||||||||||||||||||||||||||||||
| 392 | peer => $peer, | ||||||||||||||||||||||||||||||||
| 393 | ); | ||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | 58 | $self->_subscriptions->add( | |||||||||||||||||||||||||||||||
| 396 | name => $name, | ||||||||||||||||||||||||||||||||
| 397 | peer => $peer, | ||||||||||||||||||||||||||||||||
| 398 | addr => Net::Object::Peer::RefAddr->new( $peer ), | ||||||||||||||||||||||||||||||||
| 399 | unsubscribe => | ||||||||||||||||||||||||||||||||
| 400 | $peer->_emitter->subscribe( $name, $sub, peer => $self, ), | ||||||||||||||||||||||||||||||||
| 401 | ); | ||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||
| 405 | 47 | ▶ | $peer->$notify_subscribed( $self, map { $_->[0] } @register ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 406 | if $notify_subscribed; | ||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||
| 411 | #pod =method unsubscribe | ||||||||||||||||||||||||||||||||
| 412 | #pod | ||||||||||||||||||||||||||||||||
| 413 | #pod # Unsubscribe from all events from all peers. | ||||||||||||||||||||||||||||||||
| 414 | #pod $self->unsubscribe; | ||||||||||||||||||||||||||||||||
| 415 | #pod | ||||||||||||||||||||||||||||||||
| 416 | #pod # Unsubscribe from all events emitted by a peer | ||||||||||||||||||||||||||||||||
| 417 | #pod $self->unsubscribe( $peer ); | ||||||||||||||||||||||||||||||||
| 418 | #pod | ||||||||||||||||||||||||||||||||
| 419 | #pod # Unsubscribe from one or more events emitted by a peer | ||||||||||||||||||||||||||||||||
| 420 | #pod $self->unsubscribe( $peer, $event_name [, $event_name [, ... ]); | ||||||||||||||||||||||||||||||||
| 421 | #pod | ||||||||||||||||||||||||||||||||
| 422 | #pod # Unsubscribe from the peer and event specified by the passed | ||||||||||||||||||||||||||||||||
| 423 | #pod # Net::Object::Peer::Event object | ||||||||||||||||||||||||||||||||
| 424 | #pod $self->unsubscribe( $event_object ); | ||||||||||||||||||||||||||||||||
| 425 | #pod | ||||||||||||||||||||||||||||||||
| 426 | #pod # Unsubscribe from one or more events emitted by all peers | ||||||||||||||||||||||||||||||||
| 427 | #pod $self->unsubscribe( $event_name [, $event_name [, ... ] ] ) | ||||||||||||||||||||||||||||||||
| 428 | #pod | ||||||||||||||||||||||||||||||||
| 429 | #pod Unsubscribe from events/peers. After unsubscription, an I<unsubscribed> | ||||||||||||||||||||||||||||||||
| 430 | #pod event with a L<Net::Object::Peer::UnsubscribeEvent> as a payload will | ||||||||||||||||||||||||||||||||
| 431 | #pod be sent to affected peers who have subscribed to the unsubscribed event(s). | ||||||||||||||||||||||||||||||||
| 432 | #pod | ||||||||||||||||||||||||||||||||
| 433 | #pod C<$peer> may be either a L<Net::Object::Peer> or a | ||||||||||||||||||||||||||||||||
| 434 | #pod L<Net::Object::Peer::RefAddr> object. | ||||||||||||||||||||||||||||||||
| 435 | #pod | ||||||||||||||||||||||||||||||||
| 436 | #pod Note that B<Net::Object::Peer::Event> objects which are passed to | ||||||||||||||||||||||||||||||||
| 437 | #pod event handlers may have a masqueraded C<emitter> attribute. Attempting | ||||||||||||||||||||||||||||||||
| 438 | #pod to unsubscribe from that C<emitter> is unwise. Instead, pass either | ||||||||||||||||||||||||||||||||
| 439 | #pod the event object or the C<addr> field in that object, which is guaranteed | ||||||||||||||||||||||||||||||||
| 440 | #pod to identify the actual emitter subscribed to. | ||||||||||||||||||||||||||||||||
| 441 | #pod | ||||||||||||||||||||||||||||||||
| 442 | #pod =cut | ||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||
| 444 | 118 | ▶ | sub unsubscribe { | ||||||||||||||||||||||||||||||
|
Subroutine
unsubscribe: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||
| 446 | 118 | my $self = shift; | |||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||
| 448 | 118 | ▶ | return $self->_unsubscribe_all | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 449 | unless @_; | ||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||
| 451 | 11 | ▶ | if ( $_[0]->$_does( __PACKAGE__ ) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$_[0]->$Net::Object::Peer::_does(__PACKAGE__) or $_[0]->$Net::Object::Peer::_isa("Net::Object::Peer::RefAddr")
MC/DC100%2/2
$_[0]->$Net::Object::Peer::_does(__PACKAGE__) or $_[0]->$Net::Object::Peer::_isa("Net::Object::Peer::RefAddr")
$_[0]->$Net::Object::Peer::_does(__PACKAGE__)
$_[0]->$Net::Object::Peer::_isa("Net::Object::Peer::RefAddr")
Truth table100%3/3
A or B
A= $_[0]->$Net::Object::Peer::_does(__PACKAGE__)B= $_[0]->$Net::Object::Peer::_isa("Net::Object::Peer::RefAddr")
| |||||||||||||||||||||||||||||||||
| 452 | || $_[0]->$_isa( 'Net::Object::Peer::RefAddr' ) ) | ||||||||||||||||||||||||||||||||
| 453 | { | ||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||
| 455 | # $peer, $name, ... | ||||||||||||||||||||||||||||||||
| 456 | 7 | ▶ | return $self->_unsubscribe_from_peer_events( @_ ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 457 | if @_ > 1; | ||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||
| 459 | # $peer | ||||||||||||||||||||||||||||||||
| 460 | 3 | return $self->_unsubscribe_from_peer( @_ ); | |||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||
| 462 | elsif ( $_[0]->$_isa( 'Net::Object::Peer::Event' ) ) { | ||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||
| 464 | 1 | $self->_unsubscribe_from_peer_events( $_[0]->addr, $_[0]->name ); | |||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||
| 468 | # $name, ... | ||||||||||||||||||||||||||||||||
| 469 | 4 | return $self->_unsubscribe_from_events( @_ ); | |||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||
| 473 | 107 | ▶ | sub _unsubscribe_all { | ||||||||||||||||||||||||||||||
|
Subroutine
_unsubscribe_all: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||
| 475 | 107 | my $self = shift; | |||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||
| 477 | 107 | ▶ | $self->_subscriptions->remove | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 478 | if defined $self->_subscriptions; | ||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||
| 480 | # signal peers that unsubscribe has happened. | ||||||||||||||||||||||||||||||||
| 481 | 107 | $self->emit( UNSUBSCRIBED, class => UnsubscribeEvent ); | |||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||
| 483 | 107 | return; | |||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||
| 486 | 8 | ▶ | sub _find_peer_spec { | ||||||||||||||||||||||||||||||
|
Subroutine
_find_peer_spec: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||
| 488 | 8 | my $peer = shift; | |||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||
| 490 | return ( | ||||||||||||||||||||||||||||||||
| 491 | 8 | ▶ | $peer->$_does( __PACKAGE__ ) ? ( peer => $peer ) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 492 | : $peer->isa( 'Net::Object::Peer::RefAddr' ) ? ( addr => $peer ) | ||||||||||||||||||||||||||||||||
| 493 | : croak( "can't grok \$peer: $peer\n" ), | ||||||||||||||||||||||||||||||||
| 494 | ); | ||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||
| 498 | 5 | ▶ | sub _unsubscribe_from_peer_events { | ||||||||||||||||||||||||||||||
|
Subroutine
_unsubscribe_from_peer_events: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||
| 500 | 5 | my ( $self, $peer ) = ( shift, shift ); | |||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||
| 502 | 5 | my %spec = _find_peer_spec( $peer ); | |||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||
| 504 | my @unsubs = map { | ||||||||||||||||||||||||||||||||
| 505 | 5 | my $name = $_; | |||||||||||||||||||||||||||||||
| 506 | 6 | map +{ name => $name, subs => $_ }, | |||||||||||||||||||||||||||||||
| 507 | $self->_subscriptions->remove( %spec, name => $name ) | ||||||||||||||||||||||||||||||||
| 508 | } @_; | ||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||
| 510 | # if passed a refaddr, extract peer object from deleted subscriptions | ||||||||||||||||||||||||||||||||
| 511 | 5 | ▶ | if ( defined $spec{addr} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||
| 513 | # check for subs where peer is still alive | ||||||||||||||||||||||||||||||||
| 514 | 2 | ▶ | my $sub = first { defined $_->{peer} } map { $_->{subs} } @unsubs; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||
| 516 | 2 | ▶ | return unless defined $sub; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||
| 518 | 2 | $peer = $sub->{peer}; | |||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||
| 521 | 5 | ▶ | if ( @unsubs ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||
| 523 | $self->send( | ||||||||||||||||||||||||||||||||
| 524 | $peer, UNSUBSCRIBED, | ||||||||||||||||||||||||||||||||
| 525 | class => UnsubscribeEvent, | ||||||||||||||||||||||||||||||||
| 526 | 5 | event_names => [ uniqstr map { $_->{name} } @unsubs ] ); | |||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||
| 529 | 5 | return; | |||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||
| 533 | 3 | ▶ | sub _unsubscribe_from_peer { | ||||||||||||||||||||||||||||||
|
Subroutine
_unsubscribe_from_peer: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||
| 535 | 3 | my ( $self, $peer ) = @_; | |||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||
| 537 | # say $self->name, ":\tunsubscribing from ", $peer->name; | ||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||
| 539 | 3 | my %spec = _find_peer_spec( $peer ); | |||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||
| 541 | 3 | my @unsubs = $self->_subscriptions->remove( %spec ); | |||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||
| 543 | # if passed a refaddr, extract peer object from deleted subscriptions | ||||||||||||||||||||||||||||||||
| 544 | 3 | ▶ | if ( defined $spec{addr} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||
| 546 | # check for subs where peer is still alive | ||||||||||||||||||||||||||||||||
| 547 | 1 | ▶ | my $sub = first { defined $_->{peer} } @unsubs; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||
| 549 | 1 | ▶ | return unless defined $sub; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||
| 551 | 1 | $peer = $sub->{peer}; | |||||||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||
| 554 | 3 | $self->send( $peer, UNSUBSCRIBED, class => UnsubscribeEvent ); | |||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||
| 556 | 3 | return; | |||||||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||
| 559 | 4 | ▶ | sub _unsubscribe_from_events { | ||||||||||||||||||||||||||||||
|
Subroutine
_unsubscribe_from_events: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||
| 561 | 4 | my ( $self, @names ) = @_; | |||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||
| 563 | 4 | ▶ | return unless @names; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||
| 565 | 4 | my %subs; | |||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||
| 567 | my @subs = $self->_subscriptions->remove( | ||||||||||||||||||||||||||||||||
| 568 | 13 | ▶ | sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 569 | 13 | grep { $_[0]->name eq $_ } @names; | |||||||||||||||||||||||||||||||
| 570 | 4 | } ); | |||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||
| 572 | 4 | for my $sub ( grep { defined $_->{peer} } @subs ) { | |||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||
| 574 | 4 | ▶ | my $list = $subs{ 0 + $sub->{addr} } ||= [ $sub->{peer} ]; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$subs{0 + $sub->{"addr"}} ||= [$sub->{"peer"}]
MC/DC0%0/1
$subs{0 + $sub->{"addr"}} ||= [$sub->{"peer"}]
$subs{0 + $sub->{"addr"}}
Truth table50%1/2
A
A= $subs{0 + $sub->{"addr"}}
| |||||||||||||||||||||||||||||||||
| 575 | 4 | push @$list, $sub->{name}; | |||||||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||
| 578 | 4 | for my $sub ( values %subs ) { | |||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||
| 580 | 4 | my ( $peer, @event_names ) = @$sub; | |||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||
| 582 | 4 | $self->send( | |||||||||||||||||||||||||||||||
| 583 | $peer, | ||||||||||||||||||||||||||||||||
| 584 | UNSUBSCRIBED, | ||||||||||||||||||||||||||||||||
| 585 | class => UnsubscribeEvent, | ||||||||||||||||||||||||||||||||
| 586 | event_names => \@event_names, | ||||||||||||||||||||||||||||||||
| 587 | ); | ||||||||||||||||||||||||||||||||
| 588 | } | ||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||
| 591 | 4 | return; | |||||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||
| 594 | #pod =method events | ||||||||||||||||||||||||||||||||
| 595 | #pod | ||||||||||||||||||||||||||||||||
| 596 | #pod @events = $obj->events; | ||||||||||||||||||||||||||||||||
| 597 | #pod $obj->events( \@event_names | $event_name ); | ||||||||||||||||||||||||||||||||
| 598 | #pod | ||||||||||||||||||||||||||||||||
| 599 | #pod As a getter, returns a list of event names which the object may emit. | ||||||||||||||||||||||||||||||||
| 600 | #pod | ||||||||||||||||||||||||||||||||
| 601 | #pod As a setter, accepts either an arrayref or a single event name. Event | ||||||||||||||||||||||||||||||||
| 602 | #pod names must a valid Perl identifier (e.g., no C<:> or C<-> characters). | ||||||||||||||||||||||||||||||||
| 603 | #pod | ||||||||||||||||||||||||||||||||
| 604 | #pod =cut | ||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||
| 606 | 11 | ▶ | sub events { | ||||||||||||||||||||||||||||||
|
Subroutine
events: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||
| 608 | 11 | my $self = shift; | |||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||
| 610 | 11 | ▶ | if ( @_ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||
| 612 | 2 | $self->_set__events_arr( @_ ); | |||||||||||||||||||||||||||||||
| 613 | 2 | return; | |||||||||||||||||||||||||||||||
| 614 | } | ||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||
| 616 | 9 | return @{ $self->_events_arr }; | |||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||
| 620 | #pod =method detach | ||||||||||||||||||||||||||||||||
| 621 | #pod | ||||||||||||||||||||||||||||||||
| 622 | #pod $self->detach; | ||||||||||||||||||||||||||||||||
| 623 | #pod | ||||||||||||||||||||||||||||||||
| 624 | #pod Detach the object from the network. It will | ||||||||||||||||||||||||||||||||
| 625 | #pod | ||||||||||||||||||||||||||||||||
| 626 | #pod =over | ||||||||||||||||||||||||||||||||
| 627 | #pod | ||||||||||||||||||||||||||||||||
| 628 | #pod =item 1 | ||||||||||||||||||||||||||||||||
| 629 | #pod | ||||||||||||||||||||||||||||||||
| 630 | #pod Unsubscribe from all events from all peers. | ||||||||||||||||||||||||||||||||
| 631 | #pod | ||||||||||||||||||||||||||||||||
| 632 | #pod =item 2 | ||||||||||||||||||||||||||||||||
| 633 | #pod | ||||||||||||||||||||||||||||||||
| 634 | #pod Emit an C<unsubscribed> event with a L<Net::Object::Peer::UnsubscribeEvent> as a payload. | ||||||||||||||||||||||||||||||||
| 635 | #pod | ||||||||||||||||||||||||||||||||
| 636 | #pod =item 3 | ||||||||||||||||||||||||||||||||
| 637 | #pod | ||||||||||||||||||||||||||||||||
| 638 | #pod Emit a C<detach> event. | ||||||||||||||||||||||||||||||||
| 639 | #pod | ||||||||||||||||||||||||||||||||
| 640 | #pod =back | ||||||||||||||||||||||||||||||||
| 641 | #pod | ||||||||||||||||||||||||||||||||
| 642 | #pod =cut | ||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||
| 644 | 105 | ▶ | sub detach { | ||||||||||||||||||||||||||||||
|
Subroutine
detach: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 645 | 105 | my $self = shift; | |||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||
| 647 | 105 | $self->unsubscribe; | |||||||||||||||||||||||||||||||
| 648 | 105 | $self->emit( 'detach' ); | |||||||||||||||||||||||||||||||
| 649 | } | ||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||
| 652 | #pod =method subscriptions | ||||||||||||||||||||||||||||||||
| 653 | #pod | ||||||||||||||||||||||||||||||||
| 654 | #pod # return all subscriptions | ||||||||||||||||||||||||||||||||
| 655 | #pod my @subscriptions = $self->subscriptions; | ||||||||||||||||||||||||||||||||
| 656 | #pod | ||||||||||||||||||||||||||||||||
| 657 | #pod # return matching subscriptions | ||||||||||||||||||||||||||||||||
| 658 | #pod my @subscriptions = $self->subscriptions( $coderef | %spec ); | ||||||||||||||||||||||||||||||||
| 659 | #pod | ||||||||||||||||||||||||||||||||
| 660 | #pod Returns the events to which C<$self> is subscribed as a list of | ||||||||||||||||||||||||||||||||
| 661 | #pod hashrefs (see L<Net::Object::Peer::Subscription::as_hashref>). If | ||||||||||||||||||||||||||||||||
| 662 | #pod arguments are specified, only those which match are returned; see | ||||||||||||||||||||||||||||||||
| 663 | #pod L<Net::Object::Peer::Subscrition/find>; | ||||||||||||||||||||||||||||||||
| 664 | #pod | ||||||||||||||||||||||||||||||||
| 665 | #pod | ||||||||||||||||||||||||||||||||
| 666 | #pod =cut | ||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||
| 668 | 3 | ▶ | sub subscriptions { | ||||||||||||||||||||||||||||||
|
Subroutine
subscriptions: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||
| 670 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||
| 672 | 3 | ▶ | return @_ ? $self->_subscriptions->find( @_ ) : $self->_subscriptions->list; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||
| 675 | #pod =method emit | ||||||||||||||||||||||||||||||||
| 676 | #pod | ||||||||||||||||||||||||||||||||
| 677 | #pod $self->emit( $event_name, %args ); | ||||||||||||||||||||||||||||||||
| 678 | #pod | ||||||||||||||||||||||||||||||||
| 679 | #pod Broadcast the named event to all subscribed peers. C<%args> contains | ||||||||||||||||||||||||||||||||
| 680 | #pod arguments to be passed the the payload class constructor. The default | ||||||||||||||||||||||||||||||||
| 681 | #pod payload class is a L<Net::Object::Peer::Event> object; use the | ||||||||||||||||||||||||||||||||
| 682 | #pod C<class> key to specify an alternate class, which must be derived from | ||||||||||||||||||||||||||||||||
| 683 | #pod B<Net::Object::Peer::Event>. An attempt to emit an event which is not | ||||||||||||||||||||||||||||||||
| 684 | #pod supported by the emitter will cause an exception to be thrown. | ||||||||||||||||||||||||||||||||
| 685 | #pod | ||||||||||||||||||||||||||||||||
| 686 | #pod =cut | ||||||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||||||
| 688 | 261 | ▶ | sub emit { | ||||||||||||||||||||||||||||||
|
Subroutine
emit: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||||||
| 690 | 261 | my ( $self, $name ) = ( shift, shift ); | |||||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||||||
| 692 | 261 | ▶ | croak( q[\$self does not emit event "$name"\n] ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 693 | unless $self->emits_events( $name ); | ||||||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||||||
| 695 | 260 | $self->_emitter->emit( | |||||||||||||||||||||||||||||||
| 696 | $name, | ||||||||||||||||||||||||||||||||
| 697 | class => 'Net::Object::Peer::Event', | ||||||||||||||||||||||||||||||||
| 698 | emitter => $self, | ||||||||||||||||||||||||||||||||
| 699 | @_, | ||||||||||||||||||||||||||||||||
| 700 | ); | ||||||||||||||||||||||||||||||||
| 701 | } | ||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||
| 703 | #pod =method send | ||||||||||||||||||||||||||||||||
| 704 | #pod | ||||||||||||||||||||||||||||||||
| 705 | #pod $self->send( $peer, $event_name, %args ); | ||||||||||||||||||||||||||||||||
| 706 | #pod | ||||||||||||||||||||||||||||||||
| 707 | #pod This is similar to the L</emit> method, but only sends the event to the | ||||||||||||||||||||||||||||||||
| 708 | #pod specified peer. An attempt to emit an event which is not | ||||||||||||||||||||||||||||||||
| 709 | #pod supported by the emitter will cause an exception to be thrown. | ||||||||||||||||||||||||||||||||
| 710 | #pod | ||||||||||||||||||||||||||||||||
| 711 | #pod =cut | ||||||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||||||
| 713 | 15 | ▶ | sub send { | ||||||||||||||||||||||||||||||
|
Subroutine
send: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||
| 715 | 15 | my ( $self, $peer, $name ) = ( shift, shift, shift ); | |||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||
| 717 | 15 | ▶ | croak( q[\$self does not emit event "$name"\n] ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 718 | unless $self->emits_events( $name ); | ||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||
| 720 | 14 | $self->_emitter->send( | |||||||||||||||||||||||||||||||
| 721 | $peer, | ||||||||||||||||||||||||||||||||
| 722 | $name, | ||||||||||||||||||||||||||||||||
| 723 | class => 'Net::Object::Peer::Event', | ||||||||||||||||||||||||||||||||
| 724 | emitter => $self, | ||||||||||||||||||||||||||||||||
| 725 | @_, | ||||||||||||||||||||||||||||||||
| 726 | ); | ||||||||||||||||||||||||||||||||
| 727 | } | ||||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||
| 730 | #pod =method emit_args | ||||||||||||||||||||||||||||||||
| 731 | #pod | ||||||||||||||||||||||||||||||||
| 732 | #pod $self->emit_args( $event_name, @args ); | ||||||||||||||||||||||||||||||||
| 733 | #pod | ||||||||||||||||||||||||||||||||
| 734 | #pod Broadcast the named event to all subscribed peers. C<@args> will be | ||||||||||||||||||||||||||||||||
| 735 | #pod passed directly to each subscriber's callback. An attempt to emit an event which is not | ||||||||||||||||||||||||||||||||
| 736 | #pod supported by the emitter will cause an exception to be thrown. | ||||||||||||||||||||||||||||||||
| 737 | #pod | ||||||||||||||||||||||||||||||||
| 738 | #pod =cut | ||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||
| 740 | 18 | ▶ | before emit_args => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||||||
| 742 | 18 | my ( $self, $name ) = @_; | |||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||
| 744 | 18 | ▶ | croak( q[\$self does not emit event "$name"\n] ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 745 | unless $self->emits_events( $name ); | ||||||||||||||||||||||||||||||||
| 746 | 18 | }; | |||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||
| 748 | #pod =method send_args | ||||||||||||||||||||||||||||||||
| 749 | #pod | ||||||||||||||||||||||||||||||||
| 750 | #pod $self->send_args( $peer, $event_name, @args ); | ||||||||||||||||||||||||||||||||
| 751 | #pod | ||||||||||||||||||||||||||||||||
| 752 | #pod This is similar to the L</emit_args> method, but only sends the event to the | ||||||||||||||||||||||||||||||||
| 753 | #pod specified peer. An attempt to emit an event which is not | ||||||||||||||||||||||||||||||||
| 754 | #pod supported by the emitter will cause an exception to be thrown. | ||||||||||||||||||||||||||||||||
| 755 | #pod | ||||||||||||||||||||||||||||||||
| 756 | #pod =cut | ||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||
| 758 | 2 | ▶ | before send_args => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||||||
| 760 | 2 | my ( $self, $peer, $name ) = @_; | |||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||
| 762 | 2 | ▶ | croak( q[\$self does not emit event "$name"\n] ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 763 | unless $self->emits_events( $name ); | ||||||||||||||||||||||||||||||||
| 764 | 18 | }; | |||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||
| 766 | #pod =begin pod_coverage | ||||||||||||||||||||||||||||||||
| 767 | #pod | ||||||||||||||||||||||||||||||||
| 768 | #pod =head3 DEMOLISH | ||||||||||||||||||||||||||||||||
| 769 | #pod | ||||||||||||||||||||||||||||||||
| 770 | #pod =end pod_coverage | ||||||||||||||||||||||||||||||||
| 771 | #pod | ||||||||||||||||||||||||||||||||
| 772 | #pod =cut | ||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||
| 774 | 96 | ▶ | sub DEMOLISH { } | ||||||||||||||||||||||||||||||
|
Subroutine
DEMOLISH: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||||||
| 776 | 102 | ▶ | around DEMOLISH => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||||||
| 778 | 102 | my $orig = shift; | |||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||
| 780 | 102 | my ( $self, $in_global_destruction ) = @_; | |||||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||||||
| 782 | 102 | ▶ | $self->detach | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 783 | unless $in_global_destruction; | ||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||
| 785 | 102 | &$orig; | |||||||||||||||||||||||||||||||
| 786 | 18 | }; | |||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||||||
| 789 | 18 | 1; | |||||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||||||
| 791 | # | ||||||||||||||||||||||||||||||||
| 792 | # This file is part of Net-Object-Peer | ||||||||||||||||||||||||||||||||
| 793 | # | ||||||||||||||||||||||||||||||||
| 794 | # This software is Copyright (c) 2016 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||||||||||
| 795 | # | ||||||||||||||||||||||||||||||||
| 796 | # This is free software, licensed under: | ||||||||||||||||||||||||||||||||
| 797 | # | ||||||||||||||||||||||||||||||||
| 798 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||||||
| 799 | # | ||||||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||||||
| 801 | =pod | ||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||
| 803 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||
| 805 | Net::Object::Peer - Peer-to-Peer Publish/Subscribe Network of Objects | ||||||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||||||
| 807 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||||||
| 809 | version 0.06 | ||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||
| 811 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||||||
| 813 | use 5.10.0; | ||||||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||||||
| 815 | package Node; | ||||||||||||||||||||||||||||||||
| 816 | use Moo; | ||||||||||||||||||||||||||||||||
| 817 | with 'Net::Object::Peer'; | ||||||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||||||
| 819 | has name => is => ( 'ro', required => 1 ); | ||||||||||||||||||||||||||||||||
| 820 | |||||||||||||||||||||||||||||||||
| 821 | sub default_events { qw[ hey ] } | ||||||||||||||||||||||||||||||||
| 822 | |||||||||||||||||||||||||||||||||
| 823 | sub _notify_subscribed { | ||||||||||||||||||||||||||||||||
| 824 | my ( $self, $peer, $name ) = @_; | ||||||||||||||||||||||||||||||||
| 825 | say $self->name, ":\t@{[ $peer->name ]} subscribed to event $name"; | ||||||||||||||||||||||||||||||||
| 826 | } | ||||||||||||||||||||||||||||||||
| 827 | |||||||||||||||||||||||||||||||||
| 828 | sub _cb_hey { | ||||||||||||||||||||||||||||||||
| 829 | my ( $self, $event ) = @_; | ||||||||||||||||||||||||||||||||
| 830 | say $self->name, | ||||||||||||||||||||||||||||||||
| 831 | ":\t@{[ $event->emitter->name ]} sent @{[$event->name]}"; | ||||||||||||||||||||||||||||||||
| 832 | } | ||||||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||||||
| 834 | sub _cb_unsubscribed { | ||||||||||||||||||||||||||||||||
| 835 | my ( $self, $event ) = @_; | ||||||||||||||||||||||||||||||||
| 836 | say $self->name, ":\t@{[ $event->emitter->name ]} unsubscribed"; | ||||||||||||||||||||||||||||||||
| 837 | } | ||||||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||||||
| 839 | package main; | ||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||||||
| 842 | my $n1 = Node->new( name => 'N1' ); | ||||||||||||||||||||||||||||||||
| 843 | my $n2 = Node->new( name => 'N2' ); | ||||||||||||||||||||||||||||||||
| 844 | my $n3 = Node->new( name => 'N3' ); | ||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||
| 846 | # Net::Object::Peer provides an "unsubscribed" event; $n1 will be | ||||||||||||||||||||||||||||||||
| 847 | # notified when $n2 unsubscribes from it | ||||||||||||||||||||||||||||||||
| 848 | $n1->subscribe( $n2, 'unsubscribed' ); | ||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||
| 850 | # $n2 and $n3 will be notified when $n1 sends a "hey" event, and $n1 | ||||||||||||||||||||||||||||||||
| 851 | # will be notified that they have subscribed | ||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||
| 853 | $n2->subscribe( $n1, 'hey' ); | ||||||||||||||||||||||||||||||||
| 854 | $n3->subscribe( $n1, 'hey' ); | ||||||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||||||
| 856 | # both $n2 and $n3 will get notified | ||||||||||||||||||||||||||||||||
| 857 | $n1->emit( 'hey' ); | ||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||
| 859 | # only $n2 gets notified | ||||||||||||||||||||||||||||||||
| 860 | $n1->send( $n2, 'hey' ); | ||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||
| 862 | # destroy $n2; $n2 will unsubscribe from all of its events and $n1 | ||||||||||||||||||||||||||||||||
| 863 | # will be notified that $n2 has unsubscribed from it. | ||||||||||||||||||||||||||||||||
| 864 | undef $n2; | ||||||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||||||
| 866 | Resulting in: | ||||||||||||||||||||||||||||||||
| 867 | |||||||||||||||||||||||||||||||||
| 868 | N2: N1 subscribed to event unsubscribed | ||||||||||||||||||||||||||||||||
| 869 | N1: N2 subscribed to event hey | ||||||||||||||||||||||||||||||||
| 870 | N1: N3 subscribed to event hey | ||||||||||||||||||||||||||||||||
| 871 | N2: N1 sent hey | ||||||||||||||||||||||||||||||||
| 872 | N3: N1 sent hey | ||||||||||||||||||||||||||||||||
| 873 | N2: N1 sent hey | ||||||||||||||||||||||||||||||||
| 874 | N1: N2 unsubscribed | ||||||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||||||
| 877 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||||||
| 879 | B<Net::Object::Peer> is a L<Moo> L<< Role|Moo::Role >> which | ||||||||||||||||||||||||||||||||
| 880 | implements a publish/subscribe peer-to-peer messaging system, based | ||||||||||||||||||||||||||||||||
| 881 | upon L<Beam::Emitter>. Objects in the network may broadcast events | ||||||||||||||||||||||||||||||||
| 882 | to all subscribers or may send events to a particular subscriber. | ||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||
| 884 | Subscriptions and unsubscriptions are tracked and messages will be | ||||||||||||||||||||||||||||||||
| 885 | sent to affected objects upon request. | ||||||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||||||
| 887 | While B<Net::Object::Peer> is designed around the concept of nodes | ||||||||||||||||||||||||||||||||
| 888 | being objects with methods as event handlers, it retains | ||||||||||||||||||||||||||||||||
| 889 | L<Beam::Emitter>'s ability to register code references as well. | ||||||||||||||||||||||||||||||||
| 890 | |||||||||||||||||||||||||||||||||
| 891 | L<Net::Object::Peer::Cookbook> provides some recipes. | ||||||||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||||||
| 893 | =head2 Usage | ||||||||||||||||||||||||||||||||
| 894 | |||||||||||||||||||||||||||||||||
| 895 | As B<Net::Object::Peer> is purely peer based with no common message | ||||||||||||||||||||||||||||||||
| 896 | bus, a network is built up by creating a set of network nodes and | ||||||||||||||||||||||||||||||||
| 897 | linking them via subscriptions. | ||||||||||||||||||||||||||||||||
| 898 | |||||||||||||||||||||||||||||||||
| 899 | my $n1 = Node->new( name => 'N1' ); | ||||||||||||||||||||||||||||||||
| 900 | my $n2 = Node->new( name => 'N2' ); | ||||||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||||||
| 902 | $n1->subscribe( $n2, 'changed' ); | ||||||||||||||||||||||||||||||||
| 903 | |||||||||||||||||||||||||||||||||
| 904 | Here C<$n1> I<subscribes to> C<$n2>'s C<changed> event. By default, | ||||||||||||||||||||||||||||||||
| 905 | C<$n1>'s C<_cb_changed> method is invoked when C<$n2> emits a | ||||||||||||||||||||||||||||||||
| 906 | C<changed> event. | ||||||||||||||||||||||||||||||||
| 907 | |||||||||||||||||||||||||||||||||
| 908 | =head2 Events | ||||||||||||||||||||||||||||||||
| 909 | |||||||||||||||||||||||||||||||||
| 910 | An emitter must register the events that it will emit. Here's how | ||||||||||||||||||||||||||||||||
| 911 | |||||||||||||||||||||||||||||||||
| 912 | =over | ||||||||||||||||||||||||||||||||
| 913 | |||||||||||||||||||||||||||||||||
| 914 | =item 1 | ||||||||||||||||||||||||||||||||
| 915 | |||||||||||||||||||||||||||||||||
| 916 | Class defaults | ||||||||||||||||||||||||||||||||
| 917 | |||||||||||||||||||||||||||||||||
| 918 | A default set of events for the class may be specified by defining | ||||||||||||||||||||||||||||||||
| 919 | the L</default_events> class method, which should return a list | ||||||||||||||||||||||||||||||||
| 920 | of event names: | ||||||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||||||
| 922 | sub default_events{ qw[ evt1 evt2 evt3 ] } | ||||||||||||||||||||||||||||||||
| 923 | |||||||||||||||||||||||||||||||||
| 924 | =item 2 | ||||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||||
| 926 | Object defaults | ||||||||||||||||||||||||||||||||
| 927 | |||||||||||||||||||||||||||||||||
| 928 | During object construction, the L<events|/new> attribute may be used | ||||||||||||||||||||||||||||||||
| 929 | to specify a list of events. | ||||||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||||||
| 931 | =item 3 | ||||||||||||||||||||||||||||||||
| 932 | |||||||||||||||||||||||||||||||||
| 933 | Runtime manipulation | ||||||||||||||||||||||||||||||||
| 934 | |||||||||||||||||||||||||||||||||
| 935 | The L</events> object method may be used to overwrite the list | ||||||||||||||||||||||||||||||||
| 936 | of events. | ||||||||||||||||||||||||||||||||
| 937 | |||||||||||||||||||||||||||||||||
| 938 | =back | ||||||||||||||||||||||||||||||||
| 939 | |||||||||||||||||||||||||||||||||
| 940 | When a subscriber recieves an event, its registered handler for that | ||||||||||||||||||||||||||||||||
| 941 | event type is invoked. If the object creating the event used the | ||||||||||||||||||||||||||||||||
| 942 | L</emit> or L</send> methods, | ||||||||||||||||||||||||||||||||
| 943 | |||||||||||||||||||||||||||||||||
| 944 | $emitter->emit( $event_name ); | ||||||||||||||||||||||||||||||||
| 945 | |||||||||||||||||||||||||||||||||
| 946 | the event handler will be invoked as | ||||||||||||||||||||||||||||||||
| 947 | |||||||||||||||||||||||||||||||||
| 948 | $subscriber->method( $event ); | ||||||||||||||||||||||||||||||||
| 949 | |||||||||||||||||||||||||||||||||
| 950 | where C<$event> is an object derived from the L<Net::Object::Peer::Event> class. | ||||||||||||||||||||||||||||||||
| 951 | (This assumes that the handler is a method; it may be a simple callback). | ||||||||||||||||||||||||||||||||
| 952 | |||||||||||||||||||||||||||||||||
| 953 | If the event was created with the L</emit_args> or L</send_args> methods, | ||||||||||||||||||||||||||||||||
| 954 | |||||||||||||||||||||||||||||||||
| 955 | $emitter->emit_args( $event_name, @arguments ); | ||||||||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||||||
| 957 | the event handler will invoked as | ||||||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||||||
| 959 | $subscriber->method( @arguments ); | ||||||||||||||||||||||||||||||||
| 960 | |||||||||||||||||||||||||||||||||
| 961 | =head3 Subscription and Unsubscription Events | ||||||||||||||||||||||||||||||||
| 962 | |||||||||||||||||||||||||||||||||
| 963 | When a subscriber registers one or more event handlers with an emitter | ||||||||||||||||||||||||||||||||
| 964 | via the subscriber's L</subscribe> method, the emitter's | ||||||||||||||||||||||||||||||||
| 965 | C<_notify_subscribed> method will be invoked (if it exists) as | ||||||||||||||||||||||||||||||||
| 966 | |||||||||||||||||||||||||||||||||
| 967 | $emitter->_notify_subscribed( $subscriber, @event_names ); | ||||||||||||||||||||||||||||||||
| 968 | |||||||||||||||||||||||||||||||||
| 969 | If the subscription already exists, it will be unsubscribed and | ||||||||||||||||||||||||||||||||
| 970 | then resubscribed. | ||||||||||||||||||||||||||||||||
| 971 | |||||||||||||||||||||||||||||||||
| 972 | After a subscriber de-registers a handler, either explicitly via | ||||||||||||||||||||||||||||||||
| 973 | L</unsubscribe> or when the object is destroyed, it will L</emit> an | ||||||||||||||||||||||||||||||||
| 974 | C<unsubscribed> event with a L<Net::Object::Peer::UnsubscribeEvent> | ||||||||||||||||||||||||||||||||
| 975 | object as a payload. | ||||||||||||||||||||||||||||||||
| 976 | |||||||||||||||||||||||||||||||||
| 977 | While emitters are not automatically subscribed to C<unsubscribed> | ||||||||||||||||||||||||||||||||
| 978 | events, this is easily accomplished by adding code to the emitters' | ||||||||||||||||||||||||||||||||
| 979 | C<_notify_subscribed> method. | ||||||||||||||||||||||||||||||||
| 980 | |||||||||||||||||||||||||||||||||
| 981 | =head3 Detach Events | ||||||||||||||||||||||||||||||||
| 982 | |||||||||||||||||||||||||||||||||
| 983 | When an object is destroyed, it emits a C<detach> event after | ||||||||||||||||||||||||||||||||
| 984 | unsubscribing from other peers' events. | ||||||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||||||
| 986 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 987 | |||||||||||||||||||||||||||||||||
| 988 | =head2 event_handler_prefix | ||||||||||||||||||||||||||||||||
| 989 | |||||||||||||||||||||||||||||||||
| 990 | The string which prefixes default event handler method names. See | ||||||||||||||||||||||||||||||||
| 991 | L</subscribe>. It will by default be initialized to the return value | ||||||||||||||||||||||||||||||||
| 992 | of the L</default_event_handler_prefix> method. It may be specified | ||||||||||||||||||||||||||||||||
| 993 | during object construction. | ||||||||||||||||||||||||||||||||
| 994 | |||||||||||||||||||||||||||||||||
| 995 | For example, the default handler method name for an event named | ||||||||||||||||||||||||||||||||
| 996 | C<changed> would be C<_cb_changed>. The class must provide that | ||||||||||||||||||||||||||||||||
| 997 | method. See L</subscribe> for more information. | ||||||||||||||||||||||||||||||||
| 998 | |||||||||||||||||||||||||||||||||
| 999 | =head2 addr | ||||||||||||||||||||||||||||||||
| 1000 | |||||||||||||||||||||||||||||||||
| 1001 | A L<Net::Object::Peer::RefAddr> object providing a unique identity for this emitter. | ||||||||||||||||||||||||||||||||
| 1002 | |||||||||||||||||||||||||||||||||
| 1003 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 1004 | |||||||||||||||||||||||||||||||||
| 1005 | =head2 emits_events | ||||||||||||||||||||||||||||||||
| 1006 | |||||||||||||||||||||||||||||||||
| 1007 | $bool = $obj->emits_events( @event_names ); | ||||||||||||||||||||||||||||||||
| 1008 | |||||||||||||||||||||||||||||||||
| 1009 | Returns true if the object emits I<all> of the named events | ||||||||||||||||||||||||||||||||
| 1010 | |||||||||||||||||||||||||||||||||
| 1011 | =head2 default_events | ||||||||||||||||||||||||||||||||
| 1012 | |||||||||||||||||||||||||||||||||
| 1013 | @events = $class->default_events; | ||||||||||||||||||||||||||||||||
| 1014 | |||||||||||||||||||||||||||||||||
| 1015 | Returns a list of events which this class will emit, excluding the C<detach> and C<unsubscribed> events. | ||||||||||||||||||||||||||||||||
| 1016 | The default implementation returns an empty list. A per object event list may be specified via | ||||||||||||||||||||||||||||||||
| 1017 | the L</events> attribute or the C<events> option to the L<constructor|/new>. | ||||||||||||||||||||||||||||||||
| 1018 | |||||||||||||||||||||||||||||||||
| 1019 | =head2 new | ||||||||||||||||||||||||||||||||
| 1020 | |||||||||||||||||||||||||||||||||
| 1021 | $obj = Net::Object::Peer->new( %args | \%args ); | ||||||||||||||||||||||||||||||||
| 1022 | |||||||||||||||||||||||||||||||||
| 1023 | Construct a new object. The following arguments are available: | ||||||||||||||||||||||||||||||||
| 1024 | |||||||||||||||||||||||||||||||||
| 1025 | =over | ||||||||||||||||||||||||||||||||
| 1026 | |||||||||||||||||||||||||||||||||
| 1027 | =item event_handler_prefix => I<string> | ||||||||||||||||||||||||||||||||
| 1028 | |||||||||||||||||||||||||||||||||
| 1029 | The string which prefixes default event handler method names. See L</event_handler_prefix> | ||||||||||||||||||||||||||||||||
| 1030 | |||||||||||||||||||||||||||||||||
| 1031 | =item events => I<string> | I<arrayref> | ||||||||||||||||||||||||||||||||
| 1032 | |||||||||||||||||||||||||||||||||
| 1033 | The name(s) of the event(s) this object will emit (don't include the | ||||||||||||||||||||||||||||||||
| 1034 | C<unsubscribed> and C<detach> events). May be a single string or an | ||||||||||||||||||||||||||||||||
| 1035 | arrayref. If not specified, the list of events will be initialized via the | ||||||||||||||||||||||||||||||||
| 1036 | L</default_events> class method. | ||||||||||||||||||||||||||||||||
| 1037 | |||||||||||||||||||||||||||||||||
| 1038 | =back | ||||||||||||||||||||||||||||||||
| 1039 | |||||||||||||||||||||||||||||||||
| 1040 | =head2 default_event_handler_prefix | ||||||||||||||||||||||||||||||||
| 1041 | |||||||||||||||||||||||||||||||||
| 1042 | This class method returns the prefix for the default event handler method names. | ||||||||||||||||||||||||||||||||
| 1043 | It defaults to returning C<_cb_>. | ||||||||||||||||||||||||||||||||
| 1044 | |||||||||||||||||||||||||||||||||
| 1045 | =head2 build_sub | ||||||||||||||||||||||||||||||||
| 1046 | |||||||||||||||||||||||||||||||||
| 1047 | $coderef = $self->build_sub( $emitter, @tuple ); | ||||||||||||||||||||||||||||||||
| 1048 | |||||||||||||||||||||||||||||||||
| 1049 | C<build_sub> is the method responsible for creating and | ||||||||||||||||||||||||||||||||
| 1050 | compiling the code for an event handler. It is invoked | ||||||||||||||||||||||||||||||||
| 1051 | from the L</subscribe()> method, with the following parameters | ||||||||||||||||||||||||||||||||
| 1052 | |||||||||||||||||||||||||||||||||
| 1053 | =over | ||||||||||||||||||||||||||||||||
| 1054 | |||||||||||||||||||||||||||||||||
| 1055 | =item C<$emitter> | ||||||||||||||||||||||||||||||||
| 1056 | |||||||||||||||||||||||||||||||||
| 1057 | The emitter object. | ||||||||||||||||||||||||||||||||
| 1058 | |||||||||||||||||||||||||||||||||
| 1059 | =item C<@tuple> | ||||||||||||||||||||||||||||||||
| 1060 | |||||||||||||||||||||||||||||||||
| 1061 | the tuple passed to L</subscribe> for this event. | ||||||||||||||||||||||||||||||||
| 1062 | |||||||||||||||||||||||||||||||||
| 1063 | =back | ||||||||||||||||||||||||||||||||
| 1064 | |||||||||||||||||||||||||||||||||
| 1065 | The default implementation will return a L<Sub::Quote::quote_sub|Sub::Quote/quote_sub> | ||||||||||||||||||||||||||||||||
| 1066 | generated code reference for method calls and code specified as a | ||||||||||||||||||||||||||||||||
| 1067 | string. See L<Net::Object::Peer::Cookbook/Loop Detecton> for an | ||||||||||||||||||||||||||||||||
| 1068 | example of using this attribute to inline additional code in the event | ||||||||||||||||||||||||||||||||
| 1069 | handler. | ||||||||||||||||||||||||||||||||
| 1070 | |||||||||||||||||||||||||||||||||
| 1071 | =head2 subscribe | ||||||||||||||||||||||||||||||||
| 1072 | |||||||||||||||||||||||||||||||||
| 1073 | $self->subscribe( $peer, @event_tuple [, @event_tuple, ... ] ); | ||||||||||||||||||||||||||||||||
| 1074 | |||||||||||||||||||||||||||||||||
| 1075 | Subscribe to one or more events sent by C<$peer>, which must consume | ||||||||||||||||||||||||||||||||
| 1076 | the L<Net::Object::Peer> role. If C<$peer> additionally consumes the | ||||||||||||||||||||||||||||||||
| 1077 | L<Net::Object::Peer::Ephemeral> role, a strong reference to C<$peer> | ||||||||||||||||||||||||||||||||
| 1078 | is stored. (See L<Net::Object::Peer::Cookbook/Translation/Proxy Nodes>.) | ||||||||||||||||||||||||||||||||
| 1079 | An attempt to subscribe to an event the peer does not produce will throw | ||||||||||||||||||||||||||||||||
| 1080 | an exception. | ||||||||||||||||||||||||||||||||
| 1081 | |||||||||||||||||||||||||||||||||
| 1082 | The event name and the action to be performed when the event is | ||||||||||||||||||||||||||||||||
| 1083 | emitted are specified by a tuple with the following forms: | ||||||||||||||||||||||||||||||||
| 1084 | |||||||||||||||||||||||||||||||||
| 1085 | =over | ||||||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||||||
| 1087 | =item C<< $event_name >> | ||||||||||||||||||||||||||||||||
| 1088 | |||||||||||||||||||||||||||||||||
| 1089 | the event handler will invoke the C<${prefix}${event_name}> method on C<$self>, | ||||||||||||||||||||||||||||||||
| 1090 | where C<$prefix> is the L<event_handler_prefix attribute|/event_handler_prefix>. | ||||||||||||||||||||||||||||||||
| 1091 | |||||||||||||||||||||||||||||||||
| 1092 | =item C<< $event_name => { method => $method_name } >> | ||||||||||||||||||||||||||||||||
| 1093 | |||||||||||||||||||||||||||||||||
| 1094 | The event handler will invoke the C<$method_name> method on C<$self>. | ||||||||||||||||||||||||||||||||
| 1095 | |||||||||||||||||||||||||||||||||
| 1096 | =item C<< $event_name => CODEREF >> | ||||||||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||||||
| 1098 | The passed code reference is called. | ||||||||||||||||||||||||||||||||
| 1099 | |||||||||||||||||||||||||||||||||
| 1100 | =item C<< $event_name => { code => $code, capture => \%capture } >> | ||||||||||||||||||||||||||||||||
| 1101 | |||||||||||||||||||||||||||||||||
| 1102 | C<$code> is a string containing code to be run by the event handler. | ||||||||||||||||||||||||||||||||
| 1103 | C<%capture> is a hash containing variable captures. See the | ||||||||||||||||||||||||||||||||
| 1104 | documentation for "\%captures" in L<Sub::Quote/quote_sub> for more | ||||||||||||||||||||||||||||||||
| 1105 | information. | ||||||||||||||||||||||||||||||||
| 1106 | |||||||||||||||||||||||||||||||||
| 1107 | =back | ||||||||||||||||||||||||||||||||
| 1108 | |||||||||||||||||||||||||||||||||
| 1109 | If C<$peer> provides a C<_notify_subscribed> method, that will be invoked as | ||||||||||||||||||||||||||||||||
| 1110 | |||||||||||||||||||||||||||||||||
| 1111 | $peer->_notify_subscribed( $self, $event_name, ... ); | ||||||||||||||||||||||||||||||||
| 1112 | |||||||||||||||||||||||||||||||||
| 1113 | for each subscription. | ||||||||||||||||||||||||||||||||
| 1114 | |||||||||||||||||||||||||||||||||
| 1115 | =head2 unsubscribe | ||||||||||||||||||||||||||||||||
| 1116 | |||||||||||||||||||||||||||||||||
| 1117 | # Unsubscribe from all events from all peers. | ||||||||||||||||||||||||||||||||
| 1118 | $self->unsubscribe; | ||||||||||||||||||||||||||||||||
| 1119 | |||||||||||||||||||||||||||||||||
| 1120 | # Unsubscribe from all events emitted by a peer | ||||||||||||||||||||||||||||||||
| 1121 | $self->unsubscribe( $peer ); | ||||||||||||||||||||||||||||||||
| 1122 | |||||||||||||||||||||||||||||||||
| 1123 | # Unsubscribe from one or more events emitted by a peer | ||||||||||||||||||||||||||||||||
| 1124 | $self->unsubscribe( $peer, $event_name [, $event_name [, ... ]); | ||||||||||||||||||||||||||||||||
| 1125 | |||||||||||||||||||||||||||||||||
| 1126 | # Unsubscribe from the peer and event specified by the passed | ||||||||||||||||||||||||||||||||
| 1127 | # Net::Object::Peer::Event object | ||||||||||||||||||||||||||||||||
| 1128 | $self->unsubscribe( $event_object ); | ||||||||||||||||||||||||||||||||
| 1129 | |||||||||||||||||||||||||||||||||
| 1130 | # Unsubscribe from one or more events emitted by all peers | ||||||||||||||||||||||||||||||||
| 1131 | $self->unsubscribe( $event_name [, $event_name [, ... ] ] ) | ||||||||||||||||||||||||||||||||
| 1132 | |||||||||||||||||||||||||||||||||
| 1133 | Unsubscribe from events/peers. After unsubscription, an I<unsubscribed> | ||||||||||||||||||||||||||||||||
| 1134 | event with a L<Net::Object::Peer::UnsubscribeEvent> as a payload will | ||||||||||||||||||||||||||||||||
| 1135 | be sent to affected peers who have subscribed to the unsubscribed event(s). | ||||||||||||||||||||||||||||||||
| 1136 | |||||||||||||||||||||||||||||||||
| 1137 | C<$peer> may be either a L<Net::Object::Peer> or a | ||||||||||||||||||||||||||||||||
| 1138 | L<Net::Object::Peer::RefAddr> object. | ||||||||||||||||||||||||||||||||
| 1139 | |||||||||||||||||||||||||||||||||
| 1140 | Note that B<Net::Object::Peer::Event> objects which are passed to | ||||||||||||||||||||||||||||||||
| 1141 | event handlers may have a masqueraded C<emitter> attribute. Attempting | ||||||||||||||||||||||||||||||||
| 1142 | to unsubscribe from that C<emitter> is unwise. Instead, pass either | ||||||||||||||||||||||||||||||||
| 1143 | the event object or the C<addr> field in that object, which is guaranteed | ||||||||||||||||||||||||||||||||
| 1144 | to identify the actual emitter subscribed to. | ||||||||||||||||||||||||||||||||
| 1145 | |||||||||||||||||||||||||||||||||
| 1146 | =head2 events | ||||||||||||||||||||||||||||||||
| 1147 | |||||||||||||||||||||||||||||||||
| 1148 | @events = $obj->events; | ||||||||||||||||||||||||||||||||
| 1149 | $obj->events( \@event_names | $event_name ); | ||||||||||||||||||||||||||||||||
| 1150 | |||||||||||||||||||||||||||||||||
| 1151 | As a getter, returns a list of event names which the object may emit. | ||||||||||||||||||||||||||||||||
| 1152 | |||||||||||||||||||||||||||||||||
| 1153 | As a setter, accepts either an arrayref or a single event name. Event | ||||||||||||||||||||||||||||||||
| 1154 | names must a valid Perl identifier (e.g., no C<:> or C<-> characters). | ||||||||||||||||||||||||||||||||
| 1155 | |||||||||||||||||||||||||||||||||
| 1156 | =head2 detach | ||||||||||||||||||||||||||||||||
| 1157 | |||||||||||||||||||||||||||||||||
| 1158 | $self->detach; | ||||||||||||||||||||||||||||||||
| 1159 | |||||||||||||||||||||||||||||||||
| 1160 | Detach the object from the network. It will | ||||||||||||||||||||||||||||||||
| 1161 | |||||||||||||||||||||||||||||||||
| 1162 | =over | ||||||||||||||||||||||||||||||||
| 1163 | |||||||||||||||||||||||||||||||||
| 1164 | =item 1 | ||||||||||||||||||||||||||||||||
| 1165 | |||||||||||||||||||||||||||||||||
| 1166 | Unsubscribe from all events from all peers. | ||||||||||||||||||||||||||||||||
| 1167 | |||||||||||||||||||||||||||||||||
| 1168 | =item 2 | ||||||||||||||||||||||||||||||||
| 1169 | |||||||||||||||||||||||||||||||||
| 1170 | Emit an C<unsubscribed> event with a L<Net::Object::Peer::UnsubscribeEvent> as a payload. | ||||||||||||||||||||||||||||||||
| 1171 | |||||||||||||||||||||||||||||||||
| 1172 | =item 3 | ||||||||||||||||||||||||||||||||
| 1173 | |||||||||||||||||||||||||||||||||
| 1174 | Emit a C<detach> event. | ||||||||||||||||||||||||||||||||
| 1175 | |||||||||||||||||||||||||||||||||
| 1176 | =back | ||||||||||||||||||||||||||||||||
| 1177 | |||||||||||||||||||||||||||||||||
| 1178 | =head2 subscriptions | ||||||||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||||||
| 1180 | # return all subscriptions | ||||||||||||||||||||||||||||||||
| 1181 | my @subscriptions = $self->subscriptions; | ||||||||||||||||||||||||||||||||
| 1182 | |||||||||||||||||||||||||||||||||
| 1183 | # return matching subscriptions | ||||||||||||||||||||||||||||||||
| 1184 | my @subscriptions = $self->subscriptions( $coderef | %spec ); | ||||||||||||||||||||||||||||||||
| 1185 | |||||||||||||||||||||||||||||||||
| 1186 | Returns the events to which C<$self> is subscribed as a list of | ||||||||||||||||||||||||||||||||
| 1187 | hashrefs (see L<Net::Object::Peer::Subscription::as_hashref>). If | ||||||||||||||||||||||||||||||||
| 1188 | arguments are specified, only those which match are returned; see | ||||||||||||||||||||||||||||||||
| 1189 | L<Net::Object::Peer::Subscrition/find>; | ||||||||||||||||||||||||||||||||
| 1190 | |||||||||||||||||||||||||||||||||
| 1191 | =head2 emit | ||||||||||||||||||||||||||||||||
| 1192 | |||||||||||||||||||||||||||||||||
| 1193 | $self->emit( $event_name, %args ); | ||||||||||||||||||||||||||||||||
| 1194 | |||||||||||||||||||||||||||||||||
| 1195 | Broadcast the named event to all subscribed peers. C<%args> contains | ||||||||||||||||||||||||||||||||
| 1196 | arguments to be passed the the payload class constructor. The default | ||||||||||||||||||||||||||||||||
| 1197 | payload class is a L<Net::Object::Peer::Event> object; use the | ||||||||||||||||||||||||||||||||
| 1198 | C<class> key to specify an alternate class, which must be derived from | ||||||||||||||||||||||||||||||||
| 1199 | B<Net::Object::Peer::Event>. An attempt to emit an event which is not | ||||||||||||||||||||||||||||||||
| 1200 | supported by the emitter will cause an exception to be thrown. | ||||||||||||||||||||||||||||||||
| 1201 | |||||||||||||||||||||||||||||||||
| 1202 | =head2 send | ||||||||||||||||||||||||||||||||
| 1203 | |||||||||||||||||||||||||||||||||
| 1204 | $self->send( $peer, $event_name, %args ); | ||||||||||||||||||||||||||||||||
| 1205 | |||||||||||||||||||||||||||||||||
| 1206 | This is similar to the L</emit> method, but only sends the event to the | ||||||||||||||||||||||||||||||||
| 1207 | specified peer. An attempt to emit an event which is not | ||||||||||||||||||||||||||||||||
| 1208 | supported by the emitter will cause an exception to be thrown. | ||||||||||||||||||||||||||||||||
| 1209 | |||||||||||||||||||||||||||||||||
| 1210 | =head2 emit_args | ||||||||||||||||||||||||||||||||
| 1211 | |||||||||||||||||||||||||||||||||
| 1212 | $self->emit_args( $event_name, @args ); | ||||||||||||||||||||||||||||||||
| 1213 | |||||||||||||||||||||||||||||||||
| 1214 | Broadcast the named event to all subscribed peers. C<@args> will be | ||||||||||||||||||||||||||||||||
| 1215 | passed directly to each subscriber's callback. An attempt to emit an event which is not | ||||||||||||||||||||||||||||||||
| 1216 | supported by the emitter will cause an exception to be thrown. | ||||||||||||||||||||||||||||||||
| 1217 | |||||||||||||||||||||||||||||||||
| 1218 | =head2 send_args | ||||||||||||||||||||||||||||||||
| 1219 | |||||||||||||||||||||||||||||||||
| 1220 | $self->send_args( $peer, $event_name, @args ); | ||||||||||||||||||||||||||||||||
| 1221 | |||||||||||||||||||||||||||||||||
| 1222 | This is similar to the L</emit_args> method, but only sends the event to the | ||||||||||||||||||||||||||||||||
| 1223 | specified peer. An attempt to emit an event which is not | ||||||||||||||||||||||||||||||||
| 1224 | supported by the emitter will cause an exception to be thrown. | ||||||||||||||||||||||||||||||||
| 1225 | |||||||||||||||||||||||||||||||||
| 1226 | =begin pod_coverage | ||||||||||||||||||||||||||||||||
| 1227 | |||||||||||||||||||||||||||||||||
| 1228 | =head3 UnsubscribeEvent | ||||||||||||||||||||||||||||||||
| 1229 | |||||||||||||||||||||||||||||||||
| 1230 | =head3 Subscription | ||||||||||||||||||||||||||||||||
| 1231 | |||||||||||||||||||||||||||||||||
| 1232 | =head3 Listener | ||||||||||||||||||||||||||||||||
| 1233 | |||||||||||||||||||||||||||||||||
| 1234 | =head3 Emitter | ||||||||||||||||||||||||||||||||
| 1235 | |||||||||||||||||||||||||||||||||
| 1236 | =end pod_coverage | ||||||||||||||||||||||||||||||||
| 1237 | |||||||||||||||||||||||||||||||||
| 1238 | =begin pod_coverage | ||||||||||||||||||||||||||||||||
| 1239 | |||||||||||||||||||||||||||||||||
| 1240 | =head4 has_addr | ||||||||||||||||||||||||||||||||
| 1241 | |||||||||||||||||||||||||||||||||
| 1242 | =head4 BUILD | ||||||||||||||||||||||||||||||||
| 1243 | |||||||||||||||||||||||||||||||||
| 1244 | =end pod_coverage | ||||||||||||||||||||||||||||||||
| 1245 | |||||||||||||||||||||||||||||||||
| 1246 | =begin pod_coverage | ||||||||||||||||||||||||||||||||
| 1247 | |||||||||||||||||||||||||||||||||
| 1248 | =head3 DEMOLISH | ||||||||||||||||||||||||||||||||
| 1249 | |||||||||||||||||||||||||||||||||
| 1250 | =end pod_coverage | ||||||||||||||||||||||||||||||||
| 1251 | |||||||||||||||||||||||||||||||||
| 1252 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 1253 | |||||||||||||||||||||||||||||||||
| 1254 | Diab Jerius <djerius@cpan.org> | ||||||||||||||||||||||||||||||||
| 1255 | |||||||||||||||||||||||||||||||||
| 1256 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 1257 | |||||||||||||||||||||||||||||||||
| 1258 | This software is Copyright (c) 2016 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||||||||||
| 1259 | |||||||||||||||||||||||||||||||||
| 1260 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||
| 1261 | |||||||||||||||||||||||||||||||||
| 1262 | The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||||||
| 1263 | |||||||||||||||||||||||||||||||||
| 1264 | =cut | ||||||||||||||||||||||||||||||||
| 1265 | |||||||||||||||||||||||||||||||||
| 1266 | __END__ | ||||||||||||||||||||||||||||||||