AnsweredAssumed Answered

How to configure xmpp connection for file transfer using asmack in android?

Question asked by pooja on Jan 7, 2016

In My Android application I get xmpp connection initially using server_nm and 5222 port on login and done single chat message send and receive,but at time of file transfer, How I configure connection with proxy_server and  7777 port which use for file transfer?

 

       public class XmppManager {

           

            private static final int packetReplyTimeout = 500; // millis

            private String server=server_nm;

            private int port=5222;

          

           

            private ConnectionConfiguration config;

            private XMPPConnection connection;

          

            String group_name,userid;

           

            private ChatManager chatManager;

            private MessageListener messageListener;

            private Handler mHandler;

            PacketFilter filter;

            Message message;

            Context context;

            public static boolean IBB_ONLY = (System.getProperty("ibb") != null);//true;

            protected static final String STREAM_DATA_FIELD_NAME = "stream-method";

            

            private  StreamNegotiator byteStreamTransferManager;

            private  StreamNegotiator inbandTransferManager;

            

           

            // constructor

            public XmppManager(String server, int port) {

            this.server = server;

            this.port = port;

           

            }

           

            // to initialize connection

            public void init(Context c) throws XMPPException {

           

            System.out.println(String.format(

            "Initializing connection to server %1$s port %2$d", server,

            port));

           

            SmackConfiguration.setPacketReplyTimeout(50000);

           

            config = new ConnectionConfiguration(server, port);

            config.setSASLAuthenticationEnabled(false);

            config.setSecurityMode(SecurityMode.disabled);

           

            // group chat invitation

            ProviderManager.getInstance().addExtensionProvider("x",

            "http://jabber.org/protocol/muc#user",

            new GroupChatInvitation.Provider());

            // offline msgtime

            ProviderManager.getInstance().addExtensionProvider("x",

            "jabber:x:delay", new DelayInformationProvider());

          

            //file transfer method

            ProviderManager.getInstance().addIQProvider("query","http://jabber.org/protocol/bytestreams", new BytestreamsProvider());

            ProviderManager.getInstance().addIQProvider("query","http://jabber.org/protocol/disco#items", new DiscoverItemsProvider());

            ProviderManager.getInstance().addIQProvider("query","http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());

            ProviderManager.getInstance().addIQProvider("si","http://jabber.org/protocol/si", new StreamInitiationProvider());

            ProviderManager.getInstance().addIQProvider("open","http://jabber.org/protocol/ibb", new OpenIQProvider());

            ProviderManager.getInstance().addIQProvider("close","http://jabber.org/protocol/ibb", new CloseIQProvider());

            ProviderManager.getInstance().addExtensionProvider("data","http://jabber.org/protocol/ibb", new DataPacketProvider());

          

            // v-card integration

            ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp", new VCardProvider());

          

            connection = new XMPPConnection(config);

            connection.connect();

           

            System.out.println("Connected: " + connection.isConnected());

           

            chatManager = connection.getChatManager();

            context = c;

            Intent i = new Intent(context, MyService.class);

            context.startService(i);

           

            }

           

            // login to xmpp server

            public void performLogin(String username, String password,String resource)

            throws XMPPException {

            if (connection != null && connection.isConnected()) {

          

            System.out.println("connection on presence send" + connection.isConnected() + " " + username + " " + password);

            connection.login(username, password,resource);

            setStatus(true, "Online");

          

            Presence presence = new Presence(Presence.Type.available); 

                    connection.sendPacket(presence); 

           

            }

            }

       

          //using this code I try to send file

         

        public void sendfileafterresponse(String filenm,String filereceiverid )

        {

        FileTransferNegotiator.IBB_ONLY = true;

           this.setServiceEnabled(connection, true);

        FileTransferManager manager = new FileTransferManager(connection);

        OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(filereceiverid + "@chat.bobl.us/" + filereceiverid);

        File file = new File(filenm);

        try {

        System.out.println("file send to" + filenm + filereceiverid);

          transfer.sendFile(file, "test_file");

        } catch (XMPPException e) {

          e.printStackTrace();

        }

      

      

        while(!transfer.isDone()) {

          if(transfer.getStatus().equals(Status.error)) {

             System.out.println("ERROR!!! " + transfer.getError());

          } else if (transfer.getStatus().equals(Status.cancelled)

                           || transfer.getStatus().equals(Status.refused)) {

             System.out.println("Cancelled!!! " + transfer.getError());

          }

          try {

             Thread.sleep(1000L);

          } catch (InterruptedException e) {

             e.printStackTrace();

          }

        }

        if(transfer.getStatus().equals(Status.refused) || transfer.getStatus().equals(Status.error)

        || transfer.getStatus().equals(Status.cancelled)){

          System.out.println("refused cancelled error " + transfer.getError());

        } else {

          System.out.println("Success");

        }

        }

       

        }

        }

Outcomes