Some more changes to Server Console and Admin server handling.

This commit is contained in:
LevelX2 2014-08-18 21:48:59 +02:00
parent e697cae98d
commit c03dc6c2b5
13 changed files with 113 additions and 60 deletions

1
.gitignore vendored
View file

@ -12,6 +12,7 @@ Mage.Plugins/Mage.Counter.Plugin/target
Mage.Plugins/Mage.Theme.Plugin/target
Mage.Plugins/Mage.Rating.Plugin/target
Mage.Server.Console/target/
Mage.Server.Console/mageadmin.log
Mage.Server.Plugins/Mage.Deck.Constructed/target
Mage.Server.Plugins/Mage.Deck.Limited/target
Mage.Server.Plugins/Mage.Game.CommanderDuel/target

View file

@ -28,7 +28,11 @@
package mage.remote;
import java.net.*;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
/**

View file

@ -204,8 +204,10 @@ public class SessionImpl implements Session {
if (registerResult) {
sessionState = SessionState.CONNECTED;
serverState = server.getServerState();
updateDatabase();
logger.info("Connected as " + this.getUserName() == null ? "":this.getUserName() + " to MAGE server at " + connection.getHost() + ":" + connection.getPort());
if (!connection.getUsername().equals("Admin")) {
updateDatabase();
}
logger.info("Connected as " + (this.getUserName() == null ? "":this.getUserName()) + " to MAGE server at " + connection.getHost() + ":" + connection.getPort());
client.connected(this.getUserName() == null ? "":this.getUserName() +"@" + connection.getHost() + ":" + connection.getPort() +" ");
return true;
}

View file

@ -36,10 +36,10 @@ import java.util.Date;
*/
public class UserView implements Serializable {
private String userName;
private String host;
private String sessionId;
private Date timeConnected;
private final String userName;
private final String host;
private final String sessionId;
private final Date timeConnected;
public UserView(String userName, String host, String sessionId, Date timeConnected) {
this.userName = userName;

View file

@ -34,12 +34,7 @@
package mage.server.console;
import mage.remote.Connection;
import mage.remote.Connection.ProxyType;
import org.apache.log4j.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.Cursor;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
@ -48,6 +43,13 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import mage.remote.Connection;
import mage.remote.Connection.ProxyType;
import org.apache.log4j.Logger;
/**
*
@ -380,10 +382,11 @@ public class ConnectDialog extends JDialog {
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
ConsoleFrame.getPreferences().put("autoConnect", Boolean.toString(chkAutoConnect.isSelected()));
if (task != null && !task.isDone())
if (task != null && !task.isDone()) {
task.cancel(true);
else
} else {
this.setVisible(false);
}
}//GEN-LAST:event_btnCancelActionPerformed
private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectActionPerformed
@ -467,8 +470,9 @@ public class ConnectDialog extends JDialog {
private void keyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_keyTyped
char c = evt.getKeyChar();
if (!Character.isDigit(c))
if (!Character.isDigit(c)) {
evt.consume();
}
}//GEN-LAST:event_keyTyped
private void chkAutoConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkAutoConnectActionPerformed
@ -486,14 +490,14 @@ public class ConnectDialog extends JDialog {
URL serverListURL = new URL("http://download.magefree.com/files/server-list.txt");
in = new BufferedReader(new InputStreamReader(serverListURL.openStream()));
List<String> servers = new ArrayList<String>();
List<String> servers = new ArrayList<>();
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Found server: "+inputLine);
servers.add(inputLine);
}
if (servers.size() == 0) {
if (servers.isEmpty()) {
JOptionPane.showMessageDialog(null, "Couldn't find any server.");
return;
}
@ -516,7 +520,9 @@ public class ConnectDialog extends JDialog {
} catch(Exception ex) {
logger.error(ex,ex);
} finally {
if (in != null) try { in.close(); } catch (Exception e) {}
if (in != null) {
try { in.close(); } catch (Exception e) {}
}
}
}//GEN-LAST:event_jButton1ActionPerformed

View file

@ -2,7 +2,7 @@
<Form version="1.5" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
<Property name="defaultCloseOperation" type="int" value="0"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>

View file

@ -34,6 +34,8 @@
package mage.server.console;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ -80,6 +82,14 @@ public class ConsoleFrame extends javax.swing.JFrame implements MageClient {
/** Creates new form ConsoleFrame */
public ConsoleFrame() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exitApp();
}
});
initComponents();
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
@ -136,7 +146,7 @@ public class ConsoleFrame extends javax.swing.JFrame implements MageClient {
lblStatus = new javax.swing.JLabel();
consolePanel1 = new mage.server.console.ConsolePanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
jToolBar1.setFloatable(false);
jToolBar1.setRollover(true);
@ -213,6 +223,7 @@ public class ConsoleFrame extends javax.swing.JFrame implements MageClient {
logger.info("Logging level: " + logger.getEffectiveLevel());
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ConsoleFrame().setVisible(true);
logger.info("Started MAGE server console");
@ -302,4 +313,18 @@ public class ConsoleFrame extends javax.swing.JFrame implements MageClient {
public void processCallback(ClientCallback callback) {
}
public void exitApp() {
if (session.isConnected()) {
if (JOptionPane.showConfirmDialog(this, "You are currently connected. Are you sure you want to disconnect?", "Confirm disconnect", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
return;
}
session.disconnect(false);
} else {
if (JOptionPane.showConfirmDialog(this, "Are you sure you want to exit?", "Confirm exit", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
return;
}
}
dispose();
System.exit(0);
}
}

View file

@ -16,7 +16,7 @@
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jSplitPane1" alignment="1" pref="562" max="32767" attributes="0"/>
<Component id="jSplitPane1" alignment="1" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
@ -152,7 +152,7 @@
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jPanel5" alignment="0" max="32767" attributes="0"/>
<Component id="jPanel6" alignment="0" pref="301" max="32767" attributes="0"/>
<Component id="jPanel6" alignment="0" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">

View file

@ -33,18 +33,19 @@
*/
package mage.server.console;
import mage.remote.Session;
import mage.view.TableView;
import mage.view.UserView;
import org.apache.log4j.Logger;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import javax.swing.SwingWorker;
import javax.swing.table.AbstractTableModel;
import mage.remote.Session;
import mage.view.TableView;
import mage.view.UserView;
import org.apache.log4j.Logger;
/**
*
@ -54,8 +55,8 @@ public class ConsolePanel extends javax.swing.JPanel {
private static final Logger logger = Logger.getLogger(ConsolePanel.class);
private TableUserModel tableUserModel;
private TableTableModel tableTableModel;
private final TableUserModel tableUserModel;
private final TableTableModel tableTableModel;
private UpdateUsersTask updateUsersTask;
private UpdateTablesTask updateTablesTask;
@ -91,10 +92,12 @@ public class ConsolePanel extends javax.swing.JPanel {
}
public void stop() {
if (updateUsersTask != null && !updateUsersTask.isDone())
if (updateUsersTask != null && !updateUsersTask.isDone()) {
updateUsersTask.cancel(true);
if (updateTablesTask != null && !updateTablesTask.isDone())
}
if (updateTablesTask != null && !updateTablesTask.isDone()) {
updateTablesTask.cancel(true);
}
}
/** This method is called from within the constructor to
@ -289,8 +292,9 @@ public class ConsolePanel extends javax.swing.JPanel {
}
class TableUserModel extends AbstractTableModel {
private String[] columnNames = new String[]{"User Name", "Host", "Time Connected"};
private final String[] columnNames = new String[]{"User Name", "Host", "Time Connected"};
private UserView[] users = new UserView[0];
private static final DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
public void loadData(List<UserView> users) {
this.users = users.toArray(new UserView[0]);
@ -315,7 +319,7 @@ class TableUserModel extends AbstractTableModel {
case 1:
return users[arg0].getHost();
case 2:
return users[arg0].getConnectionTime().toString();
return formatter.format(users[arg0].getConnectionTime());
case 3:
return users[arg0].getSessionId();
}
@ -326,8 +330,9 @@ class TableUserModel extends AbstractTableModel {
public String getColumnName(int columnIndex) {
String colName = "";
if (columnIndex <= getColumnCount())
if (columnIndex <= getColumnCount()) {
colName = columnNames[columnIndex];
}
return colName;
}
@ -345,7 +350,7 @@ class TableUserModel extends AbstractTableModel {
}
class TableTableModel extends AbstractTableModel {
private String[] columnNames = new String[]{"Table Name", "Owner", "Game Type", "Deck Type", "Status"};
private final String[] columnNames = new String[]{"Table Name", "Owner", "Game Type", "Deck Type", "Status"};
private TableView[] tables = new TableView[0];
@ -372,16 +377,17 @@ class TableTableModel extends AbstractTableModel {
case 1:
return tables[arg0].getControllerName();
case 2:
return tables[arg0].getGameType().toString();
return tables[arg0].getGameType();
case 3:
return tables[arg0].getDeckType().toString();
return tables[arg0].getDeckType();
case 4:
return tables[arg0].getTableState().toString();
case 5:
return tables[arg0].isTournament();
case 6:
if (!tables[arg0].getGames().isEmpty())
if (!tables[arg0].getGames().isEmpty()) {
return tables[arg0].getGames().get(0);
}
return null;
case 7:
return tables[arg0].getTableId();
@ -393,8 +399,9 @@ class TableTableModel extends AbstractTableModel {
public String getColumnName(int columnIndex) {
String colName = "";
if (columnIndex <= getColumnCount())
if (columnIndex <= getColumnCount()) {
colName = columnNames[columnIndex];
}
return colName;
}
@ -406,8 +413,9 @@ class TableTableModel extends AbstractTableModel {
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex != 5)
if (columnIndex != 5) {
return false;
}
return true;
}
@ -415,8 +423,8 @@ class TableTableModel extends AbstractTableModel {
class UpdateUsersTask extends SwingWorker<Void, List<UserView>> {
private Session session;
private ConsolePanel panel;
private final Session session;
private final ConsolePanel panel;
private List<UserView> previousUsers;
private static final Logger logger = Logger.getLogger(UpdateUsersTask.class);
@ -484,9 +492,9 @@ class UpdateUsersTask extends SwingWorker<Void, List<UserView>> {
class UpdateTablesTask extends SwingWorker<Void, Collection<TableView>> {
private Session session;
private UUID roomId;
private ConsolePanel panel;
private final Session session;
private final UUID roomId;
private final ConsolePanel panel;
private static final Logger logger = Logger.getLogger(UpdateTablesTask.class);
@ -520,4 +528,4 @@ class UpdateTablesTask extends SwingWorker<Void, Collection<TableView>> {
logger.fatal("Update Tables Task error", ex);
} catch (CancellationException ex) {}
}
}
}

View file

@ -4,11 +4,11 @@ log4j.rootLogger=info, console, logfile
#console log
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p [%d{yyyy-MM-dd HH:mm [ss:SSS]}] %C{1}[%t]: %m%n
log4j.appender.console.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss,SSS} %-90m =>[%t] %C{1}.%M%n
log4j.appender.console.Threshold=DEBUG
#file log
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=mageadmin.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%-5p [%d{yyyy-MM-dd HH:mm [ss:SSS]}] %C{1}[%t]: %m%n
log4j.appender.logfile.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss,SSS} %-90m =>[%t] %C{1}.%M%n

View file

@ -69,33 +69,39 @@ public class ChatSession {
}
}
public void kill(UUID userId, DisconnectReason reason) {
public void kill(UUID userId, DisconnectReason reason) {
try {
if (reason == null) {
logger.fatal("User kill without disconnect reason userId: " + userId) ;
logger.fatal("User kill without disconnect reason userId: " + userId);
reason = DisconnectReason.Undefined;
}
if (reason != null && userId != null && clients.containsKey(userId)) {
String userName = clients.get(userId);
clients.remove(userId);
logger.debug(userName + "(" + reason.toString() +")" + " removed from chatId " + chatId) ;
logger.debug(userName + "(" + reason.toString() + ")" + " removed from chatId " + chatId);
String message;
switch (reason) {
case Disconnected:
message = " has left XMage";
break;
case LostConnection:
case LostConnection:
message = " has lost connection";
break;
default:
message = " left (" + reason.toString() +")";
case SessionExpired:
message = " session expired";
break;
case CleaningUp:
message = null;
break;
default:
message = " left (" + reason.toString() + ")";
}
if (message != null) {
broadcast(null, new StringBuilder(userName).append(message).toString(), MessageColor.BLUE, true, MessageType.STATUS);
broadcast(null, userName + message, MessageColor.BLUE, true, MessageType.STATUS);
}
}
} catch(Exception ex) {
} catch (Exception ex) {
logger.fatal("exception: " + ex.toString());
}
}

View file

@ -967,7 +967,7 @@ public class MageServerImpl implements MageServer {
public List<UserView> execute() throws MageException {
List<UserView> users = new ArrayList<>();
for (User user : UserManager.getInstance().getUsers()) {
users.add(new UserView(user.getName(), "", user.getSessionId(), user.getConnectionTime()));
users.add(new UserView(user.getName(), user.getHost(), user.getSessionId(), user.getConnectionTime()));
}
return users;
}

View file

@ -38,6 +38,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import mage.server.util.ThreadExecutor;
import mage.view.ChatMessage;
import org.apache.log4j.Logger;
/**