You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Bill Moseley <mo...@hank.org> on 2000/12/22 18:50:22 UTC

[OT] Anyone good with IPC?

Sorry for the way OT post, but this list seems to have the smartest, most
experienced, most friendly perl programmers around  -- and this question on
other perl lists failed to get any bites.

Would someone be willing to offer a bit of help off list?

I'm trying to get two programs talking in an HTTP-like protocol through a
unix pipe.  I'm first trying to get it to work between two perl programs
(below), but in the end, the "client" will be a C program (and that's a
different nut to crack).

The goal is to add a "filter" feature to the C program, where you register
some external program (called a server, in this example, since it will be
answering requests) and the C program starts the server, and then feeds
requests over and over leaving the server in memory.

A simple filter might be something that converts to lower case, or converts
text dates to a timestamp.  The C program (client) sends headers and some
content, and the filter (server) returns headers and some content.  But
it's a "Keep Alive" connection, so another request can be sent without
closing the pipe.

This approach seems simple -- at least for someone writing the filter
program.  Just read and print (non-buffered).  It's probably not very
portable -- I'd expect to fail on Windows.  (Are there better methods?)

Anyway, this is the sample code I was trying, but was not getting anywhere.
Seems like IO::Select::can_read() returns true and then I can read back the
first header, but then can_read() never returns true again.

I really need to be able to read and parse the headers, then read
Content-Length: bytes since the content can be of varying length.

> cat client.pl

#!/usr/local/bin/perl -w
use strict;

use IPC::Open2;
use IO::Select;
use IO::Handle;

my ( $rh, $wh );

my $pid = open2($rh, $wh, './server.pl');
$pid || die "Failed to open";

my $read = IO::Select->new( $rh );

$rh->autoflush;
$wh->autoflush;

for (1..2) {
    print "\n>>$0: Sending Headers:$_\n";

    print $wh "Header-number: $_\n",
              "Content-type: perl/test\n",
              "Header: test\n\n";


    # Now read the response
    while ( 1 ) {

        my $fh;
        
        if ( ($fh) = $read->can_read(0) ) {
            print "Can read!\n";

            my $buffer = <$rh>;
            #$fh->read( $buffer, 1024 );

            last unless $buffer;

            print "<<$0: Read $buffer";
        } else {
            print "Can't read sleeping...\n";
            sleep 1;
        }
    }
    print "$0: All done!\n";
}
            
    

lii@mardy:~ > cat server.pl
#!/usr/local/bin/perl -w
use strict;

$|=1;

warn "In $0 pid=$$\n";

while (1) {
    my @headers = ();
    while ( <> ) {
        chomp;
        if ( $_ ) {
            warn "$0: Read '$_'\n";
            
            push @headers, $_;
        } else {
            for ( @headers ) {
                warn "$0: Sending $_\n";
                print $_,"\n";
            }
            print "\n";
            last;
        }
    }
}
    

Bill Moseley
mailto:moseley@hank.org

Re: [OT] Anyone good with IPC?

Posted by Matt Sergeant <ma...@sergeant.org>.
On Fri, 22 Dec 2000, Bill Moseley wrote:

> The goal is to add a "filter" feature to the C program, where you register
> some external program (called a server, in this example, since it will be
> answering requests) and the C program starts the server, and then feeds
> requests over and over leaving the server in memory.

[snip]

use POE.

-- 
<Matt/>

    /||    ** Director and CTO **
   //||    **  AxKit.com Ltd   **  ** XML Application Serving **
  // ||    ** http://axkit.org **  ** XSLT, XPathScript, XSP  **
 // \\| // **     Personal Web Site: http://sergeant.org/     **
     \\//
     //\\
    //  \\