Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: POE method problem (building applications with POE)

by Orchid_NL (Novice)
on Jan 01, 2005 at 21:02 UTC ( [id://418695]=note: print w/replies, xml ) Need Help??


in reply to Re: POE method problem (building applications with POE)
in thread POE method problem (building applications with POE)

Let me start with a "Happy New Year!" to all perlmonks.
Your debugging print tip did the trick: it showed that $wheel_id (ARG1) is empty.
POE::Wheel::SocketFactory uses ARG3 not ARG1 for the wheels id.

Because of this I had to change the way the wheel id is passed from subroutine 'start' to 'factory_succes' to 'client_input'.
I used the HEAP variable (is this the correct way?)

Problem is now that it works with one telnet session. As soon a second telnet session is started the script breaks connection with telnet session one.
As far I can see this is because POE::Wheel::SocketFactory reuses the wheel id from telnet session one for telnet session two.
I don't know why it does this because according to the documentation it should start a new 'wheel'.
Can somebody help me with this problem?

In the second part of the article concurrent telnet sessions work by using POE::Component::Server:TCP.
But I'm a bit stubborn and want to get the basics right before moving on...

Here is the code I modified:

#!/usr/bin/perl -w use warnings; use strict; use Carp qw(carp croak); use POE qw( Wheel::SocketFactory Driver::SysRW Wheel::ReadWrite); { package POE::Filter::SimpleQueryString; use Carp qw(carp croak); sub new { my $class = shift; my $self = bless {}, $class; return $self; } sub get { my $self = shift; my $buffer = shift; my @chunks; foreach my $record (@$buffer) { $record =~ s/\x0d\x0a$//; my @pairs = split(/&/, $record); my %chunk; foreach my $pair (@pairs) { my ($key, $value) = split(/=/, $pair, 2); if(defined $chunk{$key}) { if(ref $chunk{$key} eq 'ARRAY') { push @{ $chunk{$key} }, $value; } else { $chunk{$key} = [ $chunk{$key}, $value ], } } else { $chunk{$key} = $value; } } push @chunks, \%chunk; } return \@chunks; } sub put { my $self = shift; my $records = shift; print "$self\n$records\n"; my @raw; foreach my $record (@$records) { my @chunks; foreach my $key (sort keys %$record) { if(ref $record->{$key}) { if(ref $record->{$key} eq 'ARRAY') { foreach my $value ( @{ $record->{$key} } ) { push @chunks, $key."=".$value; } } else { carp __PACKAGE__." cannot handle data of type ".ref $record->{$key}; } } else { push @chunks, $key."=".$record->{$key}; } } push @raw, join('&',@chunks)."\x0d\x0a"; } return \@raw; } } sub start { $_[HEAP]->{factory} = POE::Wheel::SocketFactory->new( BindAddress + => '127.0.0.1', BindPort + => '31337', SuccessEvent + => 'factory_success', FailureEvent + => 'fatal_error', SocketProtocol + => 'tcp', Reuse + => 'on', ); } sub factory_success { my ($handle, $wheel_id) = @_[ARG0, ARG3]; my $temp_rw_id = POE::Wheel::ReadWrite->new( Handle => $handle, Driver => POE::Dri +ver::SysRW->new(), Filter => POE::Fil +ter::SimpleQueryString->new(), InputEvent => 'client_ +input', ); $_[HEAP]->{clients}->{$wheel_id} = $temp_rw_id; + $_[HEAP]->{current_client}->{$temp_rw_id->ID} = $wheel_id; print "factory_success called, creating wheel $wheel_id for handle $ +handle | ( ".$temp_rw_id->ID." )\n"; } sub client_input { my ($input, $wheel_id) = @_[ARG0, ARG1]; my $factory_id = $_[HEAP]->{current_client}->{$wheel_id}; use Data::Dumper; print Dumper $input; print "wheel_id is: $wheel_id\n"; print "factory_id is: $factory_id\n"; $_[HEAP]->{clients}->{$factory_id}->put($input); } POE::Session->create( inline_states => { _start => \&start, factory_success => \&factory_ +success, client_input => \&client_i +nput, client_error => \&client_e +rror, fatal_error => sub { die +"A fatal error occurred" }, _stop => sub {}, }, ); POE::Kernel->run();

PS I mailed the editors of perl.com and asked them to fix the download link in the article.

Replies are listed 'Best First'.
Re^3: POE method problem (building applications with POE)
by RMGir (Prior) on Jan 02, 2005 at 23:46 UTC
    I don't have POE installed on this PC, but I'm pretty sure the reason you're losing your connection when you get a new one is your factory_success method.

    When you create a connection, you store it as:

    $_[HEAP]->{clients}->{$wheel_id} = $temp_rw_id;
    But $wheel_id is the same from one connection to another, so when the next connection comes along, you overwrite the last reference to the previous Wheel:RW, and it gets destroyed.

    Try keeping a hash of active wheels, something like

    $_[HEAP]->{clients}->{$wheel_id}->{$temp_rw_id}=undef;
    instead. When you're done with a wheel, just "delete" it from that hash.

    This way, you're keeping a live reference to all active wheels.

    Hope this helps!


    Mike
      Hi Mike, thanks for your reply.

      The nice editors from perl.com fixed the download link after I mailed them.
      The downloaded code clears up alot of things.
      The wheel-id from POE::Wheel::SocketFactory is indeed the same throughout the whole program run.
      Only the wheel-id in POE::Wheel:ReadWrite is different for each connection. This is the only wheel-id that needs to be stored (and destroyed).

      I changed subroutines 'factory_success' and 'client_input' accordingly and it works now!

      The code in the perl.com article and the code made available through the download link are different from each other which is a confusing for a POE newbie like me ;-)

      sub factory_success { my $handle = $_[ARG0]; my $wheel_id = POE::Wheel::ReadWrite->new( Handle => $handle, Driver => POE::Dri +ver::SysRW->new(), Filter => POE::Fil +ter::SimpleQueryString->new(), InputEvent => 'client_ +input', ); $_[HEAP]->{clients}->{$wheel_id->ID} = $wheel_id; } sub client_input { my ($input, $wheel_id) = @_[ARG0, ARG1]; use Data::Dumper; print Dumper $input; $_[HEAP]->{clients}->{$wheel_id}->put($input); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://418695]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-19 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found