Hi everybody,

I am a newcomer to POE, and I have what feels like a fairly silly question, though combing the documentation does not provide me with any pointers - perhaps I am going about this the wrong way.

The scenario is that I have a program which will do work based on queues, work comes into queues based on files appearing in directories. Enter POE::Component::DirWatch. So far so good. The DirWatch component calls a subroutine when it finds a file.

Since I have multiple directories to monitor, I spawn multiple DirWatch components. I am having trouble figuring how to make the callback tell which session initially created this DirWatch component. I could duplicate the code ('watch_dir1' and 'watch_dir2') but I am sure there is a better way. An example is definitely in order:

#!/usr/bin/perl

use warnings;
use strict;

use POE;
use POE::Component::DirWatch;

# create queue 1
POE::Session->create(
                     inline_states => {
                                       _start => \&init_queue1,
                                       watch_dir => \&watch_dir,
                                      },
                    );

POE::Session->create(
                     inline_states => {
                                       _start => \&init_queue2,
                                       watch_dir => \&watch_dir,
                                      },
                    );

sub init_queue1 {
my ($kernel, $heap, $session, $sender) = @_[KERNEL, HEAP, SESSION, SENDER];
  warn "Queue 1 starting (session id " . $session->ID . ")";

  # watch a directory for this queue
  $kernel->yield('watch_dir', 'queue1');
}

sub init_queue2 {
my ($kernel, $heap, $session, $sender) = @_[KERNEL, HEAP, SESSION, SENDER];
  warn "Queue 2 starting (session id " . $session->ID . ")";

  # watch a directory for this queue
  $kernel->yield('watch_dir', 'queue2');
}

sub watch_dir {
my ($kernel, $heap, $session, $queue) = @_[KERNEL, HEAP, SESSION, ARG0];

  # start watching the directory
  POE::Component::DirWatch->spawn(
                                  Alias        => 'dirwatch_' . $queue,
                                  Directory    => $queue,
                                  Callback     => \&found_file,
                                  PollInterval => 1,
                                 );
}

sub found_file {
my ($kernel, $heap, $session, $sender, $file) = @_[KERNEL, HEAP, SESSION, SENDER, ARG1];

warn "Found '$file' for " . $session->ID . " (from " . $sender- >ID . ")\n";
}

$poe_kernel->run();
exit(0);

This produces output like:

Queue 1 starting (session id 2) at testcase.pl line 26.
Queue 2 starting (session id 3) at testcase.pl line 34.
Found 'queue1/file1' for 4 (from 4)
Found 'queue2/file2' for 5 (from 5)

How can the 'found_file' callback 'talk to' the queue session that created the DirWatch component, rather then the DirWatch component itself?

Looking at a couple of other components, I'm wondering if it's more the norm for PoCo's to be sending their events to their parents, DirWatch seems to differ in that respect.... I may well be wrong about this, like I said I'm new :-) If that's the case I can probably roll my own. I'd just like to know if I'm doing something weird first :-)

Regards,

        Justin

--
Justin Hawkins
[EMAIL PROTECTED]



Reply via email to