-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClient.java
More file actions
151 lines (135 loc) · 4.22 KB
/
Client.java
File metadata and controls
151 lines (135 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package rpc.component;
import org.json.JSONException;
import org.json.JSONObject;
import rpc.json.message.RpcResponse;
import rpc.util.RpcLog;
import rpc.util.RpcTools;
/**
* @author 23683
*
*/
public class Client extends RpcComponent {
public static final String NAME = "global";
private final boolean DEBUG = RpcTools.DEBUG;
private boolean mLogin = false;
private int mTimeout = 25;//one minute
private String mRealm = null;
private KeepAliveThread mKeepAliveThread = new KeepAliveThread();
public static final String METHOD_LOGIN = NAME+".login";
public static final String METHOD_LOGOUT = NAME+".logout";
private static final String METHOD_KEEPALIVE = NAME+".keepAlive";
public Client() {
}
@Override
public boolean instance(JSONObject params) {
// 全静态类,所有方法不依赖于object
return true;
}
@Override
public boolean destroy(JSONObject params) {
mKeepAliveThread.interrupt();
try {
mKeepAliveThread.join();
} catch (InterruptedException e) {
}
return true;
}
/**
*
* @param name
* @param password 未做过任何加密的原始明文密码
* @return
*/
public boolean login(String name, String password) {
try {
RpcResponse rsp = internalLogin(name, password);
if (rsp.isSuccessful()) {
mLogin = true;
mKeepAliveThread.setName("KeepAliveThread");
mKeepAliveThread.setDaemon(true); // 当Client销毁时,保活线程也同步销毁
mKeepAliveThread.start();
}
return rsp.isSuccessful();
} catch (JSONException e) {
RpcLog.w(NAME , "JSONException: login()");
}
return false;
}
public boolean logout(){
if(!mLogin){
return false;
}
try {
mKeepAliveThread.interrupt();
mKeepAliveThread.join();
} catch (InterruptedException e) {
}
RpcResponse result = static_call(METHOD_LOGOUT, null, DEFAULT_WAIT_TIME);
if (result == null || !result.isSuccessful()) {
RpcLog.e(TAG, "Logout failed\n");
}
return result.isSuccessful();
}
/**
*
* @param keepalive_timeout
* @return -1 means 保活失败。 其他,服务器返回的最大保活周期
*/
public int keepAlive(int keepalive_timeout) {
if (!mLogin){
return -1;
}
JSONObject params = new JSONObject();
try {
params.put("timeout", keepalive_timeout);
RpcResponse result = static_call(METHOD_KEEPALIVE, params, DEFAULT_WAIT_TIME);
if (result.isSuccessful()) {
return ((JSONObject)result.getResult()).getInt("timeout");
} else {
RpcLog.e(TAG, "keep alive failed");
return -1;
}
} catch (JSONException e) {
e.printStackTrace();
}
return -1;
}
private RpcResponse internalLogin(String name, String password) {
try {
JSONObject params = new JSONObject();
params.put("userName", name);
params.put("password", password);
params.put("ipAddr", "127.0.0.1");
RpcResponse result = static_call(METHOD_LOGIN, params, DEFAULT_WAIT_TIME);
return result;
} catch (JSONException e) {
RpcLog.w(NAME , "JSONException: login()");
}
return null;
}
private class KeepAliveThread extends Thread {
@Override
public void run() {
int timeout = 20; //客户端预想的保活时间,实际还是得取决于服务器返回的保活时间
while(mLogin) {
timeout = keepAlive(timeout);
if (timeout == -1) {
RpcLog.d(TAG, "keep alive failed, so session will be destroyed");
// FIXME::不直接在这里销毁session,而是由Session自己去发现保活失败
break;
}
try {
Thread.sleep(timeout * 1000/2 ); // 保证一个保活周期内必然有一个保活包
} catch (InterruptedException e) {
break;
}
}
}
}
@Override
protected void initMethods() {
mMethods.add(METHOD_KEEPALIVE);
mMethods.add(METHOD_LOGIN);
mMethods.add(METHOD_LOGOUT);
}
}