Trait/ZeroMQ.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 142 CC 5 SCAR 17.6
CC
5
Cov
0%
read_event
20.0
new_topic
6.0
_build__zmq_json
2.0
CRAP 5.8
1 package AnyMQ::Trait::ZeroMQ;
2  
3 use Any::Moose 'Role';
4  
5 use AnyEvent::ZeroMQ;
6 use AnyEvent::ZeroMQ::Publish;
7 use AnyEvent::ZeroMQ::Subscribe;
8 use AnyMQ::Topic::Trait::ZeroMQ;
9 use Carp qw/croak/;
10 use JSON;
11  
12 has 'publish_address' => ( is => 'rw', isa => 'Str' );
13 has 'subscribe_address' => ( is => 'rw', isa => 'Str' );
14  
15 has '_zmq_sub' => ( is => 'rw', lazy_build => 1, isa => 'AnyEvent::ZeroMQ::Subscribe' );
16 has '_zmq_pub' => ( is => 'rw', lazy_build => 1, isa => 'AnyEvent::ZeroMQ::Publish' );
17 has '_zmq_context' => ( is => 'rw', lazy_build => 1, isa => 'ZeroMQ::Raw::Context' );
18 has '_zmq_json' => ( is => 'rw', lazy_build => 1, isa => 'JSON' );
19  
20 # topic => [ callbacks ]
21 has 'subscriptions' => (
22 traits => ['Hash'],
23 is => 'ro',
24 isa => 'HashRef[ArrayRef[CodeRef]]',
25 default => sub { {} },
26 handles => {
27 subscription_topics => 'keys',
28 },
29 );
30  
31 sub _build__zmq_json {
32 my ($self) = @_;
33 return JSON->new->utf8;
34 }
35  
36 sub _build__zmq_context {
37 my ($self) = @_;
38  
39 my $c = ZeroMQ::Raw::Context->new( threads => 10 );
40 return $c;
41 }
42  
43 sub _build__zmq_sub {
44 my ($self) = @_;
45  
46 my $address = $self->subscribe_address
47 or croak 'subscribe_address must be defined to publish messages';
48  
49 my $sub = AnyEvent::ZeroMQ::Subscribe->new(
50 context => $self->_zmq_context,
51 connect => $address,
52 );
53  
54 $sub->on_read(sub { $self->read_event(@_) });
55  
56 return $sub;
57 }
58  
59 sub _build__zmq_pub {
60 my ($self) = @_;
61  
62 my $address = $self->publish_address
63 or croak 'publish_address must be defined to publish messages';
64  
65 my $pub = AnyEvent::ZeroMQ::Publish->new(
66 context => $self->_zmq_context,
67 connect => $address,
68 );
69  
70 return $pub;
71 }
72  
73 # called when we read some data
74 sub read_event {
75 my ($self, $subscription, $json) = @_;
76  
77 my $event = $self->_zmq_json->decode($json);
78  
79 unless ($event) {
80 warn "Got invalid JSON: $json";
81 return;
82 }
83  
84 my $topic = $event->{type};
85 unless ($topic) {
86 warn "Got event with no topic type\n";
87 }
88  
89 # call event handler callbacks
90 my $cbs = $self->subscriptions->{$topic};
91  
92 unless ($cbs && @$cbs) {
93 #warn "Got event $topic but no callbacks found\n";
94 return;
95 }
96
97 foreach my $cb (@$cbs) {
98 $cb->($event);
99 }
100 }
101  
102 # calls $cb when we receive a $topic event
103 # returns ref that can be passed to unsubscribe()
104 sub subscribe {
105 my ($self, $topic, $cb) = @_;
106  
107 # make sure subscriber bus exists
108 $self->_zmq_sub;
109
110 # undef or '' means "all topics"
111 $topic ||= '';
112  
113 $self->subscriptions->{$topic} ||= [];
114 my $cbs = $self->subscriptions->{$topic};
115  
116 push @$cbs, $cb;
117  
118 $self->update_topic_subscriptions;
119  
120 # for now just use $cb as ref, should generate some unique
121 # id in the future
122 my $ref = $cb;
123 return $ref;
124 }
125  
126 # this controls what events we are subscribed to in ZMQ
127 sub update_topic_subscriptions {
128 my ($self) = @_;
129  
130 my @topics = $self->subscription_topics;
131
132 # update list of topics we are subscribed to
133 # FIXME: use trait::topics
134 # $self->_zmq_sub->topics(\@topics);
135 }
136  
137 sub unsubscribe {
138 my ($self, $topic, $ref) = @_;
139  
140 $self->subscriptions->{$topic} ||= [];
141 my $cbs = $self->subscriptions->{$topic};
142 $cbs = [ grep { $_ != $ref } @$cbs ];
143  
144 $self->update_topic_subscriptions;
145 }
146  
147 sub new_topic {
148 my ($self, $opt) = @_;
149
150 $opt = { name => $opt } unless ref $opt;
151  
152 return AnyMQ::Topic->new_with_traits(
153 traits => [ 'ZeroMQ' ],
154 %$opt,
155 bus => $self,
156 );
157 }
158  
159 sub DEMOLISH {}; after 'DEMOLISH' => sub {
160 my $self = shift;
161 my ($igd) = @_;
162  
163 return if $igd;
164  
165 # cleanup
166 };
167  
168 1;