added connection leasing + cleanup

This commit is contained in:
BetaSteward 2011-06-25 14:34:39 -04:00
parent b1408507cb
commit ade836c08a
12 changed files with 115 additions and 84 deletions

View file

@ -66,7 +66,7 @@ import org.apache.log4j.Logger;
public class MageServerImpl implements MageServer {
private final static Logger logger = Logger.getLogger("Mage Server");
private static ExecutorService rmiExecutor = ThreadExecutor.getInstance().getRMIExecutor();
private static ExecutorService callExecutor = ThreadExecutor.getInstance().getCallExecutor();
private String password;
private boolean testMode;
@ -137,7 +137,7 @@ public class MageServerImpl implements MageServer {
public void removeTable(final String sessionId, final UUID roomId, final UUID tableId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -243,15 +243,12 @@ public class MageServerImpl implements MageServer {
@Override
public void deregisterClient(final String sessionId) throws MageException {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.kill();
logger.info("Client deregistered ...");
}
SessionManager.getInstance().disconnect(sessionId);
logger.info("Client deregistered ...");
}
}
);
@ -265,7 +262,7 @@ public class MageServerImpl implements MageServer {
public void startMatch(final String sessionId, final UUID roomId, final UUID tableId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -284,7 +281,7 @@ public class MageServerImpl implements MageServer {
public void startChallenge(final String sessionId, final UUID roomId, final UUID tableId, final UUID challengeId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -303,7 +300,7 @@ public class MageServerImpl implements MageServer {
public void startTournament(final String sessionId, final UUID roomId, final UUID tableId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -332,7 +329,7 @@ public class MageServerImpl implements MageServer {
@Override
public void sendChatMessage(final UUID chatId, final String userName, final String message) throws MageException {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -349,7 +346,7 @@ public class MageServerImpl implements MageServer {
@Override
public void joinChat(final UUID chatId, final String sessionId, final String userName) throws MageException {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -366,7 +363,7 @@ public class MageServerImpl implements MageServer {
@Override
public void leaveChat(final UUID chatId, final String sessionId) throws MageException {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -417,7 +414,7 @@ public class MageServerImpl implements MageServer {
public void swapSeats(final String sessionId, final UUID roomId, final UUID tableId, final int seatNum1, final int seatNum2) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -436,7 +433,7 @@ public class MageServerImpl implements MageServer {
public void leaveTable(final String sessionId, final UUID roomId, final UUID tableId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -466,7 +463,7 @@ public class MageServerImpl implements MageServer {
public void joinGame(final UUID gameId, final String sessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -485,7 +482,7 @@ public class MageServerImpl implements MageServer {
public void joinDraft(final UUID draftId, final String sessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -504,7 +501,7 @@ public class MageServerImpl implements MageServer {
public void joinTournament(final UUID tournamentId, final String sessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -545,7 +542,7 @@ public class MageServerImpl implements MageServer {
public void sendPlayerUUID(final UUID gameId, final String sessionId, final UUID data) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -564,7 +561,7 @@ public class MageServerImpl implements MageServer {
public void sendPlayerString(final UUID gameId, final String sessionId, final String data) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -583,7 +580,7 @@ public class MageServerImpl implements MageServer {
public void sendPlayerBoolean(final UUID gameId, final String sessionId, final Boolean data) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -602,7 +599,7 @@ public class MageServerImpl implements MageServer {
public void sendPlayerInteger(final UUID gameId, final String sessionId, final Integer data) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -634,7 +631,7 @@ public class MageServerImpl implements MageServer {
public void concedeGame(final UUID gameId, final String sessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -666,7 +663,7 @@ public class MageServerImpl implements MageServer {
public void watchGame(final UUID gameId, final String sessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -685,7 +682,7 @@ public class MageServerImpl implements MageServer {
public void stopWatching(final UUID gameId, final String sessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -704,7 +701,7 @@ public class MageServerImpl implements MageServer {
public void replayGame(final UUID gameId, final String sessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -723,7 +720,7 @@ public class MageServerImpl implements MageServer {
public void startReplay(final UUID gameId, final String sessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -742,7 +739,7 @@ public class MageServerImpl implements MageServer {
public void stopReplay(final UUID gameId, final String sessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -761,7 +758,7 @@ public class MageServerImpl implements MageServer {
public void nextPlay(final UUID gameId, final String sessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -780,7 +777,7 @@ public class MageServerImpl implements MageServer {
public void previousPlay(final UUID gameId, final String sessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -816,7 +813,7 @@ public class MageServerImpl implements MageServer {
public void cheat(final UUID gameId, final String sessionId, final UUID playerId, final DeckCardLists deckList) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -867,7 +864,7 @@ public class MageServerImpl implements MageServer {
public void disconnectUser(final String sessionId, final String userSessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {
@ -886,7 +883,7 @@ public class MageServerImpl implements MageServer {
public void removeTable(final String sessionId, final UUID tableId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
rmiExecutor.execute(
callExecutor.execute(
new Runnable() {
@Override
public void run() {

View file

@ -31,6 +31,7 @@ package mage.server;
import mage.server.util.PluginClassLoader;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Map;
import javax.management.MBeanServer;
@ -47,6 +48,9 @@ import mage.server.util.config.Plugin;
import mage.server.util.config.GamePlugin;
import mage.utils.MageVersion;
import org.apache.log4j.Logger;
import org.jboss.remoting.Client;
import org.jboss.remoting.ClientDisconnectedException;
import org.jboss.remoting.ConnectionListener;
import org.jboss.remoting.InvocationRequest;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.ServerInvocationHandler;
@ -113,20 +117,40 @@ public class Main {
logger.info("Started MAGE server - listening on " + connection.toString());
if (testMode)
logger.info("MAGE server running in test mode");
} catch (IOException ex) {
logger.fatal("Failed to start server - " + connection.toString(), ex);
} catch (Exception ex) {
logger.fatal("Failed to start server - " + connection.toString(), ex);
}
}
static class ClientConnectionListener implements ConnectionListener {
@Override
public void handleConnectionException(Throwable throwable, Client client) {
Session session = SessionManager.getInstance().getSession(client.getSessionId());
if (session != null) {
String sessionName = session.getUsername() + " at " + session.getHost();
if (throwable instanceof ClientDisconnectedException) {
logger.info("client disconnected - " + sessionName);
}
else {
logger.info("connection to client lost - " + sessionName);
}
SessionManager.getInstance().disconnect(client.getSessionId());
}
}
}
static class MageTransporterServer extends TransporterServer {
Connector connector;
protected Connector connector;
public MageTransporterServer(InvokerLocator locator, Object target, String subsystem, MageServerInvocationHandler callback) throws Exception {
super(locator, target, subsystem);
connector.addInvocationHandler("callback", callback);
connector.setLeasePeriod(5000);
connector.addConnectionListener(new ClientConnectionListener());
}
public Connector getConnector() throws Exception {
@ -170,7 +194,7 @@ public class Main {
public void removeListener(InvokerCallbackHandler callbackHandler) {
ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) callbackHandler;
String sessionId = handler.getCallbackClient().getSessionId();
SessionManager.getInstance().removeSession(sessionId);
SessionManager.getInstance().disconnect(sessionId);
}
}

View file

@ -76,9 +76,8 @@ public class Session {
public String getId() {
return sessionId;
}
public void kill() {
SessionManager.getInstance().removeSession(sessionId);
TableManager.getInstance().removeSession(sessionId);
GameManager.getInstance().removeSession(sessionId);
ChatManager.getInstance().removeSession(sessionId);

View file

@ -82,8 +82,12 @@ public class SessionManager {
return false;
}
public void removeSession(String sessionId) {
sessions.remove(sessionId);
public synchronized void disconnect(String sessionId) {
Session session = sessions.get(sessionId);
if (session != null) {
session.kill();
sessions.remove(sessionId);
}
}
public Map<String, Session> getSessions() {

View file

@ -237,6 +237,11 @@ public class TableController {
return player;
}
public void kill(String sessionId) {
leaveTable(sessionId);
sessionPlayerMap.remove(sessionId);
}
public synchronized void leaveTable(String sessionId) {
if (table.getState() == TableState.WAITING || table.getState() == TableState.STARTING)
table.leaveTable(sessionPlayerMap.get(sessionId));

View file

@ -114,7 +114,9 @@ public class TableManager {
}
public void removeSession(String sessionId) {
// TODO: search through tables and remove session
for (TableController controller: controllers.values()) {
controller.kill(sessionId);
}
}
public boolean isTableOwner(UUID tableId, String sessionId) {

View file

@ -142,7 +142,7 @@ public class DraftController {
private void checkStart() {
if (allJoined()) {
ThreadExecutor.getInstance().getRMIExecutor().execute(
ThreadExecutor.getInstance().getCallExecutor().execute(
new Runnable() {
@Override
public void run() {

View file

@ -209,7 +209,7 @@ public class GameController implements GameCallback {
private void checkStart() {
if (allJoined()) {
ThreadExecutor.getInstance().getRMIExecutor().execute(
ThreadExecutor.getInstance().getCallExecutor().execute(
new Runnable() {
@Override
public void run() {

View file

@ -141,7 +141,7 @@ public class TournamentController {
private void checkStart() {
if (allJoined()) {
ThreadExecutor.getInstance().getRMIExecutor().execute(
ThreadExecutor.getInstance().getCallExecutor().execute(
new Runnable() {
@Override
public void run() {

View file

@ -36,13 +36,13 @@ import java.util.concurrent.*;
*/
public class ThreadExecutor {
private static ExecutorService rmiExecutor = Executors.newCachedThreadPool();
private static ExecutorService gameExecutor = Executors.newFixedThreadPool(ConfigSettings.getInstance().getMaxGameThreads());
private static ScheduledExecutorService timeoutExecutor = Executors.newScheduledThreadPool(5);
private static final ExecutorService callExecutor = Executors.newCachedThreadPool();
private static final ExecutorService gameExecutor = Executors.newFixedThreadPool(ConfigSettings.getInstance().getMaxGameThreads());
private static final ScheduledExecutorService timeoutExecutor = Executors.newScheduledThreadPool(5);
static {
((ThreadPoolExecutor)rmiExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor)rmiExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor)callExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor)callExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor)gameExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor)gameExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor)timeoutExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
@ -57,8 +57,8 @@ public class ThreadExecutor {
private ThreadExecutor() {}
public ExecutorService getRMIExecutor() {
return rmiExecutor;
public ExecutorService getCallExecutor() {
return callExecutor;
}
public ExecutorService getGameExecutor() {