For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > March 2004 > Net::Server example: why three alarm calls?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Net::Server example: why three alarm calls?
perl coder

2004-03-19, 12:55 pm

I'm looking at the example in the Net::Server manpage. I don't
understand the reason for the final alarm call. Can anyone explain this?
The alarm() in the while() loop makes sense because it resets the timer
after each input line is received. The last one (right after the loop
ends) doesn't seem to serve any purpose.

Here's the code in question:

sub process_request {
my $self = shift;
eval {

local $SIG{ALRM} = sub { die "Timed Out!\n" };
my $timeout = 30; # give the user 30 seconds to type a line

my $previous_alarm = alarm($timeout);
while( <STDIN> ){
s/\r?\n$//;
print "You said \"$_\"\r\n";
alarm($timeout);
}
alarm($previous_alarm);

};

if( $@=~/timed out/i ){
print STDOUT "Timed Out.\r\n";
return;
}

}


--
No crazy stuff in my email. ;-)
Malcolm Dew-Jones

2004-03-19, 12:55 pm

perl coder (perlcdr@mail.rumania) wrote:
: I'm looking at the example in the Net::Server manpage. I don't
: understand the reason for the final alarm call. Can anyone explain this?
: The alarm() in the while() loop makes sense because it resets the timer
: after each input line is received. The last one (right after the loop
: ends) doesn't seem to serve any purpose.

: Here's the code in question:

: sub process_request {
: my $self = shift;
: eval {

: local $SIG{ALRM} = sub { die "Timed Out!\n" };
: my $timeout = 30; # give the user 30 seconds to type a line

: my $previous_alarm = alarm($timeout);
: while( <STDIN> ){
: s/\r?\n$//;
: print "You said \"$_\"\r\n";
: alarm($timeout);
: }
: alarm($previous_alarm);

This restores the original alarm handler. You might also ask yourself
what happens if there are no lines of stdin at all.

: };

: if( $@=~/timed out/i ){
: print STDOUT "Timed Out.\r\n";
: return;
: }

: }


: --
: No crazy stuff in my email. ;-)

--
perl coder

2004-03-19, 12:55 pm

Malcolm Dew-Jones said:
>: alarm($previous_alarm);
>
> This restores the original alarm handler.


I just don't see the use. Sorry if I'm dense. :-) I'm trying to
understand and make sure I'm not missing something important...

The module itself doesn't use alarm() internally. And since each child
is forked, they all get their own independant alarm timer (I think?)

So why bother with saving the original timer... Unless maybe you're
using one of the non-forking Net::Server personalities. But the example
code uses Net::Server::PreFork...

> You might also ask yourself
> what happens if there are no lines of stdin at all.


Ok, I checked this... I commented out the final alarm() call, and it
still times out after 30 seconds. You can connect to the port and just
sit there and 30 sec later, the connection is closed.

Same thing happens if you type a line (or even just hit enter) after
connecting to the port. 30 sec after you sent your line, connection
is closed.


--
No crazy stuff in my email. ;-)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com