Incoming file transfer monitoring?

Hi all,

When I use the IncomingFileTransfer#recieveFile(File file) method everything is ok: file is transfered.

But I cannot manage to monitor the transfer as it is described in the javadoc:

The javadoc says:

/******************************************************************************* *********************

This method will return immedialtly, file transfer progress can be monitored through several methods:

FileTransfer.getStatus()

FileTransfer.getProgress()

FileTransfer.isDone()


Here’‘s a code snippet I’'ve written to show you my problem:

/**

  • Code snippet example.

*/

package fr.elh.im;

import java.io.File;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smackx.filetransfer.FileTransferListener;

import org.jivesoftware.smackx.filetransfer.FileTransferManager;

import org.jivesoftware.smackx.filetransfer.FileTransferRequest;

import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;

/**

  • @author me

*/

public class FileTransferHandler {

/**

  • The FileTransferManager object

*/

private FileTransferManager ftm;

/**

  • Public constructor

  • @param connection

*/

public FileTransferHandler(XMPPConnection connection) {

ftm = new FileTransferManager(connection);

ftm.addFileTransferListener(ftl);

}

/**

  • The FileTransferListener (for the example: file transfer is always

  • accepted)

*/

FileTransferListener ftl = new FileTransferListener() {

public void fileTransferRequest(FileTransferRequest ftr) {

IncomingFileTransfer ift = ftr.accept();

String incomingFileName = ftr.getFileName();

// the file that incoming

File incomingFile = new File(“C:
” + incomingFileName);

try {

// use of IncomingFileTransfer#recieveFile(File)

ift.recieveFile(incomingFile);

// TODO: how to monitor transfer as described in javadoc???

} catch (XMPPException e) {

System.out.println(“Error: could not receive file!”);

e.printStackTrace();

}

}

};

}

Could someone show me, using this code snippet, how to monitor the transfer in this case?

Thanks a lot

JTecks.

Hi,

You have to do a loop and get the progress while the transer is running. You can do something like this :

while( !ift.isDone() ) {

// Wait 1s

Thread.sleep( 1000);

// show progress

System.out.println( "Progress : " + ift.getProgress() ) ;

}

Nicolas

Hi Nicolas,

Thx for reply…

Is there a link between the size of the file which is transfered and the sleep time to use for the thread?

thx

Hi,

Not really, I put the sleep time to prevent wasting CPU time by calling getPercent too fast. You can use a smaller value , or no wait at all.

Nicolas

Hi,

I’'ve try to use your method, with or without time delay, but I always get “0.0” for progress in the loop, while the file is correctly transfered!

Any idea?

thx

JTecks

Are you using the lastest smack release ? there was a bug in previous versions and getPercent was always returning 0.0 ?

Hi,

I’'m using the Smack 2.2.1 – June 12, 2006 (1.39Mo)

from the http://www.jivesoftware.org/downloads.jsp#smack location.

Is this version ok?

thx

Jtecks

Smack 2.2.1 is ok.

Also progress does not work if you use the InputStream provided by the IncomingFileTransfer object directly. You have to use receiveFile.

But with your snippet it should work.

get percent returns a double between 0 and 1. Check is you don’'t use this value as an int somewhre, or use (ift.getPercent() * 100).

Nicolas

Hi,

I cannot find the method “getPercent()” on IncomingFileTransfer object.

I only see a “getProgress()” which returns a double.

?

JTecks.

Yes sorry, this is getProgress

Hi Nicolas,

Here’'s the code snippet which works updated with your advices:

/**

  • Code snippet example.

*/

package fr.elh.im;

import java.io.File;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smackx.filetransfer.FileTransferListener;

import org.jivesoftware.smackx.filetransfer.FileTransferManager;

import org.jivesoftware.smackx.filetransfer.FileTransferRequest;

import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;

/**

  • @author me

*/

public class FileTransferHandler {

/**

  • The FileTransferManager object

*/

private FileTransferManager ftm;

/**

  • Public constructor

  • @param connection

*/

public FileTransferHandler(XMPPConnection connection) {

ftm = new FileTransferManager(connection);

ftm.addFileTransferListener(ftl);

}

/**

  • The FileTransferListener (for the example: file transfer is always

  • accepted)

*/

FileTransferListener ftl = new FileTransferListener() {

public void fileTransferRequest(FileTransferRequest ftr) {

IncomingFileTransfer ift = ftr.accept();

String incomingFileName = ftr.getFileName();

// the file that incoming

File incomingFile = new File(“C:
” + incomingFileName);

try {

// use of IncomingFileTransfer#recieveFile(File)

ift.recieveFile(incomingFile);

// This way to monitor file transfer progress works:

System.out.println("ift: " + (incomingFileTransfer.getProgress() * 100) );

} catch (XMPPException e) {

System.out.println(“Error: could not receive file!”);

e.printStackTrace();

}

}

};

}

Thanks a lot for your help!

JTecks

I follow your codes and write a class and after login new a the class .but when there are files incoming(the sender is ok),there is no action,it does not work,even I define the action of fileTransferRequest() is to println anything.

new FileTransferHandler(connection);

I make sure the connection is got.

How do you use the class FileTransferHandler.Thanks a lot!

Hey lvye,

I don’'t understand your problem…

The code snippet works fine!!!

What happend??

Jtecks

Hello,My problem is that the listener is not working,no response,when other clients send files to the client.What ever codes I add in the fileTransferRequest() method ,the codes are never excuted.And the codes for sending files do work,are all right.

Oh,Hi ,I fix it.The cause is that the resource of the client(the resource as the type of client) is not recognized by the server.In the log of wildfire:

2006.08.02 10:42:21 Packet sent to unreachable address

The server does not recogniaze the resouce type “ECF_XMPP” but int the session of server, a has two resouce type ECF_XMPP and Smack

when I send the file ,I set receiver’'s resource type as Smack.the listener works well.

Thanks for your concern.