| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::Plugin::PubSub::Connection; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Jifty::Plugin::PubSub::Connection - Connection to browser | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | This class represents a bidirectional channel between the server and the | ||
| 13 | web browser. You may wish to subclass this class as C<YourApp::PubSub> | ||
| 14 | to override the L</connect>, L</receive>, or L</disconnect> methods. | ||
| 15 | |||
| 16 | =head1 METHODS | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | # This is _new rather than new because it should never be called by | ||
| 21 | # external code | ||
| 22 | sub _new { | ||
| 23 | my $class = shift; | ||
| 24 | my $env = shift; | ||
| 25 | |||
| 26 | my $self = bless {}, $class; | ||
| 27 | $self->{web} = Jifty::Web->new; | ||
| 28 | $self->{web}->request( | ||
| 29 | Jifty::Request->promote( Plack::Request->new( $env ) ), | ||
| 30 | ); | ||
| 31 | $self->{web}->response( Jifty::Response->new ); | ||
| 32 | { | ||
| 33 | local $Jifty::WEB = $self->{web}; | ||
| 34 | Jifty->web->setup_session; | ||
| 35 | } | ||
| 36 | |||
| 37 | $self->{api} = Jifty::API->new; | ||
| 38 | $self->{listener} = $env->{'hippie.listener'}; | ||
| 39 | $self->{client_id} = $env->{'hippie.client_id'}; | ||
| 40 | $self->{region_subs} = []; | ||
| 41 | |||
| 42 | for my $sub ( @{ Jifty->subs->retrieve($self->client_id) }) { | ||
| 43 | if ( join(" ", keys %{$sub}) eq "topic" ) { | ||
| 44 | $self->subscribe( $sub->{topic} ); | ||
| 45 | } else { | ||
| 46 | push @{ $self->{region_subs} }, $sub; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | if ( @{ $self->{region_subs} } ) { | ||
| 51 | my @subs = map {$_->{topic}} @{ $self->{region_subs} }; | ||
| 52 | $self->{region_bus} = Jifty->bus->new_listener; | ||
| 53 | $self->{region_bus}->subscribe( $_ ) | ||
| 54 | for map {Jifty->bus->topic($_)} @subs; | ||
| 55 | $self->{region_bus}->poll( sub { $self->region_event( @_ ) } ); | ||
| 56 | } | ||
| 57 | |||
| 58 | $self->subscribe( "client." . $self->client_id ); | ||
| 59 | |||
| 60 | return $self; | ||
| 61 | } | ||
| 62 | |||
| 63 | =head2 connect | ||
| 64 | |||
| 65 | Called when a connection is established from the browser. By default, | ||
| 66 | does nothing. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub connect {} | ||
| 71 | |||
| 72 | =head2 subscribe I<TOPIC> [, I<TOPIC>, ...] | ||
| 73 | |||
| 74 | Subscribes the browser to receive messages on the given topics. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub subscribe { | ||
| 79 | my $self = shift; | ||
| 80 | $self->{listener}->subscribe( $_ ) | ||
| 81 | for map { Jifty->bus->topic( $_) } @_; | ||
| 82 | } | ||
| 83 | |||
| 84 | =head2 send I<TYPE> I<DATA> | ||
| 85 | |||
| 86 | Sends an arbitrary message to the browser. It is not published to the | ||
| 87 | rest of the message bus. | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub send { | ||
| 92 | my $self = shift; | ||
| 93 | my ($type, $data) = @_; | ||
| 94 | $data->{type} = $type; | ||
| 95 | Jifty->bus->topic("client." . $self->client_id ) | ||
| 96 | ->publish( $data ); | ||
| 97 | } | ||
| 98 | |||
| 99 | =head2 receive I<DATA> | ||
| 100 | |||
| 101 | Called when a message is received from the web browser; returns true if | ||
| 102 | the message was processed, false otherwise. If you override this | ||
| 103 | method, be sure you respect this class' return value: | ||
| 104 | |||
| 105 | sub receive { | ||
| 106 | my $self = shift; | ||
| 107 | my $msg = shift; | ||
| 108 | return 1 if $self->SUPER::receive( $msg ); | ||
| 109 | # ... | ||
| 110 | } | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub receive { | ||
| 115 | my $self = shift; | ||
| 116 | my $msg = shift; | ||
| 117 | |||
| 118 | return 1 if $self->action_message($msg); | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | |||
| 122 | =head2 disconnect | ||
| 123 | |||
| 124 | Called when the connection to the browser is lost when the browser | ||
| 125 | switches to a new page. This is not immediate, but occurs after a | ||
| 126 | 15-second timeout. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub disconnect { | ||
| 131 | my $self = shift; | ||
| 132 | if ($self->{region_bus}) { | ||
| 133 | $self->{region_bus}->timeout(0); | ||
| 134 | $self->{region_bus}->unpoll; | ||
| 135 | undef $self->{region_bus}; | ||
| 136 | }; | ||
| 137 | } | ||
| 138 | |||
| 139 | =head2 web | ||
| 140 | |||
| 141 | Returns the constructed L<Jifty::Web> object which is seen as | ||
| 142 | C<Jifty->web> whenever in the context of this connection's L</connect>, | ||
| 143 | L</receive>, L</disconnect>, or when page regions are rendered to be | ||
| 144 | sent over this channel. This ensures that C<Jifty->web->current_user> | ||
| 145 | is set whenever it is relevant. | ||
| 146 | |||
| 147 | =head2 api | ||
| 148 | |||
| 149 | A new L<Jifty::API> object is instantiated for each | ||
| 150 | L<Jifty::Plugin::PubSub::Connection> object. You may wish to limit it | ||
| 151 | to limit which actions can be performed by the web browser. | ||
| 152 | |||
| 153 | =head2 listener | ||
| 154 | |||
| 155 | The L<AnyMQ::Queue> object which listens to events for the client. | ||
| 156 | |||
| 157 | =head2 client_id | ||
| 158 | |||
| 159 | Returns a unique identifier associated with this connection. | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | sub web { shift->{web} } | ||
| 164 | sub api { shift->{api} } | ||
| 165 | sub listener { shift->{listener} } | ||
| 166 | sub client_id { shift->{client_id} } | ||
| 167 | |||
| 168 | =head2 action_message I<DATA> | ||
| 169 | |||
| 170 | Creates, validates, and runs an action if it was received by the client; | ||
| 171 | called by L</receive>. | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | sub action_message { | ||
| 176 | my $self = shift; | ||
| 177 | my $msg = shift; | ||
| 178 | return unless ($msg->{type} || '') eq "jifty.action" | ||
| 179 | and defined $msg->{class}; | ||
| 180 | |||
| 181 | my $class = Jifty->api->qualify($msg->{class}); | ||
| 182 | unless (Jifty->api->is_allowed($class)) { | ||
| 183 | warn "Attempt to call denied action $class: ".Jifty->api->explain($class); | ||
| 184 | return 1; | ||
| 185 | } | ||
| 186 | my $action = Jifty->web->new_action( | ||
| 187 | class => $class, | ||
| 188 | arguments => $msg->{arguments} || {}, | ||
| 189 | ); | ||
| 190 | $action->validate; | ||
| 191 | $action->run if $action->result->success; | ||
| 192 | |||
| 193 | my $result = $action->result->as_hash; | ||
| 194 | $self->send( "jifty.result" => $result); | ||
| 195 | |||
| 196 | return 1; | ||
| 197 | } | ||
| 198 | |||
| 199 | =head2 region_event I<EVENT> | ||
| 200 | |||
| 201 | Called when one or more regions on the page needs to be rendered and | ||
| 202 | pushed to the client, as triggered by an event. The rendered regions | ||
| 203 | will be passed I<EVENT> as an C<event> variable. Currently, rendered | ||
| 204 | regions cannot alter the client's subscription set. | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | sub region_event { | ||
| 209 | my $self = shift; | ||
| 210 | my $event = shift; | ||
| 211 | my $type = $event->{type} or return; | ||
| 212 | |||
| 213 | local $Jifty::WEB = $self->web; | ||
| 214 | local $Jifty::API = $self->api; | ||
| 215 | Jifty::Record->flush_cache if Jifty::Record->can('flush_cache'); | ||
| 216 | |||
| 217 | for my $sub ( @{$self->{region_subs}} ) { | ||
| 218 | next unless $sub->{topic} eq $type; | ||
| 219 | |||
| 220 | my $content; | ||
| 221 | my $region_name; | ||
| 222 | eval { | ||
| 223 | # So we don't warn about "duplicate region"s | ||
| 224 | local Jifty->web->{'regions'} = {}; | ||
| 225 | local Jifty->web->{'region_stack'} = []; | ||
| 226 | |||
| 227 | # XXX TODO: the "first page render" method of storing subs | ||
| 228 | # in Jifty->subs doesn't work when we're in the connection | ||
| 229 | # context; no-op all such attempts. We _can_ nominally | ||
| 230 | # alter them immediately using $self->subscribe, however; | ||
| 231 | # shim in a Jifty->subs which wraps ->add and turns it into | ||
| 232 | # what we do in ->_new | ||
| 233 | local Jifty->subs->{store} = {}; | ||
| 234 | |||
| 235 | my $region = Jifty::Web::PageRegion->new( | ||
| 236 | name => $sub->{region}, | ||
| 237 | path => $sub->{path}, | ||
| 238 | arguments => $sub->{arguments}, | ||
| 239 | ); | ||
| 240 | $region_name = $region->qualified_name; | ||
| 241 | |||
| 242 | $region->enter; | ||
| 243 | Jifty->handler->buffer->push( private => 1 ); | ||
| 244 | $region->render_as_subrequest( { | ||
| 245 | %{$region->arguments}, | ||
| 246 | event => $event, | ||
| 247 | } ); | ||
| 248 | $content = Jifty->handler->buffer->pop; | ||
| 249 | $region->exit; | ||
| 250 | 1; | ||
| 251 | } or warn "$@"; | ||
| 252 | |||
| 253 | $self->send( "jifty.fragment" => { | ||
| 254 | region => $region_name, | ||
| 255 | path => $sub->{path}, | ||
| 256 | args => $sub->{arguments}, | ||
| 257 | content => $content, | ||
| 258 | mode => $sub->{mode}, | ||
| 259 | element => $sub->{element}, | ||
| 260 | %{ $sub->{attrs} || {} }, | ||
| 261 | } ); | ||
| 262 | } | ||
| 263 | |||
| 264 | # For some reason, AnyMQ makes a queue sub'd to the topic become | ||
| 265 | # undef after the poll? Re-subscribing resolves the issue. | ||
| 266 | my @subs = map {$_->{topic}} @{ $self->{region_subs} }; | ||
| 267 | $self->{region_bus}->subscribe( $_ ) | ||
| 268 | for map {Jifty->bus->topic($_)} @subs; | ||
| 269 | } | ||
| 270 | |||
| 271 | 1; |