Hello everybody,

I try to put the secket communication into the separate thread. I created
the basic thread class (BThread.pm) composed of init_th, run_th, close_th,
finish_th methods executed in turn when thread is started. There is also
Listener.pm module which inherits from BThread and should manage all the POE
sessions created within (see the listings - my question is below this).
#------------------ BThread.pm -----------------------
package Thrd::BThread;

use 5.0.0.8;
use strict;
use warnings;
use threads;

sub new {
  my $class = shift;
  my $self = {};
  $self->{THRD} = undef;
  bless($self, $class);
  return $self;
}

sub start {
  my $self = shift;
  $self->{THRD} = threads->create(\&_threadfunc, $self);
  return $self->{THRD};
}

sub init_th {
  return 1;
}

sub run_th {
}

sub close_th {
}

sub finish_th {
}

sub _threadfunc {
  my $obj = shift;
  if(1 == ($obj->init_th())) {
    $obj->run_th();
    $obj->finish_th();
  }
  $obj->close_th();
}

return 1;

#----------------- Listener.pm ------------------- 

package Comm::TCP::Listener;
use Thrd::BThread;
@ISA = qw(Thrd::BThread);

use 5.0.0.8;
use strict;
use warnings;

use POE;
use POE::Wheel::SocketFactory;

# debug level
my $debug=3;

sub new {
  my $class = shift;
  my $self = new Thrd::BThread;
  $self->{server} = undef;
  bless $self, $class;
  return $self;
}

sub init_th {
  my $self = shift;
  $self->{server} = new POE::Session(
      _start          => \&server_start,
      _stop           => \&server_stop
      #Eevent_success   => \&server_accept,
      #event_failure   => \&server_error,
  );

  return 1;
}

sub stop {
  my $self = shift;

 # how to stop the listen session (and all the posible client sessions) from
the other thread !!!!!!!!!!!!!!

  #wait for thread
  $self->{THRD}->join;
}

sub run_th {
  ($debug>1)&&print "POE::Kernel->run()\n";

  POE::Kernel->run();

  ($debug>1)&&print "POE::Kernel->run() returned\n";
}

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

   $heap->{listener} = new POE::Wheel::SocketFactory
       ( BindPort => 4123,
  Reuse => 'yes',
  SuccessEvent => 'event_success',
  FailureEvent => 'event_failure'
  );

    $kernel->alias_set("main_01");

   ($debug>1)&&print "Listen socket created\n";
}

sub server_stop {
    ($debug>1)&&print "Stopping listen socket\n";

    delete $_[HEAP]->{listener};
}

return 1;

#------------------- test.pl -------------------------

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

use Config;
$Config{useithreads} or die "recompile Perl with threads to run this
program.";

use lib 'C:/pcode2';
print "START  " . join(", ",@INC) . "\n";

use Comm::TCP::Listener;
my $object = Comm::TCP::Listener->new;

$object->start;

print "STARTED\n";

my $svalue = <>;

print "STOPPING....\n";

$object->stop;

print "\nSTOPPED\n";

#----------------------END--------------------------

OK, that was the code. As you can see POE main loop (run) and POE sessions
are encapsulated within thread within Listener class. This class has stop
method defined but I'm in trouble because I do not now how to send the
shutdown request to the listen POE session from the separate thread
(Listen::stop method is callled by the separate thread). For now I have
given up all the client session management.

Is anybody doing the similar things in perl (stopping the POE sessions form
the other thread) ?
Hope thre is someone who knows the solution
Anyway thanks in advance for paying attention.

-- 
pawo


Reply via email to