package jrummikub.control.network; import javax.swing.SwingUtilities; import jrummikub.util.Event; import jrummikub.util.LoginData; import org.jivesoftware.smack.Connection; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.XMPPError; import org.jivesoftware.smack.packet.XMPPError.Type; import org.jivesoftware.smackx.muc.MultiUserChat; public class NetworkControl { private final LoginData loginData; private Connection connection; private MultiUserChat muc; private Thread networkThread; private Event connectedEvent = new Event(); private Event connectionFailedEvent = new Event(); public NetworkControl(LoginData loginData) { this.loginData = loginData; } public void connect() { new Thread(new ConnectRunner()).run(); } public void abort() { // TODO Implement this } private static void emitLater(final Event event) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { event.emit(); } }); } private class ConnectRunner implements Runnable { @Override public void run() { connection = new XMPPConnection(loginData.getServerName()); try { connection.connect(); connection.login(loginData.getUserName(), loginData.getPassword(), "JRummikub"); muc = new MultiUserChat(connection, loginData.getChannelName()); String nickname = loginData.getUserName(); // Loop until a unused nickname is found while (true) { try { muc.join(nickname); break; // Join was successful, break the loop } catch (XMPPException e) { XMPPError error = e.getXMPPError(); if (error.getType() == Type.CANCEL && error.getCode() == 409) { // There was a conflict, try again with another nickname nickname += "_"; continue; } else { // An unknown error has occurred, cancel connect throw e; } } } emitLater(connectedEvent); } catch (XMPPException e) { connection.disconnect(); connection = null; // TODO Auto-generated catch block e.printStackTrace(); emitLater(connectionFailedEvent); } } } }