Ok, I've beat my head to a bloody pulp, searched the FAQ/cookbook
and I can't figure out what I need to do.
I copied the /WebServer from the cookbook, and use it to parse some
XML sent as an HTTP POST. Well this works great. But I need to
send an e-mail as a result of all this.
I hacked the code (attached), and when sendmail is running, this works
great. The client (sending the XML) gets the 202 status from the
webserver portion right away, and the e-mail gets sent.
When sendmail is down, the XML gets parsed OK, but then the 202 status
does NOT come back.
What I want to do is establish a second "thread" for the Net::SMTP,
and then have the "main" return. The 2nd "thread" will wait for sendmail
to come back up...
I've attached my code.
Can any kind soul help me? It's worth a Jolt Cola... really!
-r-
#!/usr/perl5.6.1/bin/perl
my $PORT_NUMBER = 8088; # The port we run the web server on
my $DEBUG = 1;
use warnings;
use strict;
use POE qw(Component::Server::TCP Filter::HTTPD);
use HTTP::Response;
use Proc::Daemon;
# This next line puts this little darling into the background.
unless($DEBUG)
{
Proc::Daemon::Init;
}
# Spawn a web server on port $PORT_NUMBER of all interfaces.
POE::Component::Server::TCP->new
( Alias => "web_server",
Port => $PORT_NUMBER,
ClientFilter => 'POE::Filter::HTTPD',
# The ClientInput function is called to deal with client input.
# Because this server uses POE::Filter::HTTPD to parse input,
# ClientInput will receive HTTP requests.
ClientInput => sub {
my ($kernel, $heap, $request) = @_[KERNEL, HEAP, ARG0];
# Filter::HTTPD sometimes generates HTTP::Response objects.
# They indicate (and contain the response for) errors that occur
# while parsing the client's HTTP request. It's easiest to send
# the responses as they are and finish up.
if ($request->isa("HTTP::Response")) {
$heap->{client}->put( $request );
$kernel->yield("shutdown");
return;
}
# The request is real and fully formed. Parse content based on
# it.
print STDERR ">>>> POE Kernel ID: ", $kernel->ID, "\n" if($DEBUG);
ParseXML($request->content());
my $response = HTTP::Response->new(202, 'Accepted');
# Once the content has been built, send it back to the client
# and schedule a shutdown.
$heap->{client}->put($response);
$kernel->yield("shutdown");
}
);
# Start POE. This will run the server until it exits.
$poe_kernel->run();
exit 0;
use XML::Parser;
sub ParseXML
{
my ($content) = @_;
my ($p)= XML::Parser->new(
Handlers => {
Start => \&Handle_Start,
End => \&Handle_End,
Char => \&Handle_Chars,
},
ProtocolEncoding => 'UTF-8',
ErrorContext => 1,
);
if($p->parse($content))
{
PostStatus("http://localhost:8080/cgi-bin/saveall", "Successful");
}
else
{
PostStatus("http://localhost:8080/cgi-bin/saveall", "Failure");
}
}
sub Handle_Start { my($Expat, $Element, %AttVals) = @_; }
sub Handle_End { my($Expat, $Element, %AttVals) = @_; }
sub Handle_Chars { my($Expat, $Element, %AttVals) = @_; }
use Net::SMTP;
sub PostStatus
{
my($URL, $Mesg) = @_;
POE::Session->create
(
args => [ "$URL", "$Mesg" ],
inline_states =>
{
_start => \&Post_Status_Start,
PostStatusVia => \&PostStatusViaSmtp,
}
);
}
sub Post_Status_Start
{
my($kernel, $heap, $URL, $Mesg) = @_[KERNEL, HEAP, ARG0, ARG1];
print STDERR "Post_Status_Start, kernel ID: ", $kernel->ID, "\n" if($DEBUG);
$kernel->yield("PostStatusVia", "$URL", "$Mesg");
}
sub PostStatusViaSmtp
{
my($kernel, $heap, $URL, $Mesg) = @_[KERNEL, HEAP, ARG0, ARG1];
print STDERR "----------------------------------------------------------\n"
if($DEBUG);
print STDERR "PostStatus to $URL $Mesg\n" if ($DEBUG);
print STDERR "PostStatusViaSmtp, kernel ID: ", $kernel->ID, "\n" if($DEBUG);
print STDERR "----------------------------------------------------------\n"
if($DEBUG);
my($smtp) = Net::SMTP->new(
'localhost',
Hello => 'localhost',
);
unless(defined($smtp) && $smtp)
{
print STDERR "Sendmail not running?\n" if($DEBUG);
# sleep 5;
$kernel->delay("PostStatusVia", 5, "$URL", "$Mesg");
return;
}
$smtp->mail('rssouthw');
$smtp->to('rssouthw');
$smtp->data();
$smtp->datasend("To: rssouthw\@localhost\n");
$smtp->datasend("From: rssouthw\@localhost\n");
$smtp->datasend("\n");
$smtp->datasend("URL: $URL\n");
$smtp->datasend("Mesg: $Mesg\n");
$smtp->dataend();
$smtp->quit();
}