mirror of
https://github.com/magefree/mage.git
synced 2025-12-25 13:02:06 -08:00
Backed out changeset: 83cb579a3761
This commit is contained in:
parent
473f6bcad9
commit
a4c3deb7ac
38 changed files with 63 additions and 2657 deletions
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
package mage.server;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import mage.server.util.PluginClassLoader;
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
|
|
@ -59,7 +60,6 @@ public class Main {
|
|||
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, 7, 2);
|
||||
|
||||
|
|
@ -88,18 +88,14 @@ public class Main {
|
|||
DeckValidatorFactory.getInstance().addDeckType(plugin.getName(), loadPlugin(plugin));
|
||||
}
|
||||
boolean testMode = false;
|
||||
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, "");
|
||||
}
|
||||
}
|
||||
Copier.setLoader(classLoader);
|
||||
setServerAddress(config.getServerAddress());
|
||||
server = new ServerImpl(config.getPort(), config.getServerName(), testMode, adminPassword);
|
||||
server = new ServerImpl(config.getPort(), config.getServerName(), testMode);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -62,7 +62,6 @@ import mage.view.DraftPickView;
|
|||
import mage.view.GameView;
|
||||
import mage.view.TableView;
|
||||
import mage.view.TournamentView;
|
||||
import mage.view.UserView;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
|
|
@ -75,16 +74,14 @@ public class ServerImpl extends RemoteServer implements Server {
|
|||
private static ExecutorService rmiExecutor = ThreadExecutor.getInstance().getRMIExecutor();
|
||||
|
||||
private boolean testMode;
|
||||
private String password;
|
||||
|
||||
public ServerImpl(int port, String name, boolean testMode, String password) {
|
||||
public ServerImpl(int port, String name, boolean testMode) {
|
||||
try {
|
||||
System.setSecurityManager(null);
|
||||
Registry reg = LocateRegistry.createRegistry(port);
|
||||
Server stub = (Server) UnicastRemoteObject.exportObject(this, port);
|
||||
reg.rebind(name, stub);
|
||||
this.testMode = testMode;
|
||||
this.password = password;
|
||||
logger.info("Started MAGE server - listening on port " + port);
|
||||
if (testMode)
|
||||
logger.info("MAGE server running in test mode");
|
||||
|
|
@ -109,16 +106,6 @@ public class ServerImpl extends RemoteServer implements Server {
|
|||
SessionManager.getInstance().getSession(sessionId).ack(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ping(UUID sessionId) {
|
||||
Session session = SessionManager.getInstance().getSession(sessionId);
|
||||
if (session != null) {
|
||||
session.ping();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID registerClient(String userName, UUID clientId, MageVersion version) throws MageException, RemoteException {
|
||||
|
||||
|
|
@ -126,7 +113,7 @@ public class ServerImpl extends RemoteServer implements Server {
|
|||
try {
|
||||
if (version.compareTo(Main.getVersion()) != 0)
|
||||
throw new MageException("Wrong client version " + version + ", expecting version " + Main.getVersion());
|
||||
sessionId = SessionManager.getInstance().createSession(userName, getClientHost(), clientId);
|
||||
sessionId = SessionManager.getInstance().createSession(userName, clientId);
|
||||
logger.info("User " + userName + " connected from " + getClientHost());
|
||||
} catch (Exception ex) {
|
||||
handleException(ex);
|
||||
|
|
@ -134,22 +121,6 @@ public class ServerImpl extends RemoteServer implements Server {
|
|||
return sessionId;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID registerAdmin(String password, MageVersion version) throws RemoteException, MageException {
|
||||
UUID sessionId = null;
|
||||
try {
|
||||
if (version.compareTo(Main.getVersion()) != 0)
|
||||
throw new MageException("Wrong client version " + version + ", expecting version " + Main.getVersion());
|
||||
if (!password.equals(this.password))
|
||||
throw new MageException("Wrong password");
|
||||
sessionId = SessionManager.getInstance().createSession(getClientHost());
|
||||
logger.info("Admin connected from " + getClientHost());
|
||||
} catch (Exception ex) {
|
||||
handleException(ex);
|
||||
}
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableView createTable(UUID sessionId, UUID roomId, MatchOptions options) throws MageException {
|
||||
|
|
@ -265,6 +236,7 @@ public class ServerImpl extends RemoteServer implements Server {
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public TableView getTable(UUID roomId, UUID tableId) throws MageException {
|
||||
try {
|
||||
|
|
@ -286,8 +258,8 @@ public class ServerImpl extends RemoteServer implements Server {
|
|||
Session session = SessionManager.getInstance().getSession(sessionId);
|
||||
if (session != null) {
|
||||
session.kill();
|
||||
logger.info("Client deregistered ...");
|
||||
}
|
||||
logger.info("Client deregistered ...");
|
||||
}
|
||||
}
|
||||
);
|
||||
|
|
@ -840,9 +812,4 @@ public class ServerImpl extends RemoteServer implements Server {
|
|||
return GameManager.getInstance().getGameView(gameId, sessionId, playerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserView> getUsers(UUID sessionId) throws RemoteException, MageException {
|
||||
return SessionManager.getInstance().getUsers(sessionId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@
|
|||
|
||||
package mage.server;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.logging.Level;
|
||||
import java.util.UUID;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.interfaces.callback.CallbackServerSession;
|
||||
|
|
@ -48,31 +48,14 @@ public class Session {
|
|||
private UUID sessionId;
|
||||
private UUID clientId;
|
||||
private String username;
|
||||
private String host;
|
||||
private int messageId = 0;
|
||||
private String ackMessage;
|
||||
private Date timeConnected;
|
||||
private long lastPing;
|
||||
private boolean isAdmin = false;
|
||||
private final CallbackServerSession callback = new CallbackServerSession();
|
||||
|
||||
public Session(String userName, String host, UUID clientId) {
|
||||
public Session(String userName, UUID clientId) {
|
||||
sessionId = UUID.randomUUID();
|
||||
this.username = userName;
|
||||
this.host = host;
|
||||
this.clientId = clientId;
|
||||
this.isAdmin = false;
|
||||
this.timeConnected = new Date();
|
||||
ping();
|
||||
}
|
||||
|
||||
public Session(String host) {
|
||||
sessionId = UUID.randomUUID();
|
||||
this.username = "Admin";
|
||||
this.host = host;
|
||||
this.isAdmin = true;
|
||||
this.timeConnected = new Date();
|
||||
ping();
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
|
|
@ -154,25 +137,4 @@ public class Session {
|
|||
return username;
|
||||
}
|
||||
|
||||
public void ping() {
|
||||
this.lastPing = System.currentTimeMillis();
|
||||
if (logger.isTraceEnabled())
|
||||
logger.trace("Ping received from" + username + ":" + sessionId);
|
||||
}
|
||||
|
||||
public boolean stillAlive() {
|
||||
return (System.currentTimeMillis() - lastPing) < 60000;
|
||||
}
|
||||
|
||||
public boolean isAdmin() {
|
||||
return isAdmin;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public Date getConnectionTime() {
|
||||
return timeConnected;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,17 +28,12 @@
|
|||
|
||||
package mage.server;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import mage.interfaces.MageException;
|
||||
import mage.view.UserView;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
|
|
@ -49,17 +44,11 @@ public class SessionManager {
|
|||
|
||||
private final static Logger logger = Logger.getLogger(SessionManager.class);
|
||||
private final static SessionManager INSTANCE = new SessionManager();
|
||||
private static ScheduledExecutorService sessionExecutor;
|
||||
|
||||
public static SessionManager getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
protected SessionManager() {
|
||||
sessionExecutor = Executors.newScheduledThreadPool(1);
|
||||
sessionExecutor.scheduleWithFixedDelay(new SessionChecker(), 30, 10, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private ConcurrentHashMap<UUID, Session> sessions = new ConcurrentHashMap<UUID, Session>();
|
||||
|
||||
public Session getSession(UUID sessionId) {
|
||||
|
|
@ -67,7 +56,7 @@ public class SessionManager {
|
|||
return sessions.get(sessionId);
|
||||
}
|
||||
|
||||
public UUID createSession(String userName, String host, UUID clientId) throws MageException {
|
||||
public UUID createSession(String userName, UUID clientId) throws MageException {
|
||||
for (Session session: sessions.values()) {
|
||||
if (session.getUsername().equals(userName)) {
|
||||
if (session.getClientId().equals(clientId)) {
|
||||
|
|
@ -79,32 +68,16 @@ public class SessionManager {
|
|||
}
|
||||
}
|
||||
}
|
||||
Session session = new Session(userName, host, clientId);
|
||||
Session session = new Session(userName, clientId);
|
||||
sessions.put(session.getId(), session);
|
||||
logger.info("Session " + session.getId() + " created for user " + userName);
|
||||
return session.getId();
|
||||
}
|
||||
|
||||
public UUID createSession(String host) throws MageException {
|
||||
Session session = new Session(host);
|
||||
sessions.put(session.getId(), session);
|
||||
logger.info("Admin session created");
|
||||
return session.getId();
|
||||
}
|
||||
|
||||
public void removeSession(UUID sessionId) {
|
||||
sessions.remove(sessionId);
|
||||
}
|
||||
|
||||
public void checkSessions() {
|
||||
for (Session session: sessions.values()) {
|
||||
if (!session.stillAlive()) {
|
||||
logger.info("Client for user " + session.getUsername() + ":" + session.getId() + " timed out - releasing resources");
|
||||
session.kill();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Map<UUID, Session> getSessions() {
|
||||
Map<UUID, Session> map = new HashMap<UUID, Session>();
|
||||
for (Map.Entry<UUID, Session> entry : sessions.entrySet()) {
|
||||
|
|
@ -112,25 +85,4 @@ public class SessionManager {
|
|||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
List<UserView> getUsers(UUID sessionId) {
|
||||
List<UserView> users = new ArrayList<UserView>();
|
||||
Session admin = sessions.get(sessionId);
|
||||
if (admin != null && admin.isAdmin()) {
|
||||
for (Session session: sessions.values()) {
|
||||
users.add(new UserView(session.getUsername(), session.getHost(), session.getId(), session.getConnectionTime()));
|
||||
}
|
||||
}
|
||||
return users;
|
||||
}
|
||||
|
||||
class SessionChecker implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
checkSessions();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue