2018-09-17 10:07:32 +03:00
|
|
|
package ru.gravit.launchserver.manangers;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import ru.gravit.launcher.LauncherAPI;
|
|
|
|
import ru.gravit.launcher.NeedGarbageCollection;
|
|
|
|
import ru.gravit.launchserver.socket.Client;
|
|
|
|
|
|
|
|
public class SessionManager implements NeedGarbageCollection {
|
|
|
|
@LauncherAPI
|
|
|
|
public static final long SESSION_TIMEOUT = 10 * 60 * 1000; // 10 минут
|
|
|
|
private Set<Client> clientSet = new HashSet<>(128);
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
public boolean addClient(Client client) {
|
|
|
|
clientSet.add(client);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-09-22 17:33:00 +03:00
|
|
|
@LauncherAPI
|
2018-09-17 10:07:32 +03:00
|
|
|
public void garbageCollection() {
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
clientSet.removeIf(c -> c.timestamp + SESSION_TIMEOUT < time);
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
public void updateClient(long session) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|