| 1 | package Protocol::DBus::Client::Mojo; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =encoding utf-8 | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Protocol::DBus::Client::Mojo - D-Bus with L<Mojolicious> | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use experimental 'signatures'; | ||
| 15 | |||
| 16 | my $dbus = Protocol::DBus::Client::Mojo::system(); | ||
| 17 | |||
| 18 | $dbus->initialize_p()->then( | ||
| 19 | sub ($msgr) { | ||
| 20 | my $a = $msgr->send_call_p( .. )->then( sub ($resp) { | ||
| 21 | # .. | ||
| 22 | } ); | ||
| 23 | |||
| 24 | my $b = $msgr->send_call_p( .. )->then( sub ($resp) { | ||
| 25 | # .. | ||
| 26 | } ); | ||
| 27 | |||
| 28 | return Mojo::Promise->all( $a, $b ); | ||
| 29 | }, | ||
| 30 | )->wait(); | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | This module provides an interface between L<Mojo::IOLoop> and | ||
| 35 | L<Protocol::DBus::Client>. It subclasses L<Protocol::DBus::Client::EventBase>. | ||
| 36 | |||
| 37 | L<Mojolicious>-based applications can use this module to interface easily | ||
| 38 | with D-Bus. | ||
| 39 | |||
| 40 | =head1 INTERFACE NOTES | ||
| 41 | |||
| 42 | This module exposes mostly the same interface as | ||
| 43 | L<Protocol::DBus::Client::AnyEvent>, except for a bit of | ||
| 44 | “Mojo-specific” behavior: | ||
| 45 | |||
| 46 | =over | ||
| 47 | |||
| 48 | =item * Returned promises, both from C<initialize()> and | ||
| 49 | the messenger object’s C<send_call()>, are instances of L<Mojo::Promise> | ||
| 50 | rather than L<Promise::ES6>. | ||
| 51 | |||
| 52 | =item * C<initialize_p()> and C<send_call_p()> exist as aliases for | ||
| 53 | C<initialize()> and C<send_call()>, respectively. | ||
| 54 | |||
| 55 | =back | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | #---------------------------------------------------------------------- | ||
| 60 | |||
| 61 | our @ISA; | ||
| 62 | |||
| 63 | use parent qw( Protocol::DBus::Client::EventBase ); | ||
| 64 | |||
| 65 | use Mojo::IOLoop (); | ||
| 66 | |||
| 67 | use constant _PROMISE_CLASS => 'Mojo::Promise'; | ||
| 68 | |||
| 69 | #---------------------------------------------------------------------- | ||
| 70 | |||
| 71 | =head1 INTERFACE | ||
| 72 | |||
| 73 | This module’s interface is identical to that of | ||
| 74 | L<Protocol::DBus::Client::AnyEvent>. See that module for more details. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub system { | ||
| 79 | return __PACKAGE__->_create_system(); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub login_session { | ||
| 83 | return __PACKAGE__->_create_login_session(); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub initialize { | ||
| 87 | return _to_mojo( shift()->SUPER::initialize(@_) ); | ||
| 88 | } | ||
| 89 | |||
| 90 | *initialize_p = __PACKAGE__->can('initialize'); | ||
| 91 | |||
| 92 | sub _to_mojo { | ||
| 93 | my ($p_es6) = @_; | ||
| 94 | |||
| 95 | return _PROMISE_CLASS()->new( sub { $p_es6->then(@_) } )->then( sub { | ||
| 96 | return bless $_[0], 'Protocol::DBus::Client::Mojo::Messenger'; | ||
| 97 | } ); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _initialize { | ||
| 101 | my ($self, $y, $n) = @_; | ||
| 102 | |||
| 103 | my $dbus = $self->{'db'}; | ||
| 104 | |||
| 105 | my $fileno = $dbus->fileno(); | ||
| 106 | |||
| 107 | open my $socket, '+>&=' . $fileno or die "open FD $fileno: $!"; | ||
| 108 | $self->{'socket'} = $socket; | ||
| 109 | |||
| 110 | my $is_write_listening; | ||
| 111 | |||
| 112 | my $reactor = Mojo::IOLoop->singleton->reactor(); | ||
| 113 | |||
| 114 | my $cb = sub { | ||
| 115 | if ( $dbus->initialize() ) { | ||
| 116 | $y->(); | ||
| 117 | } | ||
| 118 | |||
| 119 | # It seems unlikely that we’d need a write watch here. | ||
| 120 | # But just in case … | ||
| 121 | elsif ($dbus->init_pending_send()) { | ||
| 122 | $is_write_listening ||= do { | ||
| 123 | $reactor->watch($socket, 1, 1); | ||
| 124 | 1; | ||
| 125 | }; | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | $is_write_listening = 0; | ||
| 129 | $reactor->watch($socket, 1, 0); | ||
| 130 | } | ||
| 131 | }; | ||
| 132 | |||
| 133 | $reactor->io( $socket, $cb ); | ||
| 134 | $reactor->watch($socket, 0, 0); | ||
| 135 | |||
| 136 | # It does work to watch for readability right away, but only | ||
| 137 | # because poll(POLLIN) gives POLLHUP, which Mojo interprets | ||
| 138 | # as read-ready. Let’s not depend on that. | ||
| 139 | $reactor->next_tick($cb); | ||
| 140 | |||
| 141 | return; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub _flush_send_queue { | ||
| 145 | my ($dbus, $reactor, $socket, $read_yn) = @_; | ||
| 146 | |||
| 147 | my $is_empty = $dbus->flush_write_queue(); | ||
| 148 | |||
| 149 | $reactor->watch($socket, $read_yn, !$is_empty); | ||
| 150 | |||
| 151 | return; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub _set_watches_and_create_messenger { | ||
| 155 | my ($self) = @_; | ||
| 156 | |||
| 157 | my $dbus = $self->{'db'}; | ||
| 158 | |||
| 159 | my $fileno = $dbus->fileno(); | ||
| 160 | |||
| 161 | my $read_cb = $self->_create_get_message_callback(); | ||
| 162 | |||
| 163 | my $reactor = Mojo::IOLoop->singleton->reactor(); | ||
| 164 | my $socket = $self->{'socket'}; | ||
| 165 | |||
| 166 | my $paused_r = \$self->{'_paused'}; | ||
| 167 | |||
| 168 | $reactor->io( | ||
| 169 | $self->{'socket'}, | ||
| 170 | sub { | ||
| 171 | (undef, my $writable) = @_; | ||
| 172 | |||
| 173 | if ($writable) { | ||
| 174 | _flush_send_queue($dbus, $reactor, $socket, !$$paused_r); | ||
| 175 | } | ||
| 176 | else { | ||
| 177 | $read_cb->(); | ||
| 178 | } | ||
| 179 | }, | ||
| 180 | ); | ||
| 181 | |||
| 182 | $self->_resume(); | ||
| 183 | |||
| 184 | $self->{'_give_up_cr'} = sub { | ||
| 185 | Mojo::IOLoop->singleton->reactor()->remove($socket); | ||
| 186 | }; | ||
| 187 | |||
| 188 | return sub { | ||
| 189 | if ($dbus->pending_send()) { | ||
| 190 | _flush_send_queue( $dbus, $reactor, $socket, !$$paused_r ); | ||
| 191 | } | ||
| 192 | }; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub _pause { | ||
| 196 | Mojo::IOLoop->singleton->reactor()->watch( | ||
| 197 | $_[0]{'socket'}, | ||
| 198 | 0, | ||
| 199 | $_[0]{'db'}->pending_send(), | ||
| 200 | ); | ||
| 201 | } | ||
| 202 | |||
| 203 | sub _resume { | ||
| 204 | Mojo::IOLoop->singleton->reactor()->watch( | ||
| 205 | $_[0]{'socket'}, | ||
| 206 | 1, | ||
| 207 | $_[0]{'db'}->pending_send(), | ||
| 208 | ); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub DESTROY { | ||
| 212 | my ($self) = @_; | ||
| 213 | |||
| 214 | if (my $socket = delete $self->{'socket'}) { | ||
| 215 | |||
| 216 | # At global destruction the singleton might already be gone. | ||
| 217 | my $singleton = Mojo::IOLoop->singleton(); | ||
| 218 | |||
| 219 | my $reactor = $singleton && $singleton->reactor(); | ||
| 220 | $reactor->remove($socket) if $reactor; | ||
| 221 | } | ||
| 222 | |||
| 223 | $self->SUPER::DESTROY() if $ISA[0]->can('DESTROY'); | ||
| 224 | |||
| 225 | return; | ||
| 226 | } | ||
| 227 | |||
| 228 | #---------------------------------------------------------------------- | ||
| 229 | |||
| 230 | package Protocol::DBus::Client::Mojo::Messenger; | ||
| 231 | |||
| 232 | use parent 'Protocol::DBus::Client::EventMessenger'; | ||
| 233 | |||
| 234 | sub send_call { | ||
| 235 | my $p = $_[0]->SUPER::send_call( @_[ 1 .. $#_ ] ); | ||
| 236 | |||
| 237 | return Protocol::DBus::Client::Mojo::_PROMISE_CLASS()->new( sub { $p->then(@_) } ); | ||
| 238 | } | ||
| 239 | |||
| 240 | *send_call_p = __PACKAGE__->can('send_call'); | ||
| 241 | |||
| 242 | 1; |