Improvements to session state

This commit is contained in:
BetaSteward 2011-06-18 16:47:13 -04:00
parent 43e87b2ebd
commit 76878b642d

View file

@ -162,14 +162,14 @@ public class Session {
logger.fatal("Error disconnecting ...", ex);
}
}
client.disconnected();
logger.info("Disconnected ... ");
if (sessionState == SessionState.SERVER_UNAVAILABLE && showMessage) {
client.showError("Server error. You have been disconnected");
}
else {
server = null;
if (sessionState == SessionState.DISCONNECTING) {
sessionState = SessionState.DISCONNECTED;
logger.info("Disconnected ... ");
}
client.disconnected();
if (showMessage)
client.showError("Server error. You have been disconnected");
}
public boolean ping() {
@ -211,6 +211,7 @@ public class Session {
public UUID getMainRoomId() {
try {
if (sessionState == SessionState.CONNECTED)
return server.getMainRoomId();
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -222,6 +223,7 @@ public class Session {
public UUID getRoomChatId(UUID roomId) {
try {
if (sessionState == SessionState.CONNECTED)
return server.getRoomChatId(roomId);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -233,6 +235,7 @@ public class Session {
public UUID getTableChatId(UUID tableId) {
try {
if (sessionState == SessionState.CONNECTED)
return server.getTableChatId(tableId);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -244,6 +247,7 @@ public class Session {
public UUID getGameChatId(UUID gameId) {
try {
if (sessionState == SessionState.CONNECTED)
return server.getGameChatId(gameId);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -255,6 +259,7 @@ public class Session {
public TableView getTable(UUID roomId, UUID tableId) {
try {
if (sessionState == SessionState.CONNECTED)
return server.getTable(roomId, tableId);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -266,8 +271,10 @@ public class Session {
public boolean watchTable(UUID roomId, UUID tableId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.watchTable(sessionId, roomId, tableId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -278,6 +285,7 @@ public class Session {
public boolean joinTable(UUID roomId, UUID tableId, String playerName, String playerType, int skill, DeckCardLists deckList) {
try {
if (sessionState == SessionState.CONNECTED)
return server.joinTable(sessionId, roomId, tableId, playerName, playerType, skill, deckList);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -291,6 +299,7 @@ public class Session {
public boolean joinTournamentTable(UUID roomId, UUID tableId, String playerName, String playerType, int skill) {
try {
if (sessionState == SessionState.CONNECTED)
return server.joinTournamentTable(sessionId, roomId, tableId, playerName, playerType, skill);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -305,7 +314,7 @@ public class Session {
public Collection<TableView> getTables(UUID roomId) throws MageRemoteException {
lock.readLock().lock();
try {
if (server == null) return null;
if (sessionState == SessionState.CONNECTED)
return server.getTables(roomId);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -316,11 +325,13 @@ public class Session {
} finally {
lock.readLock().unlock();
}
return null;
}
public Collection<String> getConnectedPlayers(UUID roomId) throws MageRemoteException {
lock.readLock().lock();
try {
if (sessionState == SessionState.CONNECTED)
return server.getConnectedPlayers(roomId);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -331,10 +342,12 @@ public class Session {
} finally {
lock.readLock().unlock();
}
return null;
}
public TournamentView getTournament(UUID tournamentId) throws MageRemoteException {
try {
if (sessionState == SessionState.CONNECTED)
return server.getTournament(tournamentId);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -343,10 +356,12 @@ public class Session {
handleMageException(ex);
throw new MageRemoteException();
}
return null;
}
public UUID getTournamentChatId(UUID tournamentId) {
try {
if (sessionState == SessionState.CONNECTED)
return server.getTournamentChatId(tournamentId);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -358,8 +373,10 @@ public class Session {
public boolean sendPlayerUUID(UUID gameId, UUID data) {
try {
if (sessionState == SessionState.CONNECTED) {
server.sendPlayerUUID(gameId, sessionId, data);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -370,8 +387,10 @@ public class Session {
public boolean sendPlayerBoolean(UUID gameId, boolean data) {
try {
if (sessionState == SessionState.CONNECTED) {
server.sendPlayerBoolean(gameId, sessionId, data);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -382,8 +401,10 @@ public class Session {
public boolean sendPlayerInteger(UUID gameId, int data) {
try {
if (sessionState == SessionState.CONNECTED) {
server.sendPlayerInteger(gameId, sessionId, data);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -394,8 +415,10 @@ public class Session {
public boolean sendPlayerString(UUID gameId, String data) {
try {
if (sessionState == SessionState.CONNECTED) {
server.sendPlayerString(gameId, sessionId, data);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -406,6 +429,7 @@ public class Session {
public DraftPickView sendCardPick(UUID draftId, UUID cardId) {
try {
if (sessionState == SessionState.CONNECTED)
return server.sendCardPick(draftId, sessionId, cardId);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -417,8 +441,10 @@ public class Session {
public boolean joinChat(UUID chatId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.joinChat(chatId, sessionId, userName);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -430,9 +456,10 @@ public class Session {
public boolean leaveChat(UUID chatId) {
lock.readLock().lock();
try {
if (server == null) return false;
if (sessionState == SessionState.CONNECTED) {
server.leaveChat(chatId, sessionId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -446,9 +473,10 @@ public class Session {
public boolean sendChatMessage(UUID chatId, String message) {
lock.readLock().lock();
try {
if (server == null) return false;
if (sessionState == SessionState.CONNECTED) {
server.sendChatMessage(chatId, userName, message);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -461,8 +489,10 @@ public class Session {
public boolean joinGame(UUID gameId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.joinGame(gameId, sessionId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -473,8 +503,10 @@ public class Session {
public boolean joinDraft(UUID draftId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.joinDraft(draftId, sessionId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -485,8 +517,10 @@ public class Session {
public boolean joinTournament(UUID tournamentId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.joinTournament(tournamentId, sessionId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -497,8 +531,10 @@ public class Session {
public boolean watchGame(UUID gameId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.watchGame(gameId, sessionId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -509,8 +545,10 @@ public class Session {
public boolean replayGame(UUID gameId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.replayGame(gameId, sessionId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -521,6 +559,7 @@ public class Session {
public TableView createTable(UUID roomId, MatchOptions matchOptions) {
try {
if (sessionState == SessionState.CONNECTED)
return server.createTable(sessionId, roomId, matchOptions);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -532,6 +571,7 @@ public class Session {
public TableView createTournamentTable(UUID roomId, TournamentOptions tournamentOptions) {
try {
if (sessionState == SessionState.CONNECTED)
return server.createTournamentTable(sessionId, roomId, tournamentOptions);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -543,6 +583,7 @@ public class Session {
public boolean isTableOwner(UUID roomId, UUID tableId) {
try {
if (sessionState == SessionState.CONNECTED)
return server.isTableOwner(sessionId, roomId, tableId);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -554,8 +595,10 @@ public class Session {
public boolean removeTable(UUID roomId, UUID tableId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.removeTable(sessionId, roomId, tableId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -566,8 +609,10 @@ public class Session {
public boolean swapSeats(UUID roomId, UUID tableId, int seatNum1, int seatNum2) {
try {
if (sessionState == SessionState.CONNECTED) {
server.swapSeats(sessionId, roomId, tableId, seatNum1, seatNum2);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -578,8 +623,10 @@ public class Session {
public boolean leaveTable(UUID roomId, UUID tableId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.leaveTable(sessionId, roomId, tableId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -590,8 +637,10 @@ public class Session {
public boolean startGame(UUID roomId, UUID tableId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.startMatch(sessionId, roomId, tableId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -602,8 +651,10 @@ public class Session {
public boolean startTournament(UUID roomId, UUID tableId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.startTournament(sessionId, roomId, tableId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -614,8 +665,10 @@ public class Session {
public boolean startChallenge(UUID roomId, UUID tableId, UUID challengeId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.startChallenge(sessionId, roomId, tableId, challengeId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -626,6 +679,7 @@ public class Session {
public boolean submitDeck(UUID tableId, DeckCardLists deck) {
try {
if (sessionState == SessionState.CONNECTED)
return server.submitDeck(sessionId, tableId, deck);
} catch (RemoteException ex) {
handleRemoteException(ex);
@ -639,8 +693,10 @@ public class Session {
public boolean concedeGame(UUID gameId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.concedeGame(gameId, sessionId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -651,8 +707,10 @@ public class Session {
public boolean stopWatching(UUID gameId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.stopWatching(gameId, sessionId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -663,8 +721,10 @@ public class Session {
public boolean startReplay(UUID gameId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.startReplay(gameId, sessionId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -675,8 +735,10 @@ public class Session {
public boolean stopReplay(UUID gameId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.stopReplay(gameId, sessionId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -687,8 +749,10 @@ public class Session {
public boolean nextPlay(UUID gameId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.nextPlay(gameId, sessionId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -699,8 +763,10 @@ public class Session {
public boolean previousPlay(UUID gameId) {
try {
if (sessionState == SessionState.CONNECTED) {
server.previousPlay(gameId, sessionId);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -711,8 +777,10 @@ public class Session {
public boolean cheat(UUID gameId, UUID playerId, DeckCardLists deckList) {
try {
if (sessionState == SessionState.CONNECTED) {
server.cheat(gameId, sessionId, playerId, deckList);
return true;
}
} catch (RemoteException ex) {
handleRemoteException(ex);
} catch (MageException ex) {
@ -724,12 +792,11 @@ public class Session {
private void handleRemoteException(RemoteException ex) {
logger.fatal("Communication error", ex);
sessionState = SessionState.SERVER_UNAVAILABLE;
disconnect(false);
disconnect(true);
}
private void handleMageException(MageException ex) {
logger.fatal("Server error", ex);
disconnect(false);
}
private void handleGameException(GameException ex) {