package chat.user;
|
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import chat.server.moquette.message.ClientID;
|
|
|
public class SessionStore {
|
|
private static Map<String, Session> sessionMap;
|
|
static {
|
init();
|
}
|
|
private static void init() {
|
sessionMap = new ConcurrentHashMap<String, Session>();
|
}
|
|
public static Session getOrCreate(Client client) throws Exception {
|
if (client == null) {
|
return null;
|
}
|
|
String clientId = client.getId();
|
Session result = sessionMap.get(clientId);
|
|
if (result == null) {
|
result = new Session(client);
|
sessionMap.put(clientId, result);
|
}
|
|
return result;
|
}
|
|
public static Session get(ClientID clientId) {
|
if (clientId == null) {
|
return null;
|
}
|
|
if (clientId.isEmpty()) {
|
return null;
|
}
|
|
String key = clientId.getValue();
|
Session result = sessionMap.get(key);
|
|
return result;
|
}
|
|
public static Session get(String clientId) {
|
if (clientId == null) {
|
return null;
|
}
|
|
Session result = sessionMap.get(clientId);
|
return result;
|
}
|
|
public static void delete(ClientID clientID) {
|
if (clientID == null) {
|
return;
|
}
|
|
String key = clientID.getValue();
|
sessionMap.remove(key);
|
}
|
|
public static boolean contains(String clientId) {
|
if (clientId == null) {
|
return false;
|
}
|
|
return sessionMap.containsKey(clientId);
|
}
|
|
public static void clearSessionByUser(String userId) {
|
User user = User.getInstance(userId);
|
|
if (user == null) {
|
return;
|
}
|
|
Set<Client> clientSet = user.getClientSet();
|
|
for (Client client: clientSet) {
|
String clientId = client.getId();
|
Session session = sessionMap.get(clientId);
|
|
session.closeChannel();
|
sessionMap.remove(clientId);
|
}
|
}
|
|
}
|