mirror of
https://github.com/correl/mage.git
synced 2024-11-14 19:19:32 +00:00
fixed issue 25 - server waits for all clients to confirm initialization before starting game
This commit is contained in:
parent
f81c238994
commit
3ca748e16d
9 changed files with 56 additions and 45 deletions
|
@ -508,8 +508,8 @@ public class MageFrame extends javax.swing.JFrame {
|
|||
if (g != null) {
|
||||
renderSplashFrame(g);
|
||||
}
|
||||
splash.update();
|
||||
}
|
||||
splash.update();
|
||||
|
||||
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
|
|
|
@ -99,6 +99,7 @@ public class Client implements CallbackClient {
|
|||
}
|
||||
else if (callback.getMethod().equals("gameInit")) {
|
||||
session.getGame().init((GameView) callback.getData());
|
||||
session.ack("gameInit");
|
||||
}
|
||||
else if (callback.getMethod().equals("gameOver")) {
|
||||
session.getGame().modalMessage((String) callback.getData());
|
||||
|
|
|
@ -28,7 +28,6 @@
|
|||
|
||||
package mage.client.remote;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.rmi.NotBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
|
@ -46,7 +45,6 @@ import mage.Constants.RangeOfInfluence;
|
|||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.client.MageFrame;
|
||||
import mage.client.chat.ChatPanel;
|
||||
import mage.client.components.MageComponents;
|
||||
import mage.client.components.MageUI;
|
||||
import mage.client.game.GamePanel;
|
||||
import mage.client.util.Config;
|
||||
|
@ -130,6 +128,16 @@ public class Session {
|
|||
}
|
||||
}
|
||||
|
||||
public void ack(String message) {
|
||||
try {
|
||||
server.ack(message, sessionId);
|
||||
} catch (RemoteException ex) {
|
||||
handleRemoteException(ex);
|
||||
} catch (MageException ex) {
|
||||
handleMageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return server != null;
|
||||
}
|
||||
|
|
|
@ -48,6 +48,7 @@ public interface Server extends Remote, CallbackServer {
|
|||
|
||||
public UUID registerClient(String userName, UUID clientId) throws RemoteException, MageException;
|
||||
public void deregisterClient(UUID sessionId) throws RemoteException, MageException;
|
||||
public void ack(String message, UUID sessionId) throws RemoteException, MageException;
|
||||
|
||||
public ServerState getServerState() throws RemoteException, MageException;
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# Default global logging level.
|
||||
.level=FINE
|
||||
.level=FINER
|
||||
|
||||
# Set the default logging level for new ConsoleHandler instances
|
||||
java.util.logging.ConsoleHandler.level = ALL
|
|
@ -54,7 +54,6 @@ import mage.server.game.ReplayManager;
|
|||
import mage.server.game.TableManager;
|
||||
import mage.util.Logging;
|
||||
import mage.view.ChatMessage.MessageColor;
|
||||
import mage.view.GameTypeView;
|
||||
import mage.view.TableView;
|
||||
|
||||
/**
|
||||
|
@ -91,6 +90,11 @@ public class ServerImpl extends RemoteServer implements Server {
|
|||
return SessionManager.getInstance().getSession(sessionId).callback();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ack(String message, UUID sessionId) throws RemoteException, MageException {
|
||||
SessionManager.getInstance().getSession(sessionId).ack(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID registerClient(String userName, UUID clientId) throws MageException, RemoteException {
|
||||
|
||||
|
@ -447,39 +451,6 @@ public class ServerImpl extends RemoteServer implements Server {
|
|||
return null;
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public List<GameTypeView> getGameTypes() throws MageException {
|
||||
// try {
|
||||
// return GameFactory.getInstance().getGameTypes();
|
||||
// }
|
||||
// catch (Exception ex) {
|
||||
// handleException(ex);
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String[] getPlayerTypes() throws MageException {
|
||||
// try {
|
||||
// return PlayerFactory.getInstance().getPlayerTypes().toArray(new String[0]);
|
||||
// }
|
||||
// catch (Exception ex) {
|
||||
// handleException(ex);
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String[] getDeckTypes() throws MageException {
|
||||
// try {
|
||||
// return DeckValidatorFactory.getInstance().getDeckTypes().toArray(new String[0]);
|
||||
// }
|
||||
// catch (Exception ex) {
|
||||
// handleException(ex);
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void cheat(UUID gameId, UUID sessionId, UUID playerId, DeckCardLists deckList) throws MageException {
|
||||
try {
|
||||
|
|
|
@ -49,6 +49,7 @@ public class Session {
|
|||
private UUID clientId;
|
||||
private String username;
|
||||
private int messageId = 0;
|
||||
private String ackMessage;
|
||||
private final CallbackServerSession callback = new CallbackServerSession();
|
||||
|
||||
public Session(String userName, UUID clientId) {
|
||||
|
@ -77,11 +78,11 @@ public class Session {
|
|||
return null;
|
||||
}
|
||||
|
||||
public synchronized void fireCallback(ClientCallback call) {
|
||||
public synchronized void fireCallback(final ClientCallback call) {
|
||||
call.setMessageId(messageId++);
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine(sessionId + " - " + call.getMessageId() + " - " + call.getMethod());
|
||||
try {
|
||||
call.setMessageId(messageId++);
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine(sessionId + " - " + call.getMessageId() + " - " + call.getMethod());
|
||||
callback.setCallback(call);
|
||||
} catch (InterruptedException ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
|
@ -100,6 +101,18 @@ public class Session {
|
|||
fireCallback(new ClientCallback("replayGame", null));
|
||||
}
|
||||
|
||||
public void ack(String message) {
|
||||
this.ackMessage = message;
|
||||
}
|
||||
|
||||
public String getAckMessage() {
|
||||
return ackMessage;
|
||||
}
|
||||
|
||||
public void clearAck() {
|
||||
this.ackMessage = "";
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
|
|
@ -165,10 +165,15 @@ public class GameController implements GameCallback {
|
|||
GameSession gameSession = new GameSession(game, sessionId, playerId);
|
||||
gameSessions.put(playerId, gameSession);
|
||||
logger.info("player " + playerId + " has joined game " + game.getId());
|
||||
// gameSession.init(getGameView(playerId));
|
||||
ChatManager.getInstance().broadcast(chatId, "", game.getPlayer(playerId).getName() + " has joined the game", MessageColor.BLACK);
|
||||
if (allJoined()) {
|
||||
startGame();
|
||||
ThreadExecutor.getInstance().getRMIExecutor().execute(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
startGame();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -177,6 +182,8 @@ public class GameController implements GameCallback {
|
|||
for (final Entry<UUID, GameSession> entry: gameSessions.entrySet()) {
|
||||
if (!entry.getValue().init(getGameView(entry.getKey()))) {
|
||||
logger.severe("Unable to initialize client");
|
||||
//TODO: generate client error message
|
||||
return;
|
||||
}
|
||||
}
|
||||
GameWorker worker = new GameWorker(game, this);
|
||||
|
|
|
@ -60,13 +60,23 @@ public class GameWatcher {
|
|||
if (!killed) {
|
||||
Session session = SessionManager.getInstance().getSession(sessionId);
|
||||
if (session != null) {
|
||||
session.clearAck();
|
||||
session.fireCallback(new ClientCallback("gameInit", gameView));
|
||||
return true;
|
||||
if (waitForAck("gameInit"))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean waitForAck(String message) {
|
||||
Session session = SessionManager.getInstance().getSession(sessionId);
|
||||
do {
|
||||
//TODO: add timeout
|
||||
} while (!session.getAckMessage().equals(message) && !killed);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void update(final GameView gameView) {
|
||||
if (!killed) {
|
||||
Session session = SessionManager.getInstance().getSession(sessionId);
|
||||
|
|
Loading…
Reference in a new issue