Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify IMLoginManager avoid duplicate server ip address be store in list #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions mgandroid-teamtalk/src/com/mogujie/tt/imlib/IMLoginManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -39,7 +40,7 @@ public static IMLoginManager instance() {
private String loginPwd;
private String loginId;
private SocketThread loginServerThread;
private List<String> msgServerAddrs;
private Set<String> msgServerAddrs;
private int msgServerPort;
private SocketThread msgServerThread;
// todo eric make it to ContactEntity too
Expand Down Expand Up @@ -92,7 +93,7 @@ public User getLoginUser() {
public boolean isDoingLogin() {
return currentStatus <= STATUS_LOGINING_MSG_SERVER;
}

public String getLoginId() {
return loginId;
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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<String>();
msgServerAddrs = new HashSet<String>();
}

msgServerAddrs.add(resp.getStrIp1());
Expand All @@ -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() {
Expand Down