
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@451 72836036-5685-4462-b002-a69064685172
249 lines
5.9 KiB
Java
249 lines
5.9 KiB
Java
package jrummikub.control.network;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
import jrummikub.model.GameSettings;
|
|
import jrummikub.util.Connection;
|
|
import jrummikub.util.Event;
|
|
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;
|
|
|
|
/**
|
|
* Class dealing with network connection, offering and choice of network games
|
|
*/
|
|
public class NetworkControl {
|
|
private ConnectionControl connectionControl;
|
|
private IView view;
|
|
private List<Connection> connections = new ArrayList<Connection>();
|
|
private Event stopNetworkEvent = new Event();
|
|
|
|
private NetworkSettingsControl settingsControl;
|
|
private GameOfferControl gameOfferControl;
|
|
private GameJoinControl gameJoinControl;
|
|
|
|
private List<UUID> games = new ArrayList<UUID>();
|
|
private Map<UUID, GameData> gameMap = new HashMap<UUID, GameData>();
|
|
|
|
/**
|
|
* Creates a new network control
|
|
*
|
|
* @param loginData
|
|
* user's login data
|
|
* @param view
|
|
* for events and handlers
|
|
*/
|
|
public NetworkControl(final LoginData loginData, final IView view) {
|
|
this.view = view;
|
|
connectionControl = new ConnectionControl(loginData);
|
|
|
|
addConnectionControlListeners(loginData, view);
|
|
|
|
connections.add(view.getGameListPanel().getJoinEvent()
|
|
.add(new IListener1<GameData>() {
|
|
@Override
|
|
public void handle(GameData gameData) {
|
|
join(gameData);
|
|
}
|
|
}));
|
|
|
|
connections.add(view.getGameListPanel().getOpenNewGameEvent()
|
|
.add(new IListener() {
|
|
@Override
|
|
public void handle() {
|
|
createSettingsControl();
|
|
}
|
|
}));
|
|
|
|
connections.add(view.getGameListPanel().getCancelEvent()
|
|
.add(new IListener() {
|
|
@Override
|
|
public void handle() {
|
|
abort();
|
|
stopNetworkEvent.emit();
|
|
}
|
|
}));
|
|
}
|
|
|
|
private void join(GameData gameData) {
|
|
view.showGameListPanel(false);
|
|
connectionControl.joinGame(gameData.getGameID());
|
|
}
|
|
|
|
/**
|
|
* Adds the listeners for connection control events
|
|
*
|
|
* @param loginData
|
|
* player's login data
|
|
* @param view
|
|
* view for events
|
|
*/
|
|
public void addConnectionControlListeners(final LoginData loginData,
|
|
final IView view) {
|
|
connections.add(connectionControl.getConnectedEvent().add(new IListener() {
|
|
@Override
|
|
public void handle() {
|
|
view.getGameListPanel().setChannelName(loginData.getChannelName());
|
|
view.showGameListPanel(true);
|
|
}
|
|
}));
|
|
|
|
connections.add(connectionControl.getConnectionFailedEvent().add(
|
|
new IListener() {
|
|
@Override
|
|
public void handle() {
|
|
// TODO Auto-generated method stub
|
|
|
|
}
|
|
}));
|
|
|
|
connections.add(connectionControl.getGameOfferEvent().add(
|
|
new IListener1<GameData>() {
|
|
@Override
|
|
public void handle(GameData gameData) {
|
|
UUID uuid = gameData.getGameID();
|
|
|
|
GameData game = gameMap.get(uuid);
|
|
|
|
if (game == null) {
|
|
game = gameData;
|
|
gameMap.put(uuid, gameData);
|
|
games.add(uuid);
|
|
} else {
|
|
game.setGameSettings(gameData.getGameSettings());
|
|
}
|
|
|
|
updateGameList();
|
|
}
|
|
}));
|
|
connections.add(connectionControl.getGameWithdrawalEvent().add(
|
|
new IListener1<UUID>() {
|
|
@Override
|
|
public void handle(UUID uuid) {
|
|
games.remove(uuid);
|
|
gameMap.remove(uuid);
|
|
|
|
updateGameList();
|
|
}
|
|
}));
|
|
connections.add(connectionControl.getGameJoinAckEvent().add(
|
|
new IListener2<UUID, Boolean>() {
|
|
@Override
|
|
public void handle(UUID uuid, Boolean ack) {
|
|
if (ack) {
|
|
createGameJoinControl(uuid);
|
|
} else {
|
|
// TODO Error message
|
|
view.showGameListPanel(true);
|
|
}
|
|
}
|
|
}));
|
|
}
|
|
|
|
private void updateGameList() {
|
|
List<GameData> gameList = new ArrayList<GameData>();
|
|
|
|
for (UUID uuid : games) {
|
|
gameList.add(gameMap.get(uuid));
|
|
}
|
|
|
|
view.getGameListPanel().setGameList(gameList);
|
|
}
|
|
|
|
private void createGameJoinControl(UUID uuid) {
|
|
if (gameJoinControl != null) {
|
|
return;
|
|
}
|
|
|
|
GameData gameData = gameMap.get(uuid);
|
|
gameJoinControl = new GameJoinControl(connectionControl, gameData, view);
|
|
gameJoinControl.getBackEvent().add(new IListener() {
|
|
@Override
|
|
public void handle() {
|
|
gameJoinControl=null;
|
|
view.showGameListPanel(true);
|
|
}
|
|
});
|
|
gameJoinControl.startGameJoin();
|
|
}
|
|
|
|
/**
|
|
* Starts a new network connection with the sepcified data
|
|
*/
|
|
public void startNetwork() {
|
|
connectionControl.connect();
|
|
}
|
|
|
|
/**
|
|
* Ends the network connection if canceled
|
|
*/
|
|
public void abort() {
|
|
for (Connection c : connections) {
|
|
c.remove();
|
|
}
|
|
view.showGameListPanel(false);
|
|
|
|
if (settingsControl != null) {
|
|
settingsControl.abort();
|
|
}
|
|
if (gameOfferControl != null) {
|
|
gameOfferControl.abort();
|
|
}
|
|
if (gameJoinControl != null) {
|
|
gameJoinControl.abort();
|
|
}
|
|
|
|
connectionControl.disconnect();
|
|
}
|
|
|
|
/**
|
|
* Getter for stopNetworkEvent
|
|
*
|
|
* @return stopNetworkEvent
|
|
*/
|
|
public IEvent getStopNetworkEvent() {
|
|
return stopNetworkEvent;
|
|
}
|
|
|
|
private void createSettingsControl() {
|
|
if (settingsControl != null) {
|
|
return;
|
|
}
|
|
view.showGameListPanel(false);
|
|
|
|
settingsControl = new NetworkSettingsControl(
|
|
connectionControl.getNickname(), view, new GameSettings());
|
|
settingsControl.getOfferGameEvent().add(new IListener1<GameSettings>() {
|
|
@Override
|
|
public void handle(GameSettings settings) {
|
|
settingsControl = null;
|
|
createGameOfferControl(settings);
|
|
}
|
|
});
|
|
settingsControl.getBackEvent().add(new IListener() {
|
|
@Override
|
|
public void handle() {
|
|
settingsControl = null;
|
|
view.showGameListPanel(true);
|
|
}
|
|
});
|
|
settingsControl.startSettings();
|
|
}
|
|
|
|
private void createGameOfferControl(GameSettings settings) {
|
|
if (gameOfferControl != null) {
|
|
return;
|
|
}
|
|
gameOfferControl = new GameOfferControl(connectionControl, settings, view);
|
|
gameOfferControl.startGameOffer();
|
|
}
|
|
|
|
}
|