| 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.807; | ||
| 8 | use Future::AsyncAwait; | ||
| 9 | use Sublike::Extended 0.29 'sub'; | ||
| 10 | use Syntax::Keyword::Match; | ||
| 11 | |||
| 12 | use IPC::MicroSocket; | ||
| 13 | |||
| 14 | class IPC::MicroSocket::Client 0.05; | ||
| 15 | |||
| 16 | use Carp; | ||
| 17 | |||
| 18 | use Future::Selector; | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | C<IPC::MicroSocket::Client> - client connector | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | =for highlighter perl | ||
| 27 | |||
| 28 | use v5.36; | ||
| 29 | use Future::AsyncAwait; | ||
| 30 | use IPC::MicroSocket::Client; | ||
| 31 | |||
| 32 | my $client = IPC::MicroSocket::Client->new_unix( path => "my-app.sock" ); | ||
| 33 | |||
| 34 | say await $client->request( "PING" ); | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | This module provides the client connector class for L<IPC::MicroSocket>. | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | inherit IPC::MicroSocket::Connection; | ||
| 43 | |||
| 44 | field %resp_f_by_tag; | ||
| 45 | field %subscribes_by_topic; | ||
| 46 | |||
| 47 | =head1 CONSTRUCTOR | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | =head2 new_unix | ||
| 52 | |||
| 53 | $client = IPC::MicroSocket::Client->new_unix( path => $path ); | ||
| 54 | |||
| 55 | A convenience constructor for connecting a new client instance to a given | ||
| 56 | UNIX socket path. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | # class method | ||
| 61 | sub new_unix ( $class, :$path ) | ||
| 62 | { | ||
| 63 | require IO::Socket::UNIX; | ||
| 64 | |||
| 65 | my $sock = IO::Socket::UNIX->new( Peer => $path ) or | ||
| 66 | croak "Cannot connect to server - $@"; | ||
| 67 | |||
| 68 | return $class->new( fh => $sock ); | ||
| 69 | } | ||
| 70 | |||
| 71 | =head2 new_inprocess | ||
| 72 | |||
| 73 | $client = IPC::MicroSocket::Client->new_inprocess( $server ); | ||
| 74 | |||
| 75 | I<Since version 0.04.> | ||
| 76 | |||
| 77 | Creates a new client instance connected to an existing | ||
| 78 | L<IPC::MicroSocket::Server> instance within the same process. This allows | ||
| 79 | operation of code that would normally use a separately-connected client to | ||
| 80 | run from within the same process as the server. | ||
| 81 | |||
| 82 | I<Currently> the implementation uses a C<PF_UNIX> socketpair to transport | ||
| 83 | bytes as if an external socket was connected, but a later version of this | ||
| 84 | module may make use of some other, more efficient internal mechanism instead. | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | # class method | ||
| 89 | sub new_inprocess ( $class, $server ) | ||
| 90 | { | ||
| 91 | require IO::Socket::UNIX; | ||
| 92 | |||
| 93 | my ( $sock_for_server, $sock_for_client ) = IO::Socket::UNIX->socketpair( | ||
| 94 | Socket::AF_UNIX, Socket::SOCK_STREAM, 0, | ||
| 95 | ) or croak "Cannot socketpair() - $!"; | ||
| 96 | |||
| 97 | my $client = $class->new( fh => $sock_for_client ); | ||
| 98 | |||
| 99 | $server->_accepted( $sock_for_server ); | ||
| 100 | |||
| 101 | return $client; | ||
| 102 | } | ||
| 103 | |||
| 104 | method on_recv ( $sigil, @args ) | ||
| 105 | { | ||
| 106 | match( $sigil : eq ) { | ||
| 107 | case( ")" ) { | ||
| 108 | my $tag = shift @args; | ||
| 109 | my $f = delete $resp_f_by_tag{ $tag } or return; | ||
| 110 | $f->done( @args ); | ||
| 111 | } | ||
| 112 | case( "#" ) { | ||
| 113 | my $tag = shift @args; | ||
| 114 | my $f = delete $resp_f_by_tag{ $tag } or return; | ||
| 115 | $f->fail( $args[0], slurm => @args[1..$#args] ); | ||
| 116 | } | ||
| 117 | case( "!" ) { | ||
| 118 | my $topic = shift @args; | ||
| 119 | $subscribes_by_topic{ $topic } and | ||
| 120 | $subscribes_by_topic{ $topic }->( @args ); | ||
| 121 | } | ||
| 122 | default { | ||
| 123 | warn "Unrecognised message sigil $sigil\n"; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | field $selector; | ||
| 129 | method _selector () | ||
| 130 | { | ||
| 131 | return $selector if $selector; | ||
| 132 | |||
| 133 | $selector = Future::Selector->new; | ||
| 134 | $selector->add( | ||
| 135 | data => "runloop", | ||
| 136 | f => $self->_recv, | ||
| 137 | ); | ||
| 138 | |||
| 139 | return $selector; | ||
| 140 | } | ||
| 141 | |||
| 142 | =head1 METHODS | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | =head2 request | ||
| 147 | |||
| 148 | @response = await $client->request( @args ); | ||
| 149 | |||
| 150 | Sends a C<REQUEST> frame with the given arguments, waiting for a response. The | ||
| 151 | returned future will complete with its C<RESPONSE> frame. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | field $last_tag = 0; | ||
| 156 | async method request ( @args ) | ||
| 157 | { | ||
| 158 | my $tag = pack "C", ( $last_tag += 1 ) %= 256; | ||
| 159 | |||
| 160 | await $self->send( '(', $tag, @args ); | ||
| 161 | |||
| 162 | my $f = ( $resp_f_by_tag{ $tag } = Future->new ); | ||
| 163 | |||
| 164 | my $s = $self->_selector; | ||
| 165 | await $s->run_until_ready( $f ); | ||
| 166 | return await $f; | ||
| 167 | } | ||
| 168 | |||
| 169 | =head2 subscribe | ||
| 170 | |||
| 171 | await $client->subscribe( $topic, $on_recv ); | ||
| 172 | |||
| 173 | $on_recv->( @args ); | ||
| 174 | |||
| 175 | Sends a C<SUBSCRIBE> frame for the given topic name, then waits indefinitely | ||
| 176 | for C<PUBLISH> frames that match it. Each received frame will invoke the | ||
| 177 | C<$on_recv> callback. | ||
| 178 | |||
| 179 | Note that the L<Future> returned by this method should not complete in normal | ||
| 180 | circumstances but will remain pending forever. | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | async method subscribe ( $topic, $on_recv ) | ||
| 185 | { | ||
| 186 | await $self->send( '+', $topic ); | ||
| 187 | |||
| 188 | $subscribes_by_topic{ $topic } = $on_recv; | ||
| 189 | |||
| 190 | my $s = $self->_selector; | ||
| 191 | await $s->select while 1; | ||
| 192 | } | ||
| 193 | |||
| 194 | =head1 AUTHOR | ||
| 195 | |||
| 196 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | 0x55AA; |