# Simple cron-like component.  Wraps the message-
# based version in a singleton class interface.
# Copyright 2004 by Rocco Caputo.  Free software.
# Same terms as Perl itself.  Have fun!

# The ChronicSingleton class implements a single,
# class-based facade over ChronicMessages.  The
# latter class is also a singleton, but it has a
# message-passing interface.

package ChronicSingleton;

use warnings;
use strict;

use POE;
use ChronicMessages;

# Start the singleton messaging service.  Make
# addressing it convenient by giving the current
# class' name as its alias.

ChronicMessages->spawn(Alias => __PACKAGE__);

my $chronic_id = 0;

# Start a new timer.  Post an event to the
# hidden ChronicMessages component.  Return
# the new timer's ID.  It will be used as a
# handle to stop the timer.

sub start {
  my ($class, %param) = @_;

  $poe_kernel->post(
    __PACKAGE__, start => {
      Name => ++$chronic_id,
      %param,
    }
  );

  return $chronic_id;
}

# Stop a timer, based on its handle.

sub stop {
  my ($class, %param) = @_;

  $poe_kernel->post(
    __PACKAGE__, stop => {
      Name => $param{Id},
    }
  );
}

1;
