home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Object.pm < prev    next >
Encoding:
Perl POD Document  |  2006-06-03  |  17.7 KB  |  636 lines

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