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