<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># -*- perl -*-
#
# Copyright (C) 2006-2011 Daniel P. Berrange
#
# This program is free software; You can redistribute it and/or modify
# it under the same terms as Perl itself. Either:
#
# a) the GNU General Public License as published by the Free
#   Software Foundation; either version 2, or (at your option) any
#   later version,
#
# or
#
# b) the "Artistic License"
#
# The file "COPYING" distributed along with this file provides full
# details of the terms and conditions of the two licenses.

=pod

=head1 NAME

Net::DBus::ASyncReply - asynchronous method reply handler

=head1 SYNOPSIS

  use Net::DBus::Annotation qw(:call);

  my $object = $service-&gt;get_object("/org/example/systemMonitor");

  # List processes &amp; get on with other work until
  # the list is returned.
  my $asyncreply = $object-&gt;list_processes(dbus_call_async, "someuser");

  while (!$asyncreply-&gt;is_ready) {
    ... do some background work..
  }

  my $processes = $asyncreply-&gt;get_result;


=head1 DESCRIPTION

This object provides a handler for receiving asynchronous
method replies. An asynchronous reply object is generated
when making remote method call with the C&lt;dbus_call_async&gt;
annotation set.

=head1 METHODS

=over 4

=cut

package Net::DBus::ASyncReply;

use strict;
use warnings;


sub _new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self = {};
    my %params = @_;

    $self-&gt;{pending_call} = $params{pending_call} ? $params{pending_call} : die "pending_call parameter is required";
    $self-&gt;{introspector} = $params{introspector} ? $params{introspector} : undef;
    $self-&gt;{method_name} = $params{method_name} ? $params{method_name} : ($self-&gt;{introspector} ? die "method_name is parameter required for introspection" : undef);

    bless $self, $class;

    return $self;
}


=item $asyncreply-&gt;discard_result;

Indicates that the caller is no longer interested in
receiving the reply &amp; that it should be discarded. After
calling this method, this object should not be used again.

=cut

sub discard_result {
    my $self = shift;
    my $pending_call = delete $self-&gt;{pending_call};

    $pending_call-&gt;cancel;
}


=item $asyncreply-&gt;wait_for_result;

Blocks the caller waiting for completion of the of the
asynchronous reply. Upon returning from this method, the
result can be obtained with the C&lt;get_result&gt; method.

=cut

sub wait_for_result {
    my $self = shift;

    $self-&gt;{pending_call}-&gt;block;
}

=item my $boolean = $asyncreply-&gt;is_ready;

Returns a true value if the asynchronous reply is now
complete (or a timeout has occurred). When this method
returns true, the result can be obtained with the C&lt;get_result&gt;
method.

=cut

sub is_ready {
    my $self = shift;

    return $self-&gt;{pending_call}-&gt;get_completed;
}


=item $asyncreply-&gt;set_notify($coderef);

Sets a notify function which will be invoked when the
asynchronous reply finally completes. The callback will
be invoked with a single parameter which is this object.

=cut

sub set_notify {
    my $self = shift;
    my $cb = shift;

    $self-&gt;{pending_call}-&gt;set_notify(sub {
	my $pending_call = shift;

	&amp;$cb($self);
    });
}

=item my @data = $asyncreply-&gt;get_result;

Retrieves the data associated with the asynchronous reply.
If a timeout occurred, then this method will throw an
exception. This method can only be called once the reply
is complete, as indicated by the C&lt;is_ready&gt; method
returning a true value. After calling this method, this
object should no longer be used.

=cut

sub get_result {
    my $self = shift;
    my $pending_call = delete $self-&gt;{pending_call};

    my $reply = $pending_call-&gt;get_reply;

    if ($reply-&gt;isa("Net::DBus::Binding::Message::Error")) {
	my $iter = $reply-&gt;iterator();
	my $desc = $iter-&gt;get_string();
	die Net::DBus::Error-&gt;new(name =&gt; $reply-&gt;get_error_name,
				  message =&gt; $desc);
    }

    my @reply;
    if ($self-&gt;{introspector}) {
	@reply = $self-&gt;{introspector}-&gt;decode($reply, "methods", $self-&gt;{method_name}, "returns");
    } else {
	@reply = $reply-&gt;get_args_list;
    }

    return wantarray ? @reply : $reply[0];
}

1;

=pod

=back

=head1 AUTHOR

Daniel Berrange &lt;dan@berrange.com&gt;

=head1 COPYRIGHT

Copright (C) 2006-2011, Daniel Berrange.

=head1 SEE ALSO

L&lt;Net::DBus&gt;, L&lt;Net::DBus::RemoteObject&gt;, L&lt;Net::DBus::Annotation&gt;

=cut
</pre></body></html>