forked from External/mage
[refactoring][minor] Replaced all tabs with four spaces.
This commit is contained in:
parent
e646e4768d
commit
239a4fb100
2891 changed files with 79411 additions and 79411 deletions
|
|
@ -66,51 +66,51 @@ import java.util.Map;
|
|||
*/
|
||||
public class Main {
|
||||
|
||||
private static Logger logger = Logger.getLogger(Main.class);
|
||||
private static Logger logger = Logger.getLogger(Main.class);
|
||||
|
||||
private final static String testModeArg = "-testMode=";
|
||||
private final static String adminPasswordArg = "-adminPassword=";
|
||||
private final static String pluginFolder = "plugins";
|
||||
private static MageVersion version = new MageVersion(0, 8, 6, "r5");
|
||||
private final static String testModeArg = "-testMode=";
|
||||
private final static String adminPasswordArg = "-adminPassword=";
|
||||
private final static String pluginFolder = "plugins";
|
||||
private static MageVersion version = new MageVersion(0, 8, 6, "r5");
|
||||
|
||||
public static PluginClassLoader classLoader = new PluginClassLoader();
|
||||
public static TransporterServer server;
|
||||
protected static boolean testMode;
|
||||
public static PluginClassLoader classLoader = new PluginClassLoader();
|
||||
public static TransporterServer server;
|
||||
protected static boolean testMode;
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
logger.info("Starting MAGE server version " + version);
|
||||
logger.info("Logging level: " + logger.getEffectiveLevel());
|
||||
deleteSavedGames();
|
||||
ConfigSettings config = ConfigSettings.getInstance();
|
||||
for (GamePlugin plugin: config.getGameTypes()) {
|
||||
GameFactory.getInstance().addGameType(plugin.getName(), loadGameType(plugin), loadPlugin(plugin));
|
||||
}
|
||||
for (GamePlugin plugin: config.getTournamentTypes()) {
|
||||
TournamentFactory.getInstance().addTournamentType(plugin.getName(), loadTournamentType(plugin), loadPlugin(plugin));
|
||||
}
|
||||
for (Plugin plugin: config.getPlayerTypes()) {
|
||||
PlayerFactory.getInstance().addPlayerType(plugin.getName(), loadPlugin(plugin));
|
||||
}
|
||||
for (Plugin plugin: config.getDeckTypes()) {
|
||||
DeckValidatorFactory.getInstance().addDeckType(plugin.getName(), loadPlugin(plugin));
|
||||
}
|
||||
String adminPassword = "";
|
||||
for (String arg: args) {
|
||||
if (arg.startsWith(testModeArg)) {
|
||||
testMode = Boolean.valueOf(arg.replace(testModeArg, ""));
|
||||
}
|
||||
else if (arg.startsWith(adminPasswordArg)) {
|
||||
adminPassword = arg.replace(adminPasswordArg, "");
|
||||
}
|
||||
}
|
||||
Connection connection = new Connection();
|
||||
connection.setHost(config.getServerAddress());
|
||||
connection.setPort(config.getPort());
|
||||
try {
|
||||
InvokerLocator serverLocator = new InvokerLocator(connection.getURI());
|
||||
logger.info("Starting MAGE server version " + version);
|
||||
logger.info("Logging level: " + logger.getEffectiveLevel());
|
||||
deleteSavedGames();
|
||||
ConfigSettings config = ConfigSettings.getInstance();
|
||||
for (GamePlugin plugin: config.getGameTypes()) {
|
||||
GameFactory.getInstance().addGameType(plugin.getName(), loadGameType(plugin), loadPlugin(plugin));
|
||||
}
|
||||
for (GamePlugin plugin: config.getTournamentTypes()) {
|
||||
TournamentFactory.getInstance().addTournamentType(plugin.getName(), loadTournamentType(plugin), loadPlugin(plugin));
|
||||
}
|
||||
for (Plugin plugin: config.getPlayerTypes()) {
|
||||
PlayerFactory.getInstance().addPlayerType(plugin.getName(), loadPlugin(plugin));
|
||||
}
|
||||
for (Plugin plugin: config.getDeckTypes()) {
|
||||
DeckValidatorFactory.getInstance().addDeckType(plugin.getName(), loadPlugin(plugin));
|
||||
}
|
||||
String adminPassword = "";
|
||||
for (String arg: args) {
|
||||
if (arg.startsWith(testModeArg)) {
|
||||
testMode = Boolean.valueOf(arg.replace(testModeArg, ""));
|
||||
}
|
||||
else if (arg.startsWith(adminPasswordArg)) {
|
||||
adminPassword = arg.replace(adminPasswordArg, "");
|
||||
}
|
||||
}
|
||||
Connection connection = new Connection();
|
||||
connection.setHost(config.getServerAddress());
|
||||
connection.setPort(config.getPort());
|
||||
try {
|
||||
InvokerLocator serverLocator = new InvokerLocator(connection.getURI());
|
||||
if (!isAlreadyRunning(serverLocator)) {
|
||||
server = new MageTransporterServer(serverLocator, new MageServerImpl(adminPassword, testMode), MageServer.class.getName(), new MageServerInvocationHandler());
|
||||
server.start();
|
||||
|
|
@ -122,11 +122,11 @@ public class Main {
|
|||
else {
|
||||
logger.fatal("Unable to start MAGE server - another server is already started");
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
logger.fatal("Failed to start server - " + connection.toString(), ex);
|
||||
} catch (Exception ex) {
|
||||
logger.fatal("Failed to start server - " + connection.toString(), ex);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
logger.fatal("Failed to start server - " + connection.toString(), ex);
|
||||
} catch (Exception ex) {
|
||||
logger.fatal("Failed to start server - " + connection.toString(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -136,8 +136,8 @@ public class Main {
|
|||
|
||||
static boolean isAlreadyRunning(InvokerLocator serverLocator) {
|
||||
Map<String, String> metadata = new HashMap<String, String>();
|
||||
metadata.put(SocketWrapper.WRITE_TIMEOUT, "2000");
|
||||
metadata.put("generalizeSocketException", "true");
|
||||
metadata.put(SocketWrapper.WRITE_TIMEOUT, "2000");
|
||||
metadata.put("generalizeSocketException", "true");
|
||||
try {
|
||||
MageServer testServer = (MageServer) TransporterClient.createTransporterClient(serverLocator.getLocatorURI(), MageServer.class, metadata);
|
||||
if (testServer != null) {
|
||||
|
|
@ -149,64 +149,64 @@ public class Main {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
User user = UserManager.getInstance().getUser(session.getUserId());
|
||||
if (user != null)
|
||||
sessionName = user.getName() + " at " + session.getHost();
|
||||
else
|
||||
sessionName = session.getHost();
|
||||
if (throwable instanceof ClientDisconnectedException) {
|
||||
SessionManager.getInstance().disconnect(client.getSessionId(), false);
|
||||
logger.info("client disconnected - " + sessionName);
|
||||
}
|
||||
else {
|
||||
SessionManager.getInstance().disconnect(client.getSessionId(), false);
|
||||
logger.info("connection to client lost - " + sessionName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class MageTransporterServer extends TransporterServer {
|
||||
|
||||
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 {
|
||||
return connector;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Connector getConnector(InvokerLocator locator, Map config, Element xmlConfig) throws Exception {
|
||||
Connector c = super.getConnector(locator, config, xmlConfig);
|
||||
this.connector = c;
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
static class MageServerInvocationHandler implements ServerInvocationHandler {
|
||||
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;
|
||||
User user = UserManager.getInstance().getUser(session.getUserId());
|
||||
if (user != null)
|
||||
sessionName = user.getName() + " at " + session.getHost();
|
||||
else
|
||||
sessionName = session.getHost();
|
||||
if (throwable instanceof ClientDisconnectedException) {
|
||||
SessionManager.getInstance().disconnect(client.getSessionId(), false);
|
||||
logger.info("client disconnected - " + sessionName);
|
||||
}
|
||||
else {
|
||||
SessionManager.getInstance().disconnect(client.getSessionId(), false);
|
||||
logger.info("connection to client lost - " + sessionName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMBeanServer(MBeanServer server) {}
|
||||
static class MageTransporterServer extends TransporterServer {
|
||||
|
||||
@Override
|
||||
public void setInvoker(ServerInvoker invoker) {}
|
||||
protected Connector connector;
|
||||
|
||||
@Override
|
||||
public Object invoke(final InvocationRequest invocation) throws Throwable {
|
||||
String sessionId = invocation.getSessionId();
|
||||
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 {
|
||||
return connector;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Connector getConnector(InvokerLocator locator, Map config, Element xmlConfig) throws Exception {
|
||||
Connector c = super.getConnector(locator, config, xmlConfig);
|
||||
this.connector = c;
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
static class MageServerInvocationHandler implements ServerInvocationHandler {
|
||||
|
||||
@Override
|
||||
public void setMBeanServer(MBeanServer server) {}
|
||||
|
||||
@Override
|
||||
public void setInvoker(ServerInvoker invoker) {}
|
||||
|
||||
@Override
|
||||
public Object invoke(final InvocationRequest invocation) throws Throwable {
|
||||
String sessionId = invocation.getSessionId();
|
||||
Map map = invocation.getRequestPayload();
|
||||
String host;
|
||||
if (map != null) {
|
||||
|
|
@ -216,92 +216,92 @@ public class Main {
|
|||
host = "localhost";
|
||||
}
|
||||
SessionManager.getInstance().getSession(sessionId).setHost(host);
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(InvokerCallbackHandler callbackHandler) {
|
||||
ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) callbackHandler;
|
||||
try {
|
||||
String sessionId = handler.getClientSessionId();
|
||||
SessionManager.getInstance().createSession(sessionId, callbackHandler);
|
||||
} catch (Throwable ex) {
|
||||
logger.fatal("", ex);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void addListener(InvokerCallbackHandler callbackHandler) {
|
||||
ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) callbackHandler;
|
||||
try {
|
||||
String sessionId = handler.getClientSessionId();
|
||||
SessionManager.getInstance().createSession(sessionId, callbackHandler);
|
||||
} catch (Throwable ex) {
|
||||
logger.fatal("", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(InvokerCallbackHandler callbackHandler) {
|
||||
logger.fatal("removeListener called");
|
||||
// ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) callbackHandler;
|
||||
// String sessionId = handler.getCallbackClient().getSessionId();
|
||||
// SessionManager.getInstance().disconnect(sessionId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static Class<?> loadPlugin(Plugin plugin) {
|
||||
try {
|
||||
classLoader.addURL(new File(pluginFolder + "/" + plugin.getJar()).toURI().toURL());
|
||||
logger.info("Loading plugin: " + plugin.getClassName());
|
||||
return Class.forName(plugin.getClassName(), true, classLoader);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
logger.warn("Plugin not Found:" + plugin.getJar() + " - check plugin folder");
|
||||
} catch (Exception ex) {
|
||||
logger.fatal("Error loading plugin " + plugin.getJar(), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public void removeListener(InvokerCallbackHandler callbackHandler) {
|
||||
logger.fatal("removeListener called");
|
||||
// ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) callbackHandler;
|
||||
// String sessionId = handler.getCallbackClient().getSessionId();
|
||||
// SessionManager.getInstance().disconnect(sessionId);
|
||||
}
|
||||
|
||||
private static MatchType loadGameType(GamePlugin plugin) {
|
||||
try {
|
||||
classLoader.addURL(new File(pluginFolder + "/" + plugin.getJar()).toURI().toURL());
|
||||
logger.info("Loading game type: " + plugin.getClassName());
|
||||
return (MatchType) Class.forName(plugin.getTypeName(), true, classLoader).newInstance();
|
||||
} catch (ClassNotFoundException ex) {
|
||||
logger.warn("Game type not found:" + plugin.getJar() + " - check plugin folder");
|
||||
} catch (Exception ex) {
|
||||
logger.fatal("Error loading game type " + plugin.getJar(), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static TournamentType loadTournamentType(GamePlugin plugin) {
|
||||
try {
|
||||
classLoader.addURL(new File(pluginFolder + "/" + plugin.getJar()).toURI().toURL());
|
||||
logger.info("Loading tournament type: " + plugin.getClassName());
|
||||
return (TournamentType) Class.forName(plugin.getTypeName(), true, classLoader).newInstance();
|
||||
} catch (ClassNotFoundException ex) {
|
||||
logger.warn("Tournament type not found:" + plugin.getJar() + " - check plugin folder");
|
||||
} catch (Exception ex) {
|
||||
logger.fatal("Error loading game type " + plugin.getJar(), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private static Class<?> loadPlugin(Plugin plugin) {
|
||||
try {
|
||||
classLoader.addURL(new File(pluginFolder + "/" + plugin.getJar()).toURI().toURL());
|
||||
logger.info("Loading plugin: " + plugin.getClassName());
|
||||
return Class.forName(plugin.getClassName(), true, classLoader);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
logger.warn("Plugin not Found:" + plugin.getJar() + " - check plugin folder");
|
||||
} catch (Exception ex) {
|
||||
logger.fatal("Error loading plugin " + plugin.getJar(), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void deleteSavedGames() {
|
||||
File directory = new File("saved/");
|
||||
if (!directory.exists())
|
||||
directory.mkdirs();
|
||||
File[] files = directory.listFiles(
|
||||
new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.endsWith(".game");
|
||||
}
|
||||
}
|
||||
);
|
||||
for (File file : files)
|
||||
{
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
private static MatchType loadGameType(GamePlugin plugin) {
|
||||
try {
|
||||
classLoader.addURL(new File(pluginFolder + "/" + plugin.getJar()).toURI().toURL());
|
||||
logger.info("Loading game type: " + plugin.getClassName());
|
||||
return (MatchType) Class.forName(plugin.getTypeName(), true, classLoader).newInstance();
|
||||
} catch (ClassNotFoundException ex) {
|
||||
logger.warn("Game type not found:" + plugin.getJar() + " - check plugin folder");
|
||||
} catch (Exception ex) {
|
||||
logger.fatal("Error loading game type " + plugin.getJar(), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static MageVersion getVersion() {
|
||||
return version;
|
||||
}
|
||||
private static TournamentType loadTournamentType(GamePlugin plugin) {
|
||||
try {
|
||||
classLoader.addURL(new File(pluginFolder + "/" + plugin.getJar()).toURI().toURL());
|
||||
logger.info("Loading tournament type: " + plugin.getClassName());
|
||||
return (TournamentType) Class.forName(plugin.getTypeName(), true, classLoader).newInstance();
|
||||
} catch (ClassNotFoundException ex) {
|
||||
logger.warn("Tournament type not found:" + plugin.getJar() + " - check plugin folder");
|
||||
} catch (Exception ex) {
|
||||
logger.fatal("Error loading game type " + plugin.getJar(), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isTestMode() {
|
||||
return testMode;
|
||||
}
|
||||
private static void deleteSavedGames() {
|
||||
File directory = new File("saved/");
|
||||
if (!directory.exists())
|
||||
directory.mkdirs();
|
||||
File[] files = directory.listFiles(
|
||||
new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.endsWith(".game");
|
||||
}
|
||||
}
|
||||
);
|
||||
for (File file : files)
|
||||
{
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
public static MageVersion getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public static boolean isTestMode() {
|
||||
return testMode;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue