Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Adding sessions on-the-fly to POE program

by japhy (Canon)
on Oct 26, 2004 at 18:57 UTC ( #402762=perlquestion: print w/ replies, xml ) Need Help??
japhy has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a program that is supposed to create IRC bots (using POE) each time it gets a request. The program is:
use POE; use POE::Component::Server::TCP; use POE::Component::IRC; POE::Component::Server::TCP->new( Port => '9999', Address => '0.0.0.0', ClientInput => \&client_input, ); POE::Session->create( package_states => [ main => [qw( _start irc_001 )], ], ); $poe_kernel->run(); sub _start { } sub client_input { ++$x; print "creating bot-$x\n"; POE::Component::IRC->new("bot-$x"); $_[KERNEL]->post("bot-$x", 'register', 'all'); $_[KERNEL]->post("bot-$x", "connect", { Nick => "j-bot-$x", Server => 'irc.freenode.net', Port => 6667, }); print "created bot-$x\n"; } sub irc_001 { print "CONNECTED ...\n"; }
I never see the "CONNECTED" message. What am I doing wrong?
_____________________________________________________
Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Comment on Adding sessions on-the-fly to POE program
Download Code
Re: Adding sessions on-the-fly to POE program
by duff (Vicar) on Oct 26, 2004 at 19:17 UTC

    Off hand, I'd say that your _start routine isn't yielding to your irc_001 routine. But I don't do much POE, so I could be completely wrong.

Re: Adding sessions on-the-fly to POE program
by diotalevi (Canon) on Oct 26, 2004 at 19:24 UTC
    I thought that sessions with no name/alias and no further tasks pending quit. So the one session capable of handling a irc_001 message should have already exited by the time that message is thrown. I think your _start method will have to give the session a name so it will hang around to handle the irc_001 message.
      Hmm, no, that doesn't help. The problem I'm encountering is this: if I create a POE::Component::IRC object after I've created the POE::Session object, it doesn't interact.
      _____________________________________________________
      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      I thought that sessions with no name/alias and no further tasks pending quit.

      POE::Component::IRC's register state increments the refcount on the calling session, which would also have kept it alive also. japhy's only problem was that he posted register from the POE::Component::Server::TCP session, so the events (and the refcount increment) were going there instead.

      PS: I know this is a bit after the fact and a bit off-topic, but I figure the more we talk about POE the better. The Monastery seems to have a dearth of discussion about POE, which I think is a shame.

Re: Adding sessions on-the-fly to POE program
by revdiablo (Prior) on Oct 26, 2004 at 22:10 UTC

    I could be wrong, but I think you are registering to receive the PoCo::IRC's events from within the PoCo::Server::TCP session. It boils down to whether the client_input state is attached to the PoCo::Server::TCP session or to your session. The PoCo::IRC component could be sending events to the wrong one.

    Update: I believe my post is correct. I have modified massacred your code to see which session is being called:

    use POE qw(Component::Server::TCP Component::IRC); POE::Component::Server::TCP->new( Port => '9999', Address => '0.0.0.0', ClientInput => \&client_input, ); POE::Session->create( package_states => [ main => [qw(_start)], ], ); $poe_kernel->run(); sub _start { print "My session has id: ", $_[SESSION]->ID(), "\n"; } sub client_input { print "Starting bot in session: ", $_[SESSION]->ID(), "\n"; }

    And it outputs the following:

    My session has id: 3
    Starting bot in session: 4
    

    So it looks like client_input is being called on a different session than the one you need to register with POE::Component::IRC. I would probably solve this by creating a new state in your session that handles the PoCo::IRC instantiation and registration stuff. Something like this:

    use POE qw(Component::Server::TCP Component::IRC); POE::Component::Server::TCP->new( Port => '9999', Address => '0.0.0.0', ClientInput => \&client_input, ); POE::Session->create( package_states => [ main => [qw(_start create_bot)], ], ); $poe_kernel->run(); sub create_bot { print "Starting bot in session: ", $_[SESSION]->ID(), "\n"; } sub _start { print "My session has id: ", $_[SESSION]->ID(), "\n"; $_[KERNEL]->alias_set("mine"); } sub client_input { $_[KERNEL]->post(mine => 'create_bot'); }
      Thanks, rev. This is what I figured out in between posting and your response. I'm glad I diagnosed it properly and worked around it in the same manner (so I don't think I did a kludgy job).
      _____________________________________________________
      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://402762]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-08-20 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (105 votes), past polls