Our project needed persistent socket connections open as well. There is
supposed to be a standard mechanism to pass file descriptors between unix
processes, though it's bugginess level depends on your OS. There is a perl
module for this called Socket::PassAccessRights. So what you can do is
create a daemon process that just hangs round holding socket connections
open, like a socket cache basically, and passing them back and forth between
Apache processes based on some session ID or user ID or the like.

Your daemon ends up looking something like this (with lots more error
checking of course)

my %sockmap;
while (1) {
  my $clientsock = $listen->accept();
  chomp(my $sessionid = <$clientsock>);
  my $cachesock = ($sockmap{$sessionid} ||= opennewsock());
  Socket::PassAccessRights::sendfd(fileno($clientsock), fileno($cachesock));
  $clientsock->close();
}

And in your mod_perl code you do something like:

  my $serversock = IO::Socket::INET->new(Server => 'localhost', Port =>
SOCKETPOOLPORT);
  print $serversock $sessionid, "\n";
  my $Fd = Socket::PassAccessRights::recvfd(fileno($serversock));
  open(my $realsocket, "<&=$Fd");
  fcntl($realsocket, F_SETFD, 0);
  my $ofh = select($realsocket); $| = 1; select ($ofh);

If you do some experimenting, you'll get something that works, you'll also
find lots of cases that don't.

Rob

----- Original Message -----
From: "French, Shawn" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, May 30, 2002 3:53 AM
Subject: Persistent Net::Telnet Objects


> Vitals:
> Apache/1.3.20 (Win32) mod_perl/1.25_01-dev mod_ssl/2.8.4 OpenSSL/0.9.6a on
> Windows 2000 with PHP 4.21
>
> I am working on a project that requires me to have two telnet objects per
> user session opened, and accessible throughout the user's session. I have
> looked at Apache::Session and many other solutions but my problem is that
to
> keep a Net::Telnet object, I need to keep open sockets and filehandles, so
I
> cannot serialize the object and store it in a database or file.
>
> Currently I have similar code working flawlessly:
> ###
> # "startup.pl" - called when apache starts (ie. PerlRequire
> "d:/Apache/conf/startup.pl")
> ##
> use MySite::Session;
>
> ###
> # "Session.pm"
> ##
> @EXPORT = qw( %sessionHash );
> our %sessionHash;
>
> ###
> # "init_session.pl" - called IN MOD_PERL when a new session is requested
> ##
> use MySite::Session;
> $sessionHash{$session_id . "_telnetObj"} = Net::Telnet->new();
>
> ###
> # "dostuff.pl" - called IN MOD_PERL many time throughout the session
> ##
> use MySite::Session;
> my telnetObj = $sessionHash{$session_id . "_telnetObj"};
> bless (\$telnetObj, "Net::Telnet");
>
> Although this is working right now, I don't know enough [ anything? :) ]
> about Apache or mod_perl to be sure that this will work in the future.
What
> I am really concerned about is that the telnetObj will only be accessible
> from scripts run by the same child process as that which created and saved
> it.
>
> Is there a better way to do this?
>
> Thanks,
> Shawn French
>
>

Reply via email to