From c03dc6c2b5bd5af6796d7bce100bba0ee1348c36 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Mon, 18 Aug 2014 21:48:59 +0200 Subject: [PATCH] Some more changes to Server Console and Admin server handling. --- .gitignore | 1 + Mage.Common/src/mage/remote/Connection.java | 6 +- Mage.Common/src/mage/remote/SessionImpl.java | 6 +- Mage.Common/src/mage/view/UserView.java | 8 +-- .../mage/server/console/ConnectDialog.java | 30 ++++++---- .../mage/server/console/ConsoleFrame.form | 2 +- .../mage/server/console/ConsoleFrame.java | 27 ++++++++- .../mage/server/console/ConsolePanel.form | 4 +- .../mage/server/console/ConsolePanel.java | 60 +++++++++++-------- .../src/main/resources/log4j.properties | 4 +- .../main/java/mage/server/ChatSession.java | 22 ++++--- .../main/java/mage/server/MageServerImpl.java | 2 +- .../main/java/mage/server/UserManager.java | 1 + 13 files changed, 113 insertions(+), 60 deletions(-) diff --git a/.gitignore b/.gitignore index fb2638998b..5ba6afc6be 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Mage.Common/src/mage/remote/Connection.java b/Mage.Common/src/mage/remote/Connection.java index fa31b7bb61..adb4d4d8a3 100644 --- a/Mage.Common/src/mage/remote/Connection.java +++ b/Mage.Common/src/mage/remote/Connection.java @@ -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; /** diff --git a/Mage.Common/src/mage/remote/SessionImpl.java b/Mage.Common/src/mage/remote/SessionImpl.java index 23f5f1b858..5b6e24fd55 100644 --- a/Mage.Common/src/mage/remote/SessionImpl.java +++ b/Mage.Common/src/mage/remote/SessionImpl.java @@ -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; } diff --git a/Mage.Common/src/mage/view/UserView.java b/Mage.Common/src/mage/view/UserView.java index c59f9f6d9a..96cabbfbd0 100644 --- a/Mage.Common/src/mage/view/UserView.java +++ b/Mage.Common/src/mage/view/UserView.java @@ -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; diff --git a/Mage.Server.Console/src/main/java/mage/server/console/ConnectDialog.java b/Mage.Server.Console/src/main/java/mage/server/console/ConnectDialog.java index 96d13d414f..7835ae8eb9 100644 --- a/Mage.Server.Console/src/main/java/mage/server/console/ConnectDialog.java +++ b/Mage.Server.Console/src/main/java/mage/server/console/ConnectDialog.java @@ -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 servers = new ArrayList(); + List 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 diff --git a/Mage.Server.Console/src/main/java/mage/server/console/ConsoleFrame.form b/Mage.Server.Console/src/main/java/mage/server/console/ConsoleFrame.form index c03ba93846..3dc8b3a312 100644 --- a/Mage.Server.Console/src/main/java/mage/server/console/ConsoleFrame.form +++ b/Mage.Server.Console/src/main/java/mage/server/console/ConsoleFrame.form @@ -2,7 +2,7 @@
- + diff --git a/Mage.Server.Console/src/main/java/mage/server/console/ConsoleFrame.java b/Mage.Server.Console/src/main/java/mage/server/console/ConsoleFrame.java index df274b1058..ef9f660c27 100644 --- a/Mage.Server.Console/src/main/java/mage/server/console/ConsoleFrame.java +++ b/Mage.Server.Console/src/main/java/mage/server/console/ConsoleFrame.java @@ -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); + } } diff --git a/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.form b/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.form index 6bb32d56a2..3bcc254936 100644 --- a/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.form +++ b/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.form @@ -16,7 +16,7 @@ - + @@ -152,7 +152,7 @@ - + diff --git a/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.java b/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.java index 7effc6d11a..d35c75a50b 100644 --- a/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.java +++ b/Mage.Server.Console/src/main/java/mage/server/console/ConsolePanel.java @@ -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 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> { - private Session session; - private ConsolePanel panel; + private final Session session; + private final ConsolePanel panel; private List previousUsers; private static final Logger logger = Logger.getLogger(UpdateUsersTask.class); @@ -484,9 +492,9 @@ class UpdateUsersTask extends SwingWorker> { class UpdateTablesTask extends SwingWorker> { - 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> { logger.fatal("Update Tables Task error", ex); } catch (CancellationException ex) {} } -} \ No newline at end of file +} diff --git a/Mage.Server.Console/src/main/resources/log4j.properties b/Mage.Server.Console/src/main/resources/log4j.properties index b319f70d64..f07183abe9 100644 --- a/Mage.Server.Console/src/main/resources/log4j.properties +++ b/Mage.Server.Console/src/main/resources/log4j.properties @@ -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 \ No newline at end of file +log4j.appender.logfile.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss,SSS} %-90m =>[%t] %C{1}.%M%n \ No newline at end of file diff --git a/Mage.Server/src/main/java/mage/server/ChatSession.java b/Mage.Server/src/main/java/mage/server/ChatSession.java index 86ae6892a3..3359a7c1d0 100644 --- a/Mage.Server/src/main/java/mage/server/ChatSession.java +++ b/Mage.Server/src/main/java/mage/server/ChatSession.java @@ -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()); } } diff --git a/Mage.Server/src/main/java/mage/server/MageServerImpl.java b/Mage.Server/src/main/java/mage/server/MageServerImpl.java index 0b82b7db43..43d0f7b942 100644 --- a/Mage.Server/src/main/java/mage/server/MageServerImpl.java +++ b/Mage.Server/src/main/java/mage/server/MageServerImpl.java @@ -967,7 +967,7 @@ public class MageServerImpl implements MageServer { public List execute() throws MageException { List 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; } diff --git a/Mage.Server/src/main/java/mage/server/UserManager.java b/Mage.Server/src/main/java/mage/server/UserManager.java index d95d5639dc..fc6eba1d6d 100644 --- a/Mage.Server/src/main/java/mage/server/UserManager.java +++ b/Mage.Server/src/main/java/mage/server/UserManager.java @@ -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; /**