Topic/Trait/ZeroMQ.pm untested

filter: Statementstmt 0.0% 0 / 32 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 52 CC 1 SCAR 6.9
CC
1
Cov
0%
BUILD
2.0
DEMOLISH
2.0
BEGIN
2.0
CRAP 2.0
1 package AnyMQ::Topic::Trait::ZeroMQ;
2  
3 use Any::Moose 'Role';
4 use AnyEvent::ZeroMQ::Subscribe;
5  
6 has 'publisher_only' => (is => "ro", isa => "Bool");
7 has 'debug' => (is => "rw", isa => "Bool", default => 0);
8  
9 # we do not want to notify subscribers of events being published on
10 # our publisher bus, otherwise we will get duplicates if we are
11 # subscribed to those events.
12 has 'publish_to_queues' => (is => 'rw', default => 0);
13  
14 # opaque reference to our read callback
15 has 'read_callback_ref' => (is => 'rw');
16  
17 sub BUILD {}; after 'BUILD' => sub {
18 my $self = shift;
19  
20 return if $self->publisher_only;
21 return unless $self->name;
22 return unless $self->bus->subscribe_address;
23  
24 # subscribe to events
25 my $cb_ref = $self->bus->subscribe($self->name, sub {
26 my ($event) = @_;
27 $self->append_to_queues($event);
28 });
29  
30 $self->read_callback_ref($cb_ref);
31 };
32  
33 sub DEMOLISH {}; after 'DEMOLISH' => sub {
34 my ($self, $igd) = @_;
35  
36 # program shutting down, whatever
37 return if $igd;
38  
39 # uninstall our callback
40 $self->bus->unsubscribe($self->name, $self->read_callback_ref)
41 if $self->read_callback_ref;
42 };
43  
44 # send events to ZeroMQ server
45 after 'dispatch_messages' => sub {
46 my ($self, @events) = @_;
47
48 # if this bus is just listening for events, we don't need to
49 # publish the event to the zeromq server, just call callbacks
50 return unless $self->bus->publish_address;
51  
52 my $pub = $self->bus->_zmq_pub;
53  
54 # encode events as JSON and transmit them
55 foreach my $event (@events) {
56 my $json = $event;
57 if (ref $json) {
58 $json = $self->bus->_zmq_json->encode($event);
59 }
60  
61 $self->bus->_zmq_pub->publish($json);
62 }
63 };
64  
65 1;