| 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, 2024-2026 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.36; | ||
| 7 | use Object::Pad 0.817; # class :abstract | ||
| 8 | use Future::AsyncAwait; | ||
| 9 | use Sublike::Extended 0.29 'method'; | ||
| 10 | |||
| 11 | use IPC::MicroSocket; | ||
| 12 | |||
| 13 | package IPC::MicroSocket::Server 0.05; # this 'package' statement just to keep CPAN indexers happy | ||
| 14 | class IPC::MicroSocket::Server :abstract; | ||
| 15 | |||
| 16 | use Carp; | ||
| 17 | |||
| 18 | use Future::Selector; | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | C<IPC::MicroSocket::Server> - server role | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | =for highlighter perl | ||
| 27 | |||
| 28 | use v5.36; | ||
| 29 | use Future::AsyncAwait; | ||
| 30 | use Object::Pad v0.807; | ||
| 31 | use IPC::MicroSocket::Server; | ||
| 32 | |||
| 33 | class ExampleServer { | ||
| 34 | apply IPC::MicroSocket::Server; | ||
| 35 | |||
| 36 | async method on_connection_request ( $conn, $cmd, @args ) | ||
| 37 | { | ||
| 38 | say "Connection sends $cmd"; | ||
| 39 | return "Response for $cmd"; | ||
| 40 | } | ||
| 41 | |||
| 42 | method on_connection_subscribe {} | ||
| 43 | } | ||
| 44 | |||
| 45 | await ExampleServer->new_unix( path => "my-app.sock" ) | ||
| 46 | ->run; | ||
| 47 | |||
| 48 | =head1 DESCRIPTION | ||
| 49 | |||
| 50 | This module provides the server role for L<IPC::MicroSocket>. This is an | ||
| 51 | incomplete role, which requires any class that applies it to provide some | ||
| 52 | methods that contain the actual behaviour for the server. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | field $fh :param; | ||
| 57 | ADJUST { | ||
| 58 | # Normally these are real filehandles, but in unit tests they're plain | ||
| 59 | # strings. The 'ref' test just stops that from being a problem | ||
| 60 | $fh->blocking( 0 ) if ref $fh; | ||
| 61 | } | ||
| 62 | |||
| 63 | field $connection_class :param = "IPC::MicroSocket::Server::_Connection"; | ||
| 64 | |||
| 65 | field @clients :reader; | ||
| 66 | |||
| 67 | =head1 CONSTRUCTOR | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | =head2 new_unix | ||
| 72 | |||
| 73 | $server = IPC::MicroSocket::Server->new_unix( path => $path, %args ); | ||
| 74 | |||
| 75 | A convenience constructor for creating a new server instance listening on the | ||
| 76 | given UNIX socket path. | ||
| 77 | |||
| 78 | Note as this is a role, this must be invoked on a class that applies the role | ||
| 79 | and implements the missing methods. | ||
| 80 | |||
| 81 | Takes the following named arguments: | ||
| 82 | |||
| 83 | =over 4 | ||
| 84 | |||
| 85 | =item listen => INT | ||
| 86 | |||
| 87 | Sets the size of the C<listen(2)> queue; defaults to 5 if not specified. | ||
| 88 | |||
| 89 | =back | ||
| 90 | |||
| 91 | I<Since version 0.03> any other remaining arguments are passed to the instance | ||
| 92 | constructor of the underlying object class. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | method new_unix :common ( :$path, :$listen //= 5, %rest ) | ||
| 97 | { | ||
| 98 | require IO::Socket::UNIX; | ||
| 99 | |||
| 100 | my $listensock = IO::Socket::UNIX->new( | ||
| 101 | Local => $path, | ||
| 102 | Listen => $listen, | ||
| 103 | ReuseAddr => 1, | ||
| 104 | ) or croak "Cannot create socket - $@"; | ||
| 105 | |||
| 106 | return $class->new( fh => $listensock, %rest ); | ||
| 107 | } | ||
| 108 | |||
| 109 | field $selector; | ||
| 110 | method _selector () | ||
| 111 | { | ||
| 112 | return $selector if $selector; | ||
| 113 | |||
| 114 | $selector = Future::Selector->new; | ||
| 115 | |||
| 116 | $selector->add( | ||
| 117 | data => "acceptor", | ||
| 118 | gen => async sub () { | ||
| 119 | $self->_accepted( await Future::IO->accept( $fh ) ); | ||
| 120 | }, | ||
| 121 | ) if $fh; # might be false in some unit tests | ||
| 122 | |||
| 123 | return $selector; | ||
| 124 | } | ||
| 125 | |||
| 126 | async method _accepted ( $clientsock ) | ||
| 127 | { | ||
| 128 | push @clients, my $client = $connection_class->new( | ||
| 129 | server => $self, | ||
| 130 | fh => $clientsock, | ||
| 131 | ); | ||
| 132 | |||
| 133 | $self->_selector->add( | ||
| 134 | data => $client, | ||
| 135 | f => $client->run | ||
| 136 | ->on_ready(sub ( $ ) { | ||
| 137 | @clients = grep { $_ != $client } @clients; | ||
| 138 | }), | ||
| 139 | ); | ||
| 140 | } | ||
| 141 | |||
| 142 | =head1 METHODS | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | =head2 publish | ||
| 147 | |||
| 148 | $server->publish( $topic, @args ); | ||
| 149 | |||
| 150 | Sends a C<PUBLISH> frame to every connected client. | ||
| 151 | |||
| 152 | Note that this is I<not> an C<async> method; the send future for each client | ||
| 153 | becomes owned by the selector for each connected client instance individually. | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | method publish ( $topic, @args ) | ||
| 158 | { | ||
| 159 | foreach my $client ( @clients ) { | ||
| 160 | $client->is_subscribed( $topic ) and $client->publish( $topic, @args ); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | =head2 run | ||
| 165 | |||
| 166 | await $server->run; | ||
| 167 | |||
| 168 | Returns a L<Future> that represents the indefinite runtime of the server | ||
| 169 | instance. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | method run () | ||
| 174 | { | ||
| 175 | return $self->_selector->run; | ||
| 176 | } | ||
| 177 | |||
| 178 | =head1 REQUIRED METHODS | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | =head2 on_connection_request | ||
| 183 | |||
| 184 | @response = await $server->on_connection_request( $conn, $cmd, @args ); | ||
| 185 | |||
| 186 | Invoked on receipt of a C<REQUEST> frame from a connected client. It should | ||
| 187 | asynchronously return the response list to be sent back to the client. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | method on_connection_request; | ||
| 192 | |||
| 193 | =head2 on_connection_subscribe | ||
| 194 | |||
| 195 | $server->on_connection_subscribe( $conn, $topic ); | ||
| 196 | |||
| 197 | Invoked on receipt of a C<SUBSCRIBE> frame from a connected client. | ||
| 198 | |||
| 199 | =cut | ||
| 200 | |||
| 201 | method on_connection_subscribe; | ||
| 202 | |||
| 203 | # The default server connection class | ||
| 204 | class IPC::MicroSocket::Server::_Connection | ||
| 205 | { | ||
| 206 | inherit IPC::MicroSocket::ServerConnection; | ||
| 207 | |||
| 208 | field $server :param; | ||
| 209 | |||
| 210 | async method on_request ( $cmd, @args ) | ||
| 211 | { | ||
| 212 | await $server->on_connection_request( $self, $cmd, @args ); | ||
| 213 | } | ||
| 214 | |||
| 215 | method on_subscribe ( $topic ) | ||
| 216 | { | ||
| 217 | $server->on_connection_subscribe( $self, $topic ); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | =head1 AUTHOR | ||
| 222 | |||
| 223 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | 0x55AA; |