The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Protocol::DBus::Peer - base class for a D-Bus peer

SYNOPSIS

    $dbus->send_call(
        interface => 'org.freedesktop.DBus.Properties',
        member => 'GetAll',
        signature => 's',
        path => '/org/freedesktop/DBus',
        destination => 'org.freedesktop.DBus',
        body => [ 'org.freedesktop.DBus' ],
    )->then( sub { .. } );

    my $msg = $dbus->get_message();

    # Same pattern as the IO::Handle method.
    $dbus->blocking(0);

    my $fileno = $dbus->fileno();

    $dbus->flush_write_queue() if $dbus->pending_send();

    # I’m not sure why you’d want to do this, but …
    $dbus->big_endian();

DESCRIPTION

This class contains D-Bus logic that is useful in both client and server contexts. (Currently this distribution does not include a server implementation.)

METHODS

$msg = OBJ->get_message()

This returns a single instace of Protocol::DBus::Message, or undef if no message is available. It will also fire the appropriate “on_return” method on METHOD_RETURN or ERROR messages.

The backend I/O logic reads data in chunks; thus, if there is a message already available in the read buffer, no I/O is done. If you’re doing non-blocking I/O then it is thus vital that, every time the DBus socket is readable, you call this function until undef is returned.

OBJ->flush_write_queue()

Same as IO::Framed::Write’s method of the same name.

$promise = OBJ->send_call( %OPTS )

Send a METHOD_CALL message.

%OPTS are path, interface, member, destination, signature, and body. These do as you’d expect, but note that body, if given, must be an array reference.

flags may be given as an array reference of strings, e.g., NO_REPLY_EXPECTED. See the D-Bus Specification for all possible values.

The return value is an instance of Promise::ES6. Normally this promise resolves when a METHOD_RETURN arrives in response. The resolution value is a a Protocol::DBus::Message instance that represents the response. If, however, flags is given and contains NO_REPLY_EXPECTED, the promise resolves as soon as the message is sent.

If an ERROR arrives in response instead, the promise will instead reject with a Protocol::DBus::Message instance that represents that ERROR. The promise will also reject if some other error happens (e.g., an I/O error while sending the initial METHOD_CALL).

$promise = OBJ->send_return( $ORIG_MSG, %OPTS )

Send a METHOD_RETURN message.

The return is a promise that resolves when the message is sent.

Arguments are similar to send_call() except for the header differences that the D-Bus specification describes. Also, destination is not given directly but is instead inferred from the $ORIG_MSG. (Behavior is undefined if this parameter is given directly.)

$promise = OBJ->send_error( $ORIG_MSG, %OPTS )

Like send_return(), but sends an error instead. The error_name parameter is required.

$promise = OBJ->send_signal( %OPTS )

Like send_call() but sends a signal rather than a method call.

OBJ->big_endian()

Same interface as blocking(), but this sets/gets/toggles whether to send big-endian messages instead of little-endian.

By default this library uses the system’s native byte order, so you probably have little need for this function.

OBJ->preserve_variant_signatures()

Same interface as blocking(), but when this is enabled variants are given as two-member array references ([ signature => value ]), blessed as Protocol::DBus::Type::Variant instances.

For most Perl applications this is probably counterproductive.

OBJ->blocking()

Same interface as IO::Handle’s method of the same name.

OBJ->fileno()

Returns the connection socket’s file descriptor.

OBJ->pending_send()

Returns a boolean that indicates whether there is data queued up to send to the server.