Tibco::Rv::Timer - Tibco Timer event object


Tibco-Rv documentation Contained in the Tibco-Rv distribution.

Index


Code Index:

NAME

Top

Tibco::Rv::Timer - Tibco Timer event object

SYNOPSIS

Top

   my ( $timer ) = $rv->createTimer( interval => 10, sub
   {
      print "Timer event happened!\n";
   } );

   $timer->interval( 5 );

   $timer->onEvent( );

DESCRIPTION

Top

A Tibco::Rv::Timer fires an event after every specified interval. It is a subclass of Tibco::Rv::Event, so Event methods are available to Timers (documentation on Event methods are reproduced here for convenience).

CONSTRUCTOR

Top

$timer = new Tibco::Rv::Timer( %args )
   %args:
      queue => $queue,
      interval => $interval,
      callback => sub { ... }

Creates a Tibco::Rv::Timer. If not specified, queue defaults to the Default Queue, interval defaults to 1, and callback defaults to:

   sub { print "Timer fired\n" }

Create a Timer to cause an event to fire after every $interval seconds. The $interval can specify fractions of a second. When $queue dispatches such an event, it triggers the given callback.

METHODS

Top

$interval = $timer->interval

Returns the number of seconds in the interval between firings of the Timer's event.

$timer->interval( $interval ) (or $timer->resetTimerInterval( $interval ))

Set the interval at which the next Timer event will be dispatched.

$queue = $timer->queue

Returns the queue on which this Timer's events will be dispatched.

$callback = $timer->callback

Returns the callback code reference.

$timer->onEvent

Trigger an event directly. The event will be processed as if it was triggered via the event queue.

$timer->DESTROY

Cancels the Timer. Called automatically when $timer goes out of scope. Calling DESTROY more than once has no effect.

OVERRIDING EVENT CALLBACK

Top

As an alternative to passing in a callback function to the constructor, there is another way to handle events. You can subclass Tibco::Rv::Timer and override the onEvent method, as follows:

   package MyTimer;
   use base qw/ Tibco::Rv::Timer /;

   sub new
   {
      my ( $proto, %args ) = @_;
      my ( $self ) = $proto->SUPER::new( %args );
      # your initialization code
      return $self;
   }

   sub onEvent
   {
      my ( $self ) = @_;
      # process event here
      # $self->queue, $self->interval are available
   }

   # any other implementation code for your class

   1;

The Tibco::Rv::Event onEvent method simply calls the callback, so overriding onEvent allows you to process the event however you want, and you can just not use the callback.

The advantages of this method of handling events are: it is more object-oriented; you have access to the queue and interval via the $self accessor methods; and, you can have more elaborate processing of timed events without having to shove it all into one callback.

You can use your subclassed Timer as follows:

   use Tibco::Rv;
   use MyTimer;

   my ( $rv ) = new Tibco::Rv;
   my ( $myTimer ) = new MyTimer( interval => 4 );
   $rv->start;

AUTHOR

Top

Paul Sturm <sturm@branewave.com>


Tibco-Rv documentation Contained in the Tibco-Rv distribution.

package Tibco::Rv::Timer;
use base qw/ Tibco::Rv::Event /;


use vars qw/ $VERSION @CARP_NOT /;
$VERSION = '1.04';


@CARP_NOT = qw/ Tibco::Rv::Event /;


sub new
{
   my ( $proto ) = shift;
   my ( %params ) = ( queue => $Tibco::Rv::Queue::DEFAULT, interval => 1,
      callback => sub { print "Timer fired\n" } );
   my ( %args ) = @_;
   map { Tibco::Rv::die( Tibco::Rv::INVALID_ARG )
      unless ( exists $params{$_} ) } keys %args;
   %params = ( %params, %args );
   my ( $self ) = $proto->SUPER::new(
      queue => $params{queue}, callback => $params{callback} );

   $self->{interval} = $params{interval};

   my ( $status ) = Tibco::Rv::Event::Event_CreateTimer( $self->{id},
      $self->{queue}{id}, $self->{internal_nomsg_callback},
      $self->{interval} );
   Tibco::Rv::die( $status ) unless ( $status == Tibco::Rv::OK );

   return $self;
}


sub interval
{
   my ( $self ) = shift;
   return @_ ? $self->resetTimerInterval( @_ ) : $self->{interval};
}


sub resetTimerInterval
{
   my ( $self, $interval ) = @_;
   my ( $status ) =
      Tibco::Rv::Event::tibrvEvent_ResetTimerInterval( $self->{id}, $interval );
   Tibco::Rv::die( $status ) unless ( $status == Tibco::Rv::OK );
   return $self->{interval} = $interval;
}


1;