Commit 1202f63c by PotatoGim

ZeroMQ 프록시 서버의 Perl 구현 예제 추가

parent 3e22da84
#!/usr/bin/perl
#=============================================================================
#
# FILE: zmq_msg_recv.pl
#
# USAGE: ./zmq_msg_recv.pl
#
# BRIEF:
#
# OPTIONS: ---
#
# AUTHOR: Ji-Hyeon Gim (http://www.potatogim.net/)
# EMAIL: potatogim@potatogim.net
# ORGANIZATION: Network Storage Team
#
# DATE: 07/25/2014 06:07:21 PM
# VERSION: 1.0
# LICENSE: Artistic License
#
# LICENSE: Artistic License 2.0
# NOTE: ---
#
#=============================================================================
use strict;
use warnings;
......
#!/usr/bin/perl
#=============================================================================
#
# FILE: zmq_msg_send.pl
#
# USAGE: ./zmq_msg_send.pl
#
# BRIEF:
#
# OPTIONS: ---
#
# AUTHOR: Ji-Hyeon Gim (http://www.potatogim.net/)
# EMAIL: potatogim@potatogim.net
# ORGANIZATION: Network Storage Team
#
# DATE: 07/25/2014 06:19:51 PM
# VERSION: 1.0
# LICENSE: Artistic License
#
# LICENSE: Artistic License 2.0
# NOTE: ---
#
#=============================================================================
use strict;
use warnings;
......
13404: BOOM!
13404: POW!
13404: ZAP!
13404: BOOM!
13404: POW!
13404: ZAP!
13404: BOOM!
13404: POW!
13404: ZAP!
#!/usr/bin/env perl
#=============================================================================
# FILE: zmq_proxyclnt_req.pl
# USAGE: ./client.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# DATE: 01/23/2015 07:37:29 PM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use ZMQ::LibZMQ3;
use ZMQ::Constants qw/:all/;
#-----------------------------------------------------------------------------
# Variables
#-----------------------------------------------------------------------------
my $context = undef;
my $zsock_req = undef;
my $zsock_rep = undef;
my $counter = 3;
#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
sub send_req
{
if (zmq_msg_send(zmq_msg_init_data("BOOM!"), $zsock_req, 0) == -1)
{
warn zmq_strerror($!);
}
printf "%d: BOOM!\n", $$;
return;
}
sub recv_req
{
my $msg = zmq_msg_init();
if (zmq_msg_recv($msg, $zsock_req, 0) == -1)
{
warn zmq_strerror($!);
}
printf "%d: %s\n", $$, zmq_msg_data($msg);
$counter--;
return;
}
sub recv_rep
{
my $msg = zmq_msg_init();
if (zmq_msg_recv($msg, $zsock_rep, 0) == -1)
{
warn zmq_strerror($!);
}
printf "%d: %s\n", $$, zmq_msg_data($msg);
if (zmq_msg_send(zmq_msg_init_data("ZAP!"), $zsock_rep, 0) == -1)
{
warn zmq_strerror($!);
}
zmq_msg_close($msg);
return;
}
$context = zmq_ctx_new();
$zsock_req = zmq_socket($context, ZMQ_REQ);
$zsock_rep = zmq_socket($context, ZMQ_REP);
if (zmq_connect($zsock_req, "tcp://127.0.0.1:9990"))
{
warn zmq_strerror($!);
}
if (zmq_connect($zsock_rep, "tcp://127.0.0.1:9991"))
{
warn zmq_strerror($!);
}
while ($counter)
{
zmq_poll ([
{
socket => $zsock_req,
events => ZMQ_POLLOUT,
callback => \&send_req,
},
{
socket => $zsock_req,
events => ZMQ_POLLIN,
callback => \&recv_req,
},
{
socket => $zsock_rep,
events => ZMQ_POLLIN,
callback => \&recv_rep,
},
], 5000);
sleep 1;
}
zmq_close($zsock_req);
zmq_close($zsock_rep);
zmq_term($context);
1;
__END__
=encoding utf8
=head1 NAME
zmq_proxyclnt_req.pl - REQ 소켓을 사용한 프록시 클라이언트 예제
=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: zmq_msg_proxysrv_router_n_dealer.pl
# USAGE: ./zmq_msg_proxysrv_router_n_dealer.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# DATE: 01/23/2015 07:37:55 PM
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use ZMQ::LibZMQ3;
use ZMQ::Constants qw/:all/;
#-----------------------------------------------------------------------------
# Variables
#-----------------------------------------------------------------------------
my $context = undef;
my $frontend = undef;
my $backend = undef;
my $capture1 = undef;
my $capture2 = undef;
#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
sub forward_msg
{
my $flag = shift;
#
# ROUTER는 아래와 같은 형식으로 메시지를 수신한다.
#
# +-------------+
# | 식별자 |
# +-------------+
# | 빈 메시지 |
# +-------------+
# | 데이터 |
# +-------------+
#
# 따라서 ZMQ_RCVMORE 플래그가 0이 될 때까지 수신이 필요하다.
#
while (1)
{
my $msg = zmq_msg_init();
my $capmsg = zmq_msg_init();
if (zmq_msg_recv($msg, ($flag ? $frontend : $backend), 0) == -1)
{
warn zmq_strerror($!);
last;
}
zmq_msg_copy($capmsg, $msg);
my $more = zmq_getsockopt(($flag ? $frontend : $backend), ZMQ_RCVMORE);
printf "[FORWARD-%s] %s(%s)\n"
, $flag ? "TO" : "FROM"
, zmq_msg_data($msg)
, join(" ", unpack("C*", zmq_msg_data($msg)));
if (defined(zmq_msg_data($msg))
&& length(zmq_msg_data($msg))
&& zmq_msg_data($msg) eq "BOOM!")
{
$msg = zmq_msg_init_data("POW!");
}
if (zmq_msg_send($msg
, ($flag ? $backend: $frontend)
, ($more ? ZMQ_SNDMORE : 0)) == -1)
{
warn zmq_strerror($!);
last;
}
if (zmq_msg_send($capmsg
, $capture2
, ($more ? ZMQ_SNDMORE : 0)) == -1)
{
warn zmq_strerror($!);
last;
}
zmq_msg_close($msg);
zmq_msg_close($capmsg);
last if (!$more);
}
return;
}
sub capture_msg
{
my $data = undef;
while (1)
{
my $msg = zmq_msg_init();
if (zmq_msg_recv($msg, $capture1, 0) == -1)
{
warn zmq_strerror($!);
last;
}
my $more = zmq_getsockopt($capture1, ZMQ_RCVMORE);
$data = zmq_msg_data($msg);
printf "[CAPTURE] %s (%s)\n"
, $data
, join(" ", unpack("C*", $data));
zmq_msg_close($msg);
last if (!$more);
}
# data를 조작하면 끝!
return;
}
#-----------------------------------------------------------------------------
# Main
#-----------------------------------------------------------------------------
$context = zmq_ctx_new();
$capture1 = zmq_socket($context, ZMQ_PAIR);
$capture2 = zmq_socket($context, ZMQ_PAIR);
$frontend = zmq_socket($context, ZMQ_ROUTER);
$backend = zmq_socket($context, ZMQ_DEALER);
if (zmq_bind ($capture1, "inproc://capture"))
{
warn zmq_strerror($!);
exit 1;
}
if (zmq_connect ($capture2, "inproc://capture"))
{
warn zmq_strerror($!);
exit 1;
}
if (zmq_bind ($frontend, "tcp://*:9990"))
{
warn zmq_strerror($!);
exit 1;
}
if (zmq_bind ($backend, "tcp://*:9991"))
{
warn zmq_strerror($!);
exit 1;
}
while (1)
{
zmq_poll([
{
socket => $capture1,
events => ZMQ_POLLIN,
callback => \&capture_msg,
},
{
socket => $frontend,
events => ZMQ_POLLIN,
callback => sub {
forward_msg (1);
}
},
{
socket => $backend,
events => ZMQ_POLLIN,
callback => sub {
forward_msg (0);
}
}
], 5000);
}
zmq_close($frontend);
zmq_close($backend);
zmq_close($capture1);
zmq_close($capture2);
zmq_term($context);
1;
__END__
=encoding utf8
=head1 NAME
zmq_msg_proxysrv_router_n_dealer.pl - ROUTER & DEALER 소켓을 사용한 프록시 서버 예제
=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