powered by Jive Software

File Transfer not working in latest smack api release

Hi,

I am trying to use the File Transfer API with the latest smack api release.

My sys config is

  1. Dial up internet connection

  2. Both the JIDs are regd at jabber.org

  3. There are 2 applets

  4. I am able to carry on a conversation with both the applets from either side.

This is the code i am using

//Applet 1 - XMPPClient1

import java.awt.;
import java.awt.event.
;
import java.applet.;
import java.io.
;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.filetransfer.*;

/*


*/

public class XMPPTest1 extends Applet implements ActionListener
{
ConnectionConfiguration cc = null;
XMPPConnection conn = null;
Button connect, disconnect, send;
TextArea taRecv;
TextField taSend;

Button sf;
                       
public void init()
{
    connect = new Button("Connect");
    disconnect = new Button("Disconnect");
    send = new Button("Send");
    taSend = new TextField(20);
    taRecv = new TextArea("", 5, 30);
   
    sf = new Button("SendFile");
                   
    add(connect);
    add(disconnect);
    add(send);
    add(taSend);
    add(taRecv);
    add(sf);
                   
    connect.addActionListener(this);
    disconnect.addActionListener(this);
    send.addActionListener(this);
    sf.addActionListener(this);
}
   
public void actionPerformed(ActionEvent ae)
{
    String str = ae.getActionCommand();
    if(str.equals("Connect"))
        setupXMPPAndLogin();
    if(str.equals("Disconnect"))
        closeConn();
    if(str.equals("Send"))
    {
        sendMessage(taSend.getText());
        taRecv.append("me: " + taSend.getText() + "\n");
        taSend.setText("");
    }
    if(str.equals("SendFile"))
        sendFile();
       
}

void setupXMPPAndLogin()
{
    try {
       
        cc = new ConnectionConfiguration("jabber.org", 5222);
        conn = new XMPPConnection(cc);
                           
        showStatus("Attempting to connect");
        conn.connect();
        SASLAuthentication.supportSASLMechanism("PLAIN", 0);
        conn.login("username", "password");
        showStatus("Logged In!");
       
        setListener();
       
        showStatus("Listeners set");
       
    } catch(XMPPException xe) { System.out.println(xe.getMessage()); }
                   
}

void setListener()
{
    PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
    conn.addPacketListener(new PacketListener() {
        public void processPacket(Packet pkt)
        {
            Message m = (Message) pkt;
            if(m.getBody() != null)
            {
                taRecv.append("recv: " + m.getBody() + "\n");
            }
        }
       
    }, filter);
   
}

void sendMessage(String data)
{
    Message msg = new Message();
    msg.setType(Message.Type.chat);
    msg.setTo("recv.xmpp@jabber.org");
    msg.setBody(data);
    conn.sendPacket(msg);
    showStatus("Packet Dispatched");
}

void closeConn()
{
    showStatus("Logging Off!");
    conn.disconnect();
}

void sendFile()
{
    FileTransferManager ftm = new FileTransferManager(conn);
    OutgoingFileTransfer otf = ftm.createOutgoingFileTransfer("recv.xmpp@jabber.org");
   
    try {
       
        otf.sendFile(new File("test.txt"), "This is a test");
        showStatus("File Transfer started");
       
    }catch(XMPPException xe) { System.out.println(xe.getLocalizedMessage()); }
}

}

This is the other applets code

//XMPPClient2

import java.awt.;
import java.awt.event.
;
import java.applet.*;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.filetransfer.*;
import org.jivesoftware.smackx.filetransfer.FileTransfer.Status;

import java.io.*;

public class XMPPTest2 extends Applet implements ActionListener
{
ConnectionConfiguration cc = null;
XMPPConnection conn = null;
Button connect, disconnect, send;
TextArea taRecv;
TextField taSend;

public void init()
{
    connect = new Button("Connect");
    disconnect = new Button("Disconnect");
    send = new Button("Send");
    taSend = new TextField(20);
    taRecv = new TextArea("", 5, 30);
           
    add(connect);
    add(disconnect);
    add(send);
    add(taSend);
    add(taRecv);
           
    connect.addActionListener(this);
    disconnect.addActionListener(this);
    send.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
    String str = ae.getActionCommand();
    if(str.equals("Connect"))
        setupXMPPAndLogin();
    if(str.equals("Disconnect"))
        closeConn();
    if(str.equals("Send"))
    {
        sendMessage(taSend.getText());
        taRecv.append("me: " + taSend.getText() + "\n");
        taSend.setText("");
    }
       
}

void setupXMPPAndLogin()
{
    try {
       
        cc = new ConnectionConfiguration("jabber.org", 5222);
        conn = new XMPPConnection(cc);
                           
        showStatus("Attempting to connect");
        conn.connect();
        SASLAuthentication.supportSASLMechanism("PLAIN", 0);
        conn.login("username", "password");
        showStatus("Logged In!");
       
        setListener();
        setFileRecvListener();
       
        showStatus("Listeners set");
                   
    } catch(XMPPException xe) { System.out.println(xe.getMessage()); }
                   
}

void setListener()
{
    PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
    conn.addPacketListener(new PacketListener() {
        public void processPacket(Packet pkt)
        {
            Message m = (Message) pkt;
            if(m.getBody() != null)
            {
                taRecv.append("recv: " + m.getBody() + "\n");
            }
        }
       
    }, filter);
   
}

void sendMessage(String data)
{
    Message msg = new Message();
    msg.setType(Message.Type.chat);
    msg.setTo("earlenceferns@jabber.org");
    msg.setBody(data);
    conn.sendPacket(msg);
    showStatus("Packet Dispatched");
}

void closeConn()
{
    showStatus("Logging Off!");
    conn.disconnect();
}

void setFileRecvListener()
{
    final FileTransferManager ftm = new FileTransferManager(conn);
   
    ftm.addFileTransferListener(new FileTransferListener() {
       
        public void fileTransferRequest(FileTransferRequest req)
        {
            showStatus("FileTransferRequest: " + req.getDescription());
            IncomingFileTransfer itf = req.accept();
           
            try {
               
                itf.recieveFile(new File("recvd.txt"));
               
                /*new Thread()
                {
                    public void run()
                    {
                        while(!itf.isDone())
                        {
                            if(itf.getStatus().equals(Status.error)) {
                                  System.out.println("ERROR!!! " + itf.getError());
                            } else {
                                  System.out.println(itf.getStatus());
                                  System.out.println(itf.getProgress());
                            }
                           
                            try {
                                sleep(1000);
                            }catch(InterruptedException ie) {}
                        }
                    }
                }.start();*/
                                   
            } catch(XMPPException xe) { System.out.println(xe.getMessage()); }
        }
       
    });
   
}

}

When i click on the SendFile button, the status bar in the first applet shows the correct msg but

the other applet doesn’t recv any FileTransfer event.

Please HELP!!!

Cheers,

Earlence Fernandes

Hi,

I tried debugging the above app. This is the error that i get in the context of the first applet (i.e when i click on the

SendFile button)

Exception in thread “AWT-EventQueue-1” java.lang.IllegalArgumentException: IQ Type not understood
at org.jivesoftware.smackx.packet.StreamInitiation.getChildElementXML(StreamInitia tion.java:159)
at org.jivesoftware.smack.packet.IQ.toXML(IQ.java:88)
at org.jivesoftware.smackx.debugger.EnhancedDebugger$20.run(EnhancedDebugger.java: 774)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Hi,

I really need to get the File Transfer working. Can anyone out there help me???

Cheers,

Earlence

OutgoingFileTransfer otf = ftm.createOutgoingFileTransfer("recv.xmpp@jabber.org");

in above line it should also have the resource also in the address

OutgoingFileTransfer otf = ftm.createOutgoingFileTransfer("recv.xmpp@jabber.org/resource");

from the javadoc

/**

  • Creates an OutgoingFileTransfer to send a file to another user.

  • @param userID

  •        The fully qualified jabber ID with resource of the user to
    
  •        send the file to.
    
  • @return The send file object on which the negotiated transfer can be run.

*/

1 Like