home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Exporter.pm < prev    next >
Encoding:
Perl POD Document  |  2006-03-02  |  15.0 KB  |  549 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: Exporter.pm,v 1.11 2006/02/03 13:43:50 dan Exp $
  20.  
  21. =pod
  22.  
  23. =head1 NAME
  24.  
  25. Net::DBus::Exporter - Export object methods and signals to the bus
  26.  
  27. =head1 SYNOPSIS
  28.  
  29.   # Define a new package for the object we're going
  30.   # to export
  31.   package Demo::HelloWorld;
  32.  
  33.   # Specify the main interface provided by our object
  34.   use Net::DBus::Exporter qw(org.example.demo.Greeter);
  35.  
  36.   # We're going to be a DBus object
  37.   use base qw(Net::DBus::Object);
  38.  
  39.   # Export a 'Greeting' signal taking a stringl string parameter
  40.   dbus_signal("Greeting", ["string"]);
  41.  
  42.   # Export 'Hello' as a method accepting a single string
  43.   # parameter, and returning a single string value
  44.   dbus_method("Hello", ["string"], ["string"]);
  45.  
  46.   # Export 'Goodbye' as a method accepting a single string
  47.   # parameter, and returning a single string, but put it
  48.   # in the 'org.exaple.demo.Farewell' interface
  49.   dbus_method("Goodbye", ["string"], ["string"], "org.example.demo.Farewell");
  50.  
  51. =head1 DESCRIPTION
  52.  
  53. The C<Net::DBus::Exporter> module is used to export methods
  54. and signals defined in an object to the message bus. Since
  55. Perl is a loosely typed language it is not possible to automatically
  56. determine correct type information for methods to be exported.
  57. Thus when sub-classing L<Net::DBus::Object>, this package will
  58. provide the type information for methods and signals.
  59.  
  60. When importing this package, an optional argument can be supplied
  61. to specify the default interface name to associate with methods
  62. and signals, for which an explicit interface is not specified.
  63. Thus in the common case of objects only providing a single interface,
  64. this removes the need to repeat the interface name against each
  65. method exported.
  66.  
  67. =head1 SCALAR TYPES
  68.  
  69. When specifying scalar data types for parameters and return values,
  70. the following string constants must be used to denote the data
  71. type. When values corresponding to these types are (un)marshalled
  72. they are represented as the Perl SCALAR data type (see L<perldata>).
  73.  
  74. =over 4
  75.  
  76. =item "string"
  77.  
  78. A UTF-8 string of characters
  79.  
  80. =item "int16"
  81.  
  82. A 16-bit signed integer
  83.  
  84. =item "uint16"
  85.  
  86. A 16-bit unsigned integer
  87.  
  88. =item "int32"
  89.  
  90. A 32-bit signed integer
  91.  
  92. =item "uint32"
  93.  
  94. A 32-bit unsigned integer
  95.  
  96. =item "int64"
  97.  
  98. A 64-bit signed integer. NB, this type is not supported by
  99. many builds of Perl on 32-bit platforms, so if used, your
  100. data is liable to be truncated at 32-bits.
  101.  
  102. =item "uint64"
  103.  
  104. A 64-bit unsigned integer. NB, this type is not supported by
  105. many builds of Perl on 32-bit platforms, so if used, your
  106. data is liable to be truncated at 32-bits.
  107.  
  108. =item "byte"
  109.  
  110. A single 8-bit byte
  111.  
  112. =item "bool"
  113.  
  114. A boolean value
  115.  
  116. =item "double"
  117.  
  118. An IEEE double-precision floating point
  119.  
  120. =back
  121.  
  122. =head1 COMPOUND TYPES
  123.  
  124. When specifying compound data types for parameters and return
  125. values, an array reference must be used, with the first element
  126. being the name of the compound type. 
  127.  
  128. =over 4
  129.  
  130. =item ["array", ARRAY-TYPE]
  131.  
  132. An array of values, whose type os C<ARRAY-TYPE>. The C<ARRAY-TYPE>
  133. can be either a scalar type name, or a nested compound type. When
  134. values corresponding to the array type are (un)marshalled, they 
  135. are represented as the Perl ARRAY data type (see L<perldata>). If,
  136. for example, a method was declared to have a single parameter with
  137. the type, ["array", "string"], then when calling the method one
  138. would provide a array reference of strings:
  139.  
  140.     $object->hello(["John", "Doe"])
  141.  
  142. =item ["dict", KEY-TYPE, VALUE-TYPE]
  143.  
  144. A dictionary of values, more commonly known as a hash table. The
  145. C<KEY-TYPE> is the name of the scalar data type used for the dictionary
  146. keys. The C<VALUE-TYPE> is the name of the scalar, or compound
  147. data type used for the dictionary values. When values corresponding
  148. to the dict type are (un)marshalled, they are represented as the
  149. Perl HASH data type (see L<perldata>). If, for example, a method was
  150. declared to have a single parameter with the type ["dict", "string", "string"],
  151. then when calling the method one would provide a hash reference 
  152. of strings,
  153.  
  154.    $object->hello({forename => "John", surname => "Doe"});
  155.  
  156. =item ["struct", VALUE-TYPE-1, VALUE-TYPE-2]
  157.  
  158. A structure of values, best thought of as a variation on the array
  159. type where the elements can vary. Many languages have an explicit
  160. name associated with each value, but since Perl does not have a
  161. native representation of structures, they are represented by the
  162. LIST data type. If, for exaple, a method was declared to have a single
  163. parameter with the type ["struct", "string", "string"], corresponding
  164. to the C structure 
  165.  
  166.     struct {
  167.       char *forename;
  168.       char *surname;
  169.     } name;
  170.  
  171. then, when calling the method one would provide an array refernce
  172. with the values orded to match the structure
  173.  
  174.    $object->hello(["John", "Doe"]);
  175.  
  176. =back
  177.  
  178. =head1 MAGIC TYPES
  179.  
  180. When specifying introspection data for an exported service, there
  181. are a couple of so called C<magic> types. Parameters declared as
  182. magic types are not visible to clients, but instead their values
  183. are provided automatically by the server side bindings. One use of
  184. magic types is to get an extra parameter passed with the unique 
  185. name of the caller invoking the method.
  186.  
  187. =over 4
  188.  
  189. =item "caller"
  190.  
  191. The value passed in is the unique name of the caller of the method.
  192. Unique names are strings automatically assigned to client connections
  193. by the bus daemon, for example ':1.15'
  194.  
  195. =item "serial"
  196.  
  197. The value passed in is an integer within the scope of a caller, which 
  198. increments on every method call. 
  199.  
  200. =back
  201.  
  202. =head1 ANNOTATIONS
  203.  
  204. When exporting methods, signals & properties, in addition to the core
  205. data typing information, a number of metadata annotations are possible.
  206. These are specified by passing a hash reference with the desired keys
  207. as the last parameter when defining the export. The following annotations
  208. are currently supported
  209.  
  210. =over 4
  211.  
  212. =item no_return
  213.  
  214. Indicate that this method does not return any value, and thus no reply
  215. message should be sent over the wire, likewise informing the clients
  216. not to expect / wait for a reply message
  217.  
  218. =item deprecated
  219.  
  220. Indicate that use of this method/signal/property is discouraged, and 
  221. it may disappear altogether in a future release. Clients will typically
  222. print out a warning message when a deprecated method/signal/property
  223. is used.
  224.  
  225. =back
  226.  
  227. =head1 METHODS
  228.  
  229. =over 4
  230.  
  231. =cut
  232.  
  233. package Net::DBus::Exporter;
  234.  
  235. use vars qw(@ISA @EXPORT %dbus_exports %dbus_introspectors);
  236.  
  237. use Net::DBus::Binding::Introspector;
  238.  
  239. use warnings;
  240. use strict;
  241.  
  242. use Exporter;
  243. @ISA = qw(Exporter);
  244.  
  245. @EXPORT = qw(dbus_method dbus_signal dbus_property);
  246.  
  247.  
  248. sub import {
  249.     my $class = shift;
  250.  
  251.     my $caller = caller;
  252.     if (exists $dbus_exports{$caller}) {
  253.     warn "$caller is already registered with Net::DBus::Exporter";
  254.     return;
  255.     }
  256.  
  257.     $dbus_exports{$caller} = {
  258.     methods => {},
  259.     signals => {},
  260.     props => {},
  261.     };
  262.     die "usage: use Net::DBus::Exporter 'interface-name';" unless @_;
  263.  
  264.     my $interface = shift;
  265.     die "interface name '$interface' is not valid." .
  266.     "Names must consist of tokens using the characters a-z, A-Z, 0-9, _, " .
  267.     "with at least two tokens, separated by '.'\n"
  268.     unless $interface =~ /^[a-zA-Z]\w*(\.[a-zA-Z]\w*)+$/;
  269.     $dbus_exports{$caller}->{interface} = $interface;
  270.  
  271.     $class->export_to_level(1, "", @EXPORT);
  272. }
  273.  
  274. sub _dbus_introspector {
  275.     my $object = shift;
  276.     my $class = shift;
  277.  
  278.     $class = ref($object) unless $class;
  279.     die "no introspection data available for '" . 
  280.     $object->get_object_path . 
  281.     "' and object is not cast to any interface" unless $class;
  282.     
  283.     if (!exists $dbus_exports{$class}) {
  284.     # If this class has not been exported, lets look
  285.     # at the parent class & return its introspection
  286.         # data instead.
  287.     no strict 'refs';
  288.     if (defined (*{"${class}::ISA"})) {
  289.         my @isa = @{"${class}::ISA"};
  290.         foreach my $parent (@isa) {
  291.         # We don't recurse to Net::DBus::Object
  292.         # since we need to give sub-classes the
  293.         # choice of not supporting introspection
  294.         next if $parent eq "Net::DBus::Object";
  295.  
  296.         my $ins = &_dbus_introspector($object, $parent);
  297.         if ($ins) {
  298.             return $ins;
  299.         }
  300.         }
  301.     }
  302.     return undef;
  303.     }
  304.  
  305.     unless (exists $dbus_introspectors{$class}) {
  306.     my $is = Net::DBus::Binding::Introspector->new(object_path => $object->get_object_path);
  307.     
  308.     &_dbus_introspector_add(ref($object), $is);
  309.     $dbus_introspectors{$class} = $is;
  310.     }
  311.     
  312.     return $dbus_introspectors{$class};
  313. }
  314.  
  315. sub _dbus_introspector_add {
  316.     my $class = shift;
  317.     my $introspector = shift;
  318.  
  319.     my $exports = $dbus_exports{$class};
  320.     if ($exports) {
  321.     foreach my $method (keys %{$exports->{methods}}) {
  322.         my ($params, $returns, $interface, $attributes) = @{$exports->{methods}->{$method}};
  323.         $introspector->add_method($method, $params, $returns, $interface, $attributes);
  324.     }
  325.     foreach my $prop (keys %{$exports->{props}}) {
  326.         my ($type, $access, $interface, $attributes) = @{$exports->{props}->{$prop}};
  327.         $introspector->add_property($prop, $type, $access, $interface, $attributes);
  328.     }
  329.     foreach my $signal (keys %{$exports->{signals}}) {
  330.         my ($params, $interface, $attributes) = @{$exports->{signals}->{$signal}};
  331.         $introspector->add_signal($signal, $params, $interface, $attributes);
  332.     }
  333.     }
  334.     
  335.     if (defined (*{"${class}::ISA"})) {
  336.     no strict "refs";
  337.     my @isa = @{"${class}::ISA"};
  338.     foreach my $parent (@isa) {
  339.         &_dbus_introspector_add($parent, $introspector);
  340.     }
  341.     }
  342. }
  343.  
  344. =item dbus_method($name, $params, $returns, [\%annotations]);
  345.  
  346. =item dbus_method($name, $params, $returns, $interface, [\%annotations]);
  347.  
  348. Exports a method called C<$name>, having parameters whose types
  349. are defined by C<$params>, and returning values whose types are
  350. defined by C<$returns>. If the C<$interface> parameter is 
  351. provided, then the method is associated with that interface, otherwise
  352. the default interface for the calling package is used. The
  353. value for the C<$params> parameter should be an array reference
  354. with each element defining the data type of a parameter to the
  355. method. Likewise, the C<$returns> parameter should be an array 
  356. reference with each element defining the data type of a return
  357. value. If it not possible to export a method which accepts a
  358. variable number of parameters, or returns a variable number of
  359. values.
  360.  
  361. =cut
  362.  
  363. sub dbus_method {
  364.     my $name = shift;
  365.     my $params = [];
  366.     my $returns = [];
  367.     my $caller = caller;
  368.     my $interface = $dbus_exports{$caller}->{interface};
  369.     my %attributes;
  370.     
  371.     if (@_ && ref($_[0]) eq "ARRAY") {
  372.     $params = shift;
  373.     }
  374.     if (@_ && ref($_[0]) eq "ARRAY") {
  375.     $returns = shift;
  376.     }
  377.     if (@_ && !ref($_[0])) {
  378.     $interface = shift;
  379.     }
  380.     if (@_ && ref($_[0]) eq "HASH") {
  381.     %attributes = %{$_[0]};
  382.     }
  383.  
  384.     if (!$interface) {
  385.     die "interface not specified & no default interface defined";
  386.     }
  387.     
  388.     $dbus_exports{$caller}->{methods}->{$name} = [$params, $returns, $interface, \%attributes];
  389. }
  390.  
  391.  
  392. =item dbus_property($name, $type, $access, [\%attributes]);
  393.  
  394. =item dbus_property($name, $type, $access, $interface, [\%attributes]);
  395.  
  396. Exports a property called C<$name>, whose data type is C<$type>.
  397. If the C<$interface> parameter is provided, then the property is 
  398. associated with that interface, otherwise the default interface 
  399. for the calling package is used. 
  400.  
  401. =cut
  402.  
  403. sub dbus_property {
  404.     my $name = shift;
  405.     my $type = "string";
  406.     my $access = "readwrite";
  407.     my $caller = caller;
  408.     my $interface = $dbus_exports{$caller}->{interface};
  409.     my %attributes;
  410.     
  411.     if (@_ && !ref($_[0])) {
  412.     $type = shift;
  413.     }
  414.     if (@_ && !ref($_[0])) {
  415.     $access = shift;
  416.     }
  417.     if (@_ && !ref($_[0])) {
  418.     $interface = shift;
  419.     }
  420.     if ($_ && ref($_[0]) eq "HASH") {
  421.     %attributes = %{$_[0]};
  422.     }
  423.  
  424.     if (!$interface) {
  425.     die "interface not specified & no default interface defined";
  426.     }
  427.     
  428.     $dbus_exports{$caller}->{props}->{$name} = [$type, $access, $interface, \%attributes];
  429. }
  430.  
  431.  
  432. =item dbus_signal($name, $params);
  433.  
  434. =item dbus_signal($name, $params, $interface);
  435.  
  436. Exports a signal called C<$name>, having parameters whose types
  437. are defined by C<$params>, and returning values whose types are
  438. defined by C<$returns>. If the C<$interface> parameter is 
  439. provided, then the signal is associated with that interface, otherwise
  440. the default interface for the calling package is used. The
  441. value for the C<$params> parameter should be an array reference
  442. with each element defining the data type of a parameter to the
  443. signal. Signals do not have return values. It not possible to 
  444. export a signal which has a variable number of parameters.
  445.  
  446. =cut
  447.  
  448. sub dbus_signal {
  449.     my $name = shift;
  450.     my $params = [];
  451.     my $caller = caller;
  452.     my $interface = $dbus_exports{$caller}->{interface};
  453.     my %attributes;
  454.     
  455.     if (@_ && ref($_[0]) eq "ARRAY") {
  456.     $params = shift;
  457.     }
  458.     if (@_ && !ref($_[0])) {
  459.     $interface = shift;
  460.     }
  461.     if (@_ && ref($_[0]) eq "HASH") {
  462.     %attributes = %{$_[0]};
  463.     }
  464.  
  465.     if (!$interface) {
  466.     die "interface not specified & no default interface defined";
  467.     }
  468.  
  469.     $dbus_exports{$caller}->{signals}->{$name} = [$params, $interface, \%attributes];
  470. }
  471.  
  472. 1;
  473.  
  474. =back
  475.  
  476. =head1 EXAMPLES
  477.  
  478. =over 4
  479.  
  480. =item No paramters, no return values
  481.  
  482. A method which simply prints "Hello World" each time its called
  483.  
  484.    sub Hello {
  485.        my $self = shift;
  486.        print "Hello World\n";
  487.    }
  488.  
  489.    dbus_method("Hello", [], []);
  490.  
  491. =item One string parameter, returning an boolean value
  492.  
  493. A method which accepts a process name, issues the killall
  494. command on it, and returns a boolean value to indicate whether
  495. it was successful.
  496.  
  497.    sub KillAll {
  498.        my $self = shift;
  499.        my $processname = shift;
  500.        my $ret  = system("killall $processname");
  501.        return $ret == 0 ? 1 : 0;
  502.    }
  503.  
  504.    dbus_method("KillAll", ["string"], ["bool"]);
  505.  
  506. =item One list of strings parameter, returning a dictionary
  507.  
  508. A method which accepts a list of files names, stats them, and
  509. returns a dictionary containing the last modification times.
  510.  
  511.     sub LastModified {
  512.        my $self = shift;
  513.        my $files = shift;
  514.  
  515.        my %mods;
  516.        foreach my $file (@{$files}) {
  517.           $mods{$file} = (stat $file)[9];
  518.        }
  519.        return \%mods;
  520.     }
  521.  
  522.     dbus_method("LastModified", ["array", "string"], ["dict", "string", "int32"]);
  523.  
  524. =item Annotating methods with metdata
  525.  
  526. A method which is targetted for removal, and also does not
  527. return any value
  528.  
  529.     sub PlayMP3 {
  530.     my $self = shift;
  531.         my $track = shift;
  532.  
  533.         system "mpg123 $track &";
  534.     }
  535.  
  536.     dbus_method("PlayMP3", ["string"], [], { deprecated => 1, no_return => 1 });
  537.  
  538. =back
  539.  
  540. =head1 SEE ALSO
  541.  
  542. L<Net::DBus::Object>, L<Net::DBus::Binding::Introspector>
  543.  
  544. =head1 AUTHORS
  545.  
  546. Daniel P. Berrange <dan@berrange.com>
  547.  
  548. =cut
  549.