| 1 | package BoardStreams::Client::Manager; | ||
| 2 | |||
| 3 | use Mojo::Base 'Mojo::EventEmitter', -signatures; | ||
| 4 | |||
| 5 | use BoardStreams::Client::Stream; | ||
| 6 | use BoardStreams::Client::WebSockets 'make_websockets_observable'; | ||
| 7 | use BoardStreams::Client::Util 'unique_id'; | ||
| 8 | use BoardStreams::Util 'decode_json', 'eqq'; | ||
| 9 | |||
| 10 | use RxPerl::Mojo ':all'; | ||
| 11 | use Mojo::UserAgent; | ||
| 12 | |||
| 13 | no autovivification; | ||
| 14 | |||
| 15 | has url => sub { die 'missing url' }; | ||
| 16 | has ua => sub { Mojo::UserAgent->new }; | ||
| 17 | has 'connected_o'; | ||
| 18 | |||
| 19 | our $VERSION = "v0.0.36"; | ||
| 20 | |||
| 21 | sub new ($class, $url) { | ||
| 22 | my $self = $class->SUPER::new(url => $url); | ||
| 23 | |||
| 24 | # to send structured data to the webserver, next send$ | ||
| 25 | $self->{send_o} = rx_subject->new; | ||
| 26 | |||
| 27 | # long incoming messages are divided into chunks | ||
| 28 | my $ongoing_messages = {}; | ||
| 29 | |||
| 30 | # by setting pleaseBeConnected$, one initiates | ||
| 31 | # WS connection attempts | ||
| 32 | $self->{please_be_connected} = rx_subject->new; | ||
| 33 | my $websocket_o = $self->{please_be_connected}->pipe( | ||
| 34 | op_switch_map(sub ($be_connected, @) { | ||
| 35 | if ($be_connected) { | ||
| 36 | return make_websockets_observable($self->url, $self); | ||
| 37 | } | ||
| 38 | return rx_of(undef); | ||
| 39 | }), | ||
| 40 | op_distinct_until_changed(), | ||
| 41 | op_tap(sub { $ongoing_messages = {} }), | ||
| 42 | op_share(), | ||
| 43 | ); | ||
| 44 | |||
| 45 | # connect send_o to the latest websocket | ||
| 46 | $self->{send_o}->pipe( | ||
| 47 | op_with_latest_from($websocket_o), | ||
| 48 | )->subscribe(sub ($data_ws) { | ||
| 49 | my ($data, $ws) = @$data_ws; | ||
| 50 | $ws or die 'Attempted to send data while not connected'; | ||
| 51 | $ws->send({ json => $data }); | ||
| 52 | }); | ||
| 53 | |||
| 54 | # read incoming messages | ||
| 55 | $self->{incoming_o} = rx_subject->new; | ||
| 56 | $websocket_o->pipe( | ||
| 57 | op_filter(sub { defined $_ }), | ||
| 58 | op_switch_map(sub ($ws, @) { rx_from_event($ws, 'binary') }), | ||
| 59 | op_switch_map(sub ($binary_part, @) { | ||
| 60 | if (my ($bytes_prefix, $remaining) = $binary_part =~ /^\:(.+?)\:\ (.*)\z/) { | ||
| 61 | my ($identifier, $i, $is_final) = $bytes_prefix =~ /^(\S+)\ ([0-9]+)(\$)?\z/; | ||
| 62 | |||
| 63 | if ($i == 0) { | ||
| 64 | $ongoing_messages->{$identifier} = { | ||
| 65 | i => 0, | ||
| 66 | bytes => $remaining, | ||
| 67 | }; | ||
| 68 | } else { | ||
| 69 | if ($i == ($ongoing_messages->{$identifier}{i} // -2) + 1) { | ||
| 70 | delete $ongoing_messages->{$identifier}; | ||
| 71 | } | ||
| 72 | |||
| 73 | if (my $ong = $ongoing_messages->{$identifier}) { | ||
| 74 | $ong->{i} = $i; | ||
| 75 | substr($ong->{bytes}, length($ong->{bytes}), 0) = $remaining; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | if ($is_final and $i == ($ongoing_messages->{$identifier}{i} // -1)) { | ||
| 80 | my $data = decode_json $ongoing_messages->{$identifier}{bytes}; | ||
| 81 | delete $ongoing_messages->{$identifier}; | ||
| 82 | return rx_of($data); | ||
| 83 | } | ||
| 84 | |||
| 85 | return rx_EMPTY; | ||
| 86 | } | ||
| 87 | |||
| 88 | # if message is not a part | ||
| 89 | return rx_of(decode_json $binary_part); | ||
| 90 | }), | ||
| 91 | )->subscribe($self->{incoming_o}); | ||
| 92 | |||
| 93 | # incoming JSON-RPC responses | ||
| 94 | $self->{responses_o} = $self->{incoming_o}->pipe( | ||
| 95 | op_filter(sub { | ||
| 96 | eqq($_->{jsonrpc}, '2.0') | ||
| 97 | and (exists $_->{result} or exists $_->{error}) | ||
| 98 | }), | ||
| 99 | op_share(), | ||
| 100 | ); | ||
| 101 | |||
| 102 | my $config_o = $self->{incoming_o}->pipe( | ||
| 103 | op_filter(sub { eqq $_->{type}, 'config' }), | ||
| 104 | op_map(sub { $_->{data} }), | ||
| 105 | op_share(), | ||
| 106 | ); | ||
| 107 | |||
| 108 | # create connected_o to show websocket connection status (after config is received) | ||
| 109 | $self->connected_o( rx_behavior_subject->new(0) ); | ||
| 110 | rx_merge( | ||
| 111 | $config_o->pipe( | ||
| 112 | op_map_to(1), | ||
| 113 | ), | ||
| 114 | $websocket_o->pipe( | ||
| 115 | op_filter(sub { ! $_ }), | ||
| 116 | op_map_to(0), | ||
| 117 | ), | ||
| 118 | )->pipe( | ||
| 119 | op_distinct_until_changed(), | ||
| 120 | )->subscribe($self->connected_o); | ||
| 121 | $self->connected_o->subscribe(sub ($status) { $self->emit('connected', $status) }); | ||
| 122 | |||
| 123 | # ping | ||
| 124 | $config_o->pipe( | ||
| 125 | op_map(sub ($config, @) { $config->{pingInterval} - rand() }), | ||
| 126 | op_switch_map(sub ($iv, @) { | ||
| 127 | rx_timer($iv * rand(), $iv)->pipe( | ||
| 128 | op_take_until($self->connected_o->pipe(op_filter(sub { ! $_ }))), | ||
| 129 | ), | ||
| 130 | }), | ||
| 131 | )->subscribe(sub { $self->send({ type => 'ping' }) }); | ||
| 132 | |||
| 133 | # timeout | ||
| 134 | $config_o->pipe( | ||
| 135 | op_switch_map(sub ($config, @) { | ||
| 136 | $self->{incoming_o}->pipe( | ||
| 137 | op_start_with(undef), | ||
| 138 | op_switch_map(sub { | ||
| 139 | rx_of(1)->pipe( | ||
| 140 | op_delay($config->{pingInterval} + 10), | ||
| 141 | op_take_until( | ||
| 142 | $self->connected_o->pipe(op_filter(sub { !$_ })) | ||
| 143 | ), | ||
| 144 | ); | ||
| 145 | }), | ||
| 146 | ); | ||
| 147 | }), | ||
| 148 | op_with_latest_from($websocket_o), | ||
| 149 | )->subscribe(sub ($conf_ws) { | ||
| 150 | my (undef, $ws) = @$conf_ws; | ||
| 151 | $ws && $ws->finish; # TODO: or maybe some other way to close (?) | ||
| 152 | }); | ||
| 153 | |||
| 154 | return $self; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub connect ($self) { | ||
| 158 | $self->{please_be_connected}->next(1); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub disconnect ($self) { | ||
| 162 | $self->{please_be_connected}->next(0); | ||
| 163 | } | ||
| 164 | |||
| 165 | sub send ($self, $data) { | ||
| 166 | $self->connected_o->get_value or eqq($data->{type}, 'ping') | ||
| 167 | or die "Can't send data because websocket not available"; | ||
| 168 | |||
| 169 | $self->{send_o}->next($data); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub do_action ($self, $stream_name, $action_name, $payload = undef) { | ||
| 173 | $self->send({ | ||
| 174 | jsonrpc => '2.0', | ||
| 175 | method => 'doAction', | ||
| 176 | params => [$stream_name, $action_name, $payload], | ||
| 177 | }); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub do_request ($self, $stream_name, $request_name, $payload = undef) { | ||
| 181 | my $id = unique_id; | ||
| 182 | |||
| 183 | $self->send({ | ||
| 184 | jsonrpc => '2.0', | ||
| 185 | method => 'doRequest', | ||
| 186 | params => [ $stream_name, $request_name, $payload ], | ||
| 187 | id => $id, | ||
| 188 | }); | ||
| 189 | |||
| 190 | return first_value_from( | ||
| 191 | $self->{responses_o}->pipe( | ||
| 192 | op_filter(sub { eqq($_->{jsonrpc}, '2.0') and $_->{id} eq $id }), | ||
| 193 | op_take_until($self->connected_o->pipe(op_filter(sub { ! $_ }))), | ||
| 194 | op_switch_map(sub ($data, @) { | ||
| 195 | if (exists $data->{result}) { return rx_of($data->{result}) } | ||
| 196 | return rx_throw_error($data->{error}); | ||
| 197 | }), | ||
| 198 | ), | ||
| 199 | ); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub new_stream ($self, $stream_name) { | ||
| 203 | return BoardStreams::Client::Stream->new($stream_name, $self); | ||
| 204 | } | ||
| 205 | |||
| 206 | 1; |