home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Object.pm < prev    next >
Encoding:
Perl POD Document  |  2008-02-20  |  17.5 KB  |  658 lines

  1. # -*- perl -*-
  2. #
  3. # Copyright (C) 2004-2006 Daniel P. Berrange
  4. #
  5. # This program is free software; You can redistribute it and/or modify
  6. # it under the same terms as Perl itself. Either:
  7. #
  8. # a) the GNU General Public License as published by the Free
  9. #   Software Foundation; either version 2, or (at your option) any
  10. #   later version,
  11. #
  12. # or
  13. #
  14. # b) the "Artistic License"
  15. #
  16. # The file "COPYING" distributed along with this file provides full
  17. # details of the terms and conditions of the two licenses.
  18.  
  19. =pod
  20.  
  21. =head1 NAME
  22.  
  23. Net::DBus::Object - Provide objects to the bus for clients to use
  24.  
  25. =head1 SYNOPSIS
  26.  
  27.   # Connecting an object to the bus, under a service
  28.   package main;
  29.  
  30.   use Net::DBus;
  31.  
  32.   # Attach to the bus
  33.   my $bus = Net::DBus->find;
  34.  
  35.   # Acquire a service 'org.demo.Hello'
  36.   my $service = $bus->export_service("org.demo.Hello");
  37.  
  38.   # Export our object within the service
  39.   my $object = Demo::HelloWorld->new($service);
  40.  
  41.   ....rest of program...
  42.  
  43.   # Define a new package for the object we're going
  44.   # to export
  45.   package Demo::HelloWorld;
  46.  
  47.   # Specify the main interface provided by our object
  48.   use Net::DBus::Exporter qw(org.example.demo.Greeter);
  49.  
  50.   # We're going to be a DBus object
  51.   use base qw(Net::DBus::Object);
  52.  
  53.   # Export a 'Greeting' signal taking a stringl string parameter
  54.   dbus_signal("Greeting", ["string"]);
  55.  
  56.   # Export 'Hello' as a method accepting a single string
  57.   # parameter, and returning a single string value
  58.   dbus_method("Hello", ["string"], ["string"]);
  59.  
  60.   sub new {
  61.       my $class = shift;
  62.       my $service = shift;
  63.       my $self = $class->SUPER::new($service, "/org/demo/HelloWorld");
  64.  
  65.       bless $self, $class;
  66.  
  67.       return $self;
  68.   }
  69.  
  70.   sub Hello {
  71.     my $self = shift;
  72.     my $name = shift;
  73.  
  74.     $self->emit_signal("Greeting", "Hello $name");
  75.     return "Said hello to $name";
  76.   }
  77.  
  78.   # Export 'Goodbye' as a method accepting a single string
  79.   # parameter, and returning a single string, but put it
  80.   # in the 'org.exaple.demo.Farewell' interface
  81.  
  82.   dbus_method("Goodbye", ["string"], ["string"], "org.example.demo.Farewell");
  83.  
  84.   sub Goodbye {
  85.     my $self = shift;
  86.     my $name = shift;
  87.  
  88.     $self->emit_signal("Greeting", "Goodbye $name");
  89.     return "Said goodbye to $name";
  90.   }
  91.  
  92. =head1 DESCRIPTION
  93.  
  94. This the base of all objects which are exported to the
  95. message bus. It provides the core support for type introspection
  96. required for objects exported to the message. When sub-classing
  97. this object, methods can be created & tested as per normal Perl
  98. modules. Then just as the L<Exporter> module is used to export
  99. methods within a script, the L<Net::DBus::Exporter> module is
  100. used to export methods (and signals) to the message bus.
  101.  
  102. All packages inheriting from this, will automatically have the
  103. interface C<org.freedesktop.DBus.Introspectable> registered
  104. with L<Net::DBus::Exporter>, and the C<Introspect> method within
  105. this exported.
  106.  
  107. =head1 METHODS
  108.  
  109. =over 4
  110.  
  111. =cut
  112.  
  113. package Net::DBus::Object;
  114.  
  115. use 5.006;
  116. use strict;
  117. use warnings;
  118.  
  119. our $ENABLE_INTROSPECT;
  120.  
  121. BEGIN {
  122.     if ($ENV{DBUS_DISABLE_INTROSPECT}) {
  123.     $ENABLE_INTROSPECT = 0;
  124.     } else {
  125.     $ENABLE_INTROSPECT = 1;
  126.     }
  127. }
  128.  
  129. use Net::DBus::Exporter "org.freedesktop.DBus.Introspectable";
  130.  
  131. dbus_method("Introspect", [], ["string"]);
  132.  
  133. dbus_method("Get", ["string", "string"], [["variant"]], "org.freedesktop.DBus.Properties");
  134. dbus_method("Set", ["string", "string", ["variant"]], [], "org.freedesktop.DBus.Properties");
  135.  
  136. =item my $object = Net::DBus::Object->new($service, $path)
  137.  
  138. This creates a new DBus object with an path of C<$path>
  139. registered within the service C<$service>. The C<$path>
  140. parameter should be a string complying with the usual
  141. DBus requirements for object paths, while the C<$service>
  142. parameter should be an instance of L<Net::DBus::Service>.
  143. The latter is typically obtained by calling the C<export_service>
  144. method on the L<Net::DBus> object.
  145.  
  146. =item my $object = Net::DBus::Object->new($parentobj, $subpath)
  147.  
  148. This creates a new DBus child object with an path of C<$subpath>
  149. relative to its parent C<$parentobj>. The C<$subpath>
  150. parameter should be a string complying with the usual
  151. DBus requirements for object paths, while the C<$parentobj>
  152. parameter should be an instance of L<Net::DBus::Object>.
  153.  
  154. =cut
  155.  
  156. sub new {
  157.     my $class = shift;
  158.     my $self = {};
  159.  
  160.     my $parent = shift;
  161.     my $path = shift;
  162.  
  163.     $self->{parent} = $parent;
  164.     if ($parent->isa(__PACKAGE__)) {
  165.     $self->{service} = $parent->get_service;
  166.     $self->{object_path} = $parent->get_object_path . $path;
  167.     } else {
  168.     $self->{service} = $parent;
  169.     $self->{object_path} = $path;
  170.     }
  171.  
  172.     $self->{interface} = shift;
  173.     $self->{introspector} = undef;
  174.     $self->{introspected} = 0;
  175.     $self->{callbacks} = {};
  176.     $self->{children} = {};
  177.  
  178.     bless $self, $class;
  179.  
  180.     if ($self->{parent}->isa(__PACKAGE__)) {
  181.     $self->{parent}->_register_child($self);
  182.     } else {
  183.     $self->get_service->_register_object($self);
  184.     }
  185.  
  186.     return $self;
  187. }
  188.  
  189.  
  190. =item $object->disconnect();
  191.  
  192. This method disconnects the object from the bus, such that it
  193. will no longer receive messages sent by other clients. Any
  194. child objects will be recursively disconnected too. After an
  195. object has been disconnected, it is possible for Perl to
  196. garbage collect the object instance. It will also make it
  197. possible to connect a newly created object to the same path.
  198.  
  199. =cut
  200.  
  201. sub disconnect {
  202.     my $self = shift;
  203.  
  204.     return unless $self->{parent};
  205.  
  206.     foreach my $child (keys %{$self->{children}}) {
  207.     $self->_unregister_child($self->{children}->{$child});
  208.     }
  209.  
  210.     if ($self->{parent}->isa(__PACKAGE__)) {
  211.     $self->{parent}->_unregister_child($self);
  212.     } else {
  213.     $self->get_service->_unregister_object($self);
  214.     }
  215.     $self->{parent} = undef;
  216. }
  217.  
  218. =item my $bool = $object->is_connected
  219.  
  220. Returns a true value if the object is connected to the bus,
  221. and thus capable of being accessed by remote clients. Returns
  222. false if the object is disconnected & thus ready for garbage
  223. collection. All objects start off in the connected state, and
  224. will only transition if the C<disconnect> method is called.
  225.  
  226. =cut
  227.  
  228. sub is_connected {
  229.     my $self = shift;
  230.  
  231.     return 0 unless $self->{parent};
  232.  
  233.     if ($self->{parent}->isa(__PACKAGE__)) {
  234.     return $self->{parent}->is_connected;
  235.     }
  236.     return 1;
  237. }
  238.  
  239. sub DESTROY {
  240.     my $self = shift;
  241.     # XXX there are some issues during global
  242.     # destruction which need to be better figured
  243.     # out before this will work
  244.     #$self->disconnect;
  245. }
  246.  
  247. sub _register_child {
  248.     my $self = shift;
  249.     my $object = shift;
  250.  
  251.     $self->get_service->_register_object($object);
  252.     $self->{children}->{$object->get_object_path} = $object;
  253. }
  254.  
  255.  
  256. sub _unregister_child {
  257.     my $self = shift;
  258.     my $object = shift;
  259.  
  260.     $self->get_service->_unregister_object($object);
  261.     delete $self->{children}->{$object->get_object_path};
  262. }
  263.  
  264. # return a list of sub nodes for this object
  265. sub _get_sub_nodes {
  266.     my $self = shift;
  267.     my %uniq;
  268.  
  269.     my $base = "$self->{object_path}/";
  270.     foreach ( keys( %{$self->{children}} ) ) {
  271.       m/^$base([^\/]+)/;
  272.       $uniq{$1} = 1;
  273.     }
  274.  
  275.     return sort( keys( %uniq ) );
  276. }
  277.  
  278. =item my $service = $object->get_service
  279.  
  280. Retrieves the L<Net::DBus::Service> object within which this
  281. object is exported.
  282.  
  283. =cut
  284.  
  285. sub get_service {
  286.     my $self = shift;
  287.     return $self->{service};
  288. }
  289.  
  290.  
  291. =item my $path = $object->get_object_path
  292.  
  293. Retrieves the path under which this object is exported
  294.  
  295. =cut
  296.  
  297. sub get_object_path {
  298.     my $self = shift;
  299.     return $self->{object_path};
  300. }
  301.  
  302. =item $object->emit_signal_in($name, $interface, $client, @args);
  303.  
  304. Emits a signal from the object, with a name of C<$name>. If the
  305. C<$interface> parameter is defined, the signal will be scoped
  306. within that interface. If the C<$client> parameter is defined,
  307. the signal will be unicast to that client on the bus. The
  308. signal and the data types of the arguments C<@args> must have
  309. been registered with L<Net::DBus::Exporter> by calling the
  310. C<dbus_signal> method.
  311.  
  312. =cut
  313.  
  314. sub emit_signal_in {
  315.     my $self = shift;
  316.     my $name = shift;
  317.     my $interface = shift;
  318.     my $destination = shift;
  319.     my @args = @_;
  320.  
  321.     die "object is disconnected from the bus" unless $self->is_connected;
  322.  
  323.     my $con = $self->get_service->get_bus->get_connection;
  324.  
  325.     my $signal = $con->make_signal_message($self->get_object_path,
  326.                        $interface,
  327.                        $name);
  328.     if ($destination) {
  329.     $signal->set_destination($destination);
  330.     }
  331.  
  332.     my $ins = $self->_introspector;
  333.     if ($ins) {
  334.     $ins->encode($signal, "signals", $name, "params", @args);
  335.     } else {
  336.     $signal->append_args_list(@args);
  337.     }
  338.     $con->send($signal);
  339.  
  340.     # Short circuit locally registered callbacks
  341.     if (exists $self->{callbacks}->{$interface} &&
  342.     exists $self->{callbacks}->{$interface}->{$name}) {
  343.     my $cb = $self->{callbacks}->{$interface}->{$name};
  344.     &$cb(@args);
  345.     }
  346. }
  347.  
  348. =item $self->emit_signal_to($name, $client, @args);
  349.  
  350. Emits a signal from the object, with a name of C<$name>. The
  351. signal and the data types of the arguments C<@args> must have
  352. been registered with L<Net::DBus::Exporter> by calling the
  353. C<dbus_signal> method. The signal will be sent only to the
  354. client named by the C<$client> parameter.
  355.  
  356. =cut
  357.  
  358. sub emit_signal_to {
  359.     my $self = shift;
  360.     my $name = shift;
  361.     my $destination = shift;
  362.     my @args = @_;
  363.  
  364.     my $intro = $self->_introspector;
  365.     if (!$intro) {
  366.     die "no introspection data available for '" . $self->get_object_path .
  367.         "', use the emit_signal_in method instead";
  368.     }
  369.     my @interfaces = $intro->has_signal($name);
  370.     if ($#interfaces == -1) {
  371.     die "no signal with name '$name' is exported in object '" .
  372.         $self->get_object_path . "'\n";
  373.     } elsif ($#interfaces > 0) {
  374.     die "signal '$name' is exported in more than one interface of '" .
  375.         $self->get_object_path . "', use the emit_signal_in method instead.";
  376.     }
  377.     $self->emit_signal_in($name, $interfaces[0], $destination, @args);
  378. }
  379.  
  380. =item $self->emit_signal($name, @args);
  381.  
  382. Emits a signal from the object, with a name of C<$name>. The
  383. signal and the data types of the arguments C<@args> must have
  384. been registered with L<Net::DBus::Exporter> by calling the
  385. C<dbus_signal> method. The signal will be broadcast to all
  386. clients on the bus.
  387.  
  388. =cut
  389.  
  390. sub emit_signal {
  391.     my $self = shift;
  392.     my $name = shift;
  393.     my @args = @_;
  394.  
  395.     $self->emit_signal_to($name, undef, @args);
  396. }
  397.  
  398. =item $object->connect_to_signal_in($name, $interface, $coderef);
  399.  
  400. Connects a callback to a signal emitted by the object. The C<$name>
  401. parameter is the name of the signal within the object, and C<$coderef>
  402. is a reference to an anonymous subroutine. When the signal C<$name>
  403. is emitted by the remote object, the subroutine C<$coderef> will be
  404. invoked, and passed the parameters from the signal. The C<$interface>
  405. parameter is used to specify the explicit interface defining the
  406. signal to connect to.
  407.  
  408. =cut
  409.  
  410. sub connect_to_signal_in {
  411.     my $self = shift;
  412.     my $name = shift;
  413.     my $interface = shift;
  414.     my $code = shift;
  415.  
  416.     die "object is disconnected from the bus" unless $self->is_connected;
  417.  
  418.     $self->{callbacks}->{$interface} = {} unless
  419.     exists $self->{callbacks}->{$interface};
  420.     $self->{callbacks}->{$interface}->{$name} = $code;
  421. }
  422.  
  423. =item $object->connect_to_signal($name, $coderef);
  424.  
  425. Connects a callback to a signal emitted by the object. The C<$name>
  426. parameter is the name of the signal within the object, and C<$coderef>
  427. is a reference to an anonymous subroutine. When the signal C<$name>
  428. is emitted by the remote object, the subroutine C<$coderef> will be
  429. invoked, and passed the parameters from the signal.
  430.  
  431. =cut
  432.  
  433. sub connect_to_signal {
  434.     my $self = shift;
  435.     my $name = shift;
  436.     my $code = shift;
  437.  
  438.     my $ins = $self->_introspector;
  439.     if (!$ins) {
  440.     die "no introspection data available for '" . $self->get_object_path .
  441.         "', use the connect_to_signal_in method instead";
  442.     }
  443.     my @interfaces = $ins->has_signal($name);
  444.  
  445.     if ($#interfaces == -1) {
  446.     die "no signal with name '$name' is exported in object '" .
  447.         $self->get_object_path . "'\n";
  448.     } elsif ($#interfaces > 0) {
  449.     die "signal with name '$name' is exported " .
  450.         "in multiple interfaces of '" . $self->get_object_path . "'" .
  451.         "use the connect_to_signal_in method instead";
  452.     }
  453.  
  454.     $self->connect_to_signal_in($name, $interfaces[0], $code);
  455. }
  456.  
  457.  
  458. sub _dispatch {
  459.     my $self = shift;
  460.     my $connection = shift;
  461.     my $message = shift;
  462.  
  463.     # Experiment in handling dispatch for child objects internally
  464. #     my $path = $message->get_path;
  465. #     while ($path ne $self->get_object_path) {
  466. #    if (exists $self->{children}->{$path}) {
  467. #        $self->{children}->{$path}->_dispatch($connection, $message);
  468. #        return;
  469. #    }
  470. #    $path =~ s,/[^/]+$,,;
  471. #     }
  472.  
  473.     my $reply;
  474.     my $method_name = $message->get_member;
  475.     my $interface = $message->get_interface;
  476.     if ($interface eq "org.freedesktop.DBus.Introspectable") {
  477.     if ($method_name eq "Introspect" &&
  478.         $self->_introspector &&
  479.         $ENABLE_INTROSPECT) {
  480.         my $xml = $self->_introspector->format($self);
  481.         $reply = $connection->make_method_return_message($message);
  482.  
  483.         $self->_introspector->encode($reply, "methods", $method_name, "returns", $xml);
  484.     }
  485.     } elsif ($interface eq "org.freedesktop.DBus.Properties") {
  486.     if ($method_name eq "Get") {
  487.         $reply = $self->_dispatch_prop_read($connection, $message);
  488.     } elsif ($method_name eq "Set") {
  489.         $reply = $self->_dispatch_prop_write($connection, $message);
  490.     }
  491.     } elsif ($self->can($method_name)) {
  492.     my $ins = $self->_introspector;
  493.     my @ret = eval {
  494.         my @args;
  495.         if ($ins) {
  496.         @args = $ins->decode($message, "methods", $method_name, "params");
  497.         } else {
  498.         @args = $message->get_args_list;
  499.         }
  500.  
  501.         $self->$method_name(@args);
  502.     };
  503.     if ($@) {
  504.         my $name = UNIVERSAL::isa($@, "Net::DBus::Error") ? $@->name : "org.freedesktop.DBus.Error.Failed";
  505.         my $desc = UNIVERSAL::isa($@, "Net::DBus::Error") ? $@->message : $@;
  506.         $reply = $connection->make_error_message($message,
  507.                           $name,
  508.                           $desc);
  509.     } else {
  510.         $reply = $connection->make_method_return_message($message);
  511.         if ($ins) {
  512.         $self->_introspector->encode($reply, "methods", $method_name, "returns", @ret);
  513.         } else {
  514.         $reply->append_args_list(@ret);
  515.         }
  516.     }
  517.     }
  518.  
  519.     if (!$reply) {
  520.     $reply = $connection->make_error_message($message,
  521.                          "org.freedesktop.DBus.Error.Failed",
  522.                          "No such method " . ref($self) . "->" . $method_name);
  523.     }
  524.  
  525.     if ($message->get_no_reply()) {
  526.     # Not sending reply
  527.     } else {
  528.     $self->get_service->get_bus->get_connection->send($reply);
  529.     }
  530. }
  531.  
  532.  
  533. sub _dispatch_prop_read {
  534.     my $self = shift;
  535.     my $connection = shift;
  536.     my $message = shift;
  537.  
  538.     my $ins = $self->_introspector;
  539.  
  540.     if (!$ins) {
  541.     return $connection->make_error_message($message,
  542.                            "org.freedesktop.DBus.Error.Failed",
  543.                            "no introspection data exported for properties");
  544.     }
  545.  
  546.     my ($pinterface, $pname) = $ins->decode($message, "methods", "Get", "params");
  547.  
  548.     if (!$ins->has_property($pname, $pinterface)) {
  549.     return $connection->make_error_message($message,
  550.                            "org.freedesktop.DBus.Error.Failed",
  551.                            "no property '$pname' exported in interface '$pinterface'");
  552.     }
  553.  
  554.     if (!$ins->is_property_readable($pinterface, $pname)) {
  555.     return $connection->make_error_message($message,
  556.                            "org.freedesktop.DBus.Error.Failed",
  557.                            "property '$pname' in interface '$pinterface' is not readable");
  558.     }
  559.  
  560.     if ($self->can($pname)) {
  561.     my $value = eval {
  562.         $self->$pname;
  563.     };
  564.     if ($@) {
  565.         return $connection->make_error_message($message,
  566.                            "org.freedesktop.DBus.Error.Failed",
  567.                            "error reading '$pname' in interface '$pinterface': $@");
  568.     } else {
  569.         my $reply = $connection->make_method_return_message($message);
  570.  
  571.         $self->_introspector->encode($reply, "methods", "Get", "returns", $value);
  572.         return $reply;
  573.     }
  574.     } else {
  575.     return $connection->make_error_message($message,
  576.                            "org.freedesktop.DBus.Error.Failed",
  577.                            "no method to read property '$pname' in interface '$pinterface'");
  578.     }
  579. }
  580.  
  581. sub _dispatch_prop_write {
  582.     my $self = shift;
  583.     my $connection = shift;
  584.     my $message = shift;
  585.  
  586.     my $ins = $self->_introspector;
  587.  
  588.     if (!$ins) {
  589.     return $connection->make_error_message($message,
  590.                            "org.freedesktop.DBus.Error.Failed",
  591.                            "no introspection data exported for properties");
  592.     }
  593.  
  594.     my ($pinterface, $pname, $pvalue) = $ins->decode($message, "methods", "Set", "params");
  595.  
  596.     if (!$ins->has_property($pname, $pinterface)) {
  597.     return $connection->make_error_message($message,
  598.                            "org.freedesktop.DBus.Error.Failed",
  599.                            "no property '$pname' exported in interface '$pinterface'");
  600.     }
  601.  
  602.     if (!$ins->is_property_writable($pinterface, $pname)) {
  603.     return $connection->make_error_message($message,
  604.                            "org.freedesktop.DBus.Error.Failed",
  605.                            "property '$pname' in interface '$pinterface' is not writable");
  606.     }
  607.  
  608.     if ($self->can($pname)) {
  609.     eval {
  610.         $self->$pname($pvalue);
  611.     };
  612.     if ($@) {
  613.         return $connection->make_error_message($message,
  614.                            "org.freedesktop.DBus.Error.Failed",
  615.                            "error writing '$pname' in interface '$pinterface': $@");
  616.     } else {
  617.         return $connection->make_method_return_message($message);
  618.     }
  619.     } else {
  620.     return $connection->make_error_message($message,
  621.                            "org.freedesktop.DBus.Error.Failed",
  622.                            "no method to write property '$pname' in interface '$pinterface'");
  623.     }
  624. }
  625.  
  626.  
  627. sub _introspector {
  628.     my $self = shift;
  629.  
  630.     if (!$self->{introspected}) {
  631.     $self->{introspector} = Net::DBus::Exporter::_dbus_introspector(ref($self));
  632.     $self->{introspected} = 1;
  633.     }
  634.     return $self->{introspector};
  635. }
  636.  
  637. 1;
  638.  
  639.  
  640. =pod
  641.  
  642. =back
  643.  
  644. =head1 AUTHORS
  645.  
  646. Daniel P. Berrange
  647.  
  648. =head1 COPYRIGHT
  649.  
  650. Copyright (C) 2005-2006 Daniel P. Berrange
  651.  
  652. =head1 SEE ALSO
  653.  
  654. L<Net::DBus>, L<Net::DBus::Service>, L<Net::DBus::RemoteObject>,
  655. L<Net::DBus::Exporter>.
  656.  
  657. =cut
  658.