Grupos de Google ya no admite nuevas publicaciones ni suscripciones de Usenet. El contenido anterior sigue siendo visible.

Callback during FTP

Visto 15 veces
Saltar al primer mensaje no leído

Yehuda Berlinger

no leída,
3 feb 2004, 14:35:543/2/04
a
I would like to do a callback while transferring via FTP, so as to
print % finished, etc... (and to activate my Tk::Progressbar).
Net::FTP doesn't seem to support this. Is that correct? Is there any
other tool that can do this? Is there anyway to do this using the
'hash' mechanism?

Thanks in advance.

Yehuda

P.S. I would like to avoid using fork()

Ben Morrow

no leída,
3 feb 2004, 18:12:493/2/04
a

j...@actcom.co.il (Yehuda Berlinger) wrote:
> I would like to do a callback while transferring via FTP, so as to
> print % finished, etc... (and to activate my Tk::Progressbar).
> Net::FTP doesn't seem to support this. Is that correct? Is there any
> other tool that can do this? Is there anyway to do this using the
> 'hash' mechanism?

If you're downloading you could try using LW,P which I think does
support this kind of callback. Otherwise... I think the only way to do
it is with a tied filehandle. Something like (completely untested):

package My::App::FTPCallback;

use Symbol;

sub new {
my ($c, $cback) = @_;
my $H = *{Symbol::gensym};
tie $H, $c, $cback;
return $H;
}

sub TIEHANDLE {
my ($c, $cback) = @_;
return bless {
count => 0,
cback => $cback,
}, $c;
}

sub PRINT {
my $s = shift;
my $hashes = join '', @_;
$s->{count} += length $hashes;
$s->{cback}->($s->{count});
return 1;
}

package main;

my $HASH = My::App::FTPCallback->new(sub {
my $count = shift;
# do stuff here
});

$FTP->hash($HASH, 1024);

# the callback will now be called for every KB transferred, with its
# first argument the number of KB transferred so far.

Ben

--
Razors pain you / Rivers are damp
Acids stain you / And drugs cause cramp. [Dorothy Parker]
Guns aren't lawful / Nooses give
Gas smells awful / You might as well live. b...@morrow.me.uk

Yehuda Berlinger

no leída,
6 feb 2004, 2:57:166/2/04
a
Ben Morrow <use...@morrow.me.uk> wrote in message news:<bvp9th$g71$1...@wisteria.csv.warwick.ac.uk>...

...


Cool.

Actually, I decided to subclass Net::FTP, copying over the get and put
functions, and adding one parameter $cb, and one line in the
appropriate place:

&{$cb}($buf,$len) if ref $cb eq 'CODE';

For the life of me, I can't figure out why this is not in Net::FTP, as
it is trivial.

Yehuda

Steve Lidie

no leída,
6 feb 2004, 9:14:206/2/04
a

I think Ben's trick is really cool. And, generally, your method, which
I've used in the past, is great most of the time. Still, when doing
network I/O with a Tk GUI I prefer a mulit-process approach.
The child handles network activities and talks to the Tk parent via
fileselect, using pipes or sockets (or even memory mapped arrays!).

Ala Qumsieh

no leída,
7 feb 2004, 12:11:177/2/04
a
Yehuda Berlinger wrote:

> &{$cb}($buf,$len) if ref $cb eq 'CODE';

The following syntax is clearer, IMHO:

$cb->($buf, $len) if ref $cb eq 'CODE';

--Ala

0 mensajes nuevos