Re: Creating and shutting down POE::Sessions

2011-05-18 Thread Rocco Caputo
On May 18, 2011, at 09:30, Gabor Szabo wrote:

> Hi,
> 
> my baby steps in the land of POE:
> 
> I have tried to setup a small application that would listen on a TCP
> port and get commands 'start' and 'stop',
> When receiving start it should create a new POE::Session to count.
> When receiving stop it should stop the counting and destroy that
> POE::Session. It seems I managed to do it but I'd be glad to receive
> comments about the code:


[code removed]

Hi, Gabor.  Please see the revised code below.  The major change was to 
eliminate the counting session.  They didn't seem necessary, and their presence 
just complicated things.

#!/usr/bin/perl

use warnings;
use strict;
use 5.010;

use POE qw(Component::Server::TCP);

my $port = 12345;
print "telnet to $port\n";

my $session_id;

POE::Component::Server::TCP->new(
  Port=> $port,
  ClientInput => \&client_input,
  ClientConnected => \&client_connected,

  # These event handlers are registered for each client connection.
  # Each client connection has its own session, heap, etc.
  InlineStates => {
count => \&client_count,
  },
);

POE::Kernel->run();
exit;

sub client_input {
  my ($kernel, $heap, $input) = @_[KERNEL, HEAP, ARG0];

  print "client input: $input\n";

  if ($input eq 'start') {
$heap->{counter} = 0;
$kernel->yield('count');
return;
  }

  if ($input eq 'stop') {
delete $heap->{counter};
return;
  }

  $heap->{client}->put("Invalid command '$input'");
  return;
}

sub client_connected {
  $_[HEAP]{client}->put("type 'start' or 'stop':");
}

sub client_count {
  my ($kernel, $heap) = @_[KERNEL, HEAP];

  # Deleted when the counter stops.
  return unless exists $heap->{counter};

  # Set when this connection is shutting down.
  return if $heap->{shutdown};

  print "Counter: ", ++$heap->{counter}, "\n";
  $kernel->delay(count => 1);
}


-- 
Rocco Caputo 



Creating and shutting down POE::Sessions

2011-05-18 Thread Gabor Szabo
Hi,

my baby steps in the land of POE:

I have tried to setup a small application that would listen on a TCP
port and get commands 'start' and 'stop',
When receiving start it should create a new POE::Session to count.
When receiving stop it should stop the counting and destroy that
POE::Session. It seems I managed to do it but I'd be glad to receive
comments about the code:

#!/usr/bin/perl
use warnings;
use strict;
use 5.010;

use POE qw(Component::Server::TCP);

my $port = 12345;
print "telnet to $port\n";

my $session_id;

POE::Component::Server::TCP->new(
  Port=> $port,
  ClientInput => \&client_input,
  ClientConnected => \&client_connected,
);

POE::Kernel->run();
exit;


sub counter {
#print "counter\n";
return if not $_[HEAP]{run};
$_[HEAP]{count}++;
#$_[HEAP]{client}->put("Count: $_[HEAP]");
print "Count: $_[HEAP]{count}\n";
$_[KERNEL]->delay('counter' => 1);
}

sub session_start {
print "session_start\n";
print "session kernel: $_[KERNEL]\n";
$_[HEAP]{count} = 0;
$_[HEAP]{run} = 1;
$_[KERNEL]->yield('counter');
print "after calling yield\n";
}
sub session_stop {
print "session_stop\n";
}

sub stop_session {
 $_[HEAP]{run} = 0;
}



sub client_input {
  my ($heap, $input) = @_[HEAP, ARG0];

  if ($input eq 'start') {
  $session_id = POE::Session->create(
inline_states => {
_start => \&session_start,
_stop  => \&session_stop,
counter  => \&counter,
stop_session  => \&stop_session,
}
  )->ID;
  print "kernel: $_[KERNEL]\n";
  print "SID: $session_id\n";

  } elsif ($input eq 'stop') {
  print "stop received\n";
  if ($session_id) {
  $_[KERNEL]->post( $_[KERNEL]->ID_id_to_session($session_id),
'stop_session' );
  $session_id = undef;
  }
  print "stop done\n";
  } else {
  $_[HEAP]{client}->put("Invalid command '$input'");
  }

  return;
}

sub client_connected {
print "connected\n";
$_[HEAP]{client}->put("type 'start' or 'stop':");
};



-- 
Gabor Szabo
http://szabgab.com/


Re: Problem to execute "sidecar" Perl scripts when compiling POE::Component::Resolver with PAR

2011-05-18 Thread Rocco Caputo
On May 17, 2011, at 11:48, Markus Jansen wrote:
>  
> Another but smaller problem is that I have not seen an easy way to tell 
> POE::Component::Resolver all the way down
> from POE::Compoent::Client::HTTP that I would like to see less than 8 
> subprocesses for the resolver job.

Hi, Markus.

The entire POE::Component::Client::HTTP stack is customizable by configuring 
your own objects and passing them in, but it looks like I've forgotten to 
document everything.

Something like this should work:

POE::Component::Client::HTTP->spawn(
  ConnectionManager => POE::Component::Client::Keepalive->new(
resolver => POE::Component::Resolver->new(
  max_resolvers => 2,
)
  )
);

-- 
Rocco Caputo 

Re: Problem to execute "sidecar" Perl scripts when compilingPOE::Component::Resolver with PAR

2011-05-18 Thread Steffen Mueller

Hi Markus,

On 05/17/2011 05:48 PM, Markus Jansen wrote:

Any hints appreciated.


these are hints at best, but check the PAR list's archive and the PAR 
repository/history for 'reusable':


Below's what I found with a svn log | grep, maybe that helps partially 
for the $^X problem.


Beware, I haven't used this feature much.

Cheers,
Steffen


svn diff -c1055 

Index: trunk/lib/pp.pm 



=== 



--- trunk/lib/pp.pm (revision 1054) 



+++ trunk/lib/pp.pm (revision 1055)
@@ -358,6 +358,18 @@

 Run the resulting packaged script after packaging it.

+=item B<--reusable>
+
+B
+
+Make the packaged executable reusable for running arbitrary, external
+Perl scripts as if they were part of the package:
+
+  pp -o myapp --reusable someapp.pl
+  ./myapp --par-options --reuse otherapp.pl
+
+The second line will run F instead of F.
+
 =item B<-S>, B<--save>

 Do not delete generated PAR file after packaging.