generated from root/miduo_server
服务器推荐
parent
ab008e8da1
commit
cd3409f779
|
@ -13,9 +13,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
public class GetServerListController extends HttpServlet {
|
public class GetServerListController extends HttpServlet {
|
||||||
|
@ -23,6 +21,7 @@ public class GetServerListController extends HttpServlet {
|
||||||
private final static String _WHITE_LIST = "white_list";
|
private final static String _WHITE_LIST = "white_list";
|
||||||
private static ConcurrentHashMap<String, DBObject> serverInfosCache = new ConcurrentHashMap<>();
|
private static ConcurrentHashMap<String, DBObject> serverInfosCache = new ConcurrentHashMap<>();
|
||||||
private static volatile DBObject whiteListCache = new BasicDBObject();
|
private static volatile DBObject whiteListCache = new BasicDBObject();
|
||||||
|
private static volatile String recommend = "";//推荐列表
|
||||||
private static volatile long lastRefreshTime = 0;
|
private static volatile long lastRefreshTime = 0;
|
||||||
|
|
||||||
public GetServerListController() {
|
public GetServerListController() {
|
||||||
|
@ -55,36 +54,55 @@ public class GetServerListController extends HttpServlet {
|
||||||
req.put("plat", plat);
|
req.put("plat", plat);
|
||||||
|
|
||||||
List<DBObject> serverList = new ArrayList<>();
|
List<DBObject> serverList = new ArrayList<>();
|
||||||
|
List<String> serverIDList = new ArrayList<>();
|
||||||
long local = System.currentTimeMillis();
|
long local = System.currentTimeMillis();
|
||||||
if ((local - lastRefreshTime) > 60 * 1000 || serverInfosCache.isEmpty()) {
|
if ((local - lastRefreshTime) > 60 * 1000 || serverInfosCache.isEmpty()) {
|
||||||
serverInfosCache.clear();
|
serverInfosCache.clear();
|
||||||
lastRefreshTime = local;
|
lastRefreshTime = local;
|
||||||
|
recommend = "";
|
||||||
whiteListCache = BaseGlobal.getInstance().mongoDBPool.findOne(_WHITE_LIST, openId);
|
whiteListCache = BaseGlobal.getInstance().mongoDBPool.findOne(_WHITE_LIST, openId);
|
||||||
List<DBObject> serverInfoList = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, req);
|
List<DBObject> serverInfoList = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, req);
|
||||||
|
SortedSet<String> newServerList = new TreeSet<>();
|
||||||
|
String maxNumServerId= "";
|
||||||
|
int maxNum = 0;
|
||||||
for (DBObject serverInfo : serverInfoList) {
|
for (DBObject serverInfo : serverInfoList) {
|
||||||
int state = Integer.parseInt(serverInfo.get("state").toString());
|
int state = Integer.parseInt(serverInfo.get("state").toString());
|
||||||
if (state == 0 || state == -1) {
|
if (state == 0 || state == -1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//opentime
|
||||||
long time = Long.parseLong(serverInfo.get("open_time").toString());
|
long time = Long.parseLong(serverInfo.get("open_time").toString());
|
||||||
if (System.currentTimeMillis() < time) {
|
if (System.currentTimeMillis() < time) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//state
|
||||||
|
String server_id=serverInfo.get("server_id").toString();
|
||||||
|
int num = getOnlineNum(server_id);
|
||||||
if (whiteListCache != null && state == 1) {
|
if (whiteListCache != null && state == 1) {
|
||||||
state = 2;
|
state = 2;
|
||||||
} else {
|
} else {
|
||||||
if (state == 5) {
|
if (state == 5) {
|
||||||
state = getState(serverInfo.get("server_id").toString());
|
state = getState(server_id,num);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(state==2&&maxNum<num){
|
||||||
|
maxNum = num;
|
||||||
|
maxNumServerId = server_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
//new
|
||||||
int isnew = 0;
|
int isnew = 0;
|
||||||
if (serverInfo.containsField("is_new"))
|
if (serverInfo.containsField("is_new"))
|
||||||
isnew = Integer.parseInt(serverInfo.get("is_new").toString());
|
isnew = Integer.parseInt(serverInfo.get("is_new").toString());
|
||||||
|
if(isnew==1){
|
||||||
|
newServerList.add(server_id);
|
||||||
|
}
|
||||||
|
|
||||||
DBObject res = new BasicDBObject();
|
DBObject res = new BasicDBObject();
|
||||||
res.put("name", serverInfo.get("name"));
|
res.put("name", serverInfo.get("name"));
|
||||||
res.put("server_id", serverInfo.get("server_id"));
|
res.put("server_id",server_id);
|
||||||
|
|
||||||
res.put("state", state);
|
res.put("state", state);
|
||||||
res.put("open_time", serverInfo.get("open_time"));
|
res.put("open_time", serverInfo.get("open_time"));
|
||||||
res.put("isnew", isnew);
|
res.put("isnew", isnew);
|
||||||
|
@ -97,8 +115,18 @@ public class GetServerListController extends HttpServlet {
|
||||||
res.put("ip", ip);
|
res.put("ip", ip);
|
||||||
res.put("port", port);
|
res.put("port", port);
|
||||||
serverList.add(res);
|
serverList.add(res);
|
||||||
|
serverIDList.add(server_id);
|
||||||
serverInfosCache.put(serverInfo.get("server_id").toString(), res);
|
serverInfosCache.put(serverInfo.get("server_id").toString(), res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//推荐服务器 新服中选择id小的 否者流畅中选择人数最多的
|
||||||
|
if (newServerList.size() != 0) {
|
||||||
|
recommend = newServerList.first();
|
||||||
|
} else {
|
||||||
|
recommend = maxNumServerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
serverList.addAll(serverInfosCache.values());
|
serverList.addAll(serverInfosCache.values());
|
||||||
}
|
}
|
||||||
|
@ -109,17 +137,25 @@ public class GetServerListController extends HttpServlet {
|
||||||
List<String> myServerList = new ArrayList<>();
|
List<String> myServerList = new ArrayList<>();
|
||||||
int time = 0;
|
int time = 0;
|
||||||
for (Map.Entry<String, UserRecentLoginInfo> entry : userServerInfoMap.entrySet()) {
|
for (Map.Entry<String, UserRecentLoginInfo> entry : userServerInfoMap.entrySet()) {
|
||||||
|
if(!serverIDList.contains(entry.getKey())){
|
||||||
|
//服务器已清除
|
||||||
|
continue;
|
||||||
|
}
|
||||||
myServerList.add(entry.getKey());
|
myServerList.add(entry.getKey());
|
||||||
if (entry.getValue().getLoginTime() > time) {
|
if (entry.getValue().getLoginTime() > time) {
|
||||||
myLastServer = getDBObject(entry.getKey(), entry.getValue());
|
myLastServer = getDBObject(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
DBObject resp = new BasicDBObject();
|
DBObject resp = new BasicDBObject();
|
||||||
resp.put("serverList", serverList);
|
resp.put("serverList", serverList);
|
||||||
resp.put("myServerList", myServerList);
|
resp.put("myServerList", myServerList);
|
||||||
if (null != myLastServer)
|
if (null != myLastServer)
|
||||||
resp.put("lastServer", myLastServer);
|
resp.put("lastServer", myLastServer);
|
||||||
|
if(recommend!=null&&!recommend.equals("")){
|
||||||
|
resp.put("recommend", recommend);
|
||||||
|
}
|
||||||
out.print(resp.toString());
|
out.print(resp.toString());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -143,8 +179,7 @@ public class GetServerListController extends HttpServlet {
|
||||||
this.doGet(request, response);
|
this.doGet(request, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getState(String server_id) throws Exception {
|
private int getState(String server_id,int num) throws Exception {
|
||||||
int num = getOnlineNum(server_id);
|
|
||||||
int state = 2;
|
int state = 2;
|
||||||
Map<String, String> globalCfgMap = BaseGlobal.getInstance().redisApp.hgetAll(RedisKey.GLOBAL_SYS_PRO, "", String.class, -1, false);
|
Map<String, String> globalCfgMap = BaseGlobal.getInstance().redisApp.hgetAll(RedisKey.GLOBAL_SYS_PRO, "", String.class, -1, false);
|
||||||
int crowdnum = Integer.valueOf(globalCfgMap.getOrDefault("crowdnum", "0"));
|
int crowdnum = Integer.valueOf(globalCfgMap.getOrDefault("crowdnum", "0"));
|
||||||
|
|
Loading…
Reference in New Issue