Expire of users logged.

This commit is contained in:
marcins78
2013-01-09 17:15:42 +00:00
parent ffe26a9ba0
commit 3bb3c7a8e7
2 changed files with 74 additions and 15 deletions

View File

@@ -2,34 +2,63 @@ package com.gempukku.lotro.async;
import org.jboss.netty.handler.codec.http.Cookie; import org.jboss.netty.handler.codec.http.Cookie;
import org.jboss.netty.handler.codec.http.CookieDecoder; import org.jboss.netty.handler.codec.http.CookieDecoder;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.COOKIE;
import org.jboss.netty.handler.codec.http.HttpRequest; import org.jboss.netty.handler.codec.http.HttpRequest;
import java.util.*; import java.util.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.COOKIE;
public class LoggedUserHolder { public class LoggedUserHolder {
private long _loggedUserExpireLength = 1000 * 60 * 10;
private long _expireCheckInterval = 1000 * 60;
private Map<String, String> _users = Collections.synchronizedMap(new HashMap<String, String>()); private Map<String, String> _users = Collections.synchronizedMap(new HashMap<String, String>());
private Map<String, Long> _lastAccess = Collections.synchronizedMap(new HashMap<String, Long>());
private ReadWriteLock _readWriteLock = new ReentrantReadWriteLock();
private ClearExpiredRunnable _clearExpiredRunnable;
public void start() {
_clearExpiredRunnable = new ClearExpiredRunnable();
Thread thr = new Thread(_clearExpiredRunnable);
thr.start();
}
public String getLoggedUser(HttpRequest request) { public String getLoggedUser(HttpRequest request) {
CookieDecoder cookieDecoder = new CookieDecoder(); _readWriteLock.readLock().lock();
Set<Cookie> cookies = cookieDecoder.decode(request.getHeader(COOKIE)); try {
for (Cookie cookie : cookies) { CookieDecoder cookieDecoder = new CookieDecoder();
if (cookie.getName().equals("loggedUser")) { Set<Cookie> cookies = cookieDecoder.decode(request.getHeader(COOKIE));
String value = cookie.getValue(); for (Cookie cookie : cookies) {
if (value != null) { if (cookie.getName().equals("loggedUser")) {
String loggedUser = _users.get(value); String value = cookie.getValue();
if (loggedUser != null) if (value != null) {
return loggedUser; String loggedUser = _users.get(value);
if (loggedUser != null) {
_lastAccess.put(value, System.currentTimeMillis());
return loggedUser;
}
}
} }
} }
return null;
} finally {
_readWriteLock.readLock().unlock();
} }
return null;
} }
public Map<String, String> logUser(String userName) { public Map<String, String> logUser(String userName) {
Map<String, String> cookies = new HashMap<String, String>(); _readWriteLock.readLock().lock();
cookies.put("loggedUser", insertValueForUser(userName)); try {
return cookies; Map<String, String> cookies = new HashMap<String, String>();
String userValue = insertValueForUser(userName);
cookies.put("loggedUser", userValue);
_lastAccess.put(userValue, System.currentTimeMillis());
return cookies;
} finally {
_readWriteLock.readLock().unlock();
}
} }
private char[] _chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); private char[] _chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
@@ -46,4 +75,31 @@ public class LoggedUserHolder {
_users.put(randomStr, userName); _users.put(randomStr, userName);
return randomStr; return randomStr;
} }
private class ClearExpiredRunnable implements Runnable {
@Override
public void run() {
while (true) {
_readWriteLock.writeLock().lock();
try {
Iterator<Map.Entry<String, Long>> iterator = _lastAccess.entrySet().iterator();
if (iterator.hasNext()) {
Map.Entry<String, Long> lastAccess = iterator.next();
if (lastAccess.getValue() > System.currentTimeMillis() + _loggedUserExpireLength) {
String userValue = lastAccess.getKey();
_users.remove(userValue);
iterator.remove();
}
}
try {
Thread.sleep(_expireCheckInterval);
} catch (InterruptedException exp) {
}
} finally {
_readWriteLock.writeLock().unlock();
}
}
}
}
} }

View File

@@ -28,7 +28,10 @@ public class LotroServerPipelineFactory implements ChannelPipelineFactory {
ServerBuilder.fillObjectMap(objects); ServerBuilder.fillObjectMap(objects);
ServerBuilder.constructObjects(objects); ServerBuilder.constructObjects(objects);
objects.put(LoggedUserHolder.class, new LoggedUserHolder()); LoggedUserHolder loggedUserHolder = new LoggedUserHolder();
loggedUserHolder.start();
objects.put(LoggedUserHolder.class, loggedUserHolder);
LongPollingSystem longPollingSystem = new LongPollingSystem(); LongPollingSystem longPollingSystem = new LongPollingSystem();
longPollingSystem.start(); longPollingSystem.start();
objects.put(LongPollingSystem.class, longPollingSystem); objects.put(LongPollingSystem.class, longPollingSystem);