forked from booksbyus/zguide
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the rest of the Chapter Two examples
Also, modify examples previously assuming the ability to pass sockets around, which is not actually in ZeroMQ at the time of this writing.
- Loading branch information
Showing
8 changed files
with
282 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,34 @@ | ||
No-one has translated the durapub example into Perl yet. Be the first to create | ||
durapub in Perl and get one free Internet! If you're the author of the Perl | ||
binding, this is a great way to get people to use 0MQ in Perl. | ||
#!/usr/bin/perl | ||
=pod | ||
To submit a new translation email it to zeromq-dev@lists.zeromq.org. Please: | ||
Publisher for durable subscriber | ||
* Stick to identical functionality and naming used in examples so that readers | ||
can easily compare languages. | ||
* You MUST place your name as author in the examples so readers can contact you. | ||
* You MUST state in the email that you license your code under the MIT/X11 | ||
license. | ||
Based on examples/C/durapub.c; translated to Perl by darksuji | ||
Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. | ||
=cut | ||
|
||
use strict; | ||
use warnings; | ||
use 5.10.0; | ||
|
||
use ZeroMQ qw/:all/; | ||
|
||
my $context = ZeroMQ::Context->new(); | ||
|
||
# Subscriber tells us when it's ready here | ||
my $sync = $context->socket(ZMQ_PULL); | ||
$sync->bind('tcp://*:5564'); | ||
|
||
# We send updates via this socket | ||
my $publisher = $context->socket(ZMQ_PUB); | ||
$publisher->bind('tcp://*:5565'); | ||
|
||
# Wait for synchronization request | ||
$sync->recv(); | ||
|
||
# Now broadcast exactly 10 updates with pause | ||
for (my $update_count = 0; $update_count < 10; ++$update_count) { | ||
$publisher->send("Update $update_count"); | ||
sleep (1); | ||
} | ||
$publisher->send('END'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,40 @@ | ||
No-one has translated the durapub2 example into Perl yet. Be the first to create | ||
durapub2 in Perl and get one free Internet! If you're the author of the Perl | ||
binding, this is a great way to get people to use 0MQ in Perl. | ||
#!/usr/bin/perl | ||
=pod | ||
To submit a new translation email it to zeromq-dev@lists.zeromq.org. Please: | ||
Publisher for durable subscriber | ||
* Stick to identical functionality and naming used in examples so that readers | ||
can easily compare languages. | ||
* You MUST place your name as author in the examples so readers can contact you. | ||
* You MUST state in the email that you license your code under the MIT/X11 | ||
license. | ||
Based on examples/C/durapub2.c; translated to Perl by darksuji | ||
Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. | ||
=cut | ||
|
||
use strict; | ||
use warnings; | ||
use 5.10.0; | ||
|
||
use ZeroMQ qw/:all/; | ||
|
||
my $context = ZeroMQ::Context->new(); | ||
|
||
# Subscriber tells us when it's ready here | ||
my $sync = $context->socket(ZMQ_PULL); | ||
$sync->bind('tcp://*:5564'); | ||
|
||
# We send updates via this socket | ||
my $publisher = $context->socket(ZMQ_PUB); | ||
$publisher->bind('tcp://*:5565'); | ||
|
||
# Prevent publisher overflow from slow subscribers | ||
$publisher->setsockopt(ZMQ_HWM, 1) | ||
|
||
# Specify swap space in bytes, this covers all subscribers | ||
$publisher->setsockopt(ZMQ_SWAP, 25_000_000); | ||
|
||
# Wait for synchronization request | ||
$sync->recv(); | ||
|
||
# Now broadcast exactly 10 updates with pause | ||
for (my $update_count = 0; $update_count < 10; ++$update_count) { | ||
$publisher->send("Update $update_count"); | ||
sleep (1); | ||
} | ||
$publisher->send('END'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,34 @@ | ||
No-one has translated the durasub example into Perl yet. Be the first to create | ||
durasub in Perl and get one free Internet! If you're the author of the Perl | ||
binding, this is a great way to get people to use 0MQ in Perl. | ||
#!/usr/bin/perl | ||
=pod | ||
To submit a new translation email it to zeromq-dev@lists.zeromq.org. Please: | ||
Durable subscriber | ||
* Stick to identical functionality and naming used in examples so that readers | ||
can easily compare languages. | ||
* You MUST place your name as author in the examples so readers can contact you. | ||
* You MUST state in the email that you license your code under the MIT/X11 | ||
license. | ||
Based on examples/C/durasub.c; translated to Perl by darksuji | ||
Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. | ||
=cut | ||
|
||
use strict; | ||
use warnings; | ||
use 5.10.0; | ||
|
||
use ZeroMQ qw/:all/; | ||
|
||
my $context = ZeroMQ::Context->new(); | ||
|
||
# Connect our subscriber socket | ||
my $subscriber = $context->socket(ZMQ_SUB); | ||
$subscriber->setsockopt(ZMQ_IDENTITY, 'Hello'); | ||
$subscriber->setsockopt(ZMQ_SUBSCRIBE, ''); | ||
$subscriber->connect('tcp://localhost:5565'); | ||
|
||
# Synchronize with publisher | ||
my $sync = $context->socket(ZMQ_PUSH); | ||
$sync->connect('tcp://localhost:5564'); | ||
$sync->send(''); | ||
|
||
# Get updates, expect random Ctrl-C death | ||
while (1) { | ||
my $string = $subscriber->recv()->data; | ||
say $string; | ||
last if $string eq 'END'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,59 @@ | ||
No-one has translated the mtrelay example into Perl yet. Be the first to create | ||
mtrelay in Perl and get one free Internet! If you're the author of the Perl | ||
binding, this is a great way to get people to use 0MQ in Perl. | ||
#!/usr/bin/perl | ||
=pod | ||
To submit a new translation email it to zeromq-dev@lists.zeromq.org. Please: | ||
Multithreaded relay | ||
* Stick to identical functionality and naming used in examples so that readers | ||
can easily compare languages. | ||
* You MUST place your name as author in the examples so readers can contact you. | ||
* You MUST state in the email that you license your code under the MIT/X11 | ||
license. | ||
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. | ||
Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. | ||
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!'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
No-one has translated the psenvpub example into Perl yet. Be the first to create | ||
psenvpub in Perl and get one free Internet! If you're the author of the Perl | ||
binding, this is a great way to get people to use 0MQ in Perl. | ||
#!/usr/bin/perl | ||
=pod | ||
To submit a new translation email it to zeromq-dev@lists.zeromq.org. Please: | ||
Pubsub envelope publisher | ||
* Stick to identical functionality and naming used in examples so that readers | ||
can easily compare languages. | ||
* You MUST place your name as author in the examples so readers can contact you. | ||
* You MUST state in the email that you license your code under the MIT/X11 | ||
license. | ||
Based on examples/C/psenvpub.c; translated to Perl by darksuji | ||
Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. | ||
=cut | ||
|
||
use strict; | ||
use warnings; | ||
use 5.10.0; | ||
|
||
use ZeroMQ qw/:all/; | ||
|
||
# Prepare our context and publisher | ||
my $context = ZeroMQ::Context->new(); | ||
my $publisher = $context->socket(ZMQ_PUB); | ||
$publisher->bind('tcp://*:5563'); | ||
|
||
while (1) { | ||
# Write two messages, each with an envelope and content | ||
$publisher->send('A', ZMQ_SNDMORE); | ||
$publisher->send("We don't want to see this"); | ||
$publisher->send('B', ZMQ_SNDMORE); | ||
$publisher->send("We would like to see this"); | ||
sleep (1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
No-one has translated the psenvsub example into Perl yet. Be the first to create | ||
psenvsub in Perl and get one free Internet! If you're the author of the Perl | ||
binding, this is a great way to get people to use 0MQ in Perl. | ||
#!/usr/bin/perl | ||
=pod | ||
To submit a new translation email it to zeromq-dev@lists.zeromq.org. Please: | ||
Pubsub envelope subscriber | ||
* Stick to identical functionality and naming used in examples so that readers | ||
can easily compare languages. | ||
* You MUST place your name as author in the examples so readers can contact you. | ||
* You MUST state in the email that you license your code under the MIT/X11 | ||
license. | ||
Based on examples/C/psenvsub.c; translated to Perl by darksuji | ||
Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. | ||
=cut | ||
|
||
use strict; | ||
use warnings; | ||
use 5.10.0; | ||
|
||
use ZeroMQ qw/:all/; | ||
|
||
# Prepare our context and subscriber | ||
my $context = ZeroMQ::Context->new(); | ||
my $subscriber = $context->socket(ZMQ_SUB); | ||
$subscriber->connect('tcp://localhost:5563'); | ||
$subscriber->setsockopt(ZMQ_SUBSCRIBE, 'B'); | ||
|
||
while (1) { | ||
# Read envelope with address | ||
my $address = $subscriber->recv()->data; | ||
# Read message contents | ||
my $contents = $subscriber->recv()->data; | ||
printf("[%s] %s\n", $address, $contents); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,40 @@ | ||
No-one has translated the syncpub example into Perl yet. Be the first to create | ||
syncpub in Perl and get one free Internet! If you're the author of the Perl | ||
binding, this is a great way to get people to use 0MQ in Perl. | ||
#!/usr/bin/perl | ||
=pod | ||
To submit a new translation email it to zeromq-dev@lists.zeromq.org. Please: | ||
Synchronized publisher | ||
* Stick to identical functionality and naming used in examples so that readers | ||
can easily compare languages. | ||
* You MUST place your name as author in the examples so readers can contact you. | ||
* You MUST state in the email that you license your code under the MIT/X11 | ||
license. | ||
Based on examples/C/syncpub.c; translated to Perl by darksuji | ||
Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. | ||
=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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,43 @@ | ||
No-one has translated the syncsub example into Perl yet. Be the first to create | ||
syncsub in Perl and get one free Internet! If you're the author of the Perl | ||
binding, this is a great way to get people to use 0MQ in Perl. | ||
#!/usr/bin/perl | ||
=pod | ||
To submit a new translation email it to zeromq-dev@lists.zeromq.org. Please: | ||
Synchronized subscriber | ||
* Stick to identical functionality and naming used in examples so that readers | ||
can easily compare languages. | ||
* You MUST place your name as author in the examples so readers can contact you. | ||
* You MUST state in the email that you license your code under the MIT/X11 | ||
license. | ||
Based on examples/C/syncsub.c; translated to Perl by darksuji | ||
Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev. | ||
=cut | ||
|
||
use strict; | ||
use warnings; | ||
use 5.10.0; | ||
|
||
use ZeroMQ qw/:all/; | ||
|
||
my $context = ZeroMQ::Context->new(); | ||
|
||
# First, connect our subscriber socket | ||
my $subscriber = $context->socket(ZMQ_SUB); | ||
$subscriber->connect('tcp://localhost:5561'); | ||
$subscriber->setsockopt(ZMQ_SUBSCRIBE, ''); | ||
|
||
# 0MQ is so fast, we need to wait a while... | ||
sleep (1); | ||
|
||
# Second, synchronize with publisher | ||
my $syncclient = $context->socket(ZMQ_REQ); | ||
$syncclient->connect('tcp://localhost:5562'); | ||
|
||
# - send a synchronization request | ||
$syncclient->send(''); | ||
|
||
# - wait for synchronization reply | ||
$syncclient->recv(); | ||
|
||
# Third, get our updates and report how many we got | ||
my $update_count = 0; | ||
while (1) { | ||
my $string = $subscriber->recv()->data; | ||
last if $string eq 'END'; | ||
++$update_count; | ||
} | ||
say "Received $update_count updates"; |