| 1 | package POE::Component::Client::SOAP; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | our @EXPORT = ( ); | ||
| 6 | use Switch; | ||
| 7 | |||
| 8 | use Carp qw(croak); | ||
| 9 | use POE; | ||
| 10 | use POE::Component::Generic; | ||
| 11 | use base qw(Exporter); | ||
| 12 | use vars qw($VERSION); | ||
| 13 | $VERSION = '0.01'; | ||
| 14 | |||
| 15 | use POE::Component::Client::SOAP::Nonblock; | ||
| 16 | |||
| 17 | #my @reconnections = qw(60 120 240 480 960 1920 3840); | ||
| 18 | my @reconnections = qw(1 2 3); | ||
| 19 | |||
| 20 | #TODO: Take a look at Proc::BackOff | ||
| 21 | |||
| 22 | our $soap_config; #lexical scope | ||
| 23 | |||
| 24 | sub spawn { | ||
| 25 | my $package = shift; | ||
| 26 | croak "$package requires an even number of parameters" if @_ & 1; | ||
| 27 | my %args = @_; | ||
| 28 | my $self = bless ({}, $package); | ||
| 29 | |||
| 30 | $args{Alias} = 'soap-client' unless $args{Alias}; | ||
| 31 | croak "$package missing SOAP proxy" unless $args{proxy}; | ||
| 32 | croak "$package missing SOAP service" unless $args{service}; | ||
| 33 | $args{retry_reconnect} = 0 unless $args{retry_reconnect}; | ||
| 34 | |||
| 35 | |||
| 36 | $self->{CONFIG} = \%args; | ||
| 37 | $soap_config = $self->{CONFIG}; #lexical scope | ||
| 38 | |||
| 39 | $self->{count} = scalar(@reconnections); | ||
| 40 | $self->{attempts} = 0; | ||
| 41 | |||
| 42 | POE::Session->create( | ||
| 43 | object_states => [ | ||
| 44 | $self => { | ||
| 45 | _start => '_client_start', | ||
| 46 | _stop => '_client_stop', | ||
| 47 | shutdown => '_client_close', | ||
| 48 | reconnect => '_server_connect', | ||
| 49 | }, | ||
| 50 | $self => [ qw( child_sig _server_connect _client_close _pocog_error | ||
| 51 | _soap_response _soap_result _handle_error | ||
| 52 | handle_result handle_error handle_connected) ], | ||
| 53 | ], | ||
| 54 | (ref $args{options} eq 'HASH' ? (options => $args{options}) : () ), | ||
| 55 | ); | ||
| 56 | |||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | # only for debug | ||
| 61 | sub child_sig { | ||
| 62 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 63 | $self->log($kernel, 'debug', "CHLD:".$_[ARG1]); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _client_start { | ||
| 67 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 68 | $self->log($kernel, 'debug', '_client_start()'); | ||
| 69 | $kernel->alias_set($self->config('Alias')); | ||
| 70 | $kernel->sig(CHLD => 'child_sig'); | ||
| 71 | $kernel->yield('_server_connect'); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _client_stop { | ||
| 75 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 76 | $self->log($kernel, 'debug', '_client_stop()'); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _client_close { | ||
| 80 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 81 | $self->log($kernel, 'debug', '_client_close()'); | ||
| 82 | $self->handle_shutdown($kernel); | ||
| 83 | $kernel->alias_remove($self->config('Alias')); | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | sub _server_connect { | ||
| 88 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 89 | $self->log($kernel, 'debug', '_server_connect()'); | ||
| 90 | |||
| 91 | # TODO: trusting PoCo::Generic not to leak PIDs. This needs further | ||
| 92 | # revision. Don't know what PoCo:Generic does with dead childs and | ||
| 93 | # how to handle them. | ||
| 94 | |||
| 95 | unless(defined $self->{soap} and defined $self->{soap}->{litex}){ | ||
| 96 | $self->{soap}->{litex} = undef; | ||
| 97 | $self->{soap} = undef; | ||
| 98 | my $soap = POE::Component::Client::SOAP::Nonblock->new($self->{CONFIG}); | ||
| 99 | if (defined $soap) { | ||
| 100 | $self->{soap} = $soap; | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | $self->_reconnect($kernel); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | $kernel->yield('handle_connected'); | ||
| 108 | $self->{please_wait} = 0; | ||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | # log any poco generic errors | ||
| 114 | sub _pocog_error { | ||
| 115 | my( $kernel, $self, $session, $err ) = @_[ KERNEL, OBJECT, SESSION, ARG0 ]; | ||
| 116 | my $errstr = undef; | ||
| 117 | unless($err->{stderr}){ | ||
| 118 | my $op = $err->{operation}?$err->{operation}:'undef'; | ||
| 119 | my $en = $err->{errnum}?$err->{errnum}:'undef'; | ||
| 120 | my $es = $err->{errstr}?$err->{errstr}:'undef'; | ||
| 121 | $errstr = qq|OP:$op EN:$en ES:$es|; | ||
| 122 | $self->log($kernel, 'error', "_pocog_error() wheel error: $errstr"); | ||
| 123 | $self->{pocog_error} = 1; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _reconnect { | ||
| 128 | my ($self, $kernel) = @_; | ||
| 129 | |||
| 130 | $self->log($kernel, 'debug', "Attempts: $self->{attempts}, Count: $self->{count}"); | ||
| 131 | if ($self->{attempts} < $self->{count}) { | ||
| 132 | my $delay = $reconnections[$self->{attempts}]; | ||
| 133 | $self->log($kernel, 'warning', "Attempting reconnection: $self->{attempts}, waiting: $delay seconds"); | ||
| 134 | $self->{attempts}++; | ||
| 135 | $kernel->delay('reconnect', $delay); | ||
| 136 | } | ||
| 137 | else { | ||
| 138 | if ($self->config('retry_reconnect')) { | ||
| 139 | $self->log($kernel, 'warning', 'Cycling reconnection attempts, but not shutting down...'); | ||
| 140 | $self->{attempts} = 0; | ||
| 141 | $kernel->yield('reconnect'); | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | $self->log($kernel, 'warning', 'Shutting down, too many reconnection attempts'); | ||
| 145 | $kernel->yield('shutdown'); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | sub _soap_response { | ||
| 152 | my ($kernel, $self, $resp, $pobj) = @_[ KERNEL, OBJECT, ARG0, ARG1]; | ||
| 153 | |||
| 154 | $self->log($kernel, 'debug', '_soap_response'); | ||
| 155 | |||
| 156 | my $error = undef; | ||
| 157 | |||
| 158 | # error was probably because of a faulty SOAP call... | ||
| 159 | if($self->{pocog_error}){ | ||
| 160 | $error = 'Faulty PoCo::Generic Object'; | ||
| 161 | } | ||
| 162 | |||
| 163 | do{ | ||
| 164 | if ($pobj->{package} ne '') { | ||
| 165 | eval { | ||
| 166 | $pobj->result({ | ||
| 167 | session => $self->config('Alias'), | ||
| 168 | event => '_soap_result', | ||
| 169 | data => $resp->{factory}, | ||
| 170 | }); | ||
| 171 | }; | ||
| 172 | $error = $@ if $@; | ||
| 173 | } else { | ||
| 174 | $error = 'soap response has no package'; | ||
| 175 | } | ||
| 176 | } unless $error; | ||
| 177 | |||
| 178 | if ($error) { | ||
| 179 | $self->log($kernel, 'warning', | ||
| 180 | "_soap_response() exception in result(): $error"); | ||
| 181 | $kernel->yield('_handle_error', $error); | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | } | ||
| 186 | |||
| 187 | sub _soap_result { | ||
| 188 | my ($kernel, $self, $resp, $pobj) = @_[ KERNEL, OBJECT, ARG0, ARG1]; | ||
| 189 | |||
| 190 | $self->log($kernel, 'debug', '_soap_result'); | ||
| 191 | |||
| 192 | # return the object as returned by SOAP | ||
| 193 | if (defined $pobj) { | ||
| 194 | # pobj is the data, resp->{data} is the SOAP call name | ||
| 195 | $kernel->yield('handle_result', [$resp->{data},$pobj]); | ||
| 196 | } else { | ||
| 197 | my $error = 'SOAP result returned invalid object'; | ||
| 198 | $self->log($kernel, 'warning', | ||
| 199 | "_soap_result() exception in result(): $error"); | ||
| 200 | $kernel->yield('_handle_error', $error); | ||
| 201 | } | ||
| 202 | |||
| 203 | } | ||
| 204 | |||
| 205 | sub AUTOLOAD { | ||
| 206 | |||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | our $AUTOLOAD; | ||
| 210 | |||
| 211 | return undef if $self->{soap}->{fail}; | ||
| 212 | |||
| 213 | if ( $AUTOLOAD =~ m/::(\w+)$/ ) { | ||
| 214 | unless($1 eq 'DESTROY' or $self->{please_wait}){ | ||
| 215 | eval q|$self->{soap}->{litex}->|.$1.q|( | ||
| 216 | {session => $self->config('Alias'), | ||
| 217 | event => '_soap_response'}, @_)|; | ||
| 218 | if ($@) { | ||
| 219 | $self->log($poe_kernel, 'warning', | ||
| 220 | "exception in main autoload: $@"); | ||
| 221 | $poe_kernel->yield('_handle_error', $@); | ||
| 222 | } | ||
| 223 | |||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | sub _handle_error { | ||
| 230 | my ($kernel, $self, $error) = @_[KERNEL, OBJECT, ARG0]; | ||
| 231 | $self->{please_wait} = 1; | ||
| 232 | $self->log($kernel, 'error', 'Error in service. Reset, and attempting to reconnect...'); | ||
| 233 | $kernel->post($self->config('Alias'), 'handle_error', ['-ERROR-', $error]); | ||
| 234 | $self->{pocog_error} = undef; | ||
| 235 | $self->_reconnect($kernel); | ||
| 236 | } | ||
| 237 | |||
| 238 | # --------------------------------------------------------------------- | ||
| 239 | # Public accessors | ||
| 240 | # --------------------------------------------------------------------- | ||
| 241 | |||
| 242 | sub config { | ||
| 243 | my ($self, $arg) = @_; | ||
| 244 | |||
| 245 | return $self->{CONFIG}->{$arg}; | ||
| 246 | |||
| 247 | } | ||
| 248 | |||
| 249 | |||
| 250 | # --------------------------------------------------------------------- | ||
| 251 | # Public methods, these should be overridden, as needed | ||
| 252 | # --------------------------------------------------------------------- | ||
| 253 | |||
| 254 | sub log { | ||
| 255 | my ($self, $kernel, $level, $message) = @_; | ||
| 256 | } | ||
| 257 | |||
| 258 | sub handle_result { | ||
| 259 | my ($kernel, $self, $frame) = @_[KERNEL, OBJECT, ARG0]; | ||
| 260 | } | ||
| 261 | |||
| 262 | sub handle_error { | ||
| 263 | my ($kernel, $self, $frame) = @_[KERNEL, OBJECT, ARG0]; | ||
| 264 | |||
| 265 | } | ||
| 266 | |||
| 267 | sub handle_shutdown { | ||
| 268 | my ($self, $kernel) = @_; | ||
| 269 | |||
| 270 | } | ||
| 271 | |||
| 272 | sub handle_connected { | ||
| 273 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 274 | |||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | ###################################################################### | ||
| 279 | # # | ||
| 280 | # POE::Component::Client::SOAP::Nonblock # | ||
| 281 | # # | ||
| 282 | ###################################################################### | ||
| 283 | package POE::Component::Client::SOAP::Nonblock; | ||
| 284 | |||
| 285 | use warnings; | ||
| 286 | use strict; | ||
| 287 | our @EXPORT = ( ); | ||
| 288 | use Switch; | ||
| 289 | |||
| 290 | use Carp qw(croak); | ||
| 291 | use POE; | ||
| 292 | use POE::Component::Generic; | ||
| 293 | use base qw(Exporter); | ||
| 294 | use vars qw($VERSION); | ||
| 295 | $VERSION = '0.01'; | ||
| 296 | |||
| 297 | use SOAP::Lite; | ||
| 298 | use Devel::Symdump; | ||
| 299 | |||
| 300 | sub new { | ||
| 301 | |||
| 302 | my $package = shift; | ||
| 303 | my $params = shift; | ||
| 304 | my $self = bless ({}, $package); | ||
| 305 | |||
| 306 | my $soap = undef; | ||
| 307 | |||
| 308 | eval{ | ||
| 309 | $soap = SOAP::Lite->new( | ||
| 310 | proxy => $soap_config->{proxy}, | ||
| 311 | service => $soap_config->{service}, | ||
| 312 | ); | ||
| 313 | }; | ||
| 314 | |||
| 315 | return undef if $@ or !defined($soap); | ||
| 316 | |||
| 317 | croak "Bad SOAP Service (non valid URI):".$soap->{_service} | ||
| 318 | unless ($soap->{_service} =~ /^\w+\:\/\/\w+.*$/); | ||
| 319 | |||
| 320 | |||
| 321 | # all methods from all services are mapped | ||
| 322 | my @services = keys %{$soap->{_schema}->{_services}}; | ||
| 323 | my @soap_methods = ( ); | ||
| 324 | foreach my $service (@services) { | ||
| 325 | foreach my $soap_method (keys %{$soap->{_schema}->{_services}->{$service}}) { | ||
| 326 | push @soap_methods, $soap_method; | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | # defined methods | ||
| 331 | my $symdump = Devel::Symdump->new(qw(POE::Component::Client::SOAP::LiteX)); | ||
| 332 | my @defined_methods = $symdump->functions; | ||
| 333 | |||
| 334 | # map all to the LiteX namespace | ||
| 335 | foreach my $soap_method (@soap_methods) { | ||
| 336 | unless($self->_find_symbol($soap_method, \@defined_methods)){ | ||
| 337 | eval q| | ||
| 338 | sub POE::Component::Client::SOAP::LiteX::|.$soap_method.q| { | ||
| 339 | my $self = shift; | ||
| 340 | my $call = undef; | ||
| 341 | eval { | ||
| 342 | $call = $self->{soap_lite}->|.$soap_method.q|(@_); | ||
| 343 | }; | ||
| 344 | if($@){ | ||
| 345 | warn $@; | ||
| 346 | return undef; | ||
| 347 | } else { | ||
| 348 | return $call; | ||
| 349 | } | ||
| 350 | }|; | ||
| 351 | warn "Error mapping SOAP method to LiteX: $@" if $@; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | |||
| 356 | # now PoCo::Generic thinks they are real ;-) | ||
| 357 | $soap = POE::Component::Generic->spawn( | ||
| 358 | alias => 'soapgeneric', | ||
| 359 | package => 'POE::Component::Client::SOAP::LiteX', | ||
| 360 | object_options => [ | ||
| 361 | proxy => $params->{proxy}, | ||
| 362 | service => $params->{service}, | ||
| 363 | ], | ||
| 364 | factories => \@soap_methods, | ||
| 365 | error => '_pocog_error', | ||
| 366 | debug => $params->{debug}?$params->{debug}:0, | ||
| 367 | verbose => $params->{verbose}?$params->{verbose}:0, | ||
| 368 | ); | ||
| 369 | |||
| 370 | $self->{litex} = $soap?$soap:undef; | ||
| 371 | return $soap?$self:undef; | ||
| 372 | } | ||
| 373 | |||
| 374 | sub _find_symbol { | ||
| 375 | |||
| 376 | my ($self, $symbol, $where) = @_; | ||
| 377 | foreach my $s (@$where) { | ||
| 378 | $s =~ m/::(\w+)$/; | ||
| 379 | return 1 if $symbol eq $1; | ||
| 380 | } | ||
| 381 | return 0; | ||
| 382 | } | ||
| 383 | |||
| 384 | |||
| 385 | ###################################################################### | ||
| 386 | # # | ||
| 387 | # POE::Component::Client::SOAP::LiteX # | ||
| 388 | # # | ||
| 389 | ###################################################################### | ||
| 390 | package POE::Component::Client::SOAP::LiteX; | ||
| 391 | |||
| 392 | use warnings; | ||
| 393 | use strict; | ||
| 394 | our @EXPORT = ( ); | ||
| 395 | use Carp qw(croak); | ||
| 396 | use base qw(Exporter); | ||
| 397 | use vars qw($VERSION); | ||
| 398 | $VERSION = '0.01'; | ||
| 399 | |||
| 400 | use SOAP::Lite; | ||
| 401 | |||
| 402 | sub new { | ||
| 403 | my $package = shift; | ||
| 404 | croak "$package requires an even number of parameters" if @_ & 1; | ||
| 405 | my @args = @_; | ||
| 406 | my $self = bless ({}, $package); | ||
| 407 | my $soap_lite = SOAP::Lite->new(@args); | ||
| 408 | return undef unless $soap_lite; | ||
| 409 | $self->{soap_lite} = $soap_lite; | ||
| 410 | return $self; | ||
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | # Trap method not defined in the WSDL | ||
| 415 | sub AUTOLOAD { | ||
| 416 | my $self = shift; | ||
| 417 | our $AUTOLOAD; | ||
| 418 | if ( $AUTOLOAD =~ m/::(\w+)$/ ) { | ||
| 419 | unless($1 eq 'DESTROY'){ | ||
| 420 | warn "Sorry, cannot do: $1 in LiteX, since it's not defined in the WSDL."; | ||
| 421 | } | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | |||
| 426 | 1; | ||
| 427 | |||
| 428 | |||
| 429 | __END__ |