diff options
Diffstat (limited to 'src/jrummikub/control/network/NetworkControl.java')
-rw-r--r-- | src/jrummikub/control/network/NetworkControl.java | 132 |
1 files changed, 66 insertions, 66 deletions
diff --git a/src/jrummikub/control/network/NetworkControl.java b/src/jrummikub/control/network/NetworkControl.java index 4494cb6..d56ea0e 100644 --- a/src/jrummikub/control/network/NetworkControl.java +++ b/src/jrummikub/control/network/NetworkControl.java @@ -1,87 +1,87 @@ package jrummikub.control.network; -import javax.swing.SwingUtilities; +import java.util.ArrayList; +import java.util.List; +import jrummikub.util.Connection; import jrummikub.util.Event; +import jrummikub.util.IEvent; +import jrummikub.util.IListener; +import jrummikub.util.IListener1; 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; +import jrummikub.view.IGameListPanel; +import jrummikub.view.IGameListPanel.GameData; +import jrummikub.view.IView; public class NetworkControl { - private final LoginData loginData; - private Connection connection; - private MultiUserChat muc; - private Thread networkThread; + private ConnectionControl connectionControl; + private IView view; + private List<Connection> connections = new ArrayList<Connection>(); + private Event stopNetworkEvent = new Event(); - private Event connectedEvent = new Event(); - private Event connectionFailedEvent = new Event(); + public NetworkControl(LoginData loginData, final IView view) { + this.view = view; + connectionControl = new ConnectionControl(loginData); - public NetworkControl(LoginData loginData) { - this.loginData = loginData; - } + connections.add(connectionControl.getConnectedEvent().add( + new IListener() { + @Override + public void handle() { + view.showGameListPanel(true); + } + })); - public void connect() { - new Thread(new ConnectRunner()).run(); - } + connections.add(connectionControl.getConnectionFailedEvent().add( + new IListener() { + @Override + public void handle() { + // TODO Auto-generated method stub - public void abort() { - // TODO Implement this - } + } + })); - private static void emitLater(final Event event) { - SwingUtilities.invokeLater(new Runnable() { + connections.add(view.getGameListPanel().getJoinEvent() + .add(new IListener1<IGameListPanel.GameData>() { + @Override + public void handle(GameData value) { + // TODO Auto-generated method stub - @Override - public void run() { - event.emit(); - } - }); - } + } + })); + + connections.add(view.getGameListPanel().getOpenNewGameEvent() + .add(new IListener() { + @Override + public void handle() { + // TODO Auto-generated method stub - 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; + connections.add(view.getGameListPanel().getCancelEvent() + .add(new IListener() { + @Override + public void handle() { + abort(); + stopNetworkEvent.emit(); + } + })); + } - // TODO Auto-generated catch block - e.printStackTrace(); + public void startNetwork() { + connectionControl.connect(); + } - emitLater(connectionFailedEvent); - } + public void abort() { + for (Connection c : connections) { + c.remove(); } + connectionControl.disconnect(); + view.showGameListPanel(false); + } + + public IEvent getStopNetworkEvent() { + return stopNetworkEvent; } + } |