http://qs1969.pair.com?node_id=490609

grinder has asked for the wisdom of the Perl Monks concerning the following question:

I've been trying to wrap my head around POE. I have a lengthy script that sees whether it can open a socket on various ports on various machines (over 100 connecctions all told). Actually the script is short, it just takes a long time to run, since everything is done serially.

It's just too slow... so I finally found some time to investigate POE, and I have the following code:

use strict; use POE; use POE::Component::Client::TCP; my $task = [ { name => 'www.example.com', proto => 'tcp', port => 80 }, { name => 'smtp.example.com', proto => 'tcp', port => 25 }, { name => 'ns.example.com', proto => 'udp', port => 53 }, ]; for my $t( @$task ) { next if $t->{proto} ne 'tcp'; POE::Component::Client::TCP->new( RemoteAddress => $t->{host}, RemotePort => $t->{port}, Connected => sub { print "$t->{host}:$t->{proto}:$t->{port} up\n"; $poe_kernel->yield( 'shutdown' ); }, ConnectError => sub { print "$t->{host}:$t->{proto}:$t->{por +t} down\n" }, ConnectTimeout => 2, ServerInput => sub { }, ); } $poe_kernel->run();

This actually works quite well. It takes less than a second to run if all services are up, and a smidgen more than two seconds if a service is down. Much better than the 13 seconds the old script takes.

It does, however, have a major flaw, in that it doesn't know how to deal with UDP. I use the POE::Component::Client::TCP module to deal with TCP protocols, but there's no such thing as POE::Component::Client::UDP, and I don't know how to go about using the POE building blocks to make something that would work. All I need is a true/false result to the question "could I open a socket".

The other flaw that I don't like is the klugey empty ServerInput callback. It's never called, but the object cannot be constructed if this key/value pair is missing. This may mean that I'm using POE::Component::Client::TCP for a purpose for which it was not designed. Which in turn may mean that I'm paying for a lot of startup overhead that may be avoided by using some other technique. Again, just like for UDP, I'm only interested in a yes/no answer.

I also suspect that both of these flaws are probably solved by one simple(r)? solution. Can someone set me straight?

- another intruder with the mooring in the heart of the Perl