lib/Myriad/RPC/Message.pm

filter: Statementstmt 100% 12 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 16 / 16 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Myriad/Plugin.pm ↑ Summary lib/Myriad/Redis/Pending.pm »
1 package Myriad::RPC::Message;
2  
3 7 ▶use Myriad::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 our $VERSION = '1.001'; # VERSION
6 our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY
7  
8 =encoding utf8
9  
10 =head1 NAME
11  
12 Myriad::RPC::Message - RPC message implementation
13  
14 =head1 SYNOPSIS
15  
16 Myriad::RPC::Message->new();
17  
18 =head1 DESCRIPTION
19  
20 This class is to handle the decoding/encoding and verification of the RPC messages received
21 from the transport layer. It will throw an exception when the message is invalid or doesn't
22 match the structure.
23  
24 =cut
25  
26 7 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 7 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 ▶use JSON::MaybeUTF8 qw(:v1);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 has $rpc;
31 has $message_id;
32 has $transport_id;
33 has $who;
34 has $deadline;
35  
36 has $args;
37 has $stash;
38 has $response;
39 has $trace;
40  
41 =head2 message_id
42  
43 The ID of the message given by the requester.
44  
45 =cut
46  
47 method message_id { $message_id }
48  
49 =head2 transport_id
50  
51 The ID of the message given by Redis, to be used in xack later.
52  
53 =cut
54  
55 method transport_id { $transport_id };
56  
57 =head2 rpc
58  
59 The name of the procedure we are going to execute.
60  
61 =cut
62  
63 method rpc { $rpc }
64  
65 =head2 who
66  
67 A string that should identify the sender of the message for the transport.
68  
69 =cut
70  
71 method who { $who }
72  
73 =head2 deadline
74  
75 An epoch that represents when the timeout of the message.
76  
77 =cut
78  
79 method deadline { $deadline }
80  
81 =head2 args
82  
83 A JSON encoded string contains the argument of the procedure.
84  
85 =cut
86  
87 method args { $args }
88  
89 =head2 response
90  
91 The response to this message.
92  
93 =cut
94  
95 method response :lvalue { $response }
96  
97 =head2 stash
98  
99 information related to the request should be returned back to the requester.
100  
101 =cut
102  
103 method stash { $stash }
104  
105 =head2 trace
106  
107 Tracing information.
108  
109 =cut
110  
111 method trace { $trace }
112  
113 =head2 BUILD
114  
115 Build a new message.
116  
117 =cut
118  
119 BUILD(%message) {
120 $rpc = $message{rpc};
121 $who = $message{who};
122 $message_id = $message{message_id};
123 $transport_id = $message{transport_id};
124 $deadline = $message{deadline} || time + 30;
125 $args = $message{args} || {};
126 $response = $message{response} || {};
127 $stash = $message{stash} || {};
128 $trace = $message{trace} || {};
129 }
130  
131  
132 =head2 as_hash
133  
134 Return a simple hash with the message data, it mustn't return nested hashes
135 so it will convert them to JSON encoded strings.
136  
137 =cut
138  
139 method as_hash () {
140 my $data = {
141 rpc => $rpc,
142 who => $who,
143 message_id => $message_id,
144 deadline => $deadline,
145 };
146  
147 $self->apply_encoding($data, 'utf8');
148  
149 return $data;
150  
151 }
152  
153 =head2 from_hash
154  
155 a static method (can't be done with Object::Pad currently) that tries to
156 parse a hash and return a L<Myriad::RPC::Message>.
157  
158 the hash should comply with the format returned by C<as_hash>.
159  
160 =cut
161  
162 sub from_hash (%hash) {
163 is_valid(\%hash);
164 apply_decoding(\%hash, 'utf8');
165  
166 return Myriad::RPC::Message->new(%hash);
167 }
168  
169 =head2 as_json
170  
171 returns the message data as a JSON string.
172  
173 =cut
174  
175 method as_json () {
176 my $data = {
177 rpc => $rpc,
178 message_id => $message_id,
179 who => $who,
180 deadline => $deadline,
181 };
182  
183 # This step is not necessary but I'm too lazy to repeat the keys names.
184 $self->apply_encoding($data, 'text');
185 return encode_json_utf8($data);
186 }
187  
188 =head2 from_json
189  
190 a static method that tries to parse a JSON string
191 and return a L<Myriad::RPC::Message>.
192  
193 =cut
194  
195 sub from_json ($json) {
196 my $raw_message = decode_json_utf8($json);
197 is_valid($raw_message);
198 apply_decoding($raw_message, 'text');
199  
200 return Myriad::RPC::Message->new($raw_message->%*);
201 }
202  
203 =head2 is_valid
204  
205 A static method used in the C<from_*> methods family to make
206 sure that we have the needed information.
207  
208 =cut
209  
210 sub is_valid ($message) {
211 for my $field (qw(rpc message_id who deadline args)) {
212 Myriad::Exception::RPC::InvalidRequest->throw(reason => "$field is requried") unless exists $message->{$field};
213 }
214 }
215  
216 =head2 apply_encoding
217  
218 A helper method to enode the hash fields into JSON string.
219  
220 =cut
221  
222 method apply_encoding ($data, $encoding) {
223 my $encode = $encoding eq 'text' ? \&encode_json_text : \&encode_json_utf8;
224 try {
225 for my $field (qw(args response stash trace)) {
226 $data->{$field} = $encode->($self->$field);
227 }
228 } catch($e) {
229 Myriad::Exception::RPC::BadEncoding->throw(reason => $e);
230 }
231 }
232  
233 =head2 apply_decoding
234  
235 A helper sub to decode some field from JSON string into Perl hashes.
236  
237 =cut
238  
239 sub apply_decoding ($data, $encoding) {
240 my $decode = $encoding eq 'text' ? \&decode_json_text : \&decode_json_utf8;
241 try {
242 for my $field (qw(args response stash trace)) {
243 $data->{$field} = $decode->($data->{$field}) if $data->{$field};
244 }
245 } catch ($e) {
246 Myriad::Exception::RPC::BadEncoding->throw(reason => $e);
247 }
248 }
249  
250 =head2 passed_deadline
251  
252 Check if the message is stil relevent
253  
254 =cut
255  
256 method passed_deadline {
257 time > $deadline ? 1 : 0;
258 }
259  
260 1;
261  
262 =head1 AUTHOR
263  
264 Deriv Group Services Ltd. C<< DERIV@cpan.org >>.
265  
266 See L<Myriad/CONTRIBUTORS> for full details.
267  
268 =head1 LICENSE
269  
270 Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself.
271  
« lib/Myriad/Plugin.pm ↑ Summary lib/Myriad/Redis/Pending.pm »