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.CookieDecoder;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.COOKIE;
import org.jboss.netty.handler.codec.http.HttpRequest;
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 {
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, 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) {
CookieDecoder cookieDecoder = new CookieDecoder();
Set<Cookie> cookies = cookieDecoder.decode(request.getHeader(COOKIE));
for (Cookie cookie : cookies) {
if (cookie.getName().equals("loggedUser")) {
String value = cookie.getValue();
if (value != null) {
String loggedUser = _users.get(value);
if (loggedUser != null)
return loggedUser;
_readWriteLock.readLock().lock();
try {
CookieDecoder cookieDecoder = new CookieDecoder();
Set<Cookie> cookies = cookieDecoder.decode(request.getHeader(COOKIE));
for (Cookie cookie : cookies) {
if (cookie.getName().equals("loggedUser")) {
String value = cookie.getValue();
if (value != null) {
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) {
Map<String, String> cookies = new HashMap<String, String>();
cookies.put("loggedUser", insertValueForUser(userName));
return cookies;
_readWriteLock.readLock().lock();
try {
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();
@@ -46,4 +75,31 @@ public class LoggedUserHolder {
_users.put(randomStr, userName);
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.constructObjects(objects);
objects.put(LoggedUserHolder.class, new LoggedUserHolder());
LoggedUserHolder loggedUserHolder = new LoggedUserHolder();
loggedUserHolder.start();
objects.put(LoggedUserHolder.class, loggedUserHolder);
LongPollingSystem longPollingSystem = new LongPollingSystem();
longPollingSystem.start();
objects.put(LongPollingSystem.class, longPollingSystem);