Skip navigation
1857 Views 2 Replies Latest reply: Jan 2, 2009 3:18 AM by earlenceferns RSS
earlenceferns Bronze 30 posts since
Dec 23, 2008
Currently Being Moderated

Jan 1, 2009 12:28 AM

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.*;

 

/*
<applet code="XMPPTest1" width="300" height="300">
</applet>
*/

 

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

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points