Show user name in title bar of MAGE. Clear table chat after disconnect. Remove user from chat after disconnect. Some tweaking about the messages if a user disconnected/session expired.

This commit is contained in:
LevelX2 2013-04-29 17:18:43 +02:00
parent ae30e9a884
commit b6ddaabe44
12 changed files with 58 additions and 28 deletions

View file

@ -856,6 +856,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
if (session.isConnected()) { if (session.isConnected()) {
if (JOptionPane.showConfirmDialog(this, "Are you sure you want to disconnect?", "Confirm disconnect", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { if (JOptionPane.showConfirmDialog(this, "Are you sure you want to disconnect?", "Confirm disconnect", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
session.disconnect(false); session.disconnect(false);
tablesPane.clearChat();
showMessage("You have disconnected"); showMessage("You have disconnected");
} }
} else { } else {

View file

@ -275,8 +275,7 @@ class TableModel extends AbstractTableModel {
} }
public void clear() { public void clear() {
this.txtConversation.selectAll(); this.txtConversation.setText("");
this.txtConversation.replaceSelection("");
} }
/** This method is called from within the constructor to /** This method is called from within the constructor to

View file

@ -71,6 +71,9 @@ public class TablesPane extends MagePane {
tablesPanel.hideTables(); tablesPanel.hideTables();
} }
public void clearChat() {
tablesPanel.getChatPanel().clear();
}
/** This method is called from within the constructor to /** This method is called from within the constructor to
* initialize the form. * initialize the form.

View file

@ -331,6 +331,11 @@ public class TablesPanel extends javax.swing.JPanel {
} }
} }
public ChatPanel getChatPanel() {
return this.chatPanel;
}
/** This method is called from within the constructor to /** This method is called from within the constructor to
* initialize the form. * initialize the form.
* WARNING: Do NOT modify this code. The content of this method is * WARNING: Do NOT modify this code. The content of this method is

View file

@ -177,8 +177,8 @@ public class SessionImpl implements Session {
if (registerResult) { if (registerResult) {
sessionState = SessionState.CONNECTED; sessionState = SessionState.CONNECTED;
serverState = server.getServerState(); serverState = server.getServerState();
logger.info("Connected to MAGE server at " + connection.getHost() + ":" + connection.getPort()); logger.info(new StringBuilder("Connected as ").append(this.getUserName()).append(" to MAGE server at ").append(connection.getHost()).append(":").append(connection.getPort()).toString());
client.connected("Connected to " + connection.getHost() + ":" + connection.getPort() + " "); client.connected(new StringBuilder("Connected as ").append(this.getUserName()).append(" to ").append(connection.getHost()).append(":").append(connection.getPort()).append(" ").toString());
return true; return true;
} }
disconnect(false); disconnect(false);
@ -1189,7 +1189,9 @@ public class SessionImpl implements Session {
public boolean ping() { public boolean ping() {
try { try {
if (isConnected()) { if (isConnected()) {
server.ping(sessionId); if (!server.ping(sessionId)) {
logger.error(new StringBuilder("Ping failed: ").append(this.getUserName()).append(" Session: ").append(sessionId).append(" to MAGE server at ").append(connection.getHost()).append(":").append(connection.getPort()).toString());
}
} }
return true; return true;
} catch (MageException ex) { } catch (MageException ex) {

View file

@ -60,7 +60,7 @@ public class ChatManager {
} }
public void leaveChat(UUID chatId, UUID userId) { public void leaveChat(UUID chatId, UUID userId) {
chatSessions.get(chatId).kill(userId); chatSessions.get(chatId).kill(userId, User.DisconnectReason.CleaningUp);
} }
public void destroyChatSession(UUID chatId) { public void destroyChatSession(UUID chatId) {
@ -87,15 +87,16 @@ public class ChatManager {
User user = UserManager.getInstance().getUser(userId); User user = UserManager.getInstance().getUser(userId);
if (user != null) { if (user != null) {
for (ChatSession chat: chatSessions.values()) { for (ChatSession chat: chatSessions.values()) {
if (chat.hasUser(userId)) if (chat.hasUser(userId)) {
chat.broadcast(user.getName(), message, color); chat.broadcast(user.getName(), message, color);
}
} }
} }
} }
public void removeUser(UUID userId) { public void removeUser(UUID userId, User.DisconnectReason reason) {
for (ChatSession chat: chatSessions.values()) { for (ChatSession chat: chatSessions.values()) {
chat.kill(userId); chat.kill(userId, reason);
} }
} }
} }

View file

@ -64,12 +64,23 @@ public class ChatSession {
} }
} }
public void kill(UUID userId) { public void kill(UUID userId, User.DisconnectReason reason) {
if (userId != null && clients.containsKey(userId)) { if (userId != null && clients.containsKey(userId)) {
String userName = clients.get(userId); String userName = clients.get(userId);
String message;
clients.remove(userId); clients.remove(userId);
broadcast(userName, " has left", MessageColor.BLUE); switch (reason) {
logger.info(userName + " has left chat " + chatId); case Disconnected:
message = " has quit MAGE";
break;
case SessionExpired:
message = " session expired";
break;
default:
message = " has left chat";
}
broadcast(userName, message, MessageColor.BLUE);
logger.info(userName + message + " " + chatId);
} }
} }
@ -86,10 +97,12 @@ public class ChatSession {
logger.debug("Broadcasting '" + msg + "' for " + chatId); logger.debug("Broadcasting '" + msg + "' for " + chatId);
for (UUID userId: clients.keySet()) { for (UUID userId: clients.keySet()) {
User user = UserManager.getInstance().getUser(userId); User user = UserManager.getInstance().getUser(userId);
if (user != null) if (user != null) {
user.fireCallback(new ClientCallback("chatMessage", chatId, new ChatMessage(username, msg, time, color))); user.fireCallback(new ClientCallback("chatMessage", chatId, new ChatMessage(username, msg, time, color)));
else }
kill(userId); else {
kill(userId, User.DisconnectReason.CleaningUp);
}
} }
} }
} }

View file

@ -168,7 +168,7 @@ public class Main {
sessionName = session.getHost(); sessionName = session.getHost();
} }
if (throwable instanceof ClientDisconnectedException) { if (throwable instanceof ClientDisconnectedException) {
SessionManager.getInstance().disconnect(client.getSessionId(), false); SessionManager.getInstance().disconnect(client.getSessionId(), true);
logger.info("client disconnected - " + sessionName); logger.info("client disconnected - " + sessionName);
} }
else { else {

View file

@ -163,7 +163,7 @@ public class Session {
public void kill() { public void kill() {
logger.info("session killed for user " + userId); logger.info("session killed for user " + userId);
UserManager.getInstance().removeUser(userId); UserManager.getInstance().removeUser(userId, User.DisconnectReason.Disconnected);
} }
synchronized void fireCallback(final ClientCallback call) { synchronized void fireCallback(final ClientCallback call) {

View file

@ -99,7 +99,7 @@ public class SessionManager {
public synchronized void disconnect(String sessionId, boolean voluntary) { public synchronized void disconnect(String sessionId, boolean voluntary) {
Session session = sessions.get(sessionId); Session session = sessions.get(sessionId);
sessions.remove(sessionId); sessions.remove(sessionId);
if (session != null) { if (session != null) {
if (voluntary) { if (voluntary) {
session.kill(); session.kill();

View file

@ -56,6 +56,10 @@ public class User {
Created, Connected, Disconnected, Reconnected; Created, Connected, Disconnected, Reconnected;
} }
public enum DisconnectReason {
SessionExpired, Disconnected, CleaningUp;
}
private UUID userId = UUID.randomUUID(); private UUID userId = UUID.randomUUID();
private String userName; private String userName;
private String sessionId = ""; private String sessionId = "";
@ -64,7 +68,7 @@ public class User {
private Date lastActivity = new Date(); private Date lastActivity = new Date();
private UserState userState; private UserState userState;
private Map<UUID, Table> tables = new HashMap<UUID, Table>(); private Map<UUID, Table> tables = new HashMap<UUID, Table>();
private Map<UUID, GameSession> gameSessions = new HashMap<UUID, GameSession>(); private Map<UUID, GameSession> gameSessions = new HashMap<UUID, GameSession>();
private Map<UUID, DraftSession> draftSessions = new HashMap<UUID, DraftSession>(); private Map<UUID, DraftSession> draftSessions = new HashMap<UUID, DraftSession>();
private Map<UUID, TournamentSession> tournamentSessions = new HashMap<UUID, TournamentSession>(); private Map<UUID, TournamentSession> tournamentSessions = new HashMap<UUID, TournamentSession>();
private Map<UUID, TournamentSession> constructing = new HashMap<UUID, TournamentSession>(); private Map<UUID, TournamentSession> constructing = new HashMap<UUID, TournamentSession>();
@ -98,14 +102,14 @@ public class User {
this.sessionId = sessionId; this.sessionId = sessionId;
if (sessionId.isEmpty()) { if (sessionId.isEmpty()) {
userState = UserState.Disconnected; userState = UserState.Disconnected;
logger.info("User " + userName + " disconnected"); logger.info(new StringBuilder("User ").append(userName).append(" disconnected").toString());
} else if (userState == UserState.Created) { } else if (userState == UserState.Created) {
userState = UserState.Connected; userState = UserState.Connected;
logger.info("User " + userName + " created"); logger.info(new StringBuilder("User ").append(userName).append(" created").toString());
} else { } else {
userState = UserState.Reconnected; userState = UserState.Reconnected;
reconnect(); reconnect();
logger.info("User " + userName + " reconnected"); logger.info(new StringBuilder("User ").append(userName).append(" reconnected").toString());
} }
} }
@ -184,6 +188,7 @@ public class User {
} }
public boolean isExpired(Date expired) { public boolean isExpired(Date expired) {
logger.trace(new StringBuilder("isExpired: User ").append(userName).append(" lastActivity: ").append(lastActivity).append(" expired: ").append(expired).toString());
return /*userState == UserState.Disconnected && */ lastActivity.before(expired); return /*userState == UserState.Disconnected && */ lastActivity.before(expired);
} }
@ -259,7 +264,7 @@ public class User {
sideboarding.remove(tableId); sideboarding.remove(tableId);
} }
public void kill() { public void kill(DisconnectReason reason) {
for (GameSession session: gameSessions.values()) { for (GameSession session: gameSessions.values()) {
session.kill(); session.kill();
} }
@ -275,6 +280,7 @@ public class User {
TableManager.getInstance().removeTable(userId, entry.getValue().getId()); TableManager.getInstance().removeTable(userId, entry.getValue().getId());
} }
} }
ChatManager.getInstance().removeUser(userId, reason);
} }
public void setUserData(UserData userData) { public void setUserData(UserData userData) {

View file

@ -102,7 +102,7 @@ public class UserManager {
public void disconnect(UUID userId) { public void disconnect(UUID userId) {
if (userId != null) { if (userId != null) {
ChatManager.getInstance().removeUser(userId); ChatManager.getInstance().removeUser(userId, User.DisconnectReason.Disconnected);
if (users.containsKey(userId)) { if (users.containsKey(userId)) {
logger.info("user disconnected " + userId); logger.info("user disconnected " + userId);
users.get(userId).setSessionId(""); users.get(userId).setSessionId("");
@ -118,12 +118,12 @@ public class UserManager {
return false; return false;
} }
public void removeUser(UUID userId) { public void removeUser(UUID userId, User.DisconnectReason reason) {
if (users.containsKey(userId)) { if (users.containsKey(userId)) {
logger.info("user removed" + userId); logger.info("user removed" + userId);
users.get(userId).setSessionId(""); ChatManager.getInstance().removeUser(userId, reason);
ChatManager.getInstance().broadcast(userId, "has disconnected", MessageColor.BLACK); ChatManager.getInstance().broadcast(userId, "has disconnected", MessageColor.BLACK);
users.get(userId).kill(); users.get(userId).kill(User.DisconnectReason.Disconnected);
users.remove(userId); users.remove(userId);
} }
} }
@ -142,7 +142,7 @@ public class UserManager {
for (User user: users.values()) { for (User user: users.values()) {
if (user.isExpired(expired.getTime())) { if (user.isExpired(expired.getTime())) {
logger.info(user.getName() + " session expired " + user.getId()); logger.info(user.getName() + " session expired " + user.getId());
user.kill(); user.kill(User.DisconnectReason.SessionExpired);
users.remove(user.getId()); users.remove(user.getId());
} }
} }