Exobrain/Message.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Exobrain/Bus.pm ↑ Summary Exobrain.pm »
1 package Exobrain::Message;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use ZMQ::Constants qw(ZMQ_SNDMORE);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use ZMQ;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use JSON::Any;
13 use Data::Dumper;
14  
15 use Exobrain::Types qw(JSON);
16  
17 # ABSTRACT: Top-level role for all exobrain messages
18 our $VERSION = '1.08'; # VERSION
19  
20  
21 has timestamp => ( is => 'ro', isa => 'Int', default => sub { time() } );
22 has exobrain => ( is => 'rw', isa => 'Exobrain');
23 has raw => ( is => 'ro', isa => 'Ref' );
24 has source => ( is => 'ro', isa => 'Str', default => "$0" );
25 has nosend => ( is => 'ro', isa => 'Bool', default => 0 );
26 has _sent => ( is => 'rw', isa => 'Bool', default => 0 );
27 has roles => ( is => 'rw', isa => 'ArrayRef[Str]' );
28  
29 # This can be used to explicitly set the data, ignoring the
30 # payload attributes.
31 has _data => ( is => 'ro', isa => 'Ref' );
32  
33 # Many classes will provide their own way of getting summary
34 # data.
35  
36 requires qw(summary);
37  
38 # TODO: We should use JSON::XS, because we need specialised features
39 # for handling objects.
40  
41 my $json = JSON::Any->new( convert_blessed => 1 );
42  
43 # We define a dummy BUILD method so the class that consumes us doesn't
44 # need to define its own, but we can still use 'after BUILD' later.
45 # Many thanks to hobbs on #Moose for this. ;)
46  
47 sub BUILD { }
48  
49 after BUILD => sub {
50 my $self = shift;
51  
52 # Calculate our roles and attach them to the packet.
53  
54 unless ($self->roles) {
55 my @roles = map { $_->name } $self->meta->calculate_all_roles;
56  
57 # Strip prefixes
58 foreach (@roles) { s{^Exobrain::}{}; }
59  
60 $self->roles(\@roles);
61 }
62  
63 # Send our packet, unless nosend is set.
64 unless ($self->nosend) {
65 $self->send_msg;
66 }
67  
68 return;
69 };
70  
71  
72 func payload($name, @args) {
73  
74 # We need to call 'has' from the caller's perspective, so let's
75 # find out where we're being called from.
76  
77 my ($uplevel) = caller();
78 my $uphas = join('::', $uplevel, 'has');
79  
80 # Now we'll make the call, as well as adding he payload and ro
81 # attributes. We need to turn off strict 'refs' here to assure
82 # perl that really it's okay that we're using a string as a
83 # subroutine ref.
84  
85 no strict 'refs';
86 return $uphas->(
87 $name => (
88 traits => [qw(Payload)],
89 is => 'ro',
90 required => 1,
91 @args
92 )
93 );
94 }
95  
96  
97 method namespace() {
98 my $class = ref($self);
99  
100 $class =~ s/^Exobrain:://;
101  
102 return $class;
103 }
104  
105  
106 use constant PAYLOAD_CLASS => 'Exobrain::Message::Trait::Payload';
107  
108 method data() {
109 my $meta = $self->meta;
110 my @attrs = $self->meta->get_attribute_list;
111  
112 my @payloads = grep
113 { $meta->get_attribute($_)->does( PAYLOAD_CLASS ) } @attrs
114 ;
115  
116 my $data = {};
117  
118 # Walk through all our attributes and populate them into a hash.
119  
120 foreach my $attr (@payloads) {
121 $data->{ $attr } = $self->$attr;
122 }
123  
124 return $data;
125 }
126  
127  
128 method send_msg($socket?) {
129  
130 if ($self->_sent) {
131 carp "Packet of type ".$self->namespace." already sent.";
132 return;
133 }
134  
135 # If we don't have a socket, grab it from our exobrain object
136 # (if it exists)
137  
138 if (not $socket) {
139 if (my $exobrain = $self->exobrain) {
140 $socket = $exobrain->pub->_socket;
141 }
142 else {
143 croak "send_msg() is missing a socket or exobrain";
144 }
145 }
146  
147 my @frames = $self->_frames;
148  
149 $socket->send_multipart(\@frames);
150  
151 $self->_sent(1);
152  
153 return;
154 }
155  
156 # Internal method for creating the required frame structure
157  
158 method _frames() {
159 my @frames;
160  
161 push(@frames, join("_", "EXOBRAIN", $self->namespace, $self->source));
162 push(@frames, $json->encode( {
163 timestamp => $self->timestamp,
164 roles => $self->roles,
165 # TODO: Add some sort of version metadata
166 } ) );
167 push(@frames, $self->summary // "");
168 push(@frames, $json->encode( $self->data ));
169 push(@frames, $json->encode( $self->raw || {} ));
170  
171 return @frames;
172 }
173  
174  
175 method dump() {
176 my $dumpstr = "";
177  
178 foreach my $method ( qw(raw data namespace roles source summary)) {
179 my $data = $self->$method // "";
180 if (ref $data) { $data = Dumper $data };
181 $dumpstr .= "$method : $data\n";
182 }
183  
184 return $dumpstr;
185 }
186  
187  
188 package Exobrain::Message::Trait::Payload;
189 use Moose::Role;
190  
191 Moose::Util::meta_attribute_alias('Payload');
192  
193 # The payload attribute desn't actually do anything directly, but
194 # we test for it elsewhere to see if something is part of a
195 # payload that should be transmitted.
196  
197 1;
198  
199 __END__
« Exobrain/Bus.pm ↑ Summary Exobrain.pm »