| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2011-2020 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Tangence::Stream 0.33; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use v5.26; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use Tangence::Constants; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 12 | ▶ | use Tangence::Message; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # A map from request codes to method names | ||||||||||||||||||||||||||||
| 18 | # Can't use => operator because it would quote the barewords on the left, but | ||||||||||||||||||||||||||||
| 19 | # we want them as constants | ||||||||||||||||||||||||||||
| 20 | 12 | my %REQ_METHOD = ( | |||||||||||||||||||||||||||
| 21 | MSG_CALL, 'handle_request_CALL', | ||||||||||||||||||||||||||||
| 22 | MSG_SUBSCRIBE, 'handle_request_SUBSCRIBE', | ||||||||||||||||||||||||||||
| 23 | MSG_UNSUBSCRIBE, 'handle_request_UNSUBSCRIBE', | ||||||||||||||||||||||||||||
| 24 | MSG_EVENT, 'handle_request_EVENT', | ||||||||||||||||||||||||||||
| 25 | MSG_GETPROP, 'handle_request_GETPROP', | ||||||||||||||||||||||||||||
| 26 | MSG_GETPROPELEM, 'handle_request_GETPROPELEM', | ||||||||||||||||||||||||||||
| 27 | MSG_SETPROP, 'handle_request_SETPROP', | ||||||||||||||||||||||||||||
| 28 | MSG_WATCH, 'handle_request_WATCH', | ||||||||||||||||||||||||||||
| 29 | MSG_UNWATCH, 'handle_request_UNWATCH', | ||||||||||||||||||||||||||||
| 30 | MSG_UPDATE, 'handle_request_UPDATE', | ||||||||||||||||||||||||||||
| 31 | MSG_DESTROY, 'handle_request_DESTROY', | ||||||||||||||||||||||||||||
| 32 | MSG_WATCH_CUSR, 'handle_request_WATCH_CUSR', | ||||||||||||||||||||||||||||
| 33 | MSG_CUSR_NEXT, 'handle_request_CUSR_NEXT', | ||||||||||||||||||||||||||||
| 34 | MSG_CUSR_DESTROY, 'handle_request_CUSR_DESTROY', | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | MSG_GETROOT, 'handle_request_GETROOT', | ||||||||||||||||||||||||||||
| 37 | MSG_GETREGISTRY, 'handle_request_GETREGISTRY', | ||||||||||||||||||||||||||||
| 38 | MSG_INIT, 'handle_request_INIT', | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 NAME | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | C<Tangence::Stream> - base class for C<Tangence> stream-handling mixins | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | This module provides a base for L<Tangence::Client> and L<Tangence::Server>. | ||||||||||||||||||||||||||||
| 48 | It is not intended to be used directly by C<Tangence> implementation code. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | It provides the basic layer of message serialisation, deserialisation, and | ||||||||||||||||||||||||||||
| 51 | dispatching to methods that would handle the messages. Higher level classes | ||||||||||||||||||||||||||||
| 52 | are used to wrap this functionallity, and provide implementations of methods | ||||||||||||||||||||||||||||
| 53 | to handle the messages received. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | When a message is received, it will be passed to a method whose name depends | ||||||||||||||||||||||||||||
| 56 | on the code of message received. The name will be C<handle_request_>, followed | ||||||||||||||||||||||||||||
| 57 | by the name of the message code, in uppercase; for example | ||||||||||||||||||||||||||||
| 58 | C<handle_request_CALL>. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 REQUIRED METHODS | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | The following methods are required to be implemented by some class using this | ||||||||||||||||||||||||||||
| 65 | mixin. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head2 tangence_write | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | $stream->tangence_write( $data ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Write bytes of data to the connected peer. C<$data> will be a plain perl | ||||||||||||||||||||||||||||
| 74 | string. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head2 handle_request_$CODE | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | $stream->handle_request_$CODE( $token, $message ); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Invoked on receipt of a given message code. C<$token> will be some opaque perl | ||||||||||||||||||||||||||||
| 83 | scalar value, and C<$message> will be an instance of L<Tangence::Message>. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | The value of the token has no particular meaning, other than to be passed to | ||||||||||||||||||||||||||||
| 86 | the C<respond> method. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head1 PROVIDED METHODS | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | The following methods are provided by this mixin. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Accessors for Tangence::Message decoupling | ||||||||||||||||||||||||||||
| 97 | 12 | our $BUILTIN_STRUCTIDS; | |||||||||||||||||||||||||||
| 98 | 12 | our %BUILTIN_ID2STRUCT; | |||||||||||||||||||||||||||
| 99 | 12 | our %ALWAYS_PEER_HASSTRUCT; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub message_state | ||||||||||||||||||||||||||||
| 102 | 231 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
message_state: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | shift->{message_state} ||= { | ||||||||||||||||||||||||||||
| 104 | 231 | ▶ | id2struct => { %BUILTIN_ID2STRUCT }, | ||||||||||||||||||||||||||
|
Condition100%2/2
+(shift())->{"message_state"} ||= {"id2struct", {%Tangence::Stream::BUILTIN_ID2STRUCT}, "next_structid", $Tangence::Stream::BUILTIN_STRUCTIDS, "next_cursorid", 1}
MC/DC100%1/1
+(shift())->{"message_state"} ||= {"id2struct", {%Tangence::Stream::BUILTIN_ID2STRUCT}, "next_structid", $Tangence::Stream::BUILTIN_STRUCTIDS, "next_cursorid", 1}
+(shift())->{"message_state"}
Truth table100%2/2
A
A= +(shift())->{"message_state"}
| |||||||||||||||||||||||||||||
| 105 | next_structid => $BUILTIN_STRUCTIDS, | ||||||||||||||||||||||||||||
| 106 | next_cursorid => 1, | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 101 | ▶ | sub peer_hasobj { shift->{peer_hasobj} ||= {} } | ||||||||||||||||||||||||||
|
Subroutine
peer_hasobj: called CC=2 SCAR=6.9
Pod
undocumented
Condition100%2/2
+(shift())->{"peer_hasobj"} ||= {}
MC/DC100%1/1
+(shift())->{"peer_hasobj"} ||= {}
+(shift())->{"peer_hasobj"}
Truth table100%2/2
A
A= +(shift())->{"peer_hasobj"}
| |||||||||||||||||||||||||||||
| 111 | 121 | ▶ | sub peer_hasclass { shift->{peer_hasclass} ||= {} } | ||||||||||||||||||||||||||
|
Subroutine
peer_hasclass: called CC=2 SCAR=6.9
Pod
undocumented
Condition100%2/2
+(shift())->{"peer_hasclass"} ||= {}
MC/DC100%1/1
+(shift())->{"peer_hasclass"} ||= {}
+(shift())->{"peer_hasclass"}
Truth table100%2/2
A
A= +(shift())->{"peer_hasclass"}
| |||||||||||||||||||||||||||||
| 112 | 326 | ▶ | sub peer_hasstruct { shift->{peer_hasstruct} ||= { %ALWAYS_PEER_HASSTRUCT } } | ||||||||||||||||||||||||||
|
Subroutine
peer_hasstruct: called CC=2 SCAR=6.9
Pod
undocumented
Condition100%2/2
+(shift())->{"peer_hasstruct"} ||= {%Tangence::Stream::ALWAYS_PEER_HASSTRUCT}
MC/DC100%1/1
+(shift())->{"peer_hasstruct"} ||= {%Tangence::Stream::ALWAYS_PEER_HASSTRUCT}
+(shift())->{"peer_hasstruct"}
Truth table100%2/2
A
A= +(shift())->{"peer_hasstruct"}
| |||||||||||||||||||||||||||||
| 113 | 25 | ▶ | sub peer_hascursor { shift->{peer_hascursor} ||= {} } | ||||||||||||||||||||||||||
|
Subroutine
peer_hascursor: called CC=2 SCAR=6.9
Pod
undocumented
Condition100%2/2
+(shift())->{"peer_hascursor"} ||= {}
MC/DC100%1/1
+(shift())->{"peer_hascursor"} ||= {}
+(shift())->{"peer_hascursor"}
Truth table100%2/2
A
A= +(shift())->{"peer_hascursor"}
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub identity | ||||||||||||||||||||||||||||
| 116 | 28 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
identity: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | 28 | my $self = shift; | |||||||||||||||||||||||||||
| 118 | 28 | ▶ | $self->{identity} = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 28 | return $self->{identity}; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head2 tangence_closed | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | $stream->tangence_closed; | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Informs the object that the underlying connection has now been closed, and any | ||||||||||||||||||||||||||||
| 127 | attachments to C<Tangence::Object> or C<Tangence::ObjectProxy> instances | ||||||||||||||||||||||||||||
| 128 | should now be dropped. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub tangence_closed | ||||||||||||||||||||||||||||
| 133 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
tangence_closed: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | foreach my $id ( keys %{ $self->peer_hasobj } ) { | |||||||||||||||||||||||||||
| 137 | 2 | my $obj = $self->get_by_id( $id ); | |||||||||||||||||||||||||||
| 138 | 2 | $obj->unsubscribe_event( "destroy", delete $self->peer_hasobj->{$id} ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 tangence_readfrom | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | $stream->tangence_readfrom( $buffer ); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Informs the object that more data has been read from the underlying connection | ||||||||||||||||||||||||||||
| 147 | stream. Whole messages will be removed from the beginning of the C<$buffer>, | ||||||||||||||||||||||||||||
| 148 | which should be passed as a direct scalar (because it will be modified). This | ||||||||||||||||||||||||||||
| 149 | method will invoke the required C<handle_request_*> methods. Any bytes | ||||||||||||||||||||||||||||
| 150 | remaining that form the start of a partial message will be left in the buffer. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub tangence_readfrom | ||||||||||||||||||||||||||||
| 155 | 295 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
tangence_readfrom: called CC=9 SCAR=23.3
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | 295 | my $self = shift; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 295 | ▶ | while( length $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 295 | ▶ | last unless length $_[0] >= 5; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 295 | my ( $code, $len ) = unpack( "CN", $_[0] ); | |||||||||||||||||||||||||||
| 161 | 295 | ▶ | last unless length $_[0] >= 5 + $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 295 | substr( $_[0], 0, 5, "" ); | |||||||||||||||||||||||||||
| 164 | 295 | my $payload = substr( $_[0], 0, $len, "" ); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 295 | my $message = Tangence::Message->new( $self, $code, $payload ); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 295 | ▶ | if( $code < 0x80 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 147 | push @{ $self->{request_queue} }, undef; | |||||||||||||||||||||||||||
| 170 | 147 | my $token = \$self->{request_queue}[-1]; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 147 | ▶ | if( !$self->minor_version and $code != MSG_INIT ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->minor_version and $code != 127
MC/DC0%0/2
not $self->minor_version and $code != 127
not $self->minor_version
$code != 127
Truth table66%2/3
not(A) and B
A= not $self->minor_versionB= $code != 127
| |||||||||||||||||||||||||||||
| 173 | 0 | $self->respondERROR( $token, "Cannot accept any message except MSG_INIT before MSG_INIT" ); | |||||||||||||||||||||||||||
| 174 | 0 | next; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 147 | ▶ | if( my $method = $REQ_METHOD{$code} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 147 | ▶ | if( $self->can( $method ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 147 | $self->$method( $token, $message ); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | else { | ||||||||||||||||||||||||||||
| 182 | 0 | $self->respondERROR( $token, sprintf( "Cannot respond to request code 0x%02x", $code ) ); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | else { | ||||||||||||||||||||||||||||
| 186 | 0 | $self->respondERROR( $token, sprintf( "Unrecognised request code 0x%02x", $code ) ); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | else { | ||||||||||||||||||||||||||||
| 190 | 148 | my $on_response = shift @{ $self->{responder_queue} }; | |||||||||||||||||||||||||||
| 191 | 148 | $on_response->( $message ); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 2 | ▶ | sub object_destroyed ( $self, $obj, $startsub, $donesub ) | ||||||||||||||||||||||||||
|
Subroutine
object_destroyed: called CC=1 SCAR=0.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 197 | { | ||||||||||||||||||||||||||||
| 198 | 2 | $startsub->(); | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 2 | my $objid = $obj->id; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 2 | delete $self->peer_hasobj->{$objid}; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | $self->request( | ||||||||||||||||||||||||||||
| 205 | request => Tangence::Message->new( $self, MSG_DESTROY ) | ||||||||||||||||||||||||||||
| 206 | ->pack_int( $objid ), | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 2 | ▶ | on_response => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=15.0
| |||||||||||||||||||||||||||||
| 209 | 2 | my ( $message ) = @_; | |||||||||||||||||||||||||||
| 210 | 2 | my $code = $message->code; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 2 | ▶ | if( $code == MSG_OK ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 213 | 2 | $donesub->(); | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | elsif( $code == MSG_ERROR ) { | ||||||||||||||||||||||||||||
| 216 | 0 | my $msg = $message->unpack_str(); | |||||||||||||||||||||||||||
| 217 | 0 | print STDERR "Cannot get connection $self to destroy object $objid - error $msg\n"; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | else { | ||||||||||||||||||||||||||||
| 220 | 0 | print STDERR "Cannot get connection $self to destroy object $objid - code $code\n"; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | }, | ||||||||||||||||||||||||||||
| 223 | 2 | ); | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head2 request | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | $stream->request( %args ); | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Serialises a message object to pass to the C<tangence_write> method, then | ||||||||||||||||||||||||||||
| 231 | enqueues a response handler to be invoked when a reply arrives. Takes the | ||||||||||||||||||||||||||||
| 232 | following named arguments: | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =over 8 | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =item request => Tangence::Message | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | The message body | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =item on_response => CODE | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | CODE reference to the callback to be invoked when a response to the message is | ||||||||||||||||||||||||||||
| 243 | received. It will be passed the response message: | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | $on_response->( $message ); | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =back | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =head2 request (non-void) | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | $response = await $stream->request( request => $request ); | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | When called in non-void context, this method returns a L<Future> that will | ||||||||||||||||||||||||||||
| 254 | yield the response instead. In this case it should not be given an | ||||||||||||||||||||||||||||
| 255 | C<on_response> callback. | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | In this form, a C<MSG_ERROR> response will automatically turn into a failed | ||||||||||||||||||||||||||||
| 258 | Future; the subsequent C<then> or C<on_done> code will not have to handle this | ||||||||||||||||||||||||||||
| 259 | case. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =cut | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 148 | ▶ | sub request ( $self, %args ) | ||||||||||||||||||||||||||
|
Subroutine
request: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 264 | { | ||||||||||||||||||||||||||||
| 265 | 148 | ▶ | my $request = $args{request} or croak "Expected 'request'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 148 | my $f; | |||||||||||||||||||||||||||
| 268 | my $on_response; | ||||||||||||||||||||||||||||
| 269 | 148 | ▶ | if( defined wantarray ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 68 | ▶ | $args{on_response} and croak "TODO: Can't take 'on_response' and return a Future"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 68 | $f = $self->new_future; | |||||||||||||||||||||||||||
| 273 | 68 | ▶ | $on_response = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 274 | 68 | my ( $response ) = @_; | |||||||||||||||||||||||||||
| 275 | 68 | ▶ | if( $response->code == MSG_ERROR ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 7 | $f->fail( $response->unpack_str(), tangence => ); | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | else { | ||||||||||||||||||||||||||||
| 279 | 61 | $f->done( $response ); | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | 68 | }; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | else { | ||||||||||||||||||||||||||||
| 284 | 80 | ▶ | $on_response = $args{on_response} or croak "Expected 'on_response'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 148 | push @{ $self->{responder_queue} }, $on_response; | |||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 148 | my $payload = $request->payload; | |||||||||||||||||||||||||||
| 290 | 148 | $self->tangence_write( | |||||||||||||||||||||||||||
| 291 | pack "CNa*", $request->code, length($payload), $payload | ||||||||||||||||||||||||||||
| 292 | ); | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 148 | return $f; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =head2 respond | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | $stream->respond( $token, $message ); | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | Serialises a message object to be sent to the C<tangence_write> method. The | ||||||||||||||||||||||||||||
| 302 | C<$token> value that was passed to the C<handle_request_> method ensures that | ||||||||||||||||||||||||||||
| 303 | it is sent at the correct position in the stream, to allow the peer to pair it | ||||||||||||||||||||||||||||
| 304 | with the corresponding request. | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =cut | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 147 | ▶ | sub respond ( $self, $token, $message ) | ||||||||||||||||||||||||||
|
Subroutine
respond: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 309 | { | ||||||||||||||||||||||||||||
| 310 | 147 | my $payload = $message->payload; | |||||||||||||||||||||||||||
| 311 | 147 | my $response = pack "CNa*", $message->code, length($payload), $payload; | |||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 147 | $$token = $response; | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 147 | ▶ | while( defined $self->{request_queue}[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 147 | $self->tangence_write( shift @{ $self->{request_queue} } ); | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 0 | ▶ | sub respondERROR ( $self, $token, $string ) | ||||||||||||||||||||||||||
|
Subroutine
respondERROR: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 321 | { | ||||||||||||||||||||||||||||
| 322 | 0 | $self->respond( $token, Tangence::Message->new( $self, MSG_ERROR ) | |||||||||||||||||||||||||||
| 323 | ->pack_str( $string ) | ||||||||||||||||||||||||||||
| 324 | ); | ||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =head2 minor_version | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | $ver = $stream->minor_version; | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | Returns the minor version negotiated by the C<MSG_INIT> / C<MSG_INITED> | ||||||||||||||||||||||||||||
| 332 | initial message handshake. | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =cut | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | sub minor_version | ||||||||||||||||||||||||||||
| 337 | 203 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
minor_version: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 338 | 203 | my $self = shift; | |||||||||||||||||||||||||||
| 339 | 203 | ▶ | ( $self->{tangence_minor_version} ) = @_ if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | 203 | ▶ | return $self->{tangence_minor_version} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"tangence_minor_version"} // 0
MC/DC100%1/1
$self->{"tangence_minor_version"} // 0
$self->{"tangence_minor_version"}
Truth table100%2/2
A
A= $self->{"tangence_minor_version"}
| |||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | # Some (internal) methods that control new protocol features | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | # wire protocol uses typed smash data | ||||||||||||||||||||||||||||
| 346 | 31 | ▶ | sub _ver_can_typed_smash { shift->minor_version >= 4 } | ||||||||||||||||||||||||||
|
Subroutine
_ver_can_typed_smash: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | # wire protocol understands FLOAT* types | ||||||||||||||||||||||||||||
| 349 | 19 | ▶ | sub _ver_can_num_float { shift->minor_version >= 4 } | ||||||||||||||||||||||||||
|
Subroutine
_ver_can_num_float: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | =cut | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 12 | 0x55AA; | |||||||||||||||||||||||||||