| 1 | package Net::Async::Blockchain::Client::Websocket; | ||||||||||||||||||||||||||||
| 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 | 4 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Net::Async::Blockchain::Client::Websocket - Async websocket Client. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | my $loop = IO::Async::Loop->new(); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | $loop->add(my $ws_source = Ryu::Async->new()); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | $loop->add( | ||||||||||||||||||||||||||||
| 19 | my $client = Net::Async::Blockchain::Client::Websocket->new( | ||||||||||||||||||||||||||||
| 20 | endpoint => "ws://127.0.0.1:8546", | ||||||||||||||||||||||||||||
| 21 | ) | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | $client->eth_subscribe('newHeads')->each(sub {print shift->{hash}})->get; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Auto load the commands as the method parameters for the websocket calls returning them asynchronously. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =over 4 | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =back | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | ▶ | no indirect; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 4 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 4 | ▶ | use JSON::MaybeUTF8 qw(encode_json_utf8 decode_json_utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | use Protocol::WebSocket::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 4 | ▶ | use Ryu::Async; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 4 | ▶ | use curry; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 4 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 4 | ▶ | use Net::Async::WebSocket::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 4 | ▶ | use parent qw(IO::Async::Notifier); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head2 source | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Create an L<Ryu::Source> instance, if it is already defined just return | ||||||||||||||||||||||||||||
| 52 | the object | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =over 4 | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =back | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | L<Ryu::Source> | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub source : method { | ||||||||||||||||||||||||||||
| 63 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
source: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | return $self->{source} //= do { | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"source"} //= do {
MC/DC0%0/2
$self->{"source"} //= do {
$self->add_child(my $ryu = "Ryu::Async"->new);
$ryu->source
}
$self->{"source"}
do {
$self->add_child(my $ryu = "Ryu::Async"->new);
$ryu->source
}
Truth table33%1/3
A //= B
A= $self->{"source"}B= do {
$self->add_child(my $ryu = "Ryu::Async"->new);
$ryu->source
}
| |||||||||||||||||||||||||||||
| 65 | 1 | $self->add_child(my $ryu = Ryu::Async->new); | |||||||||||||||||||||||||||
| 66 | 1 | $ryu->source; | |||||||||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head2 endpoint | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Websocket endpoint | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =over 4 | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =back | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | URL containing the port if needed | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | sub endpoint : method { shift->{endpoint} } | ||||||||||||||||||||||||||
|
Subroutine
endpoint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head2 latest_subscription | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Latest subscription sent from this module | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub latest_subscription : method { shift->{latest_subscription} } | ||||||||||||||||||||||||||
|
Subroutine
latest_subscription: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 websocket_client | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Create an L<Net::Async::WebSocket::Client> instance, if it is already defined just return | ||||||||||||||||||||||||||||
| 95 | the object | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =over 4 | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =back | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | L<Net::Async::WebSocket::Client> | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub websocket_client : method { | ||||||||||||||||||||||||||||
| 106 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
websocket_client: called CC=2 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | ▶ | return $self->{websocket_client} //= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"websocket_client"} //= do {
MC/DC0%0/2
$self->{"websocket_client"} //= do {
$self->add_child(my $client = "Net::Async::WebSocket::Client"->new("on_text_frame", $self->$curry::weak(sub {
my($self, undef, $frame) = @_;
$self->source->emit(decode_json_utf8($frame));
}
), "on_ping_frame", $self->$curry::weak(sub {
my($self) = @_;
$self->websocket_client->send_pong_frame->on_fail(sub {
my $error = shift();
warn "Fail to send the pong frame, error: $error";
}
)->retain;
}
), "on_closed", $self->$curry::weak(sub {
my $self = shift();
my $error = "Connection closed by peer";
$self->source->fail($error) unless $self->source->completed->is_ready;
$self->shutdown($error);
}
), "close_on_read_eof", 1));
$client->{"framebuffer"} = "Protocol::WebSocket::Frame"->new("max_payload_size", 0);
$client
}
$self->{"websocket_client"}
do {
$self->add_child(my $client = "Net::Async::WebSocket::Client"->new("on_text_frame", $self->$curry::weak(sub {
my($self, undef, $frame) = @_;
$self->source->emit(decode_json_utf8($frame));
}
), "on_ping_frame", $self->$curry::weak(sub {
my($self) = @_;
$self->websocket_client->send_pong_frame->on_fail(sub {
my $error = shift();
warn "Fail to send the pong frame, error: $error";
}
)->retain;
}
), "on_closed", $self->$curry::weak(sub {
my $self = shift();
my $error = "Connection closed by peer";
$self->source->fail($error) unless $self->source->completed->is_ready;
$self->shutdown($error);
}
), "close_on_read_eof", 1));
$client->{"framebuffer"} = "Protocol::WebSocket::Frame"->new("max_payload_size", 0);
$client
}
Truth table66%2/3
A //= B
A= $self->{"websocket_client"}B= do {
$self->add_child(my $client = "Net::Async::WebSocket::Client"->new("on_text_frame", $self->$curry::weak(sub {
my($self, undef, $frame) = @_;
$self->source->emit(decode_json_utf8($frame));
}
), "on_ping_frame", $self->$curry::weak(sub {
my($self) = @_;
$self->websocket_client->send_pong_frame->on_fail(sub {
my $error = shift();
warn "Fail to send the pong frame, error: $error";
}
)->retain;
}
), "on_closed", $self->$curry::weak(sub {
my $self = shift();
my $error = "Connection closed by peer";
$self->source->fail($error) unless $self->source->completed->is_ready;
$self->shutdown($error);
}
), "close_on_read_eof", 1));
$client->{"framebuffer"} = "Protocol::WebSocket::Frame"->new("max_payload_size", 0);
$client
}
| |||||||||||||||||||||||||||||
| 109 | $self->add_child( | ||||||||||||||||||||||||||||
| 110 | my $client = Net::Async::WebSocket::Client->new( | ||||||||||||||||||||||||||||
| 111 | on_text_frame => $self->$curry::weak( | ||||||||||||||||||||||||||||
| 112 | sub { | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my ($self, undef, $frame) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 114 | 0 | $self->source->emit(decode_json_utf8($frame)); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | ), | ||||||||||||||||||||||||||||
| 117 | on_ping_frame => $self->$curry::weak( | ||||||||||||||||||||||||||||
| 118 | sub { | ||||||||||||||||||||||||||||
| 119 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 120 | $self->websocket_client->send_pong_frame->on_fail( | ||||||||||||||||||||||||||||
| 121 | sub { | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my $error = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 123 | 0 | warn "Fail to send the pong frame, error: $error"; | |||||||||||||||||||||||||||
| 124 | 0 | })->retain(); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | ), | ||||||||||||||||||||||||||||
| 127 | on_closed => $self->$curry::weak( | ||||||||||||||||||||||||||||
| 128 | sub { | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 130 | 0 | my $error = "Connection closed by peer"; | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | $self->source->fail($error) unless $self->source->completed->is_ready; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | $self->shutdown($error); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 1 | ), | |||||||||||||||||||||||||||
| 135 | close_on_read_eof => 1, | ||||||||||||||||||||||||||||
| 136 | )); | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | $client->{framebuffer} = Protocol::WebSocket::Frame->new(max_payload_size => 0); | |||||||||||||||||||||||||||
| 139 | 1 | $client; | |||||||||||||||||||||||||||
| 140 | }; | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head2 configure | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Any additional configuration that is not described on L<IO::Async::Notifier> | ||||||||||||||||||||||||||||
| 146 | must be included and removed here. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =over 4 | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =item * C<endpoint> | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =back | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub configure { | ||||||||||||||||||||||||||||
| 157 | 2 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
configure: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 2 | for my $k (qw(endpoint on_shutdown)) { | |||||||||||||||||||||||||||
| 160 | 4 | ▶ | $self->{$k} = delete $params{$k} if exists $params{$k}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 2 | $self->SUPER::configure(%params); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 _request | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Prepare the data to be sent to the websocket and call the request | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =over 4 | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =item * C<method> | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =item * C<@_> - any parameter required by the RPC call | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =back | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | L<Ryu::Source> | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | ▶ | async sub _request { | ||||||||||||||||||||||||||
|
Subroutine
_request: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 183 | 1 | my ($self, $method, @params) = @_; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 1 | my $url = URI->new($self->endpoint); | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # this is the client request | ||||||||||||||||||||||||||||
| 188 | 1 | my $request_call = { | |||||||||||||||||||||||||||
| 189 | id => 1, | ||||||||||||||||||||||||||||
| 190 | method => $method, | ||||||||||||||||||||||||||||
| 191 | params => [@params], | ||||||||||||||||||||||||||||
| 192 | }; | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 1 | await $self->websocket_client->connect( | |||||||||||||||||||||||||||
| 195 | url => $self->endpoint, | ||||||||||||||||||||||||||||
| 196 | req => Protocol::WebSocket::Request->new(origin => $url->host), | ||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 1 | await $self->websocket_client->send_text_frame(encode_json_utf8($request_call)); | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1 | return $self->source; | |||||||||||||||||||||||||||
| 202 | 4 | } | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head2 shutdown | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | run the configured shutdown action if any | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =over 4 | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =item * C<error> error message | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =back | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | sub shutdown { ## no critic | ||||||||||||||||||||||||||||
| 217 | 0 | ▶ | my ($self, $error) = @_; | ||||||||||||||||||||||||||
|
Subroutine
shutdown: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 0 | ▶ | if (my $code = $self->{on_shutdown} || $self->can("on_shutdown")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{'on_shutdown'} || $self->can('on_shutdown')
MC/DC0%0/2
$self->{'on_shutdown'} || $self->can('on_shutdown')
$self->{'on_shutdown'}
$self->can('on_shutdown')
Truth table0%0/3
A || B
A= $self->{'on_shutdown'}B= $self->can('on_shutdown')
| |||||||||||||||||||||||||||||
| 220 | 0 | return $code->($error); | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | 0 | return undef; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head2 eth_subscribe | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Subscribe to an event | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =over 4 | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =item * C<method> | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =item * C<@_> - any parameter required by the RPC call | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =back | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =cut | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | sub eth_subscribe { | ||||||||||||||||||||||||||||
| 240 | 1 | ▶ | my ($self, $subscription) = @_; | ||||||||||||||||||||||||||
|
Subroutine
eth_subscribe: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 241 | 1 | $self->{latest_subscription} = $subscription; | |||||||||||||||||||||||||||
| 242 | 1 | return $self->_request('eth_subscribe', $subscription); | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 4 | 1; | |||||||||||||||||||||||||||