Created IConnectionControl and preliminary MockConnectionControl

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@460 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Jannis Harder 2011-06-18 03:23:30 +02:00
parent 325802a215
commit c50fd5d701
8 changed files with 283 additions and 65 deletions

View file

@ -0,0 +1,147 @@
package jrummikub.control.network;
import java.awt.Color;
import java.util.UUID;
import jrummikub.util.GameData;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.IEvent2;
import jrummikub.util.MockEvent;
import jrummikub.util.MockEvent1;
import jrummikub.util.MockEvent2;
public class MockConnectionControl implements IConnectionControl {
/** */
public String nickname;
/** */
public MockEvent connectedEvent = new MockEvent();
/** */
public MockEvent connectionFailedEvent = new MockEvent();
/** */
public MockEvent1<GameData> gameOfferEvent = new MockEvent1<GameData>();
/** */
public MockEvent1<UUID> gameWithdrawalEvent = new MockEvent1<UUID>();
/** */
public MockEvent1<String> gameJoinEvent = new MockEvent1<String>();
/** */
public MockEvent1<String> gameLeaveEvent = new MockEvent1<String>();
/** */
public MockEvent1<Boolean> gameJoinAckEvent = new MockEvent1<Boolean>();
/** */
public MockEvent2<String, Color> changeColorEvent = new MockEvent2<String, Color>();
/** */
public GameData currentGame;
/** */
public GameData offeredGame;
/** */
public boolean connected;
/** */
public boolean failOnConnect;
@Override
public String getNickname() {
return nickname;
}
@Override
public void connect() {
if (failOnConnect) {
connectionFailedEvent.emit();
} else {
connected = true;
connectedEvent.emit();
}
}
@Override
public void disconnect() {
connected = false;
}
@Override
public IEvent getConnectedEvent() {
return connectedEvent;
}
@Override
public IEvent getConnectionFailedEvent() {
return connectionFailedEvent;
}
@Override
public IEvent1<GameData> getGameOfferEvent() {
return gameOfferEvent;
}
@Override
public IEvent1<UUID> getGameWithdrawalEvent() {
return gameWithdrawalEvent;
}
@Override
public IEvent1<String> getGameJoinEvent() {
return gameJoinEvent;
}
@Override
public IEvent1<String> getGameLeaveEvent() {
return gameLeaveEvent;
}
@Override
public IEvent1<Boolean> getGameJoinAckEvent() {
return gameJoinAckEvent;
}
@Override
public IEvent2<String, Color> getChangeColorEvent() {
return changeColorEvent;
}
@Override
public void offerGame(GameData data) {
// TODO Auto-generated method stub
}
@Override
public void withdrawGame() {
// TODO Auto-generated method stub
}
@Override
public GameData getCurrentGame() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setCurrentGame(GameData game) {
// TODO Auto-generated method stub
}
@Override
public void joinGame(GameData game) {
// TODO Auto-generated method stub
}
@Override
public void leaveGame() {
// TODO Auto-generated method stub
}
@Override
public void ackJoinGame(String recipient, boolean ack) {
// TODO Auto-generated method stub
}
@Override
public void changeColor(Color color) {
// TODO Auto-generated method stub
}
}

View file

@ -21,11 +21,11 @@ import jrummikub.view.IView;
public abstract class AbstractGameBeginControl { public abstract class AbstractGameBeginControl {
protected List<Connection> connections = new ArrayList<Connection>(); protected List<Connection> connections = new ArrayList<Connection>();
protected GameData gameData; protected GameData gameData;
protected ConnectionControl connectionControl; protected IConnectionControl connectionControl;
protected IView view; protected IView view;
protected Event backEvent = new Event(); protected Event backEvent = new Event();
public AbstractGameBeginControl(ConnectionControl connection, IView view, public AbstractGameBeginControl(IConnectionControl connection, IView view,
final GameData gameData, SettingsMode settingsMode) { final GameData gameData, SettingsMode settingsMode) {
this.connectionControl = connection; this.connectionControl = connection;
this.view = view; this.view = view;
@ -81,7 +81,7 @@ public abstract class AbstractGameBeginControl {
} }
protected abstract void goBack(); protected abstract void goBack();
public Event getBackEvent() { public Event getBackEvent() {
return backEvent; return backEvent;
} }

View file

@ -32,7 +32,7 @@ import org.jivesoftware.smack.util.Base64;
import org.jivesoftware.smackx.muc.DiscussionHistory; import org.jivesoftware.smackx.muc.DiscussionHistory;
import org.jivesoftware.smackx.muc.MultiUserChat; import org.jivesoftware.smackx.muc.MultiUserChat;
class ConnectionControl { class ConnectionControl implements IConnectionControl {
private final static String ELEMENT_NAME = "rummikub"; private final static String ELEMENT_NAME = "rummikub";
private final static String NAMESPACE = "http://home.universe-factory.net/rummikub/"; private final static String NAMESPACE = "http://home.universe-factory.net/rummikub/";
@ -61,15 +61,18 @@ class ConnectionControl {
this.loginData = loginData; this.loginData = loginData;
} }
String getNickname() { @Override
public String getNickname() {
return muc.getNickname(); return muc.getNickname();
} }
void connect() { @Override
public void connect() {
new Thread(new ConnectRunner()).start(); new Thread(new ConnectRunner()).start();
} }
void disconnect() { @Override
public void disconnect() {
connectedEvent = new Event(); connectedEvent = new Event();
connectionFailedEvent = new Event(); connectionFailedEvent = new Event();
new Thread(new Runnable() { new Thread(new Runnable() {
@ -85,45 +88,55 @@ class ConnectionControl {
}).start(); }).start();
} }
IEvent getConnectedEvent() { @Override
public IEvent getConnectedEvent() {
return connectedEvent; return connectedEvent;
} }
IEvent getConnectionFailedEvent() { @Override
public IEvent getConnectionFailedEvent() {
return connectionFailedEvent; return connectionFailedEvent;
} }
IEvent1<GameData> getGameOfferEvent() { @Override
public IEvent1<GameData> getGameOfferEvent() {
return gameOfferEvent; return gameOfferEvent;
} }
IEvent1<UUID> getGameWithdrawalEvent() { @Override
public IEvent1<UUID> getGameWithdrawalEvent() {
return gameWithdrawalEvent; return gameWithdrawalEvent;
} }
IEvent1<String> getGameJoinEvent() { @Override
public IEvent1<String> getGameJoinEvent() {
return gameJoinEvent; return gameJoinEvent;
} }
IEvent1<String> getGameLeaveEvent() { @Override
public IEvent1<String> getGameLeaveEvent() {
return gameLeaveEvent; return gameLeaveEvent;
} }
IEvent1<Boolean> getGameJoinAckEvent() { @Override
public IEvent1<Boolean> getGameJoinAckEvent() {
return gameJoinAckEvent; return gameJoinAckEvent;
} }
IEvent2<String, Color> getChangeColorEvent() { @Override
public IEvent2<String, Color> getChangeColorEvent() {
return changeColorEvent; return changeColorEvent;
} }
void offerGame(GameData data) { @Override
public void offerGame(GameData data) {
offeredGame = data; offeredGame = data;
currentGame = data; currentGame = data;
sendGameOffer(); sendGameOffer();
} }
void withdrawGame() { @Override
public void withdrawGame() {
offeredGame = null; offeredGame = null;
final UUID uuid = currentGame.getGameID(); final UUID uuid = currentGame.getGameID();
currentGame = null; currentGame = null;
@ -131,65 +144,70 @@ class ConnectionControl {
@Override @Override
public Message send() { public Message send() {
DefaultPacketExtension extension = createJRummikubExtension(); DefaultPacketExtension extension = createJRummikubExtension();
extension.setValue("messageType", "game_withdrawal"); extension.setValue("messageType", "game_withdrawal");
extension.setValue("uuid", uuid.toString()); extension.setValue("uuid", uuid.toString());
return createMessage(extension); return createMessage(extension);
} }
}).start(); }).start();
} }
GameData getCurrentGame() { @Override
public GameData getCurrentGame() {
return currentGame; return currentGame;
} }
void setCurrentGame(GameData game) { @Override
public void setCurrentGame(GameData game) {
this.currentGame = game; this.currentGame = game;
} }
void joinGame(final GameData game) { @Override
public void joinGame(final GameData game) {
setCurrentGame(game); setCurrentGame(game);
new Thread(new SendRunner() { new Thread(new SendRunner() {
@Override @Override
public Message send() { public Message send() {
DefaultPacketExtension extension = createJRummikubExtension(); DefaultPacketExtension extension = createJRummikubExtension();
extension.setValue("messageType", "game_join"); extension.setValue("messageType", "game_join");
extension.setValue("uuid", game.getGameID().toString()); extension.setValue("uuid", game.getGameID().toString());
return createMessage(extension); return createMessage(extension);
} }
}).start(); }).start();
} }
void leaveGame() { @Override
public void leaveGame() {
final UUID uuid = currentGame.getGameID(); final UUID uuid = currentGame.getGameID();
currentGame = null; currentGame = null;
new Thread(new SendRunner() { new Thread(new SendRunner() {
@Override @Override
public Message send() { public Message send() {
DefaultPacketExtension extension = createJRummikubExtension(); DefaultPacketExtension extension = createJRummikubExtension();
extension.setValue("messageType", "game_leave"); extension.setValue("messageType", "game_leave");
extension.setValue("uuid", uuid.toString()); extension.setValue("uuid", uuid.toString());
return createMessage(extension); return createMessage(extension);
} }
}).start(); }).start();
} }
void ackJoinGame(final String recipient, final boolean ack) { @Override
public void ackJoinGame(final String recipient, final boolean ack) {
final UUID uuid = currentGame.getGameID(); final UUID uuid = currentGame.getGameID();
new Thread(new SendRunner() { new Thread(new SendRunner() {
@Override @Override
public Message send() { public Message send() {
DefaultPacketExtension extension = createJRummikubExtension(); DefaultPacketExtension extension = createJRummikubExtension();
extension.setValue("messageType", "game_join_ack"); extension.setValue("messageType", "game_join_ack");
extension.setValue("uuid", uuid.toString()); extension.setValue("uuid", uuid.toString());
extension.setValue("ack", Boolean.toString(ack)); extension.setValue("ack", Boolean.toString(ack));
Message message = createMessage(extension); Message message = createMessage(extension);
message.setType(Message.Type.normal); message.setType(Message.Type.normal);
message.setTo(muc.getRoom() + "/" + recipient); message.setTo(muc.getRoom() + "/" + recipient);
@ -198,21 +216,23 @@ class ConnectionControl {
}).start(); }).start();
} }
void changeColor(final Color color) { @Override
public void changeColor(final Color color) {
final UUID uuid = currentGame.getGameID(); final UUID uuid = currentGame.getGameID();
new Thread(new SendRunner() { new Thread(new SendRunner() {
@Override @Override
public Message send() { public Message send() {
DefaultPacketExtension extension = createJRummikubExtension(); DefaultPacketExtension extension = createJRummikubExtension();
extension.setValue("messageType", "change_color"); extension.setValue("messageType", "change_color");
extension.setValue("uuid", uuid.toString()); extension.setValue("uuid", uuid.toString());
extension.setValue("color", Base64.encodeObject(color, Base64.GZIP)); extension.setValue("color",
Base64.encodeObject(color, Base64.GZIP));
return createMessage(extension); return createMessage(extension);
} }
}).start(); }).start();
} }
private void sendGameOffer() { private void sendGameOffer() {
@ -231,15 +251,15 @@ class ConnectionControl {
} }
}).start(); }).start();
} }
private void requestGames() { private void requestGames() {
new Thread(new SendRunner() { new Thread(new SendRunner() {
@Override @Override
public Message send() { public Message send() {
DefaultPacketExtension extension = createJRummikubExtension(); DefaultPacketExtension extension = createJRummikubExtension();
extension.setValue("messageType", "game_request"); extension.setValue("messageType", "game_request");
return createMessage(extension); return createMessage(extension);
} }
}); });

View file

@ -5,7 +5,6 @@ import java.util.UUID;
import jrummikub.model.GameSettings; import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type; import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Event;
import jrummikub.util.GameData; import jrummikub.util.GameData;
import jrummikub.util.IListener1; import jrummikub.util.IListener1;
import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.ISettingsPanel.SettingsMode;
@ -13,7 +12,7 @@ import jrummikub.view.IView;
public class GameJoinControl extends AbstractGameBeginControl { public class GameJoinControl extends AbstractGameBeginControl {
public GameJoinControl(final ConnectionControl connectionControl, public GameJoinControl(final IConnectionControl connectionControl,
final GameData gameData, final IView view) { final GameData gameData, final IView view) {
super(connectionControl, view, gameData, SettingsMode.NETWORK_JOIN); super(connectionControl, view, gameData, SettingsMode.NETWORK_JOIN);

View file

@ -13,7 +13,7 @@ import jrummikub.view.IView;
public class GameOfferControl extends AbstractGameBeginControl { public class GameOfferControl extends AbstractGameBeginControl {
public GameOfferControl(final ConnectionControl connectionControl, public GameOfferControl(final IConnectionControl connectionControl,
final GameSettings settings, final IView view) { final GameSettings settings, final IView view) {
super(connectionControl, view, super(connectionControl, view,
new GameData(UUID.randomUUID(), settings), new GameData(UUID.randomUUID(), settings),

View file

@ -0,0 +1,51 @@
package jrummikub.control.network;
import java.awt.Color;
import java.util.UUID;
import jrummikub.util.GameData;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.IEvent2;
interface IConnectionControl {
public String getNickname();
public void connect();
public void disconnect();
public IEvent getConnectedEvent();
public IEvent getConnectionFailedEvent();
public IEvent1<GameData> getGameOfferEvent();
public IEvent1<UUID> getGameWithdrawalEvent();
public IEvent1<String> getGameJoinEvent();
public IEvent1<String> getGameLeaveEvent();
public IEvent1<Boolean> getGameJoinAckEvent();
public IEvent2<String, Color> getChangeColorEvent();
public void offerGame(GameData data);
public void withdrawGame();
public GameData getCurrentGame();
public void setCurrentGame(GameData game);
public void joinGame(final GameData game);
public void leaveGame();
public void ackJoinGame(final String recipient, final boolean ack);
public void changeColor(final Color color);
}

View file

@ -13,7 +13,6 @@ import jrummikub.util.GameData;
import jrummikub.util.IEvent; import jrummikub.util.IEvent;
import jrummikub.util.IListener; import jrummikub.util.IListener;
import jrummikub.util.IListener1; import jrummikub.util.IListener1;
import jrummikub.util.IListener2;
import jrummikub.util.LoginData; import jrummikub.util.LoginData;
import jrummikub.view.IView; import jrummikub.view.IView;
@ -21,7 +20,7 @@ import jrummikub.view.IView;
* Class dealing with network connection, offering and choice of network games * Class dealing with network connection, offering and choice of network games
*/ */
public class NetworkControl { public class NetworkControl {
private ConnectionControl connectionControl; private IConnectionControl connectionControl;
private IView view; private IView view;
private List<Connection> connections = new ArrayList<Connection>(); private List<Connection> connections = new ArrayList<Connection>();
private Event stopNetworkEvent = new Event(); private Event stopNetworkEvent = new Event();
@ -37,9 +36,9 @@ public class NetworkControl {
* Creates a new network control * Creates a new network control
* *
* @param loginData * @param loginData
* user's login data * user's login data
* @param view * @param view
* for events and handlers * for events and handlers
*/ */
public NetworkControl(final LoginData loginData, final IView view) { public NetworkControl(final LoginData loginData, final IView view) {
this.view = view; this.view = view;
@ -82,19 +81,21 @@ public class NetworkControl {
* Adds the listeners for connection control events * Adds the listeners for connection control events
* *
* @param loginData * @param loginData
* player's login data * player's login data
* @param view * @param view
* view for events * view for events
*/ */
public void addConnectionControlListeners(final LoginData loginData, public void addConnectionControlListeners(final LoginData loginData,
final IView view) { final IView view) {
connections.add(connectionControl.getConnectedEvent().add(new IListener() { connections.add(connectionControl.getConnectedEvent().add(
@Override new IListener() {
public void handle() { @Override
view.getGameListPanel().setChannelName(loginData.getChannelName()); public void handle() {
view.showGameListPanel(true); view.getGameListPanel().setChannelName(
} loginData.getChannelName());
})); view.showGameListPanel(true);
}
}));
connections.add(connectionControl.getConnectionFailedEvent().add( connections.add(connectionControl.getConnectionFailedEvent().add(
new IListener() { new IListener() {
@ -165,10 +166,10 @@ public class NetworkControl {
GameData gameData = connectionControl.getCurrentGame(); GameData gameData = connectionControl.getCurrentGame();
gameJoinControl = new GameJoinControl(connectionControl, gameData, view); gameJoinControl = new GameJoinControl(connectionControl, gameData, view);
gameJoinControl.getBackEvent().add(new IListener() { gameJoinControl.getBackEvent().add(new IListener() {
@Override @Override
public void handle() { public void handle() {
gameJoinControl=null; gameJoinControl = null;
view.showGameListPanel(true); view.showGameListPanel(true);
} }
}); });
@ -242,11 +243,12 @@ public class NetworkControl {
if (gameOfferControl != null) { if (gameOfferControl != null) {
return; return;
} }
gameOfferControl = new GameOfferControl(connectionControl, settings, view); gameOfferControl = new GameOfferControl(connectionControl, settings,
gameOfferControl.getBackEvent().add(new IListener() { view);
gameOfferControl.getBackEvent().add(new IListener() {
@Override @Override
public void handle() { public void handle() {
gameOfferControl=null; gameOfferControl = null;
view.showGameListPanel(true); view.showGameListPanel(true);
} }
}); });

View file

@ -48,16 +48,16 @@ public class NetworkSettingsControl extends AbstractSettingsControl {
addListeners(); addListeners();
connections.add(view.getSettingsPanel().getOfferGameEvent().add( connections.add(view.getSettingsPanel().getOfferGameEvent()
new IListener() { .add(new IListener() {
@Override @Override
public void handle() { public void handle() {
offerGame(); offerGame();
} }
})); }));
connections.add(view.getSettingsPanel().getBackEvent().add( connections.add(view.getSettingsPanel().getBackEvent()
new IListener() { .add(new IListener() {
@Override @Override
public void handle() { public void handle() {
abort(); abort();
@ -96,8 +96,7 @@ public class NetworkSettingsControl extends AbstractSettingsControl {
@Override @Override
protected void update() { protected void update() {
view view.getSettingsPanel()
.getSettingsPanel()
.enableAddPlayerButton( .enableAddPlayerButton(
settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length); settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length);