| 1 | package Net::Async::Blockchain::Client::ZMQ; | ||
| 2 | |||
| 3 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '0.001'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Net::Async::Blockchain::Client::ZMQ - Async ZMQ Client. | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | my $loop = IO::Async::Loop->new(); | ||
| 15 | |||
| 16 | $loop->add(my $zmq_source = Ryu::Async->new); | ||
| 17 | |||
| 18 | $loop->add( | ||
| 19 | my $zmq_client = Net::Async::Blockchain::Client::ZMQ->new( | ||
| 20 | endpoint => 'tpc://127.0.0.1:28332', | ||
| 21 | )); | ||
| 22 | |||
| 23 | $zmq_client->subscribe('hashblock')->each(sub{print shift->{hash}})->get; | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | client for the bitcoin ZMQ server | ||
| 28 | |||
| 29 | =over 4 | ||
| 30 | |||
| 31 | =back | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | 4 | ▶ | no indirect; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 36 | |||
| 37 | 4 | ▶ | use ZMQ::LibZMQ3; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 38 | use ZMQ::Constants qw(ZMQ_RCVMORE ZMQ_SUB ZMQ_SUBSCRIBE ZMQ_RCVHWM ZMQ_FD ZMQ_DONTWAIT ZMQ_RCVTIMEO); | ||
| 39 | use IO::Async::Handle; | ||
| 40 | use Socket; | ||
| 41 | use curry; | ||
| 42 | use Ryu::Async; | ||
| 43 | |||
| 44 | use parent qw(IO::Async::Notifier); | ||
| 45 | |||
| 46 | use constant { | ||
| 47 | # https://github.com/lestrrat-p5/ZMQ/blob/master/ZMQ-Constants/lib/ZMQ/Constants.pm#L128 | ||
| 48 | ZMQ_CONNECT_TIMEOUT => 79, | ||
| 49 | }; | ||
| 50 | |||
| 51 | # Since the connect timeout is not present in the LIbZMQ3 module | ||
| 52 | # we need to add it manually | ||
| 53 | ZMQ::Constants::set_sockopt_type("int" => ZMQ_CONNECT_TIMEOUT); | ||
| 54 | |||
| 55 | =head2 source | ||
| 56 | |||
| 57 | Create an L<Ryu::Source> instance, if it is already defined just return | ||
| 58 | the object | ||
| 59 | |||
| 60 | =over 4 | ||
| 61 | |||
| 62 | =back | ||
| 63 | |||
| 64 | L<Ryu::Source> | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub source : method { | ||
| 69 | my ($self) = @_; | ||
| 70 | return $self->{source} //= do { | ||
| 71 | $self->add_child(my $ryu = Ryu::Async->new); | ||
| 72 | $ryu->source; | ||
| 73 | }; | ||
| 74 | } | ||
| 75 | |||
| 76 | =head2 endpoint | ||
| 77 | |||
| 78 | TCP ZMQ endpoint | ||
| 79 | |||
| 80 | =over 4 | ||
| 81 | |||
| 82 | =back | ||
| 83 | |||
| 84 | URL containing the port if needed, in case of DNS this will | ||
| 85 | be resolved to an IP. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub endpoint : method { shift->{endpoint} } | ||
| 90 | |||
| 91 | =head2 timeout | ||
| 92 | |||
| 93 | Timeout time for connection | ||
| 94 | |||
| 95 | =over 4 | ||
| 96 | |||
| 97 | =back | ||
| 98 | |||
| 99 | Integer time in seconds | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub timeout : method { shift->{timeout} } | ||
| 104 | |||
| 105 | =head2 msg_timeout | ||
| 106 | |||
| 107 | Timeout time for received messages, this is applied when we have a bigger | ||
| 108 | duration interval between the messages. | ||
| 109 | |||
| 110 | =over 4 | ||
| 111 | |||
| 112 | =back | ||
| 113 | |||
| 114 | Integer time in seconds | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | sub msg_timeout : method { shift->{msg_timeout} } | ||
| 119 | |||
| 120 | =head2 socket_client | ||
| 121 | |||
| 122 | ZMQ socket | ||
| 123 | |||
| 124 | =over 4 | ||
| 125 | |||
| 126 | =back | ||
| 127 | |||
| 128 | return the socket for ZMQ L<ZMQ::LibZMQ3> | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub socket_client : method { | ||
| 133 | shift->{socket_client}; | ||
| 134 | } | ||
| 135 | |||
| 136 | =head2 configure | ||
| 137 | |||
| 138 | Any additional configuration that is not described on L<IO::Async::Notifier> | ||
| 139 | must be included and removed here. | ||
| 140 | |||
| 141 | If this class receive a DNS as endpoint this will be resolved on this method | ||
| 142 | to an IP address. | ||
| 143 | |||
| 144 | =over 4 | ||
| 145 | |||
| 146 | =item * C<endpoint> | ||
| 147 | |||
| 148 | =item * C<timeout> connection timeout (seconds) | ||
| 149 | |||
| 150 | =item * C<msg_timeout> msg interval timetout (seconds) | ||
| 151 | |||
| 152 | =back | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub configure { | ||
| 157 | my ($self, %params) = @_; | ||
| 158 | |||
| 159 | for my $k (qw(endpoint timeout msg_timeout on_shutdown)) { | ||
| 160 | $self->{$k} = delete $params{$k} if exists $params{$k}; | ||
| 161 | } | ||
| 162 | |||
| 163 | $self->SUPER::configure(%params); | ||
| 164 | |||
| 165 | my $uri = URI->new($self->endpoint); | ||
| 166 | my $host = $uri->host; | ||
| 167 | |||
| 168 | # Resolve DNS if needed | ||
| 169 | if ($host !~ /(\d+(\.|$)){4}/) { | ||
| 170 | my @addresses = gethostbyname($host) or die "Can't resolve @{[$host]}: $!"; | ||
| 171 | @addresses = map { inet_ntoa($_) } @addresses[4 .. $#addresses]; | ||
| 172 | |||
| 173 | my $address = $addresses[0]; | ||
| 174 | |||
| 175 | $self->{endpoint} = $self->{endpoint} =~ s/$host/$address/r; | ||
| 176 | } | ||
| 177 | |||
| 178 | } | ||
| 179 | |||
| 180 | =head2 subscribe | ||
| 181 | |||
| 182 | Connect to the ZMQ server and start the subscription | ||
| 183 | |||
| 184 | =over 4 | ||
| 185 | |||
| 186 | =item * C<subscription> subscription string name | ||
| 187 | |||
| 188 | =back | ||
| 189 | |||
| 190 | L<Ryu::Source> | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | sub subscribe { | ||
| 195 | my ($self, $subscription) = @_; | ||
| 196 | |||
| 197 | # one thread | ||
| 198 | my $ctxt = zmq_ctx_new(1); | ||
| 199 | die "zmq_ctc_new failed with $!" unless $ctxt; | ||
| 200 | |||
| 201 | my $socket = zmq_socket($ctxt, ZMQ_SUB); | ||
| 202 | $self->{socket_client} = $socket; | ||
| 203 | |||
| 204 | # zmq_setsockopt_string is not exported | ||
| 205 | ZMQ::LibZMQ3::zmq_setsockopt_string($socket, ZMQ_SUBSCRIBE, $subscription); | ||
| 206 | |||
| 207 | # set connection timeout | ||
| 208 | zmq_setsockopt($socket, ZMQ_CONNECT_TIMEOUT, $self->timeout) if $self->timeout; | ||
| 209 | |||
| 210 | my $connect_response = zmq_connect($socket, $self->endpoint); | ||
| 211 | $self->shutdown("zmq_connect failed with $!") unless $connect_response == 0; | ||
| 212 | |||
| 213 | # receive message timeout | ||
| 214 | zmq_setsockopt($socket, ZMQ_RCVTIMEO, $self->msg_timeout) if $self->msg_timeout; | ||
| 215 | |||
| 216 | # create a reader for IO::Async::Handle using the ZMQ socket file descriptor | ||
| 217 | my $fd = zmq_getsockopt($socket, ZMQ_FD); | ||
| 218 | open(my $io, '<&', $fd) or $self->shutdown("Unable to open file descriptor"); | ||
| 219 | |||
| 220 | $self->add_child( | ||
| 221 | my $handle = IO::Async::Handle->new( | ||
| 222 | read_handle => $io, | ||
| 223 | on_closed => $self->$curry::weak( | ||
| 224 | sub { | ||
| 225 | my $self = shift; | ||
| 226 | close($io); | ||
| 227 | my $error = "Connection closed by peer"; | ||
| 228 | $self->source->fail($error) unless $self->source->completed->is_ready; | ||
| 229 | $self->shutdown($error); | ||
| 230 | } | ||
| 231 | ), | ||
| 232 | on_read_ready => $self->$curry::weak( | ||
| 233 | sub { | ||
| 234 | my $self = shift; | ||
| 235 | while (my @msg = $self->_recv_multipart($socket)) { | ||
| 236 | my $hex = unpack('H*', zmq_msg_data($msg[1])); | ||
| 237 | $self->source->emit($hex); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | ), | ||
| 241 | )); | ||
| 242 | |||
| 243 | return $self->source; | ||
| 244 | } | ||
| 245 | |||
| 246 | =head2 _recv_multipart | ||
| 247 | |||
| 248 | Since each response is partial we need to join them | ||
| 249 | |||
| 250 | =over 4 | ||
| 251 | |||
| 252 | =item * C<subscription> subscription string name | ||
| 253 | |||
| 254 | =back | ||
| 255 | |||
| 256 | Multipart response array | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | sub _recv_multipart { | ||
| 261 | my ($self, $socket) = @_; | ||
| 262 | |||
| 263 | my @multipart; | ||
| 264 | |||
| 265 | push @multipart, zmq_recvmsg($socket, ZMQ_DONTWAIT); | ||
| 266 | while (zmq_getsockopt($socket, ZMQ_RCVMORE)) { | ||
| 267 | push @multipart, zmq_recvmsg($socket, ZMQ_DONTWAIT); | ||
| 268 | } | ||
| 269 | |||
| 270 | return @multipart; | ||
| 271 | } | ||
| 272 | |||
| 273 | =head2 shutdown | ||
| 274 | |||
| 275 | run the configured shutdown action if any | ||
| 276 | |||
| 277 | =over 4 | ||
| 278 | |||
| 279 | =item * C<error> error message | ||
| 280 | |||
| 281 | =back | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | sub shutdown { ## no critic | ||
| 286 | my ($self, $error) = @_; | ||
| 287 | |||
| 288 | if (my $code = $self->{on_shutdown} || $self->can("on_shutdown")) { | ||
| 289 | return $code->($error); | ||
| 290 | } | ||
| 291 | return undef; | ||
| 292 | } | ||
| 293 | |||
| 294 | 1; | ||