MySQL.pm untested

filter: Statementstmt 0.0% 0 / 147 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 221 CC 16 SCAR 34.6
CC
16
Cov
0%
_socket_birth
110.0
_do_fork
20.0
_server_start
12.0
CRAP 31.8
1 package POE::Component::Server::MySQL;
2 use Moose;
3 use MooseX::MethodAttributes;
4  
5 our $VERSION = "0.02";
6  
7 use POE;
8 use POE::Kernel;
9 use POE qw(Component::Server::TCP);
10 use POE::Filter::Block;
11  
12  
13 use Socket qw(INADDR_ANY inet_ntoa inet_aton AF_INET AF_UNIX PF_UNIX);
14 use Errno qw(ECONNABORTED ECONNRESET);
15  
16 use Carp qw( croak );
17 use Class::Inspector;
18  
19 use Module::Find;
20 use Data::Dumper;
21  
22 use Time::HiRes qw(gettimeofday tv_interval);
23  
24 use POE::Component::Server::MySQL::Client;
25  
26 has 'address' => (is => 'rw', isa => 'Str');
27 has 'port' => (is => 'rw', isa => 'Int');
28  
29 has 'local_dsn' => (is => 'rw', isa => 'Str');
30 has 'local_user' => (is => 'rw', isa => 'Str');
31 has 'local_password' => (is => 'rw', isa => 'Str');
32  
33 has 'listener' => (is => 'rw', isa => 'Any');
34 has 'session' => (is => 'rw', isa => 'Any');
35  
36 sub DEBUG {1}
37  
38 sub BUILD {
39 my ($self, $opt) = @_;
40
41 POE::Session->create(
42 object_states => [
43 $self => {
44 _start => '_server_start',
45 _stop => '_server_stop',
46 _socket_birth => '_socket_birth',
47 _socket_death => '_socket_death',
48 }
49 ],
50 inline_states => {
51 _do_fork => \&_do_fork,
52 },
53 );
54  
55 return $self;
56 }
57  
58 sub _server_start {
59 my ( $self, $kernel, $session, $heap ) = @_[ OBJECT, KERNEL, SESSION, HEAP];
60
61 $self->port(23306) unless $self->port;
62 $self->address('127.0.0.1') unless $self->address;
63
64 print "Ready to accept clients in namespace ".ref($self)." on ".$self->address.":".$self->port." \n";
65
66 $heap->{children} = {};
67 $heap->{is_a_child} = 0;
68 $heap->{max_processes} = 5;
69
70 $self->listener(POE::Wheel::SocketFactory->new(
71 BindAddress => $self->address,
72 BindPort => $self->port,
73 Reuse => 'yes',
74 SuccessEvent => '_socket_birth',
75 FailureEvent => '_socket_death',
76 ));
77  
78 $kernel->yield('_do_fork');
79 }
80  
81 sub _server_stop {
82 my ( $self, $kernel, $session, $heap ) = @_[ OBJECT, KERNEL, SESSION, HEAP];
83 print "Stop server \n";
84 $self->listener(undef);
85 $self->session(undef);
86  
87 }
88  
89 sub _do_fork {
90 my ($kernel, $heap) = @_[KERNEL, HEAP];
91  
92 return if $heap->{is_a_child};
93  
94 while (scalar(keys %{$heap->{children}}) < $heap->{max_processes}) {
95 my $pid = fork();
96  
97 unless (defined($pid)) {
98 $kernel->delay(do_fork => 1);
99 return;
100 }
101  
102 if ($pid) {
103 print "Server $$ forked child $pid\n";
104 $heap->{children}->{$pid} = 1;
105 $kernel->sig_child($pid, "got_sig_child");
106 next;
107 }
108  
109 $kernel->has_forked();
110 $heap->{is_a_child} = 1;
111 $heap->{children} = {};
112 return;
113 }
114 }
115  
116  
117 sub _socket_birth {
118 my ( $self, $kernel, $session, $heap ) = @_[ OBJECT, KERNEL, SESSION, HEAP];
119 my ($socket, $address, $port) = @_[ARG0, ARG1, ARG2];
120 $address = inet_ntoa($address);
121  
122 my $client = POE::Component::Server::MySQL::Client->new({
123 server_class => ref($self),
124 });
125
126 print '$self->local_dsn = '.$self->local_dsn."\n";
127
128 $client->local_dsn($self->local_dsn);
129 $client->local_user($self->local_user);
130 $client->local_password($self->local_password);
131
132 POE::Session->create(
133 object_states => [
134 $client => {
135 client_input => 'client_input',
136 client_error => 'client_error',
137 shutdown => 'shutdown',
138 client_connect => 'client_connect',
139 }
140 ],
141 inline_states => {
142 _start => sub {
143 my ( $kernel, $session, $heap ) = @_[KERNEL, SESSION, HEAP];
144
145 $heap->{client} = POE::Wheel::ReadWrite->new(
146 Handle => $socket,
147 Driver => POE::Driver::SysRW->new(),
148 Filter => POE::Filter::Block->new(
149 LengthCodec => [ \&_length_encoder, \&_length_decoder ]
150 ),
151 InputEvent => 'client_input',
152 ErrorEvent => 'client_error',
153 );
154
155 $client->wheel($heap->{client});
156 $client->session_id($session->ID);
157 $client->session($session);
158  
159 # register system states
160 my @methods = Class::Inspector->methods(
161 'POE::Component::Server::MySQL::Client',
162 'expanded',
163 'public'
164 );
165  
166 foreach my $method (@{ $methods[0] }) {
167 my ($full, $class, $method, undef ) = @{ $method };
168 next if $method =~ /^send_/
169 or $method eq 'write'
170 or $method eq 'new_definition';
171 if ($class eq 'POE::Component::Server::MySQL::Client') {
172 $session->_register_state($method, $client);
173 }
174 }
175
176 my @dispatchers;
177
178 my @modules = findsubmod(ref($self));
179
180 foreach my $module (@modules) {
181 next unless $module;
182
183 unless (Class::Inspector->loaded($module)) {
184 require Class::Inspector->filename($module);
185 }
186
187 my @methods = Class::Inspector->methods($module, 'expanded', 'public');
188
189 foreach my $method (@{ $methods[0] }) {
190 my ($full, $class, $method, undef ) = @{ $method };
191 next unless $module->meta->get_method($method);
192
193 my $attrs;
194
195 eval {
196 $attrs = $module->meta->get_method($method)->attributes;
197 };
198
199 next if $@;
200
201 foreach my $attr (@{$attrs}) {
202
203 if ($attr =~ /Regexp\(('|")(.*)('|")\)/io) { #"
204 my $eval_str = 'push @dispatchers, {
205 regexp => '.$2.',
206 method => $method,
207 };';
208 eval($eval_str);
209 }
210 elsif ($attr =~ /Match\(('|")(.*)('|")\)/io) { #"
211 push @dispatchers, {
212 match => $2,
213 method => $method,
214 };
215 }
216 elsif ($attr =~ /Default/io) {
217 $client->default({
218 default => 1,
219 method => $method,
220 });
221 }
222
223 Moose::Util::apply_all_roles(ref($client), ($module));
224 $client->session->_register_state($method, $client);
225
226 }
227
228 }
229
230 }
231  
232 $client->dispatchers(\@dispatchers);
233
234 $kernel->yield('client_connect');
235 },
236 _child => sub { },
237 },
238 );
239  
240 }
241  
242 sub _socket_death {
243 my ( $self, $kernel, $session, $heap ) = @_[ OBJECT, KERNEL, SESSION, HEAP];
244 print "_socket_death \n";
245 }
246  
247 sub run {
248 POE::Kernel->run();
249 }
250  
251 sub _length_encoder {
252 return;
253  
254 }
255  
256 sub _length_decoder {
257 my $stuff = shift;
258  
259 if (length($$stuff) > 1) {
260 return length($$stuff);
261 }
262 else {
263 return 1;
264 }
265 }
266  
267 =head1 NAME
268  
269 POE::Component::Server::MySQL - A MySQL POE Server
270  
271 =head1 DESCRIPTION
272  
273 This modules helps building a MySQL proxy in which you can write
274 handler to deal with specific queries.
275  
276 You can modifiy the query, write a specific response, relay a query
277 or do wahtever you want within each handler.
278  
279 This is the evolution of POE::Component::DBIx::MyServer, it
280 uses Moose and POE.
281  
282 =head1 SYNOPSYS
283  
284 First you create a server class that extends POE::Component::Server::MySQL.
285  
286 package MyMySQL;
287 use Moose;
288
289 extends 'POE::Component::Server::MySQL';
290 with 'MooseX::Getopt';
291  
292 Then in a perl script you can instantiate your new server
293  
294 use MyMySQL;
295 my $server = MyMySQL->new_with_options();
296 $server->run;
297  
298 In the MyMySQL namespace you can add roles which will act as handlers
299 for your trapped queries:
300  
301 package MyMySQL::OnSteroids;
302 use MooseX::MethodAttributes::Role;
303
304 sub fortune : Regexp('qr{fortune}io') {
305 my ($self) = @_;
306
307 my $fortune = `fortune`;
308 chomp($fortune);
309
310 $self->send_results(['fortune'],[[$fortune]]);
311
312 }
313  
314 =head1 AUTHORS
315  
316 Eriam Schaffter, C<eriam@cpan.org> with original work
317 done by Philip Stoev in the DBIx::MyServer module.
318  
319 =head1 BUGS
320  
321 At least one, in specific cases the servers sends several
322 packets instead of a single one. It works fine with most clients
323 but it crashes Toad for MySQL for example.
324  
325 =head1 COPYRIGHT
326  
327 This program is free software, you can redistribute it and/or modify it
328 under the same terms as Perl itself.
329  
330 =cut
331  
332  
333  
334 1;
335