Here's some simple code to pass messages between peers:

The sender:

sub transport_message { my $self = shift; my $message_string = shift; my $peer = shift; my $headers = new HTTP::Headers; $headers->date(time); $headers->content_type('text/plain'); $headers->server('Net::Distributed'); my $request = new HTTP::Request ( 'POST', "http://" . $peer->address, $headers, $message_string ); unless ($self->{UserAgent}) { $self->{UserAgent} = new LWP::UserAgent; $self->{UserAgent}->agent ("Net::Distributed::Transport/$VERSION"); $self->{UserAgent}->from ($self->{email}); $self->{UserAgent}->parse_head(0); $self->{UserAgent}->timeout($self->{send_timeout}); # send_timeout normally 5 secs } my $ua = $self->{UserAgent}; my $response = $ua->request($request); if ($response->is_success) { return 1; # of course, there may be problems further down the line } else { $self->_debug("Got " . $response->status_line . " from " . $peer->ad +dress, 0); return 0; } }
The receiver:

sub accept_messages { my $self = shift; unless ($self->{daemon}) { my ($localaddr, $localport) = split /:/, $self->{address}; $self->{daemon} = new HTTP::Daemon ( LocalAddr => $localaddr, LocalPort => $localport ); ref $self->{daemon} or $self->_debug("Could not create HTTP::Daemon +$! $@", 3); $self->{daemon}->timeout($self->{timeout}) if $self->{timeout}; # normally timeout is 20 } local $SIG{PIPE} = sub { warn 'PIPE WHILE READING'; undef $self->{daemon}; }; CONNECTION: while (1) { my $conn = $self->{daemon}->accept; # blocks return undef unless $conn; my $req = $conn->get_request; unless ($req) { $self->_debug( 'Problem reading from connection: ' . $conn->reason ); next CONNECTION; } if ($req->method ne 'POST') { my $r = new HTTP::Response (405,'POST method only' ); $conn->send_response($r); $conn->close; next CONNECTION; } elsif (my $content = $req->content) { my $r = new HTTP::Response(200, 'OK'); $conn->send_response($r); $conn->close; return $content; } } }

A lot of messages are flying about, and it gets pretty busy. I keep getting 'broken pipe' signals. Of course, I can ignore them with $SIG{PIPE} = 'IGNORE', but my attempts to send and receive messages still fail with timeouts.

What am I doing wrong? I've read perlipc and understand that I am trying to write to a broken pipe, but how can I stop this happening? Ideally, I need to make sure that the server queues messages and handles them one by one.

TVM

dave hj~


In reply to HTTP::Daemon and broken pipes by dash2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.