Hi,

I am looking for assistance in programing an TCP Socket server. Effectively,
I am looking for a skeleton to do the following:

- Server starts up, and starts x amounts of threads (working)
    - All threads connects to an remote server via TCP and idles, keeping
the connections alive (working)
- Server starts an listening socket using IO::Socket::INET (working)
- Server forks (non-blocking) when an client connects (working)
    - Client must now give an command, and that command must be passed to
the thread queues, to be executed by the thread pool as threads becomes
available *whilst the client waits on the socket*.
    - The thread will respond with content to the client via the socket
    - Client quits

The threads implementation is based on Jerry Hedden's example of thread
pooling (
http://cpansearch.perl.org/src/JDHEDDEN/threads-1.86/examples/pool_reuse.pl
).  The code via term does -exactly- what I require, and I've had no issues
what so ever to amend and change this to work as I require it to do over
term.  However, I am having significant issues combining this with a socket.
I am absolutely sure however that this is something that I am doing wrong,
more than likely, the entire application needs to be threaded and therefore
re-written from scratch.

The basic theory is that there will be x amount of sleeping threads, and the
socket will receive more connections than what there is threads.  The entire
application thus pools connections to retrieve content from remote servers.

My IO::Socket::INET has successfully started and is listening, and I fork
using
  print "Server waiting for connections\n";
  my $Client;
  while ($Client = $Socket->accept()) {
    my $pid;
    while (not defined ($pid = fork())) {
      sleep 0.5;
    }
    if ($pid) {
      close($Client);
    } else {
      $Client->autoflush(1);
      close $Socket;
      &DoClientSocket();

And following Jerry's example code, the threads work by (this is what the
client socket would need to execute as far as I can see):
    # Work loop
    do {
        # Indicate that were are ready to do work
        printf("Idle     -> %2d\n", $tid);
        $IDLE_QUEUE->enqueue($tid);

        # Wait for work from the queue
        my $work = $work_q->dequeue();

        # If no more work, exit
        last if ($work < 0);

        # Do some work while monitoring $TERM
        printf("            %2d <- Working\n", $tid);
        while (($work > 0) && ! $TERM) {
            $work -= sleep($work);
        }

        # Loop back to idle state if not told to terminate
    } while (! $TERM);

But I am really at a loss as to how to integrate the two.  The sub used by
IO::Socket::INET (even if passing the variables and hash refs as parameters,
i.e. &ClientSocket(\%work_queues, $tid)) does not successfully pass any work
to the thread pool, and the thread pool also seems to be 'idling' rather
than checking and processing work given to it. I believe this is because
IO::Socket::INET waits for client connections, and does not give the thread
pool time to look for and do it's queued work as well.

Can anyone please assist a semi newbie?  I need this to work relatively
urgently and I would prefer to rather take this off list and spend half an
hour or an hour with an individual able to assist (just don't want to sit
here spamming the lists).  At this stage, I'm even willing to pay if that's
what it needs to come down to...

Many thanks,
Chris.




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to