Commit 6df5be57 by PotatoGim

ZeroMQ Dealer to Dealer 프록시의 Perl 구현 예제 추가

parent 1202f63c
#!/usr/bin/env perl
#=============================================================================
# FILE: dealer-client.pl
# USAGE: ./dealer-client.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# DATE: 01/26/2015 08:21:49 PM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use ZMQ::LibZMQ3;
use ZMQ::Constants qw/:all/;
my $context = zmq_ctx_new();
my $socket = zmq_socket($context, ZMQ_DEALER);
my $seq = 0;
zmq_setsockopt($socket, ZMQ_IDENTITY, $$);
zmq_connect ($socket, "ipc:///tmp/test_proxy.ipc");
while (1)
{
zmq_msg_send (zmq_msg_init(), $socket, ZMQ_SNDMORE);
zmq_msg_send (zmq_msg_init_data($seq++), $socket, 0);
while (1)
{
my $msg = zmq_msg_init();
zmq_msg_recv ($msg, $socket, 0);
my $more = zmq_getsockopt($socket, ZMQ_RCVMORE);
print "RECV: ", zmq_msg_data($msg), "\n";
zmq_msg_close($msg);
last if (!$more);
}
sleep 1;
}
1;
__END__
=encoding utf8
=head1 NAME
dealer-client.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@potatogim.net>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by Ji-Hyeon Gim <potatogim@potatogim.net>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
#!/usr/bin/env perl
#=============================================================================
# FILE: dealer-proxy.pl
# USAGE: ./dealer-proxy.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# DATE: 01/26/2015 10:20:03 PM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use ZMQ::LibZMQ3;
use ZMQ::Constants qw/:all/;
my $context = zmq_ctx_new();
my $frontend = zmq_socket($context, ZMQ_ROUTER);
my $backend = zmq_socket($context, ZMQ_DEALER);
zmq_bind ($frontend, "ipc:///tmp/test_proxy.ipc");
zmq_connect ($backend, "ipc:///tmp/test.ipc");
while (1)
{
zmq_poll ([
{
socket => $frontend,
events => ZMQ_POLLIN,
callback => sub
{
while (1)
{
my $msg = zmq_msg_init();
zmq_msg_recv($msg, $frontend, 0);
my $more = zmq_getsockopt($frontend, ZMQ_RCVMORE);
print "F: ", zmq_msg_data($msg), "\n";
zmq_msg_send($msg, $backend, $more ? ZMQ_SNDMORE : 0);
zmq_msg_close($msg);
last if (!$more);
}
}
},
{
socket => $backend,
events => ZMQ_POLLIN,
callback => sub
{
while (1)
{
my $msg = zmq_msg_init();
zmq_msg_recv($msg, $backend, 0);
my $more = zmq_getsockopt($backend, ZMQ_RCVMORE);
print "B: ", zmq_msg_data($msg), "\n";
zmq_msg_send($msg, $frontend, $more ? ZMQ_SNDMORE : 0);
zmq_msg_close($msg);
last if (!$more);
}
}
}
], 5000);
}
1;
__END__
=encoding utf8
=head1 NAME
dealer-proxy.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@potatogim.net>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by Ji-Hyeon Gim <potatogim@potatogim.net>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
#!/usr/bin/env perl
#=============================================================================
# FILE: dealer-server.pl
# USAGE: ./dealer-server.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# DATE: 01/26/2015 08:21:52 PM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use ZMQ::LibZMQ3;
use ZMQ::Constants qw/:all/;
my $context = zmq_ctx_new();
my $socket = zmq_socket($context, ZMQ_DEALER);
my $seq = 0;
zmq_setsockopt($socket, ZMQ_IDENTITY, $$);
zmq_bind ($socket, "ipc:///tmp/test.ipc");
while (1)
{
zmq_poll ([
{
socket => $socket,
events => ZMQ_POLLIN,
callback => sub
{
my @msgs = ();
while (1)
{
my $msg = zmq_msg_init();
zmq_msg_recv ($msg, $socket, 0);
print "RECV: ", zmq_msg_data($msg), "\n";
last if (!zmq_getsockopt($socket, ZMQ_RCVMORE));
my $fmsg = zmq_msg_init();
zmq_msg_copy ($fmsg, $msg);
push (@msgs, $fmsg);
zmq_msg_close($msg);
}
push (@msgs, zmq_msg_init_data($seq++));
while (my $msg = shift(@msgs))
{
zmq_msg_send ($msg, $socket, scalar(@msgs) ? ZMQ_SNDMORE : 0);
}
}
}
], 5000);
print "POLL...\n";
}
1;
__END__
=encoding utf8
=head1 NAME
dealer-server.pl -
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@potatogim.net>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by Ji-Hyeon Gim <potatogim@potatogim.net>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment