Launcher/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/SessionManager.java
2019-06-03 11:00:46 +03:00

62 lines
1.7 KiB
Java

package pro.gravit.launchserver.manangers;
import java.util.HashSet;
import java.util.Set;
import pro.gravit.launcher.NeedGarbageCollection;
import pro.gravit.launchserver.socket.Client;
public class SessionManager implements NeedGarbageCollection {
public static final long SESSION_TIMEOUT = 3 * 60 * 60 * 1000; // 3 часа
public static final boolean GARBAGE_SERVER = Boolean.parseBoolean(System.getProperty("launcher.garbageSessionsServer", "false"));
private HashSet<Client> clientSet = new HashSet<>(128);
public boolean addClient(Client client) {
clientSet.add(client);
return true;
}
@Override
public void garbageCollection() {
long time = System.currentTimeMillis();
clientSet.removeIf(c -> (c.timestamp + SESSION_TIMEOUT < time) && ((c.type == Client.Type.USER) || ((c.type == Client.Type.SERVER) && GARBAGE_SERVER)));
}
public Client getClient(long session) {
for (Client c : clientSet)
if (c.session == session) return c;
return null;
}
public Client getOrNewClient(long session) {
for (Client c : clientSet)
if (c.session == session) return c;
Client newClient = new Client(session);
clientSet.add(newClient);
return newClient;
}
public void updateClient(long session) {
for (Client c : clientSet) {
if (c.session == session) {
c.up();
return;
}
}
Client newClient = new Client(session);
clientSet.add(newClient);
}
public Set<Client> getSessions() {
return clientSet;
}
public void loadSessions(Set<Client> set) {
clientSet.addAll(set);
}
}