#!/usr/bin/perl

use lib 'lib';
use strict;
use warnings;

use Getopt::Compact;
use ZMQ::LibZMQ3;
use ZMQ::Constants qw(ZMQ_REQ);

our $MAX_MSGLEN = 255;

=head1 NAME

mcbain-zmq-client - example client for McBain APIs running with ZeroMQ

=head1 SYNOPSIS

	$ mcbain-zmq-client --frontend-port 5559 '{ "path": "GET:/status" }'

=head1 DESCRIPTION

This is a simple client script to test out L<McBain> APIs running L<with ZeroMQ|McBain::WithZeroMQ>.
It is adapted from the L<client example|http://zguide.zeromq.org/pl:rrclient> in the ZeroMQ guide.

=head1 OPTIONS

=head2 -f, --frontend-port

The port where the L<broker|mcbain-zmq-broker> is listening for requests. Defaults to 5559.

=head1 ARGUMENTS

The script expects to receive a JSON payload to forward to the API. McBain APIs running with ZeroMQ
expect the payload to hold a key called C<path> with the full name of the route/method to invoke, for
example C<GET:/math/sum>.

=cut

my $go = Getopt::Compact->new(
	name => 'mcbain-zmq-client',
	args => 'json_payload',
	struct => [
		[[qw(f frontend-port)], qq(the port where clients send requests to), ':s']
	]
);
my $opts = $go->opts;

die q!You must provide a JSON payload, for example '{ "path": "GET:/math/sum", "one": 1, "two": 5 }'!
	unless $ARGV[0];

my $port = $opts->{'frontend-port'} || 5559;

my $context = zmq_init();

# Socket to talk to server
my $requester = zmq_socket($context, ZMQ_REQ);
zmq_connect($requester, 'tcp://localhost:'.$port);

# send the request
zmq_send($requester, $ARGV[0], -1);

# receive the reply
my $size = zmq_recv($requester, my $buf, $MAX_MSGLEN);
my $string = $size >= 0 ? substr($buf, 0, $size) : '';
print $string, "\n";

=head1 BUGS AND LIMITATIONS

Please report any bugs or feature requests to
C<bug-McBain-WithZeroMQ@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=McBain-WithZeroMQ>.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

	perldoc mcbain-zmq-client

You can also look for information at:

=over 4
 
=item * RT: CPAN's request tracker
 
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=McBain-WithZeroMQ>
 
=item * AnnoCPAN: Annotated CPAN documentation
 
L<http://annocpan.org/dist/McBain-WithZeroMQ>
 
=item * CPAN Ratings
 
L<http://cpanratings.perl.org/d/McBain-WithZeroMQ>
 
=item * Search CPAN
 
L<http://search.cpan.org/dist/McBain-WithZeroMQ/>
 
=back
 
=head1 AUTHOR
 
Ido Perlmuter <ido@ido50.net>. Originally by Daisuke Maki (lestrrat)
and Alexander D'Archangel (darksuji).

=head1 SEE ALSO

L<McBain>

=head1 LICENSE AND COPYRIGHT
 
Copyright (c) 2014, Ido Perlmuter C<< ido@ido50.net >>.
 
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself, either version
5.8.1 or any later version. See L<perlartistic|perlartistic>
and L<perlgpl|perlgpl>.
 
The full text of the license can be found in the
LICENSE file included with this module.
 
=head1 DISCLAIMER OF WARRANTY
 
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.
 
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
 
=cut

1;
__END__
