Search in sources :

Example 61 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project xabber-android by redsolution.

the class MUCManager method requestHostedRooms.

public static void requestHostedRooms(final String account, final String serviceName, final HostedRoomsListener listener) {
    final XMPPConnection xmppConnection = AccountManager.getInstance().getAccount(account).getConnectionThread().getXMPPConnection();
    final Thread thread = new Thread("Get hosted rooms on server " + serviceName + " for account " + account) {

        @Override
        public void run() {
            Collection<HostedRoom> hostedRooms = null;
            try {
                hostedRooms = MultiUserChatManager.getInstanceFor(xmppConnection).getHostedRooms(serviceName);
            } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException e) {
                e.printStackTrace();
            }
            final Collection<HostedRoom> finalHostedRooms = hostedRooms;
            Application.getInstance().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    listener.onHostedRoomsReceived(finalHostedRooms);
                }
            });
        }
    };
    thread.start();
}
Also used : XMPPConnection(org.jivesoftware.smack.XMPPConnection) HostedRoom(org.jivesoftware.smackx.muc.HostedRoom) ConnectionThread(com.xabber.android.data.connection.ConnectionThread)

Example 62 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project xabber-android by redsolution.

the class MUCManager method joinRoom.

/**
     * Requests to join to the room.
     *
     * @param requested Whether user request to join the room.
     */
public void joinRoom(final String account, final String room, boolean requested) {
    final XMPPConnection xmppConnection;
    final RoomChat roomChat;
    final String nickname;
    final String password;
    final Thread thread;
    roomChat = getRoomChat(account, room);
    if (roomChat == null) {
        Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
        return;
    }
    RoomState state = roomChat.getState();
    if (state == RoomState.available || state == RoomState.occupation) {
        Application.getInstance().onError(R.string.ALREADY_JOINED);
        return;
    }
    if (state == RoomState.creating || state == RoomState.joining) {
        Application.getInstance().onError(R.string.ALREADY_IN_PROGRESS);
        return;
    }
    nickname = roomChat.getNickname();
    password = roomChat.getPassword();
    requestToWriteRoom(account, room, nickname, password, true);
    ConnectionThread connectionThread = AccountManager.getInstance().getAccount(account).getConnectionThread();
    if (connectionThread == null) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    xmppConnection = connectionThread.getXMPPConnection();
    if (xmppConnection == null) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    final MultiUserChat multiUserChat;
    try {
        multiUserChat = MultiUserChatManager.getInstanceFor(xmppConnection).getMultiUserChat(room);
    } catch (IllegalStateException e) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    roomChat.setState(RoomState.joining);
    roomChat.setMultiUserChat(multiUserChat);
    roomChat.setRequested(requested);
    thread = new Thread("Join to room " + room + " from " + account) {

        @Override
        public void run() {
            try {
                if (roomChat.getMultiUserChat() != multiUserChat) {
                    return;
                }
                multiUserChat.join(nickname, password);
                Application.getInstance().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (roomChat.getMultiUserChat() != multiUserChat) {
                            return;
                        }
                        if (roomChat.getState() == RoomState.joining) {
                            roomChat.setState(RoomState.occupation);
                        }
                        removeAuthorizationError(account, room);
                        RosterManager.getInstance().onContactChanged(account, room);
                    }
                });
                return;
            } catch (final XMPPException.XMPPErrorException e) {
                Application.getInstance().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (roomChat.getMultiUserChat() != multiUserChat) {
                            return;
                        }
                        roomChat.setState(RoomState.error);
                        addAuthorizationError(account, room);
                        XMPPError xmppError = e.getXMPPError();
                        if (xmppError != null && xmppError.getCondition() == XMPPError.Condition.conflict) {
                            Application.getInstance().onError(R.string.NICK_ALREADY_USED);
                        } else if (xmppError != null && xmppError.getCondition() == XMPPError.Condition.not_authorized) {
                            Application.getInstance().onError(R.string.AUTHENTICATION_FAILED);
                        } else {
                            Application.getInstance().onError(R.string.NOT_CONNECTED);
                        }
                        RosterManager.getInstance().onContactChanged(account, room);
                    }
                });
                return;
            } catch (IllegalStateException e) {
            } catch (Exception e) {
                LogManager.exception(this, e);
            }
            Application.getInstance().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (roomChat.getMultiUserChat() != multiUserChat) {
                        return;
                    }
                    roomChat.setState(RoomState.waiting);
                    Application.getInstance().onError(R.string.NOT_CONNECTED);
                    RosterManager.getInstance().onContactChanged(account, room);
                }
            });
        }
    };
    thread.setDaemon(true);
    thread.start();
}
Also used : MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPError(org.jivesoftware.smack.packet.XMPPError) SmackException(org.jivesoftware.smack.SmackException) NetworkException(com.xabber.android.data.NetworkException) XMPPException(org.jivesoftware.smack.XMPPException) ConnectionThread(com.xabber.android.data.connection.ConnectionThread) ConnectionThread(com.xabber.android.data.connection.ConnectionThread)

Example 63 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project intellij-plugins by JetBrains.

the class JabberFacade_ConnectionTest method suite.

public static Test suite() {
    TestSuite testSuite = new TestSuite();
    XMPPConnection ourConnection;
    try {
        ourConnection = new XMPPConnection(LOCALHOST);
    } catch (XMPPException e) {
        return testSuite;
    }
    ourConnection.close();
    testSuite.addTestSuite(JabberFacade_ConnectionTest.class);
    return testSuite;
}
Also used : TestSuite(junit.framework.TestSuite) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPException(org.jivesoftware.smack.XMPPException)

Example 64 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project intellij-plugins by JetBrains.

the class JabberFacade_ConnectionTest method testConnectListener.

public void testConnectListener() throws Throwable {
    final boolean[] connected = new boolean[1];
    final boolean[] disconnected = new boolean[1];
    final boolean[] authenticated = new boolean[1];
    myFacade.addConnectionListener(new ConnectionListener() {

        @Override
        public void connected(XMPPConnection connection) {
            connected[0] = true;
        }

        @Override
        public void disconnected(boolean onError) {
            disconnected[0] = true;
        }

        @Override
        public void authenticated() {
            authenticated[0] = true;
        }
    });
    assertFalse("sanity check", connected[0] || disconnected[0] || authenticated[0]);
    createGoodAccount(myUser, myFacade);
    assertTrue("should call listener", connected[0] && authenticated[0]);
    assertFalse(disconnected[0]);
    myFacade.disconnect();
    assertTrue(disconnected[0]);
}
Also used : ConnectionListener(jetbrains.communicator.jabber.ConnectionListener) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Aggregations

XMPPConnection (org.jivesoftware.smack.XMPPConnection)64 XMPPException (org.jivesoftware.smack.XMPPException)14 Message (org.jivesoftware.smack.packet.Message)9 SmackException (org.jivesoftware.smack.SmackException)7 IQ (org.jivesoftware.smack.packet.IQ)7 InputStream (java.io.InputStream)6 OutputStream (java.io.OutputStream)6 ArrayList (java.util.ArrayList)6 SynchronousQueue (java.util.concurrent.SynchronousQueue)6 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)6 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)6 TimeoutException (java.util.concurrent.TimeoutException)5 StanzaCollector (org.jivesoftware.smack.StanzaCollector)5 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)5 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)5 ConnectionThread (com.xabber.android.data.connection.ConnectionThread)4 ConnectionConfiguration (org.jivesoftware.smack.ConnectionConfiguration)4 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)4 Packet (org.jivesoftware.smack.packet.Packet)4 Jid (org.jxmpp.jid.Jid)4