I feel kind of lame promoting my own module, but this is what that would look like with
RPC::Lite, following the same sort of structure. It's not too much shorter, but it's far more concise and less prone to errors from using the lower-level calls yourself. This example would get a few lines shorter with some of the improvements I have in mind for the API (client construction and response pumping stuff). Note that this code is tested and working.
use RPC::Lite::Client;
our @clients;
our @server_info;
sub setup {
foreach my $info (@server_info) {
push @clients, RPC::Lite::Client->new({
Transport => "TCP:Host=$info->{host},Port=$info->{port}",
# default serializer is JSON, and XML is also available
});
}
}
sub query {
my ($what) = @_;
my ($responses, $result);
# anon sub will be called with any responses from the server
foreach my $client (@clients) {
$client->AsyncRequest( sub { $responses++; $result += $_[0] },
'getCount', $what );
}
# The API should support aggregating multiple clients so you can
# replace the inner foreach with one call, but it doesn't yet.
while ($responses < @clients) {
foreach my $client (@clients) {
$client->HandleResponse; # pump server for data
}
}
return $result;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.