« blib/lib/Lemonldap/NG/Common/PSGI.pm
↑ Summary
blib/lib/Lemonldap/NG/Common/Apache/Session/REST.pm »
| 1 | package Lemonldap::NG::Common::MessageBroker::Web; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use IO::Socket::INET; | ||
| 5 | use IO::Socket::SSL; | ||
| 6 | use IO::Select; | ||
| 7 | use Lemonldap::NG::Common::FormEncode; | ||
| 8 | use Lemonldap::NG::Common::UserAgent; | ||
| 9 | use JSON; | ||
| 10 | use Protocol::WebSocket::Client; | ||
| 11 | |||
| 12 | our $VERSION = '2.22.0'; | ||
| 13 | |||
| 14 | use constant DEFAULTWS => 'localhost:8080'; | ||
| 15 | our $pr = '::MessageBroker::Web:'; | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my ( $class, $conf, $logger ) = @_; | ||
| 19 | my $args = $conf->{messageBrokerOptions} // {}; | ||
| 20 | my $ssl = ''; | ||
| 21 | unless ( $args->{server} ) { | ||
| 22 | $args->{server} = DEFAULTWS; | ||
| 23 | $logger->info("$pr no server given"); | ||
| 24 | } | ||
| 25 | elsif ( $args->{server} =~ m#^(?:(?:http|ws)(s)?://)?(.+?)/*$# ) { | ||
| 26 | $args->{server} = $2; | ||
| 27 | $ssl = 's' if $1; | ||
| 28 | } | ||
| 29 | else { | ||
| 30 | $logger->error("$pr unparsable server '$args->{server}'"); | ||
| 31 | $args->{server} = DEFAULTWS; | ||
| 32 | } | ||
| 33 | $logger->debug("$pr using server $args->{server}"); | ||
| 34 | my $self = bless { | ||
| 35 | logger => $logger, | ||
| 36 | server => $args->{server}, | ||
| 37 | messages => {}, | ||
| 38 | ssl => $ssl, | ||
| 39 | token => $args->{token}, | ||
| 40 | ua => $args->{ua} || Lemonldap::NG::Common::UserAgent->new($conf), | ||
| 41 | }, $class; | ||
| 42 | $self->{ua}->env_proxy(); | ||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub publish { | ||
| 47 | my ( $self, $channel, $msg ) = @_; | ||
| 48 | die 'Not a hash msg' unless ref $msg eq 'HASH'; | ||
| 49 | $msg->{channel} = $channel; | ||
| 50 | my $j = eval { JSON::to_json($msg) }; | ||
| 51 | if ($@) { | ||
| 52 | $self->logger->error("$pr message error: $@"); | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | my $req = HTTP::Request->new( | ||
| 56 | POST => "http$self->{ssl}://$self->{server}/publish", | ||
| 57 | [ | ||
| 58 | 'Content-Length' => length($j), | ||
| 59 | ( | ||
| 60 | $self->{token} | ||
| 61 | ? ( Authorization => "Bearer $self->{token}" ) | ||
| 62 | : () | ||
| 63 | ) | ||
| 64 | ], | ||
| 65 | $j | ||
| 66 | ); | ||
| 67 | my $resp = $self->ua->request($req); | ||
| 68 | $resp->is_success | ||
| 69 | ? ( $self->logger->debug("$pr publish $msg->{action}") ) | ||
| 70 | : ( $self->logger->error( "$pr publish error: " . $resp->status_line ) ); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub subscribe { | ||
| 74 | my ( $self, $channel ) = @_; | ||
| 75 | return | ||
| 76 | if $self->{channels} | ||
| 77 | and $self->{channels} =~ /^(?:.*,)?$channel(?:,.*)?$/; | ||
| 78 | $self->{channels} = | ||
| 79 | $self->{channels} ? "$self->{channels},$channel" : $channel; | ||
| 80 | $self->{messages}{$channel} = []; | ||
| 81 | $self->logger->debug("$pr subscribe to $self->{channels}"); | ||
| 82 | my $sock = $self->_connect; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub getNextMessage { | ||
| 86 | my ( $self, $channel ) = @_; | ||
| 87 | return undef | ||
| 88 | unless $self->{ws} and defined $self->{messages}{$channel}; | ||
| 89 | return shift( @{ $self->{messages}{$channel} } ) | ||
| 90 | if @{ $self->{messages}{$channel} }; | ||
| 91 | $self->_read_socket; | ||
| 92 | return shift( @{ $self->{messages}{$channel} } ) | ||
| 93 | if @{ $self->{messages}{$channel} }; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub waitForNextMessage { | ||
| 97 | my ( $self, $channel ) = @_; | ||
| 98 | return undef | ||
| 99 | unless $self->{messages}{$channel}; | ||
| 100 | my $res; | ||
| 101 | do { | ||
| 102 | $res = $self->getNextMessage($channel); | ||
| 103 | sleep 1 unless $res; | ||
| 104 | } while ( !$res ); | ||
| 105 | return $res; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _connect { | ||
| 109 | my ($self) = @_; | ||
| 110 | my ( $host, $port ) = split /:/, $self->{server}, 2; | ||
| 111 | |||
| 112 | # If port is not defined, use 80 or 443 | ||
| 113 | $port = | ||
| 114 | ( $port && $port =~ /^(\d+)/ ) ? $1 : $self->{ssl} eq 's' ? 443 : 80; | ||
| 115 | $host =~ s|/.*$||; | ||
| 116 | my $sock = IO::Socket::INET->new( | ||
| 117 | PeerHost => $host, | ||
| 118 | PeerPort => $port, | ||
| 119 | Proto => 'tcp', | ||
| 120 | Timeout => 5, | ||
| 121 | ) | ||
| 122 | or do { | ||
| 123 | $self->logger->error("$pr Failed to connect to $self->{server}: $!"); | ||
| 124 | $self->{connected} = 0; | ||
| 125 | return; | ||
| 126 | }; | ||
| 127 | $self->logger->debug("$pr connected"); | ||
| 128 | |||
| 129 | if ( $self->{ssl} ) { | ||
| 130 | $sock = IO::Socket::SSL->start_SSL( $sock, SSL_verify_mode => 0 ) | ||
| 131 | or do { | ||
| 132 | $self->logger->error("$pr SSL error: $!"); | ||
| 133 | $self->{connected} = 0; | ||
| 134 | return; | ||
| 135 | }; | ||
| 136 | $self->logger->debug("$pr connection upgraded to TLS"); | ||
| 137 | } | ||
| 138 | my $url = "ws$self->{ssl}://$self->{server}/subscribe?" | ||
| 139 | . build_urlencoded( channels => $self->{channels} ); | ||
| 140 | $self->logger->debug("$pr connects to $url"); | ||
| 141 | my $client = Protocol::WebSocket::Client->new( url => $url ); | ||
| 142 | |||
| 143 | $client->on( | ||
| 144 | read => sub { | ||
| 145 | my ( $c, $buf ) = @_; | ||
| 146 | if ( $buf =~ /^{.*}$/ ) { | ||
| 147 | eval { | ||
| 148 | my $data = JSON::decode_json($buf); | ||
| 149 | if ( $data->{channel} | ||
| 150 | && defined $self->{messages}->{ $data->{channel} } ) | ||
| 151 | { | ||
| 152 | push @{ $self->{messages}->{ $data->{channel} } }, | ||
| 153 | $data; | ||
| 154 | } | ||
| 155 | else { | ||
| 156 | $self->logger->info( | ||
| 157 | "$pr received a message for an unknown channel"); | ||
| 158 | } | ||
| 159 | }; | ||
| 160 | $self->logger->error("$pr unable to read websocket: $@") | ||
| 161 | if ($@); | ||
| 162 | } | ||
| 163 | else { | ||
| 164 | $self->logger->warn("$pr received an unreadable message: $buf"); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | ); | ||
| 168 | |||
| 169 | $client->on( | ||
| 170 | write => sub { | ||
| 171 | my ( $c, $buf ) = @_; | ||
| 172 | print $sock $buf; | ||
| 173 | } | ||
| 174 | ); | ||
| 175 | |||
| 176 | $client->on( | ||
| 177 | error => sub { | ||
| 178 | $self->logger->error("$pr websocket error: $_[1]"); | ||
| 179 | } | ||
| 180 | ); | ||
| 181 | |||
| 182 | $client->{hs}->{req}->{headers} = | ||
| 183 | [ Authorization => "Bearer $self->{token}", ] | ||
| 184 | if $self->{token}; | ||
| 185 | |||
| 186 | $client->connect(); | ||
| 187 | |||
| 188 | my $buf; | ||
| 189 | $sock->sysread( $buf, 4096 ); | ||
| 190 | $client->read($buf); | ||
| 191 | |||
| 192 | $self->{socket} = $sock; | ||
| 193 | $self->{selector} = IO::Select->new($sock); | ||
| 194 | $self->{ws} = $client; | ||
| 195 | $self->{connected} = 1; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub _read_socket { | ||
| 199 | my ($self) = @_; | ||
| 200 | return unless $self->{connected}; | ||
| 201 | return unless $self->{selector}->can_read(0.01); | ||
| 202 | my $sock = $self->{socket}; | ||
| 203 | my $buf; | ||
| 204 | my $n = sysread( $sock, $buf, 4096 ); | ||
| 205 | |||
| 206 | if ( !defined $n || $n == 0 ) { | ||
| 207 | warn "Connection lost, trying to reconnect...\n"; | ||
| 208 | $self->{connected} = 0; | ||
| 209 | $self->_connect; | ||
| 210 | return; | ||
| 211 | } | ||
| 212 | |||
| 213 | $self->{ws}->read($buf); | ||
| 214 | } | ||
| 215 | |||
| 216 | # Accessors | ||
| 217 | sub logger { | ||
| 218 | return $_[0]->{logger}; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub ua { | ||
| 222 | return $_[0]->{ua}; | ||
| 223 | } | ||
| 224 | |||
| 225 | 1; |