summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/control')
-rw-r--r--src/jrummikub/control/ApplicationControl.java20
-rw-r--r--src/jrummikub/control/network/ConnectionControl.java114
-rw-r--r--src/jrummikub/control/network/NetworkControl.java132
3 files changed, 193 insertions, 73 deletions
diff --git a/src/jrummikub/control/ApplicationControl.java b/src/jrummikub/control/ApplicationControl.java
index 29114a1..85fb663 100644
--- a/src/jrummikub/control/ApplicationControl.java
+++ b/src/jrummikub/control/ApplicationControl.java
@@ -28,7 +28,7 @@ public class ApplicationControl {
* Creates a new application control
*
* @param view
- * the view to use
+ * the view to use
*/
public ApplicationControl(final IView view) {
this.view = view;
@@ -54,10 +54,11 @@ public class ApplicationControl {
new IListener3<GameSettings, GameState, IRoundState>() {
@Override
- public void handle(GameSettings settings, GameState gameState,
- IRoundState roundState) {
+ public void handle(GameSettings settings,
+ GameState gameState, IRoundState roundState) {
abortControls();
- gameControl = new GameControl(settings, saveControl, view);
+ gameControl = new GameControl(settings, saveControl,
+ view);
addGameControlListeners(gameControl);
gameControl.continueGame(gameState, roundState);
@@ -154,10 +155,15 @@ public class ApplicationControl {
}
private void createNetworkControl(LoginData loginData) {
- networkControl = new NetworkControl(loginData);
+ networkControl = new NetworkControl(loginData, view);
- // TODO Add listeners
+ networkControl.getStopNetworkEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ startApplication();
+ }
+ });
- networkControl.connect();
+ networkControl.startNetwork();
}
}
diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java
new file mode 100644
index 0000000..9777853
--- /dev/null
+++ b/src/jrummikub/control/network/ConnectionControl.java
@@ -0,0 +1,114 @@
+package jrummikub.control.network;
+
+import javax.swing.SwingUtilities;
+
+import jrummikub.util.Event;
+import jrummikub.util.IEvent;
+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;
+
+class ConnectionControl {
+ private final LoginData loginData;
+ private volatile Connection connection;
+ private volatile MultiUserChat muc;
+
+ private Event connectedEvent = new Event();
+ private Event connectionFailedEvent = new Event();
+
+ ConnectionControl(LoginData loginData) {
+ this.loginData = loginData;
+ }
+
+ void connect() {
+ new Thread(new ConnectRunner()).start();
+ }
+
+ void disconnect() {
+ connectedEvent = new Event();
+ connectionFailedEvent = new Event();
+ new Thread(new DisconnectRunner()).start();
+
+ }
+
+ IEvent getConnectedEvent() {
+ return connectedEvent;
+ }
+
+ IEvent getConnectionFailedEvent() {
+ return connectionFailedEvent;
+ }
+
+ 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() {
+ synchronized (ConnectionControl.this) {
+ 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);
+ }
+ }
+ }
+ }
+
+ private class DisconnectRunner implements Runnable {
+ @Override
+ public void run() {
+ synchronized (ConnectionControl.this) {
+ if (connection != null) {
+ connection.disconnect();
+ connection = null;
+ }
+ }
+ }
+ }
+}
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;
}
+
}