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 / Connection.pm < prev    next >
Encoding:
Perl POD Document  |  2006-08-30  |  13.3 KB  |  544 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: Connection.pm,v 1.8 2006/01/27 15:34:24 dan Exp $
  20.  
  21. =pod
  22.  
  23. =head1 NAME
  24.  
  25. Net::DBus::Binding::Connection - A connection between client and server
  26.  
  27. =head1 SYNOPSIS
  28.  
  29. Creating a connection to a server and sending a message
  30.  
  31.   use Net::DBus::Binding::Connection;
  32.  
  33.   my $con = Net::DBus::Binding::Connection->new(address => "unix:path=/path/to/socket");
  34.  
  35.   $con->send($message);
  36.  
  37. Registering message handlers
  38.  
  39.   sub handle_something {
  40.       my $con = shift;
  41.       my $msg = shift;
  42.  
  43.       ... do something with the message...
  44.   }
  45.  
  46.   $con->register_message_handler(
  47.     "/some/object/path",
  48.     \&handle_something);
  49.  
  50. Hooking up to an event loop:
  51.  
  52.   my $reactor = Net::DBus::Binding::Reactor->new();
  53.  
  54.   $reactor->manage($con);
  55.  
  56.   $reactor->run();
  57.  
  58. =head1 DESCRIPTION
  59.  
  60. An outgoing connection to a server, or an incoming connection
  61. from a client. The methods defined on this module have a close
  62. correspondance to the dbus_connection_XXX methods in the C API,
  63. so for further details on their behaviour, the C API documentation
  64. may be of use.
  65.  
  66. =head1 METHODS
  67.  
  68. =over 4
  69.  
  70. =cut
  71.  
  72. package Net::DBus::Binding::Connection;
  73.  
  74. use 5.006;
  75. use strict;
  76. use warnings;
  77. use Carp;
  78.  
  79. use Net::DBus;
  80. use Net::DBus::Binding::Message::MethodReturn;
  81. use Net::DBus::Binding::PendingCall;
  82.  
  83. =item my $con = Net::DBus::Binding::Connection->new(address => "unix:path=/path/to/socket");
  84.  
  85. Creates a new connection to the remove server specified by
  86. the parameter C<address>. 
  87.  
  88. =cut
  89.  
  90. sub new {
  91.     my $proto = shift;
  92.     my $class = ref($proto) || $proto;
  93.     my %params = @_;
  94.     my $self = {};
  95.  
  96.     $self->{address} = exists $params{address} ? $params{address} : (exists $params{connection} ? "" : confess "address parameter is required");
  97.     $self->{connection} = exists $params{connection} ? $params{connection} : Net::DBus::Binding::Connection::_open($self->{address});
  98.  
  99.     bless $self, $class;
  100.  
  101.     $self->{connection}->_set_owner($self);
  102.  
  103.     return $self;
  104. }
  105.  
  106.  
  107. =item $status = $con->is_connected();
  108.  
  109. Returns zero if the connection has been disconnected,
  110. otherwise a positive value is returned.
  111.  
  112. =cut
  113.  
  114. sub is_connected {
  115.     my $self = shift;
  116.     
  117.     return $self->{connection}->dbus_connection_get_is_connected();
  118. }
  119.  
  120. =item $status = $con->is_authenticated();
  121.  
  122. Returns zero if the connection has not yet successfully
  123. completed authentication, otherwise a positive value is
  124. returned.
  125.  
  126. =cut
  127.  
  128. sub is_authenticated {
  129.     my $self = shift;
  130.     
  131.     return $self->{connection}->dbus_connection_get_is_authenticated();
  132. }
  133.  
  134.  
  135. =item $con->disconnect()
  136.  
  137. Closes this connection to the remote host. This method
  138. is called automatically during garbage collection (ie
  139. in the DESTROY method) if the programmer forgets to
  140. explicitly disconnect.
  141.  
  142. =cut
  143.  
  144. sub disconnect {
  145.     my $self = shift;
  146.     
  147.     $self->{connection}->dbus_connection_close();
  148. }
  149.  
  150. =item $con->flush()
  151.  
  152. Blocks execution until all data in the outgoing data
  153. stream has been sent. This method will not re-enter
  154. the application event loop.
  155.  
  156. =cut
  157.  
  158. sub flush {
  159.     my $self = shift;
  160.     
  161.     $self->{connection}->dbus_connection_flush();
  162. }
  163.  
  164.  
  165. =item $con->send($message)
  166.  
  167. Queues a message up for sending to the remote host.
  168. The data will be sent asynchronously as the applications
  169. event loop determines there is space in the outgoing 
  170. socket send buffer. To force immediate sending of the
  171. data, follow this method will a call to C<flush>. This
  172. method will return the serial number of the message,
  173. which can be used to identify a subsequent reply (if
  174. any).
  175.  
  176. =cut
  177.  
  178. sub send {
  179.     my $self = shift;
  180.     my $msg = shift;
  181.     
  182.     return $self->{connection}->_send($msg->{message});
  183. }
  184.  
  185. =item my $reply = $con->send_with_reply_and_block($msg, $timeout);
  186.  
  187. Queues a message up for sending to the remote host
  188. and blocks until it has been sent, and a corresponding
  189. reply received. The return value of this method will
  190. be a C<Net::DBus::Binding::Message::MethodReturn> or C<Net::DBus::Binding::Message::Error>
  191. object.
  192.  
  193. =cut
  194.  
  195. sub send_with_reply_and_block {
  196.     my $self = shift;
  197.     my $msg = shift;
  198.     my $timeout = shift;
  199.  
  200.     my $reply = $self->{connection}->_send_with_reply_and_block($msg->{message}, $timeout);
  201.  
  202.     my $type = $reply->dbus_message_get_type;
  203.     if ($type == &Net::DBus::Binding::Message::MESSAGE_TYPE_ERROR) {
  204.     return Net::DBus::Binding::Message::Error->new(replyto => $msg,
  205.                                message => $reply);
  206.     } elsif ($type == &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN) {
  207.     return Net::DBus::Binding::Message::MethodReturn->new(call => $msg,
  208.                                   message => $reply);
  209.     } else {
  210.     confess "unknown method reply type $type";
  211.     }
  212. }
  213.  
  214.  
  215. =item my $pending_call = $con->send_with_reply($msg, $timeout);
  216.  
  217. Queues a message up for sending to the remote host
  218. and returns immediately providing a reference to a
  219. C<Net::DBus::Binding::PendingCall> object. This object
  220. can be used to wait / watch for a reply. This allows
  221. methods to be processed asynchronously.
  222.  
  223. =cut
  224.  
  225. sub send_with_reply {
  226.     my $self = shift;
  227.     my $msg = shift;
  228.     my $timeout = shift;
  229.  
  230.     my $reply = $self->{connection}->_send_with_reply($msg->{message}, $timeout);
  231.  
  232.     return Net::DBus::Binding::PendingCall->new(method_call => $msg,
  233.                         pending_call => $reply);
  234. }
  235.  
  236.  
  237. =item $con->dispatch;
  238.  
  239. Dispatches any pending messages in the incoming queue
  240. to their message handlers. This method is typically
  241. called on each iteration of the main application event
  242. loop where data has been read from the incoming socket.
  243.  
  244. =cut
  245.  
  246. sub dispatch {
  247.     my $self = shift;
  248.     
  249.     $self->{connection}->_dispatch();
  250. }
  251.  
  252.  
  253. =item $message = $con->borrow_message
  254.  
  255. Temporarily removes the first message from the incoming
  256. message queue. No other thread may access the message
  257. while it is 'borrowed', so it should be replaced in the
  258. queue with the C<return_message> method, or removed 
  259. permanently with th C<steal_message> method as soon as
  260. is practical.
  261.  
  262. =cut
  263.  
  264. sub borrow_message {
  265.     my $self = shift;
  266.     
  267.     my $msg = $self->{connection}->dbus_connection_borrow_message();
  268.     return Net::DBus::Binding::Message->new(message => $msg);
  269. }
  270.  
  271. =item $con->return_message($msg)
  272.  
  273. Replaces a previously borrowed message in the incoming
  274. message queue for subsequent dispatch to registered 
  275. message handlers.
  276.  
  277. =cut
  278.  
  279. sub return_message {
  280.     my $self = shift;
  281.     my $msg = shift;
  282.     
  283.     $self->{connection}->dbus_connection_return_message($msg->{message});
  284. }
  285.  
  286.  
  287. =item $con->steal_message($msg)
  288.  
  289. Permanently remove a borrowed message from the incoming
  290. message queue. No registered message handlers will now
  291. be run for this message.
  292.  
  293. =cut
  294.  
  295. sub steal_message {
  296.     my $self = shift;
  297.     my $msg = shift;
  298.     
  299.     $self->{connection}->dbus_connection_steal_borrowed_message($msg->{message});
  300. }
  301.  
  302. =item $msg = $con->pop_message();
  303.  
  304. Permanently removes the first message on the incoming
  305. message queue, without running any registered message
  306. handlers. If you have hooked the connection up to an
  307. event loop (C<Net::DBus::Binding::Reactor> for example), you probably
  308. don't want to be calling this method.
  309.  
  310. =cut
  311.  
  312. sub pop_message {
  313.     my $self = shift;
  314.     
  315.     my $msg = $self->{connection}->dbus_connection_pop_message();
  316.     return Net::DBus::Binding::Message->new(message => $msg);
  317. }
  318.  
  319. =item $con->set_watch_callbacks(\&add_watch, \&remove_watch, \&toggle_watch);
  320.  
  321. Register a set of callbacks for adding, removing & updating 
  322. watches in the application's event loop. Each parameter
  323. should be a code reference, which on each invocation, will be
  324. supplied with two parameters, the connection object and the
  325. watch object. If you are using a C<Net::DBus::Binding::Reactor> object
  326. as the application event loop, then the 'manage' method on
  327. that object will call this on your behalf.
  328.  
  329. =cut
  330.  
  331. sub set_watch_callbacks {
  332.     my $self = shift;
  333.     my $add = shift;
  334.     my $remove = shift;
  335.     my $toggled = shift;
  336.  
  337.     $self->{add_watch} = $add;
  338.     $self->{remove_watch} = $remove;
  339.     $self->{toggled_watch} = $toggled;
  340.  
  341.     $self->{connection}->_set_watch_callbacks();
  342. }
  343.  
  344. =item $con->set_timeout_callbacks(\&add_timeout, \&remove_timeout, \&toggle_timeout);
  345.  
  346. Register a set of callbacks for adding, removing & updating 
  347. timeouts in the application's event loop. Each parameter
  348. should be a code reference, which on each invocation, will be
  349. supplied with two parameters, the connection object and the
  350. timeout object. If you are using a C<Net::DBus::Binding::Reactor> object
  351. as the application event loop, then the 'manage' method on
  352. that object will call this on your behalf.
  353.  
  354. =cut
  355.  
  356. sub set_timeout_callbacks {
  357.     my $self = shift;
  358.     my $add = shift;
  359.     my $remove = shift;
  360.     my $toggled = shift;
  361.  
  362.     $self->{add_timeout} = $add;
  363.     $self->{remove_timeout} = $remove;
  364.     $self->{toggled_timeout} = $toggled;
  365.  
  366.     $self->{connection}->_set_timeout_callbacks();
  367. }
  368.  
  369. =item $con->register_object_path($path, \&handler)
  370.  
  371. Registers a handler for messages whose path matches
  372. that specified in the C<$path> parameter. The supplied
  373. code reference will be invoked with two parameters, the
  374. connection object on which the message was received,
  375. and the message to be processed (an instance of the
  376. C<Net::DBus::Binding::Message> class).
  377.  
  378. =cut
  379.  
  380. sub register_object_path {
  381.     my $self = shift;
  382.     my $path = shift;
  383.     my $code = shift;
  384.  
  385.     my $callback = sub {
  386.     my $con = shift;
  387.     my $msg = shift;
  388.  
  389.     &$code($con, Net::DBus::Binding::Message->new(message => $msg));
  390.     };
  391.     $self->{connection}->_register_object_path($path, $callback);
  392. }
  393.  
  394. =item $con->unregister_object_path($path)
  395.  
  396. Unregisters the handler associated with the object path C<$path>. The
  397. handler would previously have been registered with the C<register_object_path>
  398. or C<register_fallback> methods.
  399.  
  400. =cut
  401.  
  402. sub unregister_object_path {
  403.     my $self = shift;
  404.     my $path = shift;
  405.     $self->{connection}->_unregister_object_path($path);
  406. }
  407.  
  408.  
  409. =item $con->register_fallback($path, \&handler)
  410.  
  411. Registers a handler for messages whose path starts with 
  412. the prefix specified in the C<$path> parameter. The supplied
  413. code reference will be invoked with two parameters, the
  414. connection object on which the message was received,
  415. and the message to be processed (an instance of the
  416. C<Net::DBus::Binding::Message> class).
  417.  
  418. =cut
  419.  
  420. sub register_fallback {
  421.     my $self = shift;
  422.     my $path = shift;
  423.     my $code = shift;
  424.  
  425.     my $callback = sub {
  426.     my $con = shift;
  427.     my $msg = shift;
  428.  
  429.     &$code($con, Net::DBus::Binding::Message->new(message => $msg));
  430.     };
  431.  
  432.     $self->{connection}->_register_fallback($path, $callback);
  433. }
  434.  
  435.  
  436. =item $con->set_max_message_size($bytes)
  437.  
  438. Sets the maximum allowable size of a single incoming
  439. message. Messages over this size will be rejected
  440. prior to exceeding this threshold. The message size
  441. is specified in bytes.
  442.  
  443. =cut
  444.  
  445. sub set_max_message_size {
  446.     my $self = shift;
  447.     my $size = shift;
  448.     
  449.     $self->{connection}->dbus_connection_set_max_message_size($size);
  450. }
  451.  
  452. =item $bytes = $con->get_max_message_size();
  453.  
  454. Retrieves the maximum allowable incoming
  455. message size. The returned size is measured
  456. in bytes.
  457.  
  458. =cut
  459.  
  460. sub get_max_message_size {
  461.     my $self = shift;
  462.     
  463.     return $self->{connection}->dbus_connection_get_max_message_size;
  464. }
  465.  
  466. =item $con->set_max_received_size($bytes)
  467.  
  468. Sets the maximum size of the incoming message queue.
  469. Once this threashold is exceeded, no more messages will
  470. be read from wire before one or more of the existing
  471. messages are dispatched to their registered handlers.
  472. The implication is that the message queue can exceed
  473. this threshold by at most the size of a single message.
  474.  
  475. =cut
  476.  
  477. sub set_max_received_size {
  478.     my $self = shift;
  479.     my $size = shift;
  480.     
  481.     $self->{connection}->dbus_connection_set_max_received_size($size);
  482. }
  483.  
  484. =item $bytes $con->get_max_received_size()
  485.  
  486. Retrieves the maximum incoming message queue size.
  487. The returned size is measured in bytes.
  488.  
  489. =cut
  490.  
  491. sub get_max_received_size {
  492.     my $self = shift;
  493.     
  494.     return $self->{connection}->dbus_connection_get_max_received_size;
  495. }
  496.  
  497.  
  498. =item $con->add_filter($coderef);
  499.  
  500. Adds a filter to the connection which will be invoked whenever a
  501. message is received. The C<$coderef> should be a reference to a
  502. subroutine, which returns a true value if the message should be
  503. filtered out, or a false value if the normal message dispatch
  504. should be performed.
  505.  
  506. =cut
  507.  
  508. sub add_filter {
  509.     my $self = shift;
  510.     my $callback = shift;
  511.     
  512.     $self->{connection}->_add_filter($callback);
  513. }
  514.  
  515.  
  516. sub _message_filter {
  517.     my $self = shift;
  518.     my $rawmsg = shift;
  519.     my $code = shift;
  520.     
  521.     my $msg = Net::DBus::Binding::Message->new(message => $rawmsg);
  522.     return &$code($self, $msg);
  523. }
  524.  
  525. 1;
  526.  
  527. =pod
  528.  
  529. =back
  530.  
  531. =head1 SEE ALSO
  532.  
  533. L<Net::DBus::Binding::Server>, L<Net::DBus::Binding::Bus>, L<Net::DBus::Binding::Message::Signal>, L<Net::DBus::Binding::Message::MethodCall>, L<Net::DBus::Binding::Message::MethodReturn>, L<Net::DBus::Binding::Message::Error>
  534.  
  535. =head1 AUTHOR
  536.  
  537. Daniel Berrange E<lt>dan@berrange.comE<gt>
  538.  
  539. =head1 COPYRIGHT
  540.  
  541. Copyright 2004 by Daniel Berrange
  542.  
  543. =cut
  544.