Fix the nasty digging into action_hash
[catagits/Catalyst-Engine-STOMP.git] / lib / Catalyst / Engine / Stomp.pm
CommitLineData
0a663589 1package Catalyst::Engine::Stomp;
2use Moose;
0a663589 3use List::MoreUtils qw/ uniq /;
4use HTTP::Request;
5use Net::Stomp;
03c90167 6use namespace::autoclean;
7
8extends 'Catalyst::Engine::Embeddable';
9
c39783ed 10our $VERSION = '0.05';
0a663589 11
12has connection => (is => 'rw', isa => 'Net::Stomp');
13has conn_desc => (is => 'rw', isa => 'Str');
14
15=head1 NAME
16
17Catalyst::Engine::Stomp - write message handling apps with Catalyst.
18
19=head1 SYNOPSIS
20
21 # In a server script:
22
23 BEGIN {
24 $ENV{CATALYST_ENGINE} = 'Stomp';
25 require Catalyst::Engine::Stomp;
5ec5e0b1 26 }
0a663589 27
28 MyApp->config->{Engine::Stomp} =
29 {
30 hostname => '127.0.0.1',
31 port => 61613,
32 };
33 MyApp->run();
34
35 # In a controller, or controller base class:
d78bb739 36 use base qw/ Catalyst::Controller::MessageDriven /;
0a663589 37
d78bb739 38 # then create actions, which map as message types
39 sub testaction : Local {
40 my ($self, $c) = @_;
0a663589 41
d78bb739 42 # Reply with a minimal response message
43 my $response = { type => 'testaction_response' };
44 $c->stash->{response} = $response;
45 }
46
0a663589 47=head1 DESCRIPTION
48
49Write a Catalyst app connected to a Stomp messagebroker, not HTTP. You
5ec5e0b1 50need a controller that understands messaging, as well as this engine.
0a663589 51
52This is single-threaded and single process - you need to run multiple
53instances of this engine to get concurrency, and configure your broker
54to load-balance across multiple consumers of the same queue.
55
d78bb739 56Controllers are mapped to Stomp queues, and a controller base class is
57provided, Catalyst::Controller::MessageDriven, which implements
5ec5e0b1 58YAML-serialized messages, mapping a top-level YAML "type" key to
59the action.
d78bb739 60
0a663589 61=head1 METHODS
62
63=head2 run
64
65App entry point. Starts a loop listening for messages.
66
67=cut
68
69sub run {
70 my ($self, $app, $oneshot) = @_;
71
72 die 'No Engine::Stomp configuration found'
bf8937b7 73 unless ref $app->config->{'Engine::Stomp'} eq 'HASH';
0a663589 74
ce723b34 75 my @queues = map { $app->controller($_)->action_namespace } $app->controllers;
0a663589 76
bf8937b7 77 # connect up
0a663589 78 my %template = %{$app->config->{'Engine::Stomp'}};
bf8937b7 79 $self->connection(Net::Stomp->new(\%template));
80 $self->connection->connect();
81 $self->conn_desc($template{hostname}.':'.$template{port});
0a663589 82
bf8937b7 83 # subscribe, with client ack.
0a663589 84 foreach my $queue (@queues) {
bf8937b7 85 my $queue_name = "/queue/$queue";
86 $self->connection->subscribe({
5ec5e0b1 87 destination => $queue_name,
88 ack => 'client'
bf8937b7 89 });
0a663589 90 }
91
bf8937b7 92 # enter loop...
93 while (1) {
94 my $frame = $self->connection->receive_frame();
95 $self->handle_stomp_frame($app, $frame);
96 last if $ENV{ENGINE_ONESHOT};
97 }
98 exit 0;
0a663589 99}
100
101=head2 prepare_request
102
103Overridden to add the source broker to the request, in place of the
104client IP address.
105
106=cut
107
108sub prepare_request {
03c90167 109 my ($self, $c, $req, $res_ref) = @_;
110 shift @_;
111 $self->next::method(@_);
112 $c->req->address($self->conn_desc);
0a663589 113}
114
115=head2 finalize_headers
116
d78bb739 117Overridden to dump out any errors encountered, since you won't get a
118"debugging" message as for HTTP.
0a663589 119
120=cut
121
122sub finalize_headers {
03c90167 123 my ($self, $c) = @_;
124 my $error = join "\n", @{$c->error};
125 if ($error) {
126 $c->log->debug($error);
127 }
128 return $self->next::method($c);
0a663589 129}
130
131=head2 handle_stomp_frame
132
d78bb739 133Dispatch according to Stomp frame type.
0a663589 134
135=cut
136
137sub handle_stomp_frame {
03c90167 138 my ($self, $app, $frame) = @_;
139
140 my $command = $frame->command();
141 if ($command eq 'MESSAGE') {
142 $self->handle_stomp_message($app, $frame);
143 }
144 elsif ($command eq 'ERROR') {
145 $self->handle_stomp_error($app, $frame);
146 }
147 else {
148 $app->log->debug("Got unknown Stomp command: $command");
149 }
0a663589 150}
151
152=head2 handle_stomp_message
153
d78bb739 154Dispatch a Stomp message into the Catalyst app.
0a663589 155
156=cut
157
158sub handle_stomp_message {
03c90167 159 my ($self, $app, $frame) = @_;
160
161 # queue -> controller
162 my $queue = $frame->headers->{destination};
163 my ($controller) = $queue =~ m|^/queue/(.*)$|;
164
165 # set up request
166 my $config = $app->config->{'Engine::Stomp'};
167 my $url = 'stomp://'.$config->{hostname}.':'.$config->{port}.'/'.$controller;
168 my $req = HTTP::Request->new(POST => $url);
169 $req->content($frame->body);
170 $req->content_length(length $frame->body);
171
172 # dispatch
173 my $response;
174 $app->handle_request($req, \$response);
175
176 # reply, if header set
177 if (my $reply_to = $response->headers->header('X-Reply-Address')) {
178 my $reply_queue = '/remote-temp-queue/' . $reply_to;
179 $self->connection->send({ destination => $reply_queue, body => $response->content });
180 }
181
182 # ack the message off the queue now we've replied / processed
183 $self->connection->ack( { frame => $frame } );
0a663589 184}
185
186=head2 handle_stomp_error
187
d78bb739 188Log any Stomp error frames we receive.
0a663589 189
190=cut
191
192sub handle_stomp_error {
03c90167 193 my ($self, $app, $frame) = @_;
5ec5e0b1 194
03c90167 195 my $error = $frame->headers->{message};
196 $app->log->debug("Got Stomp error: $error");
0a663589 197}
198
d78bb739 199__PACKAGE__->meta->make_immutable;
200