Mercury/Controller/PubSub/Cascade.pm

filter: Statementstmt 100% 39 / 39 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 100% 52 / 52 CC 3 SCAR 3.4
CC
3
Cov
100%
_pattern
2.0
_send_message
2.0
BEGIN
1.0
CRAP 1.4
« Mercury/Controller/PubSub.pm ↑ Summary Mojolicious/Plugin/Mercury.pm »
1 package Mercury::Controller::PubSub::Cascade;
2 5 our $VERSION = '0.016';
3 # ABSTRACT: Pub/sub controller with a topic heirarchy and cascading
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod # myapp.pl
8 #pod use Mojolicious::Lite;
9 #pod plugin 'Mercury';
10 #pod websocket( '/pub/*topic' )
11 #pod ->to( controller => 'PubSub::Cascade', action => 'pub' );
12 #pod websocket( '/sub/*topic' )
13 #pod ->to( controller => 'PubSub::Cascade', action => 'sub' );
14 #pod
15 #pod =head1 DESCRIPTION
16 #pod
17 #pod This controller enables a L<pubE<sol>sub pattern|Mercury::Pattern::PubSub> on
18 #pod a pair of endpoints (L<publish|/publish> and L<subscribe|/subscribe>.
19 #pod
20 #pod In this variant, topics are organized into a heirarchy. Subscribers can
21 #pod subscribe to higher branch of the tree to recieve messages from all the
22 #pod publishers on lower branches of the tree. So, a subscriber to C</foo>
23 #pod will receive messages sent to C</foo>, C</foo/bar>, and C</foo/bar/baz>.
24 #pod
25 #pod For more information on the pub/sub pattern, see L<Mercury::Pattern::PubSub>.
26 #pod
27 #pod =head1 SEE ALSO
28 #pod
29 #pod =over
30 #pod
31 #pod =item L<Mercury::Pattern::PubSub>
32 #pod
33 #pod =item L<Mercury::Controller::PubSub>
34 #pod
35 #pod =item L<Mercury>
36 #pod
37 #pod =back
38 #pod
39 #pod =cut
40  
41 5 ▶use Mojo::Base 'Mojolicious::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶use Mercury::Pattern::PubSub;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 #pod =method publish
45 #pod
46 #pod $app->routes->websocket( '/pub/*topic' )
47 #pod ->to( controller => 'PubSub::Cascade', action => 'publish' );
48 #pod
49 #pod Controller action to connect a websocket as a publisher. A publish
50 #pod client sends messages through the socket. The message will be sent to
51 #pod all of the connected subscribers for the topic and all parent topics.
52 #pod
53 #pod This endpoint requires a C<topic> in the stash.
54 #pod
55 #pod =cut
56  
57 4 ▶sub publish {
Subroutine publish: called CC=1 SCAR=0.0
Pod documented
58 4 my ( $c ) = @_;
59 4 my $topic = $c->stash( 'topic' );
60 4 my $pattern = $c->_pattern( $topic );
61 4 $pattern->add_publisher( $c->tx );
62  
63 # Send messages to parent topics
64 3 ▶ $c->tx->on( message => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 3 my ( $tx, $msg ) = @_;
66 3 $c->_send_message( $topic, $msg );
67 4 } );
68  
69 4 $c->rendered( 101 );
70 }
71  
72 #pod =method subscribe
73 #pod
74 #pod $app->routes->websocket( '/sub/*topic' )
75 #pod ->to( controller => 'PubSub::Cascade', action => 'subscribe' );
76 #pod
77 #pod Controller action to connect a websocket as a subscriber. A subscriber
78 #pod will recieve every message sent by publishers to the current topic and
79 #pod any child topics.
80 #pod
81 #pod This endpoint requires a C<topic> in the stash.
82 #pod
83 #pod =cut
84  
85 5 ▶sub subscribe {
Subroutine subscribe: called CC=1 SCAR=0.0
Pod documented
86 5 my ( $c ) = @_;
87 5 my $pattern = $c->_pattern( $c->stash( 'topic' ) );
88 5 $pattern->add_subscriber( $c->tx );
89 5 $c->rendered( 101 );
90 }
91  
92 #pod =method post
93 #pod
94 #pod Post a new message to the given topic without subscribing or
95 #pod establishing a WebSocket connection. This allows new messages to be
96 #pod pushed by any HTTP client.
97 #pod
98 #pod =cut
99  
100 2 ▶sub post {
Subroutine post: called CC=1 SCAR=0.0
Pod documented
101 2 my ( $c ) = @_;
102 2 my $topic = $c->stash( 'topic' );
103 2 my $pattern = $c->_pattern( $topic );
104 2 $pattern->send_message( $c->req->body );
105 2 $c->_send_message( $topic, $c->req->body );
106 2 $c->render(
107 status => 200,
108 text => '',
109 );
110 }
111  
112 #=method _pattern
113 #
114 # my $pattern = $c->_pattern( $topic );
115 #
116 # Get or create the L<Mercury::Pattern::PubSub> object for the given
117 # topic.
118 #
119 #=cut
120  
121 11 ▶sub _pattern {
Subroutine _pattern: called CC=2 SCAR=6.9
122 11 my ( $c, $topic ) = @_;
123 11 my $pattern = $c->mercury->pattern( 'PubSub::Cascade' => $topic );
124 11 ▶ if ( !$pattern ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattern) 3 8 11
125 3 $pattern = Mercury::Pattern::PubSub->new;
126 3 $c->mercury->pattern( 'PubSub::Cascade' => $topic => $pattern );
127 }
128 11 return $pattern;
129 }
130  
131 #=method _send_message
132 #
133 # $c->_send_message( $topic, $msg );
134 #
135 # Send the given message out on all the appropriate topics. This handles
136 # the "Cascade" part.
137 #=cut
138  
139 5 ▶sub _send_message {
Subroutine _send_message: called CC=2 SCAR=6.9
140 5 my ( $c, $topic, $msg ) = @_;
141 5 my @parts = split m{/}, $topic;
142 my @patterns =
143 # Only pattern objects that have been created
144 2 grep { defined }
145 # Change topics into pattern objects
146 2 map { $c->mercury->pattern( 'PubSub::Cascade' => $_ ) }
147 # Build parent topics
148 5 map { join '/', @parts[0..$_] }
149 0..$#parts-1;
150 5 $_->send_message( $msg ) for @patterns;
151 }
152  
153 5 1;
154  
155 __END__
« Mercury/Controller/PubSub.pm ↑ Summary Mojolicious/Plugin/Mercury.pm »