| 1 |
# -*- perl -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2011 Red Hat |
| 4 |
# |
| 5 |
# This program is free software; You can redistribute it and/or modify |
| 6 |
# it under 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 "LICENSE" 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 |
Sys::Virt::Stream - Represent & manage a libvirt stream |
| 24 |
|
| 25 |
=head1 DESCRIPTION |
| 26 |
|
| 27 |
The C<Sys::Virt::Stream> module represents a stream managed |
| 28 |
by the virtual machine monitor. |
| 29 |
|
| 30 |
=head1 METHODS |
| 31 |
|
| 32 |
=over 4 |
| 33 |
|
| 34 |
=cut |
| 35 |
|
| 36 |
package Sys::Virt::Stream; |
| 37 |
|
| 38 |
use strict; |
| 39 |
use warnings; |
| 40 |
|
| 41 |
|
| 42 |
sub _new { |
| 43 |
my $proto = shift; |
| 44 |
my $class = ref($proto) || $proto; |
| 45 |
my %params = @_; |
| 46 |
|
| 47 |
my $con = exists $params{connection} ? $params{connection} : die "connection parameter is required"; |
| 48 |
my $self = Sys::Virt::Stream::_new_obj($con, $params{flags}); |
| 49 |
|
| 50 |
bless $self, $class; |
| 51 |
|
| 52 |
return $self; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
=item my $st Sys::Virt::Stream->new($conn, $flags); |
| 57 |
|
| 58 |
Creates a new data stream, ready for use with a stream based |
| 59 |
API. The optional C<$flags> parameter can be used to configure |
| 60 |
the stream as non-blocking |
| 61 |
|
| 62 |
=item $st->abort() |
| 63 |
|
| 64 |
Abort I/O on the stream. Either this function or C<finish> must |
| 65 |
be called on any stream which has been activated |
| 66 |
|
| 67 |
=item $st->finish() |
| 68 |
|
| 69 |
Complete I/O on the stream. Either this function or C<abort> must |
| 70 |
be called on any stream which has been activated |
| 71 |
|
| 72 |
=item $rv = $st->recv($data, $nbytes, $flags=0) |
| 73 |
|
| 74 |
Receive up to C<$nbytes> worth of data, copying into C<$data>. |
| 75 |
Returns the number of bytes read, or -3 if hole is reached and |
| 76 |
C<$flags> contains RECV_STOP_AT_HOLE, or -2 if I/O would block, |
| 77 |
or -1 on error. The C<$flags> parameter accepts the following |
| 78 |
flags: |
| 79 |
|
| 80 |
=over 4 |
| 81 |
|
| 82 |
=item Sys::Virt::Stream::RECV_STOP_AT_HOLE |
| 83 |
|
| 84 |
If this flag is set, the C<recv> function will stop reading from |
| 85 |
stream if it has reached a hole. In that case, -3 is returned and |
| 86 |
C<recv_hole> should be called to get the hole size. |
| 87 |
|
| 88 |
=back |
| 89 |
|
| 90 |
=item $rv = $st->send($data, $nbytes) |
| 91 |
|
| 92 |
Send up to C<$nbytes> worth of data, copying from C<$data>. |
| 93 |
Returns the number of bytes sent, or -2 if I/O would block, |
| 94 |
or -1 on error. |
| 95 |
|
| 96 |
=item $rv = $st->recv_hole($flags=0) |
| 97 |
|
| 98 |
Determine the amount of the empty space (in bytes) to be created |
| 99 |
in a stream's target file when uploading or downloading sparsely |
| 100 |
populated files. This is the counterpart to C<send_hole>. The |
| 101 |
optional C<$flags> parameter is currently unused and defaults to |
| 102 |
zero if omitted. |
| 103 |
|
| 104 |
=item $st->send_hole($length, $flags=0) |
| 105 |
|
| 106 |
Rather than transmitting empty file space, this method directs |
| 107 |
the stream target to create C<$length> bytes of empty space. |
| 108 |
This method would be used when uploading or downloading sparsely |
| 109 |
populated files to avoid the needless copy of empty file space. |
| 110 |
The optional C<$flags> parameter is currently unused and defaults |
| 111 |
to zero if omitted. |
| 112 |
|
| 113 |
=item $st->recv_all($handler) |
| 114 |
|
| 115 |
Receive all data available from the stream, invoking |
| 116 |
C<$handler> to process the data. The C<$handler> |
| 117 |
parameter must be a function which expects three |
| 118 |
arguments, the C<$st> stream object, a scalar containing |
| 119 |
the data received and a data byte count. The function |
| 120 |
should return the number of bytes processed, or -1 |
| 121 |
upon error. |
| 122 |
|
| 123 |
=item $st->send_all($handler) |
| 124 |
|
| 125 |
Send all data produced by C<$handler> to the stream. |
| 126 |
The C<$handler> parameter must be a function which |
| 127 |
expects three arguments, the C<$st> stream object, a |
| 128 |
scalar which must be filled with data and a maximum |
| 129 |
data byte count desired. The function should return |
| 130 |
the number of bytes filled, 0 on end of file, or |
| 131 |
-1 upon error |
| 132 |
|
| 133 |
=item $st->sparse_recv_all($handler, $hole_handler) |
| 134 |
|
| 135 |
Receive all data available from the sparse stream, invoking |
| 136 |
C<$handler> to process the data. The C<$handler> parameter must |
| 137 |
be a function which expects three arguments, the C<$st> stream |
| 138 |
object, a scalar containing the data received and a data byte |
| 139 |
count. The function should return the number of bytes processed, |
| 140 |
or -1 upon error. The second argument C<$hole_handler> is a |
| 141 |
function which expects two arguments: the C<$st> stream and a |
| 142 |
scalar, number describing the size of the hole in the stream (in |
| 143 |
bytes). The C<$hole_handler> is expected to return a non-negative |
| 144 |
number on success (usually 0) and a negative number (usually -1) |
| 145 |
otherwise. |
| 146 |
|
| 147 |
=item $st->sparse_send_all($handler, $hole_handler, $skip_handler) |
| 148 |
|
| 149 |
Send all data produced by C<$handler> to the stream. The |
| 150 |
C<$handler> parameter must be a function which expects three |
| 151 |
arguments, the C<$st> stream object, a scalar which must be |
| 152 |
filled with data and a maximum data byte count desired. The |
| 153 |
function should return the number of bytes filled, 0 on end of |
| 154 |
file, or -1 upon error. The second argument C<$hole_handler> is a |
| 155 |
function expecting just one argument C<$st> and returning an |
| 156 |
array of two elements (C<$in_data>, C<$section_len>) where |
| 157 |
C<$in_data> has zero or non-zero value if underlying file is in a |
| 158 |
hole or data section respectively. The C<$section_len> then is the |
| 159 |
number of remaining bytes in the current section in the |
| 160 |
underlying file. Finally, the third C<$skip_handler> is a function |
| 161 |
expecting two arguments C<$st> and C<$length> which moves cursor |
| 162 |
in the underlying file for C<$length> bytes. The C<$skip_handler> |
| 163 |
is expected to return a non-negative number on success (usually |
| 164 |
0) and a negative number (usually -1) otherwise. |
| 165 |
|
| 166 |
=item $st->add_callback($events, $coderef) |
| 167 |
|
| 168 |
Register a callback to be invoked whenever the stream has |
| 169 |
one or more events from C<$events> mask set. The C<$coderef> |
| 170 |
must be a subroutine that expects 2 parameters, the original |
| 171 |
C<$st> object and the new C<$events> mask |
| 172 |
|
| 173 |
=item $st->update_callback($events) |
| 174 |
|
| 175 |
Change the event mask for a previously registered |
| 176 |
callback to C<$events> |
| 177 |
|
| 178 |
=item $st->remove_callback(); |
| 179 |
|
| 180 |
Remove a previously registered callback |
| 181 |
|
| 182 |
=back |
| 183 |
|
| 184 |
=head1 CONSTANTS |
| 185 |
|
| 186 |
=over 4 |
| 187 |
|
| 188 |
=item Sys::Virt::Stream::NONBLOCK |
| 189 |
|
| 190 |
Create a stream which will not block when performing I/O |
| 191 |
|
| 192 |
=item Sys::Virt::Stream::EVENT_READABLE |
| 193 |
|
| 194 |
The stream has data available for read without blocking |
| 195 |
|
| 196 |
=item Sys::Virt::Stream::EVENT_WRITABLE |
| 197 |
|
| 198 |
The stream has ability to write data without blocking |
| 199 |
|
| 200 |
=item Sys::Virt::Stream::EVENT_ERROR |
| 201 |
|
| 202 |
An error occurred on the stream |
| 203 |
|
| 204 |
=item Sys::Virt::Stream::EVENT_HANGUP |
| 205 |
|
| 206 |
The remote end of the stream closed |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
=cut |
| 211 |
|
| 212 |
|
| 213 |
1; |
| 214 |
|
| 215 |
=back |
| 216 |
|
| 217 |
=head1 AUTHORS |
| 218 |
|
| 219 |
Daniel P. Berrange <[email protected]> |
| 220 |
|
| 221 |
=head1 COPYRIGHT |
| 222 |
|
| 223 |
Copyright (C) 2006-2009 Red Hat |
| 224 |
Copyright (C) 2006-2007 Daniel P. Berrange |
| 225 |
|
| 226 |
=head1 LICENSE |
| 227 |
|
| 228 |
This program is free software; you can redistribute it and/or modify |
| 229 |
it under the terms of either the GNU General Public License as published |
| 230 |
by the Free Software Foundation (either version 2 of the License, or at |
| 231 |
your option any later version), or, the Artistic License, as specified |
| 232 |
in the Perl README file. |
| 233 |
|
| 234 |
=head1 SEE ALSO |
| 235 |
|
| 236 |
L<Sys::Virt>, L<Sys::Virt::Error>, C<http://libvirt.org> |
| 237 |
|
| 238 |
=cut |