<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># -*- perl -*-
#
# Copyright (C) 2004-2011 Daniel P. Berrange
#
# This program is free software; You can redistribute it and/or modify
# it under the same terms as Perl itself. Either:
#
# a) the GNU General Public License as published by the Free
#   Software Foundation; either version 2, or (at your option) any
#   later version,
#
# or
#
# b) the "Artistic License"
#
# The file "COPYING" distributed along with this file provides full
# details of the terms and conditions of the two licenses.

=pod

=head1 NAME

Net::DBus::RemoteObject - Access objects provided on the bus

=head1 SYNOPSIS

  my $service = $bus-&gt;get_service("org.freedesktop.DBus");
  my $object = $service-&gt;get_object("/org/freedesktop/DBus");

  print "Names on the bus {\n";
  foreach my $name (sort @{$object-&gt;ListNames}) {
      print "  ", $name, "\n";
  }
  print "}\n";

=head1 DESCRIPTION

This module provides the API for accessing remote objects available
on the bus. It uses the autoloader to fake the presence of methods
based on the API of the remote object. There is also support for
setting callbacks against signals, and accessing properties of the
object.

=head1 METHODS

=over 4

=cut

package Net::DBus::RemoteObject;

use 5.006;
use strict;
use warnings;

our $AUTOLOAD;

use Net::DBus::Binding::Introspector;
use Net::DBus::ASyncReply;
use Net::DBus::Annotation qw(:call);

#
# BEGIN WARNING
#
# Each method added to this module prevents an application from
# being able to invoke a similarly named method on a remote object.
#
# As such the aim is that no further public methods should be
# added. All private methods should have a _net_dbus_ prefix on
# them too.
#
# END WARNING
#


=item my $object = Net::DBus::RemoteObject-&gt;new($service, $object_path[, $interface], \%params);

Creates a new handle to a remote object. The C&lt;$service&gt; parameter is an instance
of the L&lt;Net::DBus::RemoteService&gt; method, and C&lt;$object_path&gt; is the identifier of
an object exported by this service, for example C&lt;/org/freedesktop/DBus&gt;. For remote
objects which implement more than one interface it is possible to specify an optional
name of an interface as the third parameter. This is only really required, however, if
two interfaces in the object provide methods with the same name, since introspection
data can be used to automatically resolve the correct interface to call cases where
method names are unique. Rather than using this constructor directly, it is preferable
to use the C&lt;get_object&gt; method on L&lt;Net::DBus::RemoteService&gt;, since this caches handles
to remote objects, eliminating unnecessary introspection data lookups.

The C&lt;%params&gt; parameter contains extra configuration parameters for the object. Currently
a single parameter is supported, C&lt;timeout&gt; which takes a value in milliseconds to use as
the timeout for method calls on the object.

=cut


sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self = {};

    $self-&gt;{service} = shift;
    $self-&gt;{object_path}  = shift;
    $self-&gt;{interface} = @_ ? shift : undef;
    $self-&gt;{interfaces} = {};
    $self-&gt;{introspected} = 0;
    $self-&gt;{signal_handlers} = {};
    $self-&gt;{signal_id} = 0;

    my %params = @_;
    $self-&gt;{timeout} = $params{timeout};

    bless $self, $class;

    return $self;
}

=item my $object = $object-&gt;as_interface($interface);

Casts the object to a specific interface, returning a new instance of the
L&lt;Net::DBus::RemoteObject&gt; specialized to the desired interface. It is only
necessary to cast objects to a specific interface, if two interfaces
export methods or signals with the same name, or the remote object does not
support introspection.

=cut

sub as_interface {
    my $self = shift;
    my $interface = shift;

    die "already cast to " . $self-&gt;{interface} . "'"
	if $self-&gt;{interface};

    unless (exists $self-&gt;{interfaces}-&gt;{$interface}) {
	$self-&gt;{interfaces}-&gt;{$interface} = 
	    $self-&gt;new($self-&gt;{service},
		       $self-&gt;{object_path},
		       $interface);
    }
    return $self-&gt;{interfaces}-&gt;{$interface};
}

=item my $service = $object-&gt;get_service

Retrieves a handle for the remote service on which this object is
attached. The returned object is an instance of L&lt;Net::DBus::RemoteService&gt;

=cut

sub get_service {
    my $self = shift;
    return $self-&gt;{service};
}

=item my $path = $object-&gt;get_object_path

Retrieves the unique path identifier for this object within the
service.

=cut

sub get_object_path {
    my $self = shift;
    return $self-&gt;{object_path};
}

=item my $object = $object-&gt;get_child_object($subpath, [$interface])

Retrieves a handle to a child of this object, identified
by the relative path C&lt;$subpath&gt;. The returned object
is an instance of C&lt;Net::DBus::RemoteObject&gt;. The optional
C&lt;$interface&gt; parameter can be used to immediately cast
the object to a specific type.

=cut

sub get_child_object {
    my $self = shift;
    my $path = shift;
    my $interface = @_ ? shift : undef;
    my $fullpath = $self-&gt;{object_path} . $path;

    return $self-&gt;new($self-&gt;get_service,
		      $fullpath,
		      $interface);
}

sub _net_dbus_introspector {
    my $self = shift;


    unless ($self-&gt;{introspected}) {
	my $con = $self-&gt;{service}-&gt;get_bus()-&gt;get_connection();

	my $call = $con-&gt;make_method_call_message($self-&gt;{service}-&gt;get_service_name(),
						  $self-&gt;{object_path},
						  "org.freedesktop.DBus.Introspectable",
						  "Introspect");

	my $xml = eval {
	    my $reply = $con-&gt;send_with_reply_and_block($call, 60 * 1000);

	    my $iter = $reply-&gt;iterator;
	    return $iter-&gt;get(&amp;Net::DBus::Binding::Message::TYPE_STRING);
	};
	if ($@) {
	    if (UNIVERSAL::isa($@, "Net::DBus::Error") &amp;&amp;
		($@-&gt;{name} eq "org.freedesktop.DBus.Error.ServiceUnknown" ||
		$@-&gt;{name} eq "org.freedesktop.DBus.Error.NoReply")) {
		die $@;
	    } else {
		# Ignore other failures, since its probably
		# just that the object doesn't implement
		# the introspect method. Of course without
		# the introspect method we can't tell for sure
		# if this is the case..
		#warn "could not introspect object: $@";
	    }
	}
	if ($xml) {
	    $self-&gt;{introspector} = Net::DBus::Binding::Introspector-&gt;new(xml =&gt; $xml,
									  object_path =&gt; $self-&gt;{object_path});
	}
	$self-&gt;{introspected} = 1;
    }
    return $self-&gt;{introspector};
}


=item my $sigid = $object-&gt;connect_to_signal($name, $coderef);

Connects a callback to a signal emitted by the object. The C&lt;$name&gt;
parameter is the name of the signal within the object, and C&lt;$coderef&gt;
is a reference to an anonymous subroutine. When the signal C&lt;$name&gt;
is emitted by the remote object, the subroutine C&lt;$coderef&gt; will be
invoked, and passed the parameters from the signal. A unique C&lt;$sigid&gt;
will be returned, which can be later passed to C&lt;disconnect_from_signal&gt;
to remove the handler

=cut

sub connect_to_signal {
    my $self = shift;
    my $name = shift;
    my $code = shift;

    my $ins = $self-&gt;_net_dbus_introspector;
    my $interface = $self-&gt;{interface};
    if (!$interface) {
	if (!$ins) {
	    die "no introspection data available for '" . $self-&gt;get_object_path .
		"', and object is not cast to any interface";
	}
	my @interfaces = $ins-&gt;has_signal($name);

	if ($#interfaces == -1) {
	    die "no signal with name '$name' is exported in object '" .
		$self-&gt;get_object_path . "'\n";
	} elsif ($#interfaces &gt; 0) {
	    warn "signal with name '$name' is exported " .
		"in multiple interfaces of '" . $self-&gt;get_object_path . "'" .
		"connecting to first interface only\n";
	}
	$interface = $interfaces[0];
    }

    if ($ins &amp;&amp;
	$ins-&gt;has_signal($name, $interface) &amp;&amp;
	$ins-&gt;is_signal_deprecated($name, $interface)) {
	warn "signal $name in interface $interface on " . $self-&gt;get_object_path . " is deprecated";
    }

    my $cb = sub {
	my $signal = shift;
	my $ins = $self-&gt;_net_dbus_introspector;
	my @params;
	if ($ins) {
	    @params = $ins-&gt;decode($signal, "signals", $signal-&gt;get_member, "params");
	} else {
	    @params = $signal-&gt;get_args_list;
	}

	foreach my $handler (@{$self-&gt;{signal_handlers}-&gt;{$signal-&gt;get_member}-&gt;{handlers}}) {
	    my ($id, $cb) = @{$handler};
	    &amp;$cb(@params);
	}
    };
    if (!exists $self-&gt;{signal_handlers}-&gt;{$name}) {
	$self-&gt;{signal_handlers}-&gt;{$name} = { cb =&gt; $cb, handlers =&gt; [] };
	$self-&gt;get_service-&gt;
	    get_bus()-&gt;
	    _add_signal_receiver($cb,
				 $name,
				 $interface,
				 $self-&gt;{service}-&gt;get_service_name(),
				 $self-&gt;{object_path});
    }
    my $sigid = ++$self-&gt;{signal_id};
    push @{$self-&gt;{signal_handlers}-&gt;{$name}-&gt;{handlers}}, [$sigid, $code];
    return $sigid;
}


=item $object-&gt;disconnect_from_signal($name, $sigid);

Disconnects from a signal emitted by the object. The C&lt;$name&gt;
parameter is the name of the signal within the object. The
C&lt;$sigid&gt; must be the unique signal handler ID returned by
a previous C&lt;connect_to_signal&gt; method call.

=cut

sub disconnect_from_signal {
    my $self = shift;
    my $name = shift;
    my $sigid = shift;

    my $ins = $self-&gt;_net_dbus_introspector;
    my $interface = $self-&gt;{interface};
    if (!$interface) {
	if (!$ins) {
	    die "no introspection data available for '" . $self-&gt;get_object_path .
		"', and object is not cast to any interface";
	}
	my @interfaces = $ins-&gt;has_signal($name);

	if ($#interfaces == -1) {
	    die "no signal with name '$name' is exported in object '" .
		$self-&gt;get_object_path . "'\n";
	} elsif ($#interfaces &gt; 0) {
	    warn "signal with name '$name' is exported " .
		"in multiple interfaces of '" . $self-&gt;get_object_path . "'" .
		"connecting to first interface only\n";
	}
	$interface = $interfaces[0];
    }

    my @handlers;
    foreach my $handler (@{$self-&gt;{signal_handlers}-&gt;{$name}-&gt;{handlers}}) {
	my ($thissigid, $cb) = @{$handler};
	if ($thissigid != $sigid) {
	    push @handlers, $handler;
	}
    }
    if (@handlers) {
	$self-&gt;{signal_handlers}-&gt;{$name}-&gt;{handlers} = \@handlers;
    } else {
	$self-&gt;get_service-&gt;
	    get_bus()-&gt;
	    _remove_signal_receiver($self-&gt;{signal_handlers}-&gt;{$name}-&gt;{cb},
				    $name,
				    $interface,
				    $self-&gt;{service}-&gt;get_service_name(),
				    $self-&gt;{object_path});
	delete $self-&gt;{signal_handlers}-&gt;{$name};
    }
}


sub DESTROY {
    # No op merely to stop AutoLoader trying to
    # call DESTROY on remote object
}

sub _net_dbus_timeout {
    my $self = shift;

    if (defined $self-&gt;{timeout}) {
	return $self-&gt;{timeout};
    }
    if (defined $self-&gt;get_service()-&gt;timeout()) {
	return $self-&gt;get_service()-&gt;timeout();
    }
    return $self-&gt;get_service()-&gt;get_bus()-&gt;timeout();
}


sub AUTOLOAD {
    my $self = shift;
    my $sub = $AUTOLOAD;

    my $mode = dbus_call_sync;
    my $timeout;

    while (@_ &amp;&amp; UNIVERSAL::isa($_[0], "Net::DBus::Annotation")) {
	if ($_[0] eq dbus_call_sync ||
	    $_[0] eq dbus_call_async ||
	    $_[0] eq dbus_call_noreply) {
	    $mode = shift;
	} elsif ($_[0] eq dbus_call_timeout) {
	    shift;
	    $timeout = shift;
	} else {
	    die "Unknown annotation $_[0]";
	}
    }
    $timeout = $self-&gt;_net_dbus_timeout() unless defined $timeout;

    (my $name = $AUTOLOAD) =~ s/.*:://;

    my $interface = $self-&gt;{interface};

    # If introspection data is available, use that
    # to resolve correct interface (if object is not
    # cast to an explicit interface already)
    my $ins = $self-&gt;_net_dbus_introspector();
    if ($ins) {
	if ($interface) {
	    if ($ins-&gt;has_method($name, $interface)) {
		return $self-&gt;_net_dbus_call_method($mode, $timeout,
						    $name, $interface, 1,
						    @_);
	    }
	    if ($ins-&gt;has_property($name, $interface)) {
		if ($ins-&gt;is_property_deprecated($name, $interface)) {
		    warn "property $name in interface $interface on " . $self-&gt;get_object_path . " is deprecated";
		}

		if (@_) {
		    $self-&gt;_net_dbus_call_method($mode, $timeout,
						 "Set", "org.freedesktop.DBus.Properties", 1,
						 $interface, $name, $_[0]);
		    return ();
		} else {
		    return $self-&gt;_net_dbus_call_method($mode, $timeout,
							"Get", "org.freedesktop.DBus.Properties", 1,
							$interface, $name);
		}
	    }
	} else {
	    my @interfaces = $ins-&gt;has_method($name);

	    if (@interfaces) {
		if ($#interfaces &gt; 0) {
		    die "method with name '$name' is exported " .
			"in multiple interfaces of '" . $self-&gt;get_object_path . "'";
		}
		return $self-&gt;_net_dbus_call_method($mode, $timeout,
						    $name, $interfaces[0], 1, @_);
	    }
	    @interfaces = $ins-&gt;has_property($name);

	    if (@interfaces) {
		if ($#interfaces &gt; 0) {
		    die "property with name '$name' is exported " .
			"in multiple interfaces of '" . $self-&gt;get_object_path . "'";
		}
		$interface = $interfaces[0];
		if ($ins-&gt;is_property_deprecated($name, $interface)) {
		    warn "property $name in interface $interface on " . $self-&gt;get_object_path . " is deprecated";
		}
		if (@_) {
		    $self-&gt;_net_dbus_call_method($mode, $timeout,
						 "Set", "org.freedesktop.DBus.Properties", 1,
						 $interface, $name, $_[0]);
		    return ();
		} else {
		    return $self-&gt;_net_dbus_call_method($mode, $timeout,
							"Get", "org.freedesktop.DBus.Properties", 1,
							$interface, $name);
		}
	    }
	}
    }

    if (!$interface) {
	die "no introspection data available for method '" . $name . "' in object '" .
	    $self-&gt;get_object_path . "', and object is not cast to any interface";
    }

    return $self-&gt;_net_dbus_call_method($mode, $timeout,
					$name, $interface,
					0, @_);
}


sub _net_dbus_call_method {
    my $self = shift;
    my $mode = shift;
    my $timeout = shift;
    my $name = shift;
    my $interface = shift;
    my $introspect = shift;

    my $con = $self-&gt;{service}-&gt;get_bus()-&gt;get_connection();

    my $ins = $introspect ? $self-&gt;_net_dbus_introspector : undef;
    if ($ins &amp;&amp;
	$ins-&gt;is_method_deprecated($name, $interface)) {
	warn "method '$name' in interface $interface on object " . $self-&gt;get_object_path . " is deprecated\n";
    }

    my $call = $con-&gt;make_method_call_message($self-&gt;{service}-&gt;get_service_name(),
					      $self-&gt;{object_path},
					      $interface,
					      $name);

    #$call-&gt;set_destination($self-&gt;get_service-&gt;get_owner_name);

    if ($ins) {
	$ins-&gt;encode($call, "methods", $name, "params", @_);
    } else {
	$call-&gt;append_args_list(@_);
    }

    if ($mode == dbus_call_sync) {
	my $reply = $con-&gt;
	    send_with_reply_and_block($call, $timeout);

	my @reply;
	if ($ins) {
	    @reply = $ins-&gt;decode($reply, "methods", $name, "returns");
	} else {
	    @reply = $reply-&gt;get_args_list;
	}

	return wantarray ? @reply : $reply[0];
    } elsif ($mode == dbus_call_async) {
	my $pending_call = $self-&gt;{service}-&gt;
	    get_bus()-&gt;
	    get_connection()-&gt;
	    send_with_reply($call, $timeout);
	my $reply = Net::DBus::ASyncReply-&gt;_new(pending_call =&gt; $pending_call,
						($ins ? (introspector =&gt; $ins,
							 method_name =&gt; $name)
						 : ()));
	return $reply;
    } elsif ($mode == dbus_call_noreply) {
	$call-&gt;set_no_reply(1);
	$self-&gt;{service}-&gt;
	    get_bus()-&gt;
	    get_connection()-&gt;
	    send($call, $timeout);
    } else {
	die "unsupported annotation '$mode'";
    }
}


1;

=pod

=back

=head1 AUTHOR

Daniel Berrange &lt;dan@berrange.com&gt;

=head1 COPYRIGHT

Copright (C) 2004-2011, Daniel Berrange.

=head1 SEE ALSO

L&lt;Net::DBus::RemoteService&gt;, L&lt;Net::DBus::Object&gt;, L&lt;Net::DBus::Annotation&gt;

=cut
</pre></body></html>