forked from External/mage
retrying without jars
This commit is contained in:
parent
a4c3deb7ac
commit
0e66c5e030
23 changed files with 2646 additions and 57 deletions
|
|
@ -37,8 +37,10 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.swing.JOptionPane;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.client.MageFrame;
|
||||
|
|
@ -61,6 +63,7 @@ import mage.view.GameTypeView;
|
|||
import mage.view.TableView;
|
||||
import mage.view.TournamentTypeView;
|
||||
import mage.view.TournamentView;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -68,7 +71,8 @@ import mage.view.TournamentView;
|
|||
*/
|
||||
public class Session {
|
||||
|
||||
private final static Logger logger = Logging.getLogger(Session.class.getName());
|
||||
private final static Logger logger = Logger.getLogger(Session.class);
|
||||
private static ScheduledExecutorService sessionExecutor = Executors.newScheduledThreadPool(1);
|
||||
|
||||
private UUID sessionId;
|
||||
private Server server;
|
||||
|
|
@ -81,6 +85,7 @@ public class Session {
|
|||
private Map<UUID, DraftPanel> drafts = new HashMap<UUID, DraftPanel>();
|
||||
private Map<UUID, TournamentPanel> tournaments = new HashMap<UUID, TournamentPanel>();
|
||||
private CallbackClientDaemon callbackDaemon;
|
||||
private ScheduledFuture<?> future;
|
||||
private MageUI ui = new MageUI();
|
||||
|
||||
public Session(MageFrame frame) {
|
||||
|
|
@ -112,20 +117,21 @@ public class Session {
|
|||
sessionId = server.registerClient(userName, client.getId(), frame.getVersion());
|
||||
callbackDaemon = new CallbackClientDaemon(sessionId, client, server);
|
||||
serverState = server.getServerState();
|
||||
future = sessionExecutor.scheduleWithFixedDelay(new ServerPinger(), 5, 5, TimeUnit.SECONDS);
|
||||
logger.info("Connected to RMI server at " + serverName + ":" + port);
|
||||
frame.setStatusText("Connected to " + serverName + ":" + port + " ");
|
||||
frame.enableButtons();
|
||||
return true;
|
||||
} catch (MageException ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
logger.fatal("", ex);
|
||||
disconnect();
|
||||
JOptionPane.showMessageDialog(frame, "Unable to connect to server. " + ex.getMessage());
|
||||
} catch (RemoteException ex) {
|
||||
logger.log(Level.SEVERE, "Unable to connect to server - ", ex);
|
||||
logger.fatal("Unable to connect to server - ", ex);
|
||||
disconnect();
|
||||
JOptionPane.showMessageDialog(frame, "Unable to connect to server. " + ex.getMessage());
|
||||
} catch (NotBoundException ex) {
|
||||
logger.log(Level.SEVERE, "Unable to connect to server - ", ex);
|
||||
logger.fatal("Unable to connect to server - ", ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
@ -134,7 +140,6 @@ public class Session {
|
|||
|
||||
if (isConnected()) {
|
||||
try {
|
||||
frame.hideTables();
|
||||
for (UUID chatId: chats.keySet()) {
|
||||
server.leaveChat(chatId, sessionId);
|
||||
}
|
||||
|
|
@ -145,18 +150,26 @@ public class Session {
|
|||
try {
|
||||
//TODO: stop daemon
|
||||
server.deregisterClient(sessionId);
|
||||
server = null;
|
||||
logger.info("Disconnected ... ");
|
||||
} catch (RemoteException ex) {
|
||||
logger.log(Level.SEVERE, "Error disconnecting ...", ex);
|
||||
logger.fatal("Error disconnecting ...", ex);
|
||||
} catch (MageException ex) {
|
||||
logger.log(Level.SEVERE, "Error disconnecting ...", ex);
|
||||
logger.fatal("Error disconnecting ...", ex);
|
||||
}
|
||||
frame.setStatusText("Not connected ");
|
||||
frame.disableButtons();
|
||||
removeServer();
|
||||
}
|
||||
}
|
||||
|
||||
private void removeServer() {
|
||||
if (future != null && !future.isDone())
|
||||
future.cancel(true);
|
||||
server = null;
|
||||
frame.hideTables();
|
||||
frame.setStatusText("Not connected");
|
||||
frame.disableButtons();
|
||||
logger.info("Disconnected ... ");
|
||||
JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Disconnected.", "Disconnected", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
|
||||
public void ack(String message) {
|
||||
try {
|
||||
server.ack(message, sessionId);
|
||||
|
|
@ -167,6 +180,17 @@ public class Session {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean ping() {
|
||||
try {
|
||||
return server.ping(sessionId);
|
||||
} catch (RemoteException ex) {
|
||||
handleRemoteException(ex);
|
||||
} catch (MageException ex) {
|
||||
handleMageException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return server != null;
|
||||
}
|
||||
|
|
@ -717,26 +741,19 @@ public class Session {
|
|||
}
|
||||
|
||||
private void handleRemoteException(RemoteException ex) {
|
||||
logger.log(Level.SEVERE, "Communication error", ex);
|
||||
if (ex instanceof java.rmi.ConnectException) {
|
||||
server = null;
|
||||
frame.setStatusText("Not connected");
|
||||
frame.disableButtons();
|
||||
JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Communication error - disconnecting.", "Error", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
else
|
||||
JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Communication error.", "Error", JOptionPane.ERROR_MESSAGE);
|
||||
logger.fatal("Communication error", ex);
|
||||
removeServer();
|
||||
}
|
||||
|
||||
private void handleMageException(MageException ex) {
|
||||
logger.log(Level.SEVERE, "Server error", ex);
|
||||
logger.fatal("Server error", ex);
|
||||
JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Critical server error. Disconnecting", "Error", JOptionPane.ERROR_MESSAGE);
|
||||
disconnect();
|
||||
frame.disableButtons();
|
||||
}
|
||||
|
||||
private void handleGameException(GameException ex) {
|
||||
logger.log(Level.WARNING, "Game error", ex.getMessage());
|
||||
logger.warn(ex.getMessage());
|
||||
JOptionPane.showMessageDialog(MageFrame.getDesktop(), ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
|
||||
|
|
@ -752,5 +769,24 @@ public class Session {
|
|||
public Server getServerRef() {
|
||||
return server;
|
||||
}
|
||||
|
||||
class ServerPinger implements Runnable {
|
||||
|
||||
private int missed = 0;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!ping()) {
|
||||
missed++;
|
||||
if (missed > 10) {
|
||||
logger.info("Connection to server timed out");
|
||||
removeServer();
|
||||
}
|
||||
}
|
||||
else {
|
||||
missed = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue