2019-06-02 05:03:08 +03:00
|
|
|
package pro.gravit.launchserver.manangers;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2019-01-15 06:35:39 +03:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2019-06-03 10:58:10 +03:00
|
|
|
import pro.gravit.launcher.NeedGarbageCollection;
|
|
|
|
import pro.gravit.launchserver.socket.Client;
|
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public class SessionManager implements NeedGarbageCollection {
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2019-03-23 06:40:05 +03:00
|
|
|
public static final long SESSION_TIMEOUT = 3 * 60 * 60 * 1000; // 3 часа
|
2019-01-02 17:04:52 +03:00
|
|
|
public static final boolean GARBAGE_SERVER = Boolean.parseBoolean(System.getProperty("launcher.garbageSessionsServer", "false"));
|
2018-12-24 12:23:00 +03:00
|
|
|
private HashSet<Client> clientSet = new HashSet<>(128);
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public boolean addClient(Client client) {
|
|
|
|
clientSet.add(client);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void garbageCollection() {
|
|
|
|
long time = System.currentTimeMillis();
|
2019-01-02 17:04:52 +03:00
|
|
|
clientSet.removeIf(c -> (c.timestamp + SESSION_TIMEOUT < time) && ((c.type == Client.Type.USER) || ((c.type == Client.Type.SERVER) && GARBAGE_SERVER)));
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|
|
|
|
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public Client getClient(long session) {
|
|
|
|
for (Client c : clientSet)
|
2018-09-22 17:33:00 +03:00
|
|
|
if (c.session == session) return c;
|
2018-09-17 10:07:32 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public Client getOrNewClient(long session) {
|
|
|
|
for (Client c : clientSet)
|
2018-09-22 17:33:00 +03:00
|
|
|
if (c.session == session) return c;
|
2018-09-17 10:07:32 +03:00
|
|
|
Client newClient = new Client(session);
|
|
|
|
clientSet.add(newClient);
|
|
|
|
return newClient;
|
|
|
|
}
|
|
|
|
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public void updateClient(long session) {
|
2018-09-27 00:30:43 +03:00
|
|
|
for (Client c : clientSet) {
|
2018-09-22 17:33:00 +03:00
|
|
|
if (c.session == session) {
|
2018-09-17 10:07:32 +03:00
|
|
|
c.up();
|
|
|
|
return;
|
|
|
|
}
|
2018-09-27 00:30:43 +03:00
|
|
|
}
|
|
|
|
Client newClient = new Client(session);
|
|
|
|
clientSet.add(newClient);
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|
2019-01-15 06:35:39 +03:00
|
|
|
|
|
|
|
public Set<Client> getSessions() {
|
2018-12-24 12:23:00 +03:00
|
|
|
return clientSet;
|
|
|
|
}
|
2019-01-15 06:35:39 +03:00
|
|
|
|
|
|
|
public void loadSessions(Set<Client> set) {
|
2018-12-24 12:23:00 +03:00
|
|
|
clientSet.addAll(set);
|
|
|
|
}
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|