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 {
protected List<Connection> connections = new ArrayList<Connection>();
protected GameData gameData;
protected ConnectionControl connectionControl;
protected IConnectionControl connectionControl;
protected IView view;
protected Event backEvent = new Event();
public AbstractGameBeginControl(ConnectionControl connection, IView view,
public AbstractGameBeginControl(IConnectionControl connection, IView view,
final GameData gameData, SettingsMode settingsMode) {
this.connectionControl = connection;
this.view = view;

View file

@ -32,7 +32,7 @@ import org.jivesoftware.smack.util.Base64;
import org.jivesoftware.smackx.muc.DiscussionHistory;
import org.jivesoftware.smackx.muc.MultiUserChat;
class ConnectionControl {
class ConnectionControl implements IConnectionControl {
private final static String ELEMENT_NAME = "rummikub";
private final static String NAMESPACE = "http://home.universe-factory.net/rummikub/";
@ -61,15 +61,18 @@ class ConnectionControl {
this.loginData = loginData;
}
String getNickname() {
@Override
public String getNickname() {
return muc.getNickname();
}
void connect() {
@Override
public void connect() {
new Thread(new ConnectRunner()).start();
}
void disconnect() {
@Override
public void disconnect() {
connectedEvent = new Event();
connectionFailedEvent = new Event();
new Thread(new Runnable() {
@ -85,45 +88,55 @@ class ConnectionControl {
}).start();
}
IEvent getConnectedEvent() {
@Override
public IEvent getConnectedEvent() {
return connectedEvent;
}
IEvent getConnectionFailedEvent() {
@Override
public IEvent getConnectionFailedEvent() {
return connectionFailedEvent;
}
IEvent1<GameData> getGameOfferEvent() {
@Override
public IEvent1<GameData> getGameOfferEvent() {
return gameOfferEvent;
}
IEvent1<UUID> getGameWithdrawalEvent() {
@Override
public IEvent1<UUID> getGameWithdrawalEvent() {
return gameWithdrawalEvent;
}
IEvent1<String> getGameJoinEvent() {
@Override
public IEvent1<String> getGameJoinEvent() {
return gameJoinEvent;
}
IEvent1<String> getGameLeaveEvent() {
@Override
public IEvent1<String> getGameLeaveEvent() {
return gameLeaveEvent;
}
IEvent1<Boolean> getGameJoinAckEvent() {
@Override
public IEvent1<Boolean> getGameJoinAckEvent() {
return gameJoinAckEvent;
}
IEvent2<String, Color> getChangeColorEvent() {
@Override
public IEvent2<String, Color> getChangeColorEvent() {
return changeColorEvent;
}
void offerGame(GameData data) {
@Override
public void offerGame(GameData data) {
offeredGame = data;
currentGame = data;
sendGameOffer();
}
void withdrawGame() {
@Override
public void withdrawGame() {
offeredGame = null;
final UUID uuid = currentGame.getGameID();
currentGame = null;
@ -140,15 +153,18 @@ class ConnectionControl {
}).start();
}
GameData getCurrentGame() {
@Override
public GameData getCurrentGame() {
return currentGame;
}
void setCurrentGame(GameData game) {
@Override
public void setCurrentGame(GameData game) {
this.currentGame = game;
}
void joinGame(final GameData game) {
@Override
public void joinGame(final GameData game) {
setCurrentGame(game);
new Thread(new SendRunner() {
@Override
@ -163,7 +179,8 @@ class ConnectionControl {
}).start();
}
void leaveGame() {
@Override
public void leaveGame() {
final UUID uuid = currentGame.getGameID();
currentGame = null;
new Thread(new SendRunner() {
@ -179,7 +196,8 @@ class ConnectionControl {
}).start();
}
void ackJoinGame(final String recipient, final boolean ack) {
@Override
public void ackJoinGame(final String recipient, final boolean ack) {
final UUID uuid = currentGame.getGameID();
new Thread(new SendRunner() {
@Override
@ -198,7 +216,8 @@ class ConnectionControl {
}).start();
}
void changeColor(final Color color) {
@Override
public void changeColor(final Color color) {
final UUID uuid = currentGame.getGameID();
new Thread(new SendRunner() {
@Override
@ -207,7 +226,8 @@ class ConnectionControl {
extension.setValue("messageType", "change_color");
extension.setValue("uuid", uuid.toString());
extension.setValue("color", Base64.encodeObject(color, Base64.GZIP));
extension.setValue("color",
Base64.encodeObject(color, Base64.GZIP));
return createMessage(extension);
}

View file

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

View file

@ -13,7 +13,7 @@ import jrummikub.view.IView;
public class GameOfferControl extends AbstractGameBeginControl {
public GameOfferControl(final ConnectionControl connectionControl,
public GameOfferControl(final IConnectionControl connectionControl,
final GameSettings settings, final IView view) {
super(connectionControl, view,
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.IListener;
import jrummikub.util.IListener1;
import jrummikub.util.IListener2;
import jrummikub.util.LoginData;
import jrummikub.view.IView;
@ -21,7 +20,7 @@ import jrummikub.view.IView;
* Class dealing with network connection, offering and choice of network games
*/
public class NetworkControl {
private ConnectionControl connectionControl;
private IConnectionControl connectionControl;
private IView view;
private List<Connection> connections = new ArrayList<Connection>();
private Event stopNetworkEvent = new Event();
@ -88,10 +87,12 @@ public class NetworkControl {
*/
public void addConnectionControlListeners(final LoginData loginData,
final IView view) {
connections.add(connectionControl.getConnectedEvent().add(new IListener() {
connections.add(connectionControl.getConnectedEvent().add(
new IListener() {
@Override
public void handle() {
view.getGameListPanel().setChannelName(loginData.getChannelName());
view.getGameListPanel().setChannelName(
loginData.getChannelName());
view.showGameListPanel(true);
}
}));
@ -168,7 +169,7 @@ public class NetworkControl {
gameJoinControl.getBackEvent().add(new IListener() {
@Override
public void handle() {
gameJoinControl=null;
gameJoinControl = null;
view.showGameListPanel(true);
}
});
@ -242,11 +243,12 @@ public class NetworkControl {
if (gameOfferControl != null) {
return;
}
gameOfferControl = new GameOfferControl(connectionControl, settings, view);
gameOfferControl = new GameOfferControl(connectionControl, settings,
view);
gameOfferControl.getBackEvent().add(new IListener() {
@Override
public void handle() {
gameOfferControl=null;
gameOfferControl = null;
view.showGameListPanel(true);
}
});

View file

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