fixed client/server ping + added server console

This commit is contained in:
BetaSteward 2011-05-14 23:28:07 -04:00
parent 0b1527a206
commit 473f6bcad9
38 changed files with 2657 additions and 63 deletions

View file

@ -72,13 +72,11 @@
<EmptySpace min="-2" max="-2" attributes="0"/>
<Component id="jLabel5" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" max="-2" attributes="0"/>
<Component id="pnlPacks" pref="57" max="32767" attributes="0"/>
<Component id="pnlPacks" pref="59" max="32767" attributes="0"/>
<EmptySpace min="-2" pref="11" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="103" groupAlignment="3" attributes="0">
<Component id="spnNumPlayers" alignment="0" max="32767" attributes="1"/>
<Component id="jLabel2" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<Component id="spnNumPlayers" alignment="0" pref="22" max="32767" attributes="1"/>
<Component id="jLabel2" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="pnlDraftOptions" max="32767" attributes="1"/>
</Group>
<EmptySpace max="-2" attributes="0"/>

View file

@ -261,12 +261,11 @@ public class NewTournamentDialog extends MageDialog {
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pnlPacks, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
.addComponent(pnlPacks, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(spnNumPlayers)
.addComponent(jLabel2))
.addComponent(spnNumPlayers, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
.addComponent(jLabel2)
.addComponent(pnlDraftOptions, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

View file

@ -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;
}
}
}
}

View file

@ -190,10 +190,11 @@ public class TablesPanel extends javax.swing.JPanel {
}
public void hideTables() {
if (tableWaitingDialog.isVisible()) {
if (tableWaitingDialog != null && tableWaitingDialog.isVisible()) {
tableWaitingDialog.closeDialog();
}
updateTask.cancel(true);
if (updateTask != null)
updateTask.cancel(true);
this.chatPanel.disconnect();
Component c = this.getParent();

View file

@ -21,7 +21,7 @@
<EmptySpace max="-2" attributes="0"/>
<Component id="jLabel1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="cbPlayerType" min="-2" pref="138" max="-2" attributes="0"/>
<Component id="cbPlayerType" min="-2" pref="150" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="pnlPlayerName" max="32767" attributes="0"/>
</Group>
@ -78,11 +78,11 @@
<Group type="102" alignment="0" attributes="0">
<Component id="jLabel3" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="cbLevel" min="-2" pref="31" max="-2" attributes="0"/>
<Component id="cbLevel" min="-2" pref="45" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="jLabel2" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="txtPlayerName" pref="225" max="32767" attributes="0"/>
<Component id="txtPlayerName" pref="199" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>

View file

@ -116,11 +116,11 @@ public class TournamentPlayerPanel extends javax.swing.JPanel {
.addGroup(pnlPlayerNameLayout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cbLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cbLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtPlayerName, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE))
.addComponent(txtPlayerName, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE))
);
pnlPlayerNameLayout.setVerticalGroup(
pnlPlayerNameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
@ -140,7 +140,7 @@ public class TournamentPlayerPanel extends javax.swing.JPanel {
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cbPlayerType, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cbPlayerType, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pnlPlayerName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);