forked from booksbyus/zguide
/
syncpub.pl
40 lines (30 loc) · 841 Bytes
/
syncpub.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
#!/usr/bin/perl
=pod
Synchronized publisher
Based on examples/C/syncpub.c; translated to Perl by darksuji
=cut
use strict;
use warnings;
use 5.10.0;
use ZeroMQ qw/:all/;
# We wait for 10 subscribers
use constant SUBSCRIBERS_EXPECTED => 10;
my $context = ZeroMQ::Context->new();
# Socket to talk to clients
my $publisher = $context->socket(ZMQ_PUB);
$publisher->bind('tcp://*:5561');
# Socket to receive signals
my $syncservice = $context->socket(ZMQ_REP);
$syncservice->bind('tcp://*:5562');
# Get synchronization from subscribers
for (1 .. SUBSCRIBERS_EXPECTED) {
# - wait for synchronization request
$syncservice->recv();
# - send synchronization reply
$syncservice->send('');
}
# Now broadcast exactly 1M updates followed by END
for (1 .. 1_000_000) {
$publisher->send('Rhubarb');
}
$publisher->send('END');