forked from booksbyus/zguide
/
mtrelay.pl
59 lines (41 loc) · 1.17 KB
/
mtrelay.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/perl
=pod
Multithreaded relay
NOTE: As of v0.9, ZeroMQ does not allow us to pass sockets around so as to
retain compatibility with ligzmq-2.0. This example is therefore not a precise
rendition of the official, libzmq-2.1-exploiting C example.
Based on examples/C/mtrelay.c; translated to Perl by darksuji
=cut
use strict;
use warnings;
use 5.10.0;
use threads;
use ZeroMQ qw/:all/;
sub step1 {
my ($context) = @_;
my $socket = $context->socket(ZMQ_PAIR);
$socket->connect('inproc://step2');
# Signal downstream to step 2
$socket->send('');
return;
}
sub step2 {
my ($context) = @_;
my $socket = $context->socket(ZMQ_PAIR);
$socket->connect('inproc://step3');
my $receiver = $context->socket(ZMQ_PAIR);
$receiver->bind('inproc://step2');
threads->create('step1', $context)->detach();
# Wait for signal
$receiver->recv();
# Signal downstream to step 3
$socket->send('');
return;
}
my $context = ZeroMQ::Context->new();
my $receiver = $context->socket(ZMQ_PAIR);
$receiver->bind('inproc://step3');
threads->create('step2', $context)->detach();
# Wait for signal
$receiver->recv();
say 'Test successful!';