📄 usermanager.java
字号:
if (f == null) {
f = new Vector ();
fshipList.put (fname, f);
}
f.addElement (u);
}
/**
* removes a friendship-relation
* @param u The user which wants to unregister fname as friend
* @param fname the name of the user which will be unregisterd with u
*/
public void removeFriendship (User u, String fname) {
Vector f = (Vector) fshipList.get (fname);
if (f == null)
return;
f.remove (u);
if (f.size()==0)
fshipList.remove(f);
}
/**
* cleanup the userspace. if a user times out, we have to remove
* this user from all lists and also unregister his key
* The touching of users connected via proxy-servers will also be done within here
* Also the ScheduledActions will be triggered here
*/
public void run () {
short ccm = 0;
while (Server.srv.isRunning ()) try {
Server.log ("UserManager.run: loop-start", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
long currTime = System.currentTimeMillis ();
long lowestValue = currTime + Server.srv.USER_REMOVE_SCHEDULE_TIME;
// first step through the removable users-list to realy logout this users
for (Enumeration e = removableUsers.elements (); e.hasMoreElements (); ) {
User cu = (User) e.nextElement ();
synchronized (cu) {
if (cu.getRemoveWhen () == 0 && cu.conn != null) {
StringBuffer tsb = new StringBuffer ("user unscheduled from remove-list: ");
tsb.append (cu.getName ());
tsb.append ("@");
tsb.append (cu.conn.toString());
if (cu.recover ()) {
while (removableUsers.contains (cu))
removableUsers.removeElement (cu);
Server.log (tsb.toString (), Server.MSG_AUTH, Server.LVL_VERY_VERBOSE);
continue;
}
tsb.append (" UNABLE TO RECOVER!");
Server.log (tsb.toString (), Server.MSG_ERROR, Server.LVL_MAJOR);
}
if (cu.getRemoveWhen () > currTime) {
if (lowestValue > cu.getRemoveWhen ())
lowestValue = cu.getRemoveWhen ();
continue;
}
synchronized (removableUsers) {
while (removableUsers.contains (cu))
removableUsers.removeElement (cu);
}
cu.removeNow (false);
}
}
// check all users if they where not active for the last [USER_TIMEOUT] millis
try {
lowestValue = checkUsers (lowestValue);
ccm=0;
} catch (ConcurrentModificationException cme) {
Server.log ("UserManager: Concurrent modification", Server.MSG_STATE, Server.LVL_VERBOSE);
ccm++;
}
if (ccm > 3) {
Server.log ("UserManager: Too many concurent modifications, syncing now", Server.MSG_STATE, Server.LVL_MINOR);
synchronized (this) {
lowestValue = checkUsers (lowestValue);
}
ccm = 0;
}
// checking the ScheduledActions
for (Enumeration e = schedule.elements (); e.hasMoreElements (); ) {
ScheduledAction sa = (ScheduledAction) e.nextElement ();
if (usrName.get (sa.getUser ().getName ()) == null) {
schedule.removeElement (sa);
}
long st = sa.getStartTime ();
if (st > currTime) {
if (st < lowestValue) lowestValue = st;
continue;
}
sa.execute ();
schedule.removeElement (sa);
}
long sleepTime = lowestValue - System.currentTimeMillis ();
if (sleepTime <= 0) continue;
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ie) { }
} catch (Exception e) {
Server.debug ("UserManager encountered excpetion", e, Server.MSG_ERROR, Server.LVL_MAJOR);
}
}
private long checkUsers (long lowestValue) {
long currTime = System.currentTimeMillis ();
long val = Server.srv.USER_REMOVE_SCHEDULE_TIME * 10;
for (Iterator i = usrCookie.values ().iterator (); i.hasNext (); ) {
User cu = (User) i.next();
if (cu == null)
continue;
synchronized (cu) {
// if user is already removing continue
if ((cu.isJoining ()
&& (currTime - cu.getSessionStart()) > val)
|| removableUsers.contains (cu))
continue;
if (usrName.get(cu.getName().toLowerCase())==null) {
cu.removeNow(false);
continue;
}
lowestValue = checkUser (cu, lowestValue, currTime);
}
}
for (Iterator i = usrName.values ().iterator (); i.hasNext (); ) {
User cu = (User) i.next();
if (cu != null && usrCookie.get(cu.getCookie())==null) {
cu.removeNow(false);
}
}
return lowestValue;
}
private long checkUser (User cu, long lowestValue, long currTime) {
if (cu.getGroup () == null) {
Server.log ("UserManager.checkUsers: User " + cu.getName() + "has no associated Group (Will be removed)", Server.MSG_STATE, Server.LVL_VERBOSE);
CentralSelector.dropKey(cu.getKey());
return lowestValue;
}
cu.checkReferences();
if (cu.conn == null) {
cu.scheduleToRemove();
return lowestValue;
}
// check for open channel and if it is valide (if not schedule to remove)
SelectionKey sk = cu.getKey ();
if (sk == null) {
StringBuffer tsb = new StringBuffer ("UserManager.checkUsersForValidity: removing user (SelectionKey is null): ");
tsb.append (cu.getName ());
if (cu.conn!=null) {
tsb.append ("@");
tsb.append (cu.conn.toString());
}
Server.log (tsb.toString (), Server.MSG_AUTH, Server.LVL_MAJOR);
cu.scheduleToRemove ();
return lowestValue;
} else if (!sk.channel ().isOpen ()) {
StringBuffer tsb = new StringBuffer ("UserManager.checkUsersForValidity: removing user (SocketChannel not open): ");
tsb.append (cu.getName ());
if (cu.conn!=null) {
tsb.append ("@");
tsb.append (cu.conn.toString());
}
Server.log (tsb.toString (), Server.MSG_AUTH, Server.LVL_MAJOR);
CentralSelector.dropKey (sk);
cu.scheduleToRemove ();
return lowestValue;
}
long cuTimeout = cu.lastActive () + (cu.isAway () ? Server.srv.USER_AWAY_TIMEOUT : Server.srv.USER_TIMEOUT);
if (currTime < cuTimeout) {
if (currTime - cu.lastRecievedMessage > Server.srv.TOUCH_USER_DELAY) {
Server.log ("UserManager.checkUsers: touching user", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
cu.touch ();
}
if (cuTimeout < lowestValue)
lowestValue = cuTimeout;
return lowestValue;
}
StringBuffer tsb = new StringBuffer ("UserManager.checkUsersForValidity: User timed out ").append (cu.getName ());
Server.log (tsb.toString (), Server.MSG_STATE, Server.LVL_MAJOR);
MessageParser mp = new MessageParser ();
mp.setMessageTemplate("message.q");
mp.setSender(cu);
cu.sendMessage (mp);
return lowestValue;
}
/**
* interface MessageDestination
*/
public synchronized void sendMessage (IContainer mc) {
for (Iterator i = users (); i.hasNext (); ) {
User cu = (User) i.next ();
cu.sendMessage (mc);
}
}
public Enumeration onlineVips () {
return onlineVips.elements();
}
public Iterator users () {
return usrCookie.values ().iterator ();
}
public void scheduleAction (short action, long startTime, User usr, User sender) {
ScheduledAction sa = new ScheduledAction (action, startTime, usr, sender);
if (schedule.contains (sa))
return;
schedule.addElement (sa);
}
public int getHighWaterMark () {
return highWaterMark;
}
public void updateVips (Vector nVips) {
Vector removed = (Vector) vips.clone();
removed.removeAll(nVips);
Vector added = (Vector) nVips.clone();
added.removeAll(vips);
UserManager umgr = UserManager.mgr;
for (Enumeration e = added.elements(); e.hasMoreElements(); ) {
String uname = (String) e.nextElement();
User cu = umgr.getUserByName(uname);
if (cu != null)
cu.setPermission(IUserRights.ROLE_VIP);
vips.addElement(uname);
}
for (Enumeration e = removed.elements(); e.hasMoreElements();) {
String uname = (String) e.nextElement();
User cu = umgr.getUserByName(uname);
if (cu != null)
cu.setPermission(IUserRights.ROLE_USER);
while (vips.contains(uname))
vips.removeElement(uname);
}
}
public void updateModerators (Vector nMod) {
Vector removed = (Vector) moderators.clone();
removed.removeAll(nMod);
Vector added = (Vector) nMod.clone();
added.removeAll(moderators);
UserManager umgr = UserManager.mgr;
for (Enumeration e = added.elements(); e.hasMoreElements(); ) {
String uname = (String) e.nextElement();
User cu = umgr.getUserByName(uname);
if (cu != null)
cu.setPermission(IUserRights.ROLE_VIP);
moderators.addElement(uname);
}
for (Enumeration e = removed.elements(); e.hasMoreElements();) {
String uname = (String) e.nextElement();
User cu = umgr.getUserByName(uname);
if (cu != null)
cu.setPermission(IUserRights.ROLE_USER);
while (moderators.contains(uname))
moderators.removeElement(uname);
}
}
public Enumeration vips () {
return vips.elements();
}
public Enumeration moderators () {
return moderators.elements();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -