POE: recurring server Alarms in a custom POE::Component

77 views Asked by At

I'm trying to set up a client-server application with perl POE.

After reading some of the POE documentation on CPAN and trying some of the examples of the cookbook, finaly reccuring Alarms, I have the following problem:

The events in my custom component are fired inside the (client) session.

package POE::Component::OXO_TCP_Server;

use strict;
use warnings;
use POE;
use POE::Filter::Reference;
use POE::Component::Server::TCP;

sub new {
    my ($proto, %args) = @_;
    my $class = ref ($proto) || $proto;

    my $send_up_intervall   = 10;
    my %default = (
      Alias         => "oxo_tcp_server",
      Address       => "localhost",
      Port          => 12345,
      ClientFilter  => "POE::Filter::Reference",
      ClientInput   => \&handle_client_input,

      ClientDisconnected => sub {
          $_[KERNEL]->yield("shutdown");
      },

      SessionParams => [ options => { debug => 1, trace => 1 } ],

      InlineStates => {
        _start => sub {
          $_[HEAP]->{next_alarm_time} = int(time()) + 1;
          $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time});
        },

        tick => sub {
          print "tick at ", time(), "\n";
          $_[HEAP]->{next_alarm_time}++;
          $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time});
        }, 
      }
    );

    # add \%args; to \%default
    my $self = POE::Component::Server::TCP->new(%default);
    return $self;
}

sub handle_client_input {...}

1;

When I send some input to the server the events

=== 8071 === 2 -> tick (from ../lib/POE/Component/OXO_TCP_Server.pm at 65) tick at 1433857644
...

are fired.

But I want them to be fired independent form the client (only by time).

How can I do that? I did not find this in the documentation or the cookbook.

To avoid overloading _start I also tried to inititalize the timer in

Started => sub {
  $_[HEAP]->{next_alarm_time} = int(time()) + 1;
  $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time});
},  

at the first view it looks like it is working because the debug says:

=== 8093 === 1 -> tick (from ../lib/POE/Component/OXO_TCP_Server.pm at 47)

but it isn't. May be it is still the wrong session. Ideas?

Thanks.

1

There are 1 answers

0
smartmeta On BEST ANSWER

I found one way my self. I added an additional session:

POE::Session->create(
  inline_states => {
        _start => sub {

          $_[HEAP]->{next_alarm_time} = int(time()) + 1;
          $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time});
        },

        tick => sub {
          print "tick at ", time(), "\n";
          $_[HEAP]->{next_alarm_time}++;
          $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time});
        },
  },
);

This way it independent form the POE::Component::Server::TCP. May be there are are other/better ways.