Mercury/Pattern/PubSub.pm

filter: Statementstmt 100% 44 / 44 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 100% 64 / 64 CC 6 SCAR 6.3
CC
6
Cov
100%
remove_subscriber
3.0
remove_publisher
3.0
send_message
2.0
CRAP 1.9
« Mercury/Pattern/Bus.pm ↑ Summary Mercury/Controller/Bus.pm »
1 package Mercury::Pattern::PubSub;
2 8 our $VERSION = '0.016';
3 # ABSTRACT: Manage a pub/sub pattern for a single topic
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod # Connect the publisher
8 #pod my $pub_ua = Mojo::UserAgent->new;
9 #pod my $pub_tx = $ua->websocket( '/pub/foo' );
10 #pod
11 #pod # Connect the subscriber socket
12 #pod my $sub_ua = Mojo::UserAgent->new;
13 #pod my $sub_tx = $ua->websocket( '/sub/foo' );
14 #pod
15 #pod # Connect the two sockets using pub/sub
16 #pod my $pattern = Mercury::Pattern::PubSub->new;
17 #pod $pattern->add_publisher( $pub_tx );
18 #pod $pattern->add_subscriber( $sub_tx );
19 #pod
20 #pod # Send a message
21 #pod $sub_tx->on( message => sub {
22 #pod my ( $tx, $msg ) = @_;
23 #pod print $msg; # Hello, World!
24 #pod } );
25 #pod $pub_tx->send( 'Hello, World!' );
26 #pod
27 #pod =head1 DESCRIPTION
28 #pod
29 #pod This pattern connects publishers, which send messages, to subscribers,
30 #pod which recieve messages. Each message sent by a publisher will be
31 #pod received by all connected subscribers. This pattern is useful for
32 #pod sending notification events and logging.
33 #pod
34 #pod =head1 SEE ALSO
35 #pod
36 #pod =over
37 #pod
38 #pod =item L<Mercury::Controller::PubSub>
39 #pod
40 #pod =item L<Mercury>
41 #pod
42 #pod =back
43 #pod
44 #pod =cut
45  
46 8 ▶use Mojo::Base '-base';
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 #pod =attr subscribers
49 #pod
50 #pod Arrayref of connected websockets ready to receive messages
51 #pod
52 #pod =cut
53  
54 4 ▶has subscribers => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
55  
56 #pod =attr publishers
57 #pod
58 #pod Arrayref of connected websockets ready to publish messages
59 #pod
60 #pod =cut
61  
62 4 ▶has publishers => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
63  
64 #pod =method add_subscriber
65 #pod
66 #pod $pat->add_subscriber( $tx );
67 #pod
68 #pod Add the connection as a subscriber. Subscribers will receive all messages
69 #pod sent by publishers.
70 #pod
71 #pod =cut
72  
73 7 ▶sub add_subscriber {
Subroutine add_subscriber: called CC=1 SCAR=0.0
Pod documented
74 7 my ( $self, $tx ) = @_;
75 7 ▶ $tx->on( finish => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 7 my ( $tx ) = @_;
77 7 $self->remove_subscriber( $tx );
78 7 } );
79 7 push @{ $self->subscribers }, $tx;
80 7 return;
81 }
82  
83 #pod =method remove_subscriber
84 #pod
85 #pod $pat->remove_subscriber( $tx );
86 #pod
87 #pod Remove a subscriber. Called automatically when a subscriber socket is
88 #pod closed.
89 #pod
90 #pod =cut
91  
92 7 ▶sub remove_subscriber {
Subroutine remove_subscriber: called CC=3 SCAR=11.0
Pod documented
93 7 my ( $self, $tx ) = @_;
94 7 my @subs = @{ $self->subscribers };
95 7 for my $i ( 0.. $#subs ) {
96 11 ▶ if ( $subs[$i] eq $tx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subs[$i] eq $tx) 7 4 11
97 7 splice @subs, $i, 1;
98 7 return;
99 }
100 }
101 }
102  
103 #pod =method add_publisher
104 #pod
105 #pod $pat->add_publisher( $tx );
106 #pod
107 #pod Add a publisher to this topic. Publishers send messages to all
108 #pod subscribers.
109 #pod
110 #pod =cut
111  
112 5 ▶sub add_publisher {
Subroutine add_publisher: called CC=1 SCAR=0.0
Pod documented
113 5 my ( $self, $tx ) = @_;
114 4 ▶ $tx->on( message => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
115 4 my ( $tx, $msg ) = @_;
116 4 $self->send_message( $msg );
117 5 } );
118 5 ▶ $tx->on( finish => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
119 5 my ( $tx ) = @_;
120 5 $self->remove_publisher( $tx );
121 5 } );
122 5 push @{ $self->publishers }, $tx;
123 5 return;
124 }
125  
126 #pod =method remove_publisher
127 #pod
128 #pod $pat->remove_publisher( $tx );
129 #pod
130 #pod Remove a publisher from the list. Called automatically when the
131 #pod publisher socket is closed.
132 #pod
133 #pod =cut
134  
135 5 ▶sub remove_publisher {
Subroutine remove_publisher: called CC=3 SCAR=11.0
Pod documented
136 5 my ( $self, $tx ) = @_;
137 5 my @pubs = @{ $self->publishers };
138 5 for my $i ( 0.. $#pubs ) {
139 6 ▶ if ( $pubs[$i] eq $tx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pubs[$i] eq $tx) 5 1 6
140 5 splice @pubs, $i, 1;
141 5 return;
142 }
143 }
144 }
145  
146 #pod =method send_message
147 #pod
148 #pod $pat->send_message( $message );
149 #pod
150 #pod Send a message to all subscribers.
151 #pod
152 #pod =cut
153  
154 8 ▶sub send_message {
Subroutine send_message: called CC=2 SCAR=6.9
Pod documented
155 8 my ( $self, $message ) = @_;
156 8 $_->send( $message ) for @{ $self->subscribers };
157 8 return;
158 }
159  
160 8 1;
161  
162 __END__
« Mercury/Pattern/Bus.pm ↑ Summary Mercury/Controller/Bus.pm »