home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Binding / Message.pm < prev    next >
Encoding:
Perl POD Document  |  2006-04-12  |  10.7 KB  |  466 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: Message.pm,v 1.13 2006/02/03 13:30:14 dan Exp $
  20.  
  21. =pod
  22.  
  23. =head1 NAME
  24.  
  25. Net::DBus::Binding::Message - Base class for messages
  26.  
  27. =head1 SYNOPSIS
  28.  
  29. Sending a message
  30.  
  31.   my $msg = new Net::DBus::Binding::Message::Signal;
  32.   my $iterator = $msg->iterator;
  33.  
  34.   $iterator->append_byte(132);
  35.   $iterator->append_int32(14241);
  36.  
  37.   $connection->send($msg);
  38.  
  39. =head1 DESCRIPTION
  40.  
  41. Provides a base class for the different kinds of
  42. message that can be sent/received. Instances of
  43. this class are never instantiated directly, rather
  44. one of the four sub-types L<Net::DBus::Binding::Message::Signal>,
  45. L<Net::DBus::Binding::Message::MethodCall>, L<Net::DBus::Binding::Message::MethodReturn>,
  46. L<Net::DBus::Binding::Message::Error> should be used.
  47.  
  48. =head1 CONSTANTS
  49.  
  50. The following constants are defined in this module. They are
  51. not exported into the caller's namespace & thus must be referenced
  52. with their fully qualified package names
  53.  
  54. =over 4
  55.  
  56. =item TYPE_ARRAY
  57.  
  58. Constant representing the signature value associated with the
  59. array data type.
  60.  
  61. =item TYPE_BOOLEAN
  62.  
  63. Constant representing the signature value associated with the
  64. boolean data type.
  65.  
  66. =item TYPE_BYTE
  67.  
  68. Constant representing the signature value associated with the
  69. byte data type.
  70.  
  71. =item TYPE_DICT_ENTRY
  72.  
  73. Constant representing the signature value associated with the
  74. dictionary entry data type.
  75.  
  76. =item TYPE_DOUBLE
  77.  
  78. Constant representing the signature value associated with the
  79. IEEE double precision floating point data type.
  80.  
  81. =item TYPE_INT16
  82.  
  83. Constant representing the signature value associated with the
  84. signed 16 bit integer data type.
  85.  
  86. =item TYPE_INT32
  87.  
  88. Constant representing the signature value associated with the
  89. signed 32 bit integer data type.
  90.  
  91. =item TYPE_INT64
  92.  
  93. Constant representing the signature value associated with the
  94. signed 64 bit integer data type.
  95.  
  96. =item TYPE_OBJECT_PATH
  97.  
  98. Constant representing the signature value associated with the
  99. object path data type.
  100.  
  101. =item TYPE_STRING
  102.  
  103. Constant representing the signature value associated with the
  104. UTF-8 string data type.
  105.  
  106. =item TYPE_SIGNATURE
  107.  
  108. Constant representing the signature value associated with the
  109. signature data type.
  110.  
  111. =item TYPE_STRUCT
  112.  
  113. Constant representing the signature value associated with the
  114. struct data type.
  115.  
  116. =item TYPE_UINT16
  117.  
  118. Constant representing the signature value associated with the
  119. unsigned 16 bit integer data type.
  120.  
  121. =item TYPE_UINT32
  122.  
  123. Constant representing the signature value associated with the
  124. unsigned 32 bit integer data type.
  125.  
  126. =item TYPE_UINT64
  127.  
  128. Constant representing the signature value associated with the
  129. unsigned 64 bit integer data type.
  130.  
  131. =item TYPE_VARIANT
  132.  
  133. Constant representing the signature value associated with the
  134. variant data type.
  135.  
  136. =back
  137.  
  138. =head1 METHODS
  139.  
  140. =over 4
  141.  
  142. =cut
  143.  
  144. package Net::DBus::Binding::Message;
  145.  
  146. use 5.006;
  147. use strict;
  148. use warnings;
  149. use Carp;
  150.  
  151. use Net::DBus::Binding::Iterator;
  152. use Net::DBus::Binding::Message::Signal;
  153. use Net::DBus::Binding::Message::MethodCall;
  154. use Net::DBus::Binding::Message::MethodReturn;
  155. use Net::DBus::Binding::Message::Error;
  156.  
  157. =item my $msg = Net::DBus::Binding::Message->new(message => $rawmessage);
  158.  
  159. Creates a new message object, initializing it with the underlying C
  160. message object given by the C<message> object. This constructor is
  161. intended for internal use only, instead refer to one of the four
  162. sub-types for this class for specific message types
  163.  
  164. =cut
  165.  
  166. sub new {
  167.     my $proto = shift;
  168.     my $class = ref($proto) || $proto;
  169.     my %params = @_;
  170.     my $self = {};
  171.  
  172.     $self->{message} = exists $params{message} ? $params{message} : 
  173.     (Net::DBus::Binding::Message::_create(exists $params{type} ? $params{type} : confess "type parameter is required"));
  174.  
  175.     bless $self, $class;
  176.     
  177.     if ($class eq "Net::DBus::Binding::Message") {
  178.     $self->_specialize;
  179.     }
  180.  
  181.     return $self;
  182. }
  183.  
  184. sub _specialize {
  185.     my $self = shift;
  186.     
  187.     my $type = $self->get_type;
  188.     if ($type == &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_CALL) {
  189.     bless $self, "Net::DBus::Binding::Message::MethodCall";
  190.     } elsif ($type == &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN) {
  191.     bless $self, "Net::DBus::Binding::Message::MethodReturn";
  192.     } elsif ($type == &Net::DBus::Binding::Message::MESSAGE_TYPE_ERROR) {
  193.     bless $self, "Net::DBus::Binding::Message::Error";
  194.     } elsif ($type == &Net::DBus::Binding::Message::MESSAGE_TYPE_SIGNAL) {
  195.     bless $self, "Net::DBus::Binding::Message::Signal";
  196.     } else {
  197.     warn "Unknown message type $type\n";
  198.     }
  199. }
  200.  
  201. =item my $type = $msg->get_type
  202.  
  203. Retrieves the type code for this message. The returned value corresponds
  204. to one of the four C<Net::DBus::Binding::Message::MESSAGE_TYPE_*> constants.
  205.  
  206. =cut
  207.  
  208. sub get_type {
  209.     my $self = shift;
  210.  
  211.     return $self->{message}->dbus_message_get_type;
  212. }
  213.  
  214. =item my $interface = $msg->get_interface
  215.  
  216. Retrieves the name of the interface targetted by this message, possibly
  217. an empty string if there is no applicable interface for this message.
  218.  
  219. =cut
  220.  
  221. sub get_interface {
  222.     my $self = shift;
  223.     
  224.     return $self->{message}->dbus_message_get_interface;
  225. }
  226.  
  227. =item my $path = $msg->get_path
  228.  
  229. Retrieves the object path associated with the message, possibly an
  230. empty string if there is no applicable object for this message.
  231.  
  232. =cut
  233.  
  234. sub get_path {
  235.     my $self = shift;
  236.     
  237.     return $self->{message}->dbus_message_get_path;
  238. }
  239.  
  240. =item my $name = $msg->get_destination
  241.  
  242. Retrieves the uniqe or well-known bus name for client intended to be
  243. the recipient of the message. Possibly returns an empty string if
  244. the message is being broadcast to all clients.
  245.  
  246. =cut
  247.  
  248. sub get_destination {
  249.     my $self = shift;
  250.     
  251.     return $self->{message}->dbus_message_get_destination;
  252. }
  253.  
  254. =item my $name = $msg->get_sender
  255.  
  256. Retireves the unique name of the client sending the message
  257.  
  258. =cut
  259.  
  260. sub get_sender {
  261.     my $self = shift;
  262.     
  263.     return $self->{message}->dbus_message_get_sender;
  264. }
  265.  
  266. =item my $serial = $msg->get_serial
  267.  
  268. Retrieves the unique serial number of this message. The number
  269. is guarenteed unique for as long as the connection over which
  270. the message was sent remains open. May return zero, if the message
  271. is yet to be sent.
  272.  
  273. =cut
  274.  
  275. sub get_serial {
  276.     my $self = shift;
  277.     
  278.     return $self->{message}->dbus_message_get_serial;
  279. }
  280.  
  281. =item my $name = $msg->get_member
  282.  
  283. For method calls, retrieves the name of the method to be invoked,
  284. while for signals, retrieves the name of the signal.
  285.  
  286. =cut
  287.  
  288. sub get_member {
  289.     my $self = shift;
  290.     
  291.     return $self->{message}->dbus_message_get_member;
  292. }
  293.  
  294. =item my $sig = $msg->get_signature
  295.  
  296. Retrieves a string representing the type signature of the values
  297. packed into the body of the message.
  298.  
  299. =cut
  300.  
  301. sub get_signature {
  302.     my $self = shift;
  303.     
  304.     return $self->{message}->dbus_message_get_signature;
  305. }
  306.  
  307. =item $msg->set_sender($name)
  308.  
  309. Set the name of the client sending the message. The name must
  310. be the unique name of the client.
  311.  
  312. =cut
  313.  
  314. sub set_sender {
  315.     my $self = shift;
  316.     $self->{message}->dbus_message_set_sender(@_);
  317. }
  318.  
  319. =item $msg->set_destination($name)
  320.  
  321. Set the name of the intended recipient of the message. This is
  322. typically used for signals to switch them from broadcast to
  323. unicast.
  324.  
  325. =cut
  326.  
  327. sub set_destination {
  328.     my $self = shift;
  329.     $self->{message}->dbus_message_set_destination(@_);
  330. }
  331.  
  332. =item my $iterator = $msg->iterator;
  333.  
  334. Retrieves an iterator which can be used for reading or
  335. writing fields of the message. The returned object is
  336. an instance of the C<Net::DBus::Binding::Iterator> class.
  337.  
  338. =cut
  339.  
  340. sub iterator {
  341.     my $self = shift;
  342.     my $append = @_ ? shift : 0;
  343.     
  344.     if ($append) {
  345.     return Net::DBus::Binding::Message::_iterator_append($self->{message});
  346.     } else {
  347.     return Net::DBus::Binding::Message::_iterator($self->{message});
  348.     }
  349. }
  350.  
  351. =item $boolean = $msg->get_no_reply()
  352.  
  353. Gets the flag indicating whether the message is expecting
  354. a reply to be sent. 
  355.  
  356. =cut
  357.  
  358. sub get_no_reply {
  359.     my $self = shift;
  360.     
  361.     return $self->{message}->dbus_message_get_no_reply;
  362. }
  363.  
  364. =item $msg->set_no_reply($boolean)
  365.  
  366. Toggles the flag indicating whether the message is expecting
  367. a reply to be sent. All method call messages expect a reply
  368. by default. By toggling this flag the communication latency
  369. is reduced by removing the need for the client to wait
  370.  
  371. =cut
  372.  
  373.  
  374. sub set_no_reply {
  375.     my $self = shift;
  376.     my $flag = shift;
  377.     
  378.     $self->{message}->dbus_message_set_no_reply($flag);
  379. }
  380.  
  381. =item my @values = $msg->get_args_list
  382.  
  383. De-marshall all the values in the body of the message, using the 
  384. message signature to identify data types. The values are returned
  385. as a list.
  386.  
  387. =cut
  388.  
  389. sub get_args_list {
  390.     my $self = shift;
  391.     
  392.     my @ret;    
  393.     my $iter = $self->iterator;
  394.     if ($iter->get_arg_type() != &Net::DBus::Binding::Message::TYPE_INVALID) {
  395.     do {
  396.         push @ret, $iter->get();
  397.     } while ($iter->next);
  398.     }
  399.  
  400.     return @ret;
  401. }
  402.  
  403. =item $msg->append_args_list(@values)
  404.  
  405. Append a set of values to the body of the message. Values will
  406. be encoded as either a string, list or dictionary as appropriate
  407. to their Perl data type. For more specific data typing needs,
  408. the L<Net::DBus::Binding::Iterator> object should be used instead.
  409.  
  410. =cut
  411.  
  412. sub append_args_list {
  413.     my $self = shift;
  414.     my @args = @_;
  415.     
  416.     my $iter = $self->iterator(1);
  417.     foreach my $arg (@args) {
  418.     $iter->append($arg);
  419.     }
  420. }
  421.  
  422. # To keep autoloader quiet
  423. sub DESTROY {
  424. }
  425.  
  426. sub AUTOLOAD {
  427.     # This AUTOLOAD is used to 'autoload' constants from the constant()
  428.     # XS function.
  429.  
  430.     my $constname;
  431.     our $AUTOLOAD;
  432.     ($constname = $AUTOLOAD) =~ s/.*:://;
  433.  
  434.     croak "&Net::DBus::Binding::Message::constant not defined" if $constname eq '_constant';
  435.  
  436.     if (!exists $Net::DBus::Binding::Message::_constants{$constname}) {
  437.         croak "no such constant \$Net::DBus::Binding::Message::$constname";
  438.     }
  439.  
  440.     {
  441.     no strict 'refs';
  442.     *$AUTOLOAD = sub { $Net::DBus::Binding::Message::_constants{$constname} };
  443.     }
  444.     goto &$AUTOLOAD;
  445. }
  446.  
  447. 1;
  448.  
  449. =pod
  450.  
  451. =back
  452.  
  453. =head1 SEE ALSO
  454.  
  455. L<Net::DBus::Binding::Server>, L<Net::DBus::Binding::Connection>, L<Net::DBus::Binding::Message::Signal>, L<Net::DBus::Binding::Message::MethodCall>, L<Net::DBus::Binding::Message::MethodReturn>, L<Net::DBus::Binding::Message::Error>
  456.  
  457. =head1 AUTHOR
  458.  
  459. Daniel Berrange E<lt>dan@berrange.comE<gt>
  460.  
  461. =head1 COPYRIGHT
  462.  
  463. Copyright 2004 by Daniel Berrange
  464.  
  465. =cut
  466.