From f82ef8df8cc8a1342648c29101401e37ae273e82 Mon Sep 17 00:00:00 2001 From: TimonWang Date: Thu, 30 Oct 2014 16:31:00 +0800 Subject: [PATCH] modify server List to server Set, avoid duplicated server ips --- .../com/mogujie/tt/imlib/IMLoginManager.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/mgandroid-teamtalk/src/com/mogujie/tt/imlib/IMLoginManager.java b/mgandroid-teamtalk/src/com/mogujie/tt/imlib/IMLoginManager.java index e8c7b37..1758b6d 100644 --- a/mgandroid-teamtalk/src/com/mogujie/tt/imlib/IMLoginManager.java +++ b/mgandroid-teamtalk/src/com/mogujie/tt/imlib/IMLoginManager.java @@ -1,7 +1,7 @@ package com.mogujie.tt.imlib; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import android.content.Intent; import android.os.Handler; @@ -20,6 +20,7 @@ import com.mogujie.tt.log.Logger; import com.mogujie.tt.packet.base.DataBuffer; import com.mogujie.tt.ui.utils.Md5Helper; +import com.mogujie.tt.utils.DateUtil; public class IMLoginManager extends IMManager { private static IMLoginManager inst; @@ -39,7 +40,7 @@ public static IMLoginManager instance() { private String loginPwd; private String loginId; private SocketThread loginServerThread; - private List msgServerAddrs; + private Set msgServerAddrs; private int msgServerPort; private SocketThread msgServerThread; // todo eric make it to ContactEntity too @@ -92,7 +93,7 @@ public User getLoginUser() { public boolean isDoingLogin() { return currentStatus <= STATUS_LOGINING_MSG_SERVER; } - + public String getLoginId() { return loginId; } @@ -108,19 +109,19 @@ public IMLoginManager() { public boolean relogin() { logger.d("login#relogin"); - + if (isDoingLogin()) { logger.d("login#isDoingLogin, no need"); return false; } - + if (loggined) { logger.d("login#already logined, no need"); return false; } - + connectLoginServer(); - + return true; } @@ -251,8 +252,9 @@ public void onRepMsgServerAddrs(DataBuffer buffer) { return; } + // use Set to store the result, avoid duplicated server address if (msgServerAddrs == null) { - msgServerAddrs = new ArrayList(); + msgServerAddrs = new HashSet(); } msgServerAddrs.add(resp.getStrIp1()); @@ -268,8 +270,9 @@ public void onRepMsgServerAddrs(DataBuffer buffer) { private String pickLoginServerIp() { // todo eric - // pick the second one right now - return msgServerAddrs.get(1); + // pick server based on current timestamp + int index = DateUtil.getCurTimeStamp() % msgServerAddrs.size(); + return msgServerAddrs.toArray(new String[0])[index]; } private void disconnectLoginServer() {