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

  1. # -*- perl -*-
  2. #
  3. # Copyright (C) 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::ASyncReply - asynchronous method reply handler
  24.  
  25. =head1 SYNOPSIS
  26.  
  27.   use Net::DBus::Annotation qw(:call);
  28.  
  29.   my $object = $service->get_object("/org/example/systemMonitor");
  30.  
  31.   # List processes & get on with other work until
  32.   # the list is returned.
  33.   my $asyncreply = $object->list_processes(dbus_call_async, "someuser");
  34.  
  35.   while (!$asyncreply->is_ready) {
  36.     ... do some background work..
  37.   }
  38.  
  39.   my $processes = $asyncreply->get_result;
  40.  
  41.  
  42. =head1 DESCRIPTION
  43.  
  44. This object provides a handler for receiving asynchronous
  45. method replies. An asynchronous reply object is generated
  46. when making remote method call with the C<dbus_call_async>
  47. annotation set.
  48.  
  49. =head1 METHODS
  50.  
  51. =over 4
  52.  
  53. =cut
  54.  
  55. package Net::DBus::ASyncReply;
  56.  
  57. use strict;
  58. use warnings;
  59.  
  60.  
  61. sub _new {
  62.     my $proto = shift;
  63.     my $class = ref($proto) || $proto;
  64.     my $self = {};
  65.     my %params = @_;
  66.  
  67.     $self->{pending_call} = $params{pending_call} ? $params{pending_call} : die "pending_call parameter is required";
  68.     $self->{introspector} = $params{introspector} ? $params{introspector} : undef;
  69.     $self->{method_name} = $params{method_name} ? $params{method_name} : ($self->{introspector} ? die "method_name is parameter required for introspection" : undef);
  70.  
  71.     bless $self, $class;
  72.  
  73.     return $self;
  74. }
  75.  
  76.  
  77. =item $asyncreply->discard_result;
  78.  
  79. Indicates that the caller is no longer interested in
  80. recieving the reply & that it should be discarded. After
  81. calling this method, this object should not be used again.
  82.  
  83. =cut
  84.  
  85. sub discard_result {
  86.     my $self = shift;
  87.  
  88.     $self->{pending_call}->cancel;
  89. }
  90.  
  91.  
  92. =item $asyncreply->wait_for_result;
  93.  
  94. Blocks the caller waiting for completion of the of the
  95. asynchronous reply. Upon returning from this method, the
  96. result can be obtained with the C<get_result> method.
  97.  
  98. =cut
  99.  
  100. sub wait_for_result {
  101.     my $self = shift;
  102.  
  103.     $self->{pending_call}->block;
  104. }
  105.  
  106. =item my $boolean = $asyncreply->is_ready;
  107.  
  108. Returns a true value if the asynchronous reply is now
  109. complete (or a timeout has occurred). When this method
  110. returns true, the result can be obtained with the C<get_result>
  111. method.
  112.  
  113. =cut
  114.  
  115. sub is_ready {
  116.     my $self = shift;
  117.  
  118.     return $self->{pending_call}->get_completed;
  119. }
  120.  
  121.  
  122. =item $asyncreply->set_notify($coderef);
  123.  
  124. Sets a notify function which will be invoked when the
  125. asynchronous reply finally completes. The callback will
  126. be invoked with a single parameter which is this object.
  127.  
  128. =cut
  129.  
  130. sub set_notify {
  131.     my $self = shift;
  132.     my $cb = shift;
  133.  
  134.     $self->{pending_call}->set_notify(sub {
  135.     my $pending_call = shift;
  136.  
  137.     &$cb($self);
  138.     });
  139. }
  140.  
  141. =item my @data = $asyncreply->get_result;
  142.  
  143. Retrieves the data associated with the asynchronous reply.
  144. If a timeout occurred, then this method will throw an
  145. exception. This method can only be called once the reply
  146. is complete, as indicated by the C<is_ready> method
  147. returning a true value. After calling this method, this
  148. object should no longer be used.
  149.  
  150. =cut
  151.  
  152. sub get_result {
  153.     my $self = shift;
  154.  
  155.     my $reply = $self->{pending_call}->get_reply;
  156.  
  157.     if ($reply->isa("Net::DBus::Binding::Message::Error")) {
  158.     my $iter = $reply->iterator();
  159.     my $desc = $iter->get_string();
  160.     die Net::DBus::Error->new(name => $reply->get_error_name,
  161.                   message => $desc);
  162.     }
  163.  
  164.     my @reply;
  165.     if ($self->{introspector}) {
  166.     @reply = $self->{introspector}->decode($reply, "methods", $self->{method_name}, "returns");
  167.     } else {
  168.     @reply = $reply->get_args_list;
  169.     }
  170.  
  171.     return wantarray ? @reply : $reply[0];
  172. }
  173.  
  174. 1;
  175.  
  176. =pod
  177.  
  178. =back
  179.  
  180. =head1 AUTHOR
  181.  
  182. Daniel Berrange <dan@berrange.com>
  183.  
  184. =head1 COPYRIGHT
  185.  
  186. Copright (C) 2006, Daniel Berrange.
  187.  
  188. =head1 SEE ALSO
  189.  
  190. L<Net::DBus>, L<Net::DBus::RemoteObject>, L<Net::DBus::Annotation>
  191.  
  192. =cut
  193.