line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package ZMQ::Declare; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$ZMQ::Declare::VERSION = '0.03'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
46287
|
use 5.008001; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
80
|
|
7
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
68
|
|
8
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
52
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
10282
|
use ZeroMQ (); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
require ZMQ::Declare::Constants; |
13
|
|
|
|
|
|
|
require ZMQ::Declare::Types; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
require ZMQ::Declare::ZDCF; |
16
|
|
|
|
|
|
|
require ZMQ::Declare::Application; |
17
|
|
|
|
|
|
|
require ZMQ::Declare::Device; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
require Exporter; |
20
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
21
|
|
|
|
|
|
|
our @EXPORT_OK = @ZMQ::Declare::Constants::EXPORT_OK; |
22
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
23
|
|
|
|
|
|
|
'all' => \@EXPORT_OK, |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
1; |
27
|
|
|
|
|
|
|
__END__ |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 NAME |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
ZMQ::Declare - Declarative 0MQ Infrastructure |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 SYNOPSIS |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
use ZMQ::Declare; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Read the network "topology" (who talks to whom and how) from a shared |
38
|
|
|
|
|
|
|
# file or alternatively, provide an equivalent nested Perl data structure. |
39
|
|
|
|
|
|
|
my $spec = ZMQ::Declare::ZDCF->new(tree => 'mynetwork.zdcf'); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Pick the device in your network that this code path is to implement |
42
|
|
|
|
|
|
|
my $broker = $spec->application("events")->device("event_broker"); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Set up your main loop |
45
|
|
|
|
|
|
|
$broker->implementation( sub { |
46
|
|
|
|
|
|
|
my ($runtime) = @_; |
47
|
|
|
|
|
|
|
my $input_sock = $runtime->get_socket_by_name("event_listener"); |
48
|
|
|
|
|
|
|
my $output_sock = $runtime->get_socket_by_name("work_distributor"); |
49
|
|
|
|
|
|
|
while (1) { |
50
|
|
|
|
|
|
|
... recv, send, recv, send ... |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
}); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Kick it off. This will create the actual 0MQ objects, make |
55
|
|
|
|
|
|
|
# connections, configure them, potentially fork off many processes, |
56
|
|
|
|
|
|
|
# and then hand control to your main loop with everything set up! |
57
|
|
|
|
|
|
|
$broker->run(); |
58
|
|
|
|
|
|
|
# If this was not the broker but the implementation for the event processors: |
59
|
|
|
|
|
|
|
#$worker->run(nforks => 20); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Actual, runnable examples can be found in the F<examples/> |
62
|
|
|
|
|
|
|
subdirectory of the C<ZMQ::Declare> distribution. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 DESCRIPTION |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
B<This is experimental software. Interfaces and implementation are subject to |
67
|
|
|
|
|
|
|
change. If you are interested in using this in production, please get in touch |
68
|
|
|
|
|
|
|
to gauge the current state of stability.> |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
B<One guaranteed user-visible change will be that the underlying libzmq |
71
|
|
|
|
|
|
|
wrapper will be switched from ZeroMQ.pm to ZMQ.pm (with ZMQ::LibZMQ2 or 3 as backend) |
72
|
|
|
|
|
|
|
when ZMQ.pm becomes stable.> |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
0MQ is a light-weight messaging library built on TCP. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
The Perl module C<ZMQ::Declare> aims to provide a declarative and/or |
77
|
|
|
|
|
|
|
configuration-driven way of establishing a network of distributed processes |
78
|
|
|
|
|
|
|
that collaborate to perform a certain task. |
79
|
|
|
|
|
|
|
The individual processes ("applications" in ZMQ::Declare) can each have one or |
80
|
|
|
|
|
|
|
more threads ("devices" in 0MQ speak) which talk to one another |
81
|
|
|
|
|
|
|
using 0MQ. For example, such a setup could be an entire event |
82
|
|
|
|
|
|
|
processing stack that has many different clients producing events, |
83
|
|
|
|
|
|
|
a broker, many event processing workers, and a result aggregator. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Normally using the common Perl binding, L<ZeroMQ>, requires you to |
86
|
|
|
|
|
|
|
explicitly write out the code to create 0MQ context and sockets, and |
87
|
|
|
|
|
|
|
to write the connect/bind logic for each socket. Since the use of |
88
|
|
|
|
|
|
|
0MQ commonly implies that multiple disjunct piece of code talk |
89
|
|
|
|
|
|
|
to one another, it's easy to either scatter this logic in many places |
90
|
|
|
|
|
|
|
or re-invent application-specific network configurations. |
91
|
|
|
|
|
|
|
(Which side of the connection is supposed to C<bind()> and which is |
92
|
|
|
|
|
|
|
supposed to C<connect()> again?) |
93
|
|
|
|
|
|
|
For what it's worth, I've always felt that the networked components |
94
|
|
|
|
|
|
|
that I've written were simply flying in close formation instead of |
95
|
|
|
|
|
|
|
being obvious parts of a single stack. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
C<ZMQ::Declare> is an attempt to concentrate the information about |
98
|
|
|
|
|
|
|
your I<network> of 0MQ sockets and connections in one place, to |
99
|
|
|
|
|
|
|
create and connect all sockets for you, and to allow you to focus |
100
|
|
|
|
|
|
|
on the actual implementation of the various devices that talk |
101
|
|
|
|
|
|
|
to one another using 0MQ. It turns out that I am not the only one |
102
|
|
|
|
|
|
|
who thought this would come in useful: L<http://rfc.zeromq.org> |
103
|
|
|
|
|
|
|
defines a standard device configuration data structure (it does |
104
|
|
|
|
|
|
|
not specify encoding format) called I<ZDCF> (ZeroMQ Device |
105
|
|
|
|
|
|
|
Configuration File). |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Despite the name I<ZDCF>, there's no technical need |
108
|
|
|
|
|
|
|
for this information to live in a file. C<ZMQ::Declare> implements |
109
|
|
|
|
|
|
|
ZDCF file reading/decoding (parsing) as well as some degree of |
110
|
|
|
|
|
|
|
B<validation>. This is implemented in the L<ZMQ::Declare::ZDCF> |
111
|
|
|
|
|
|
|
class which represents a single such configuration. The default |
112
|
|
|
|
|
|
|
decoder/encoder assumes JSON input/output, but is pluggable. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
The envisioned typical use of C<ZMQ::Declare> is that you write |
115
|
|
|
|
|
|
|
a single I<ZDCF> specification file or data structure that defines |
116
|
|
|
|
|
|
|
various applications and devices in your network and how they |
117
|
|
|
|
|
|
|
interact with one another. This approach means that as long as |
118
|
|
|
|
|
|
|
you have a library to handle I<ZDCF> files, |
119
|
|
|
|
|
|
|
you can write your devices in a multitude of programming languages |
120
|
|
|
|
|
|
|
and mix and match to your heart's content. For example, you might |
121
|
|
|
|
|
|
|
choose to implement your tight-loop message broker in C for performance, |
122
|
|
|
|
|
|
|
but prefer to write the parallelizable worker components in |
123
|
|
|
|
|
|
|
Perl for ease of development. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
For details on the ZDCF format, please refer to L<ZMQ::Declare::ZDCF>. |
126
|
|
|
|
|
|
|
For a domain specific language for defining ZDCF structures in pure Perl, |
127
|
|
|
|
|
|
|
see L<ZMQ::Declare::DSL>. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 SEE ALSO |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
L<ZMQ::Declare::ZDCF>, |
132
|
|
|
|
|
|
|
L<ZMQ::Declare::Application>, |
133
|
|
|
|
|
|
|
L<ZMQ::Declare::Device>, |
134
|
|
|
|
|
|
|
L<ZMQ::Declare::Device::Runtime>, |
135
|
|
|
|
|
|
|
L<ZMQ::Declare::DSL> |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
L<ZeroMQ> |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 AUTHOR |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Steffen Mueller E<lt>smueller@cpan.orgE<gt> |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Copyright (C) 2011,2012,2014 by Steffen Mueller |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
148
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.1 or, |
149
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |