powered by Jive Software

Problem - Multiple File Transfer

Hi,

In my scenario, there are 3 users (Let me call them A, B and C) A and B will send files to C. A and B will send 100 files and these files are same files. I’m sending the files in a for loop (stg. like below)

for (int fIndx = 0; fIndx < farrFile.length; ++fIndx) {             if (!farrFile[fIndx].isHidden()) {
                //String localPath = farrFile[fIndx].getPath();
                String localPath = "/home/small_ticket/testSources/";
                String description = farrFile[fIndx].getName();
                String filename = farrFile[fIndx].getName();
                String regPath = "/test/";
                String jid = smackTest.connection.getRoster().getPresence("utestrec@10.1.1.141/Smack").getFrom();
                smackTest.sendFileTo(jid, localPath, filename, gregPath, description);
            }
        }

the sendFileTo method is as follows

public void sendFileTo(String jid, String localpath, String filename, String gregPath, String description) {         File file = new File(localpath, filename);
                if (file.exists()) {
            FileTransferManager transferManager = new FileTransferManager(
                    connection);
            FileTransferNegotiator.setServiceEnabled(connection, true);
            final OutgoingFileTransfer transfer = transferManager.createOutgoingFileTransfer(jid);
            try {
                transfer.sendFile(file, description, gregPath);
                while (!transfer.isDone()) {
                    System.out.println(transfer.getStatus());
                    System.out.println(transfer.getProgress() + " is done!");
                    Thread.sleep(100);
                }
                FileTransfer.Status status = transfer.getStatus();
                if (status == Status.negotiating_stream) {
                    System.out.println(transfer.getStatus());
                } else if (status == Status.error) {
                    if (transfer.getException() != null) {
                        System.out.println("Error occured during file transfer." + transfer.getException().getMessage());
                    }
                } else if (transfer.isDone()) {
                    System.out.println("Transfer is done!");
                }
            } catch (XMPPException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }     }

I’m starting to execute A’s code first and then right after I’m starting B. C accepts the A’s requests and file transfers are taking place between these two. But B’s status always stays in the negotiation and it cannot start file transfer… Here is the code for accepting the file request

final FileTransferManager manager = new FileTransferManager(connection);         // Create the listener
        manager.addFileTransferListener(new FileTransferListener() {             public void fileTransferRequest(final FileTransferRequest request) {
                Runnable runnable = new HandleFileRequest(request);
                Thread thread = new Thread(runnable);
                thread.start();
            }
        });
    }
public class HandleFileRequest implements Runnable {
        private FileTransferRequest request;         public HandleFileRequest(FileTransferRequest fileTransferRequest) {
            this.request = fileTransferRequest;
        }         public void run() {
            final IncomingFileTransfer transfer = request.accept();
              if (! new File (System.getProperty("user.home") + "/temp/" + request.getRequestor() + "/" ).exists()) {
                new File (System.getProperty("user.home") + "/temp/" + request.getRequestor() + "/" ).mkdirs();
            }
            final File fFile = new File(System.getProperty("user.home") + "/temp/" + request.getRequestor() + "/" +request.getFileName());
                Runnable runnable = new ReceiveFile(transfer, fFile, request);
                Thread thread = new Thread(runnable);
                thread.start();
        }     }     public class ReceiveFile implements Runnable {
        IncomingFileTransfer transfer;
        File fFile;
        FileTransferRequest request;         public ReceiveFile(IncomingFileTransfer fileTransfer, File fFile, FileTransferRequest fileTransferRequest) {
            this.transfer = fileTransfer;
            this.fFile = fFile;
            this.request = fileTransferRequest;
        }
        public void run() {
            try
            {
                transfer.recieveFile(fFile);
                System.out.println("File Received for:" + request.getPath() + " Size:" + request.getFileSize());
                while (!transfer.isDone()) {
                    System.out.println(transfer.getStatus());
                    System.out.println(transfer.getProgress() + " is done!");
                    Thread.sleep(100);
                }
                FileTransfer.Status status = transfer.getStatus();
                if (status == Status.negotiating_stream) {
                    System.out.println(transfer.getStatus());
                } else if (status == Status.error) {
                    if (transfer.getException() != null) {
                        System.out.println("Error occured during file transfer." + transfer.getException().getMessage());
                    }
                } else if (transfer.isDone()) {
                    System.out.println("Transfer is done!");
                }
                            } catch (InterruptedException ex) {
                Logger.getLogger(ReceiverTest.class.getName()).log(Level.SEVERE, null, ex);
            } catch (XMPPException ex) {
                Logger.getLogger(ReceiverTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }     }

Is there any one has an idea about this? Any idea will be highly appreciated

Thanks