#!/usr/bin/perl -w
# $Id: queue.perl,v 1.1 2000/12/03 13:27:12 rcaputo Exp $

# This is a simple job queue.

$| = 1;
use strict;
use POE;
use POE::Filter::Line;
use POE::Wheel::Run;

# POE::Session->create call.
my $email = 'rwhite@niuhi.com';
POE::Session->create
( inline_states =>
  { _start  => \&child_start,
  _stop   => \&child_stop,
  stdout  => \&child_stdout,
  },
  args => [$email],
);

# Check the passed arg with a forked checker.
sub child_start {
  my ($kernel, $session, $parent, $heap, $email) = @_[KERNEL, SESSION, SENDER, HEAP, ARG0];
  # Remember the parent.
  warn "Child ", $session->ID, " will validate $email.\n";
  # Check the passed email address.
  my $program = [ "./check_addr.pl" ];
  my $wheel = POE::Wheel::Run->new(
                  Program    => $program,
                  #StdinEvent  => 'stdin',  # Event to emit when stdin is flushed to child.
                  StdoutEvent => 'stdout', # Event to emit with child stdout information.
                  #StderrEvent => &stderr, # Event to emit with child stderr information.    # Identify the chil
                  Filter => POE::Filter::Line->new(), # Or some other filter.    # May also specify filters per
                  );
   $wheel->put($email);
}

# The child has finished whatever it was supposed to do.  Send the
# result of its labor to stdout.
sub stdout {
  my ($heap, $input, $wheel_id) = @_[HEAP, ARG0, ARG1];
  print "Child process in wheel $wheel_id wrote to STDOUT: $input\n";
}

# same thing with the stderr.
sub stderr {
  my ($heap, $input, $wheel_id) = @_[HEAP, ARG0, ARG1];
  print "Child process in wheel $wheel_id wrote to STDERR: $input\n";
}

# The child has stopped.  Display a message to help illustrate what's
# going on.
sub child_stop {
  my $session = $_[SESSION];
  warn "Child ", $session->ID, " is stopped.\n";
}

$poe_kernel->run();
exit;
