#!/usr/bin/perl

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

use Getopt::Compact;
use ZMQ::LibZMQ3;
use ZMQ::Constants qw(ZMQ_ROUTER ZMQ_DEALER ZMQ_POLLIN ZMQ_RCVMORE ZMQ_SNDMORE);

=head1 NAME

mcbain-zmq-broker - example broker for McBAIN APIs running with ZeroMQ

=head1 SYNOPSIS

	$ mcbain-zmq-broker --frontend-port 5559 --backend-port 5560

=head1 DESCRIPTION

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

=head1 OPTIONS

=head2 -f, --frontend-port

The port where L<clients|mcbain-zmq-clients> send requests to. Defaults to 5559.

=head2 -b, --backend-port

The port where workers (i.e. your API) is listening for requests. Defaults to 5560.

=cut

my $go = Getopt::Compact->new(
	name => 'mcbain-zmq-broker',
	struct => [
		[[qw(f frontend-port)], qq(the port where clients send requests to), ':s'],
		[[qw(b backend-port)], qq(the port where workers listen for requests), ':s']
	]
);
my $opts = $go->opts;

my $fport = $opts->{'frontend-port'} || 5559;
my $bport = $opts->{'backend-port'} || 5560;

# Prepare our context and sockets
my $context = zmq_init();
my $frontend = zmq_socket($context, ZMQ_ROUTER);
my $backend = zmq_socket($context, ZMQ_DEALER);
zmq_bind($frontend, 'tcp://*:'.$fport);
zmq_bind($backend, 'tcp://*:'.$bport);

# Initialize poll set
my @poll = (
	{
		socket => $frontend,
		events => ZMQ_POLLIN,
		callback => sub {
			while (1) {
				# Process all parts of the message
				my $message = zmq_recvmsg($frontend);
				my $more = zmq_getsockopt($frontend, ZMQ_RCVMORE);
				zmq_sendmsg($backend, $message, $more ? ZMQ_SNDMORE : 0);
				last unless $more;
			}
		}
	}, {
		socket => $backend,
		events => ZMQ_POLLIN,
		callback => sub {
			while (1) {
				# Process all parts of the message
				my $message = zmq_recvmsg($backend);
				my $more = zmq_getsockopt($backend, ZMQ_RCVMORE);
				zmq_sendmsg($frontend, $message, $more ? ZMQ_SNDMORE : 0);
				last unless $more;
			}
		}
	}
);

while (1) {
	zmq_poll(\@poll);
}

=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-broker

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__
