-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathClient.java
More file actions
442 lines (395 loc) · 14.6 KB
/
Client.java
File metadata and controls
442 lines (395 loc) · 14.6 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package com.lark.oapi.ws;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.lark.oapi.google.protobuf.ByteString;
import com.lark.oapi.core.enums.BaseUrlEnum;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.event.EventDispatcher;
import com.lark.oapi.okhttp.*;
import com.lark.oapi.ws.enums.FrameType;
import com.lark.oapi.ws.enums.MessageType;
import com.lark.oapi.ws.exception.ClientException;
import com.lark.oapi.ws.exception.HeaderNotFoundException;
import com.lark.oapi.ws.exception.ServerException;
import com.lark.oapi.ws.exception.ServerUnreachableException;
import com.lark.oapi.ws.model.ClientConfig;
import com.lark.oapi.ws.model.Endpoint;
import com.lark.oapi.ws.model.EndpointResp;
import com.lark.oapi.ws.pb.Pbbp2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static com.lark.oapi.ws.Constant.*;
public class Client {
private static final Logger log = LoggerFactory.getLogger(Client.class);
protected final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2 + 1);
protected Boolean autoReconnect;
protected WebSocket conn;
protected String connUrl;
protected volatile Boolean isReconnecting;
private String appId;
private String appSecret;
private EventDispatcher eventHandler;
private String domain;
private String serviceId;
private String connId;
private Integer reconnectNonce;
private Integer reconnectCount;
private Integer reconnectInterval;
private Integer pingInterval;
private OkHttpClient httpClient;
private Cache<String, byte[][]> cache;
private Client(Builder builder) {
this.appId = builder.appId;
this.appSecret = builder.appSecret;
this.eventHandler = builder.eventHandler;
this.autoReconnect = builder.autoReconnect != null ? builder.autoReconnect : true;
this.domain = builder.domain != null ? builder.domain : BaseUrlEnum.FeiShu.getUrl();
this.reconnectNonce = 30;
this.reconnectCount = -1;
this.reconnectInterval = 120;
this.pingInterval = 120;
this.httpClient = new OkHttpClient();
this.isReconnecting = false;
this.cache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build();
}
public void start() {
try {
this.connect();
} catch (ClientException e) {
log.error(e.toString());
throw e;
} catch (Throwable t) {
log.error(t.toString());
this.disconnect();
if (this.autoReconnect) {
this.reconnect();
}
}
this.executor.execute(this::pingLoop);
}
private void pingLoop() {
while (true) {
try {
if (this.conn != null) {
Pbbp2.Frame frame = newPingFrame(Integer.parseInt(this.serviceId));
this.conn.send(com.lark.oapi.okio.ByteString.of(frame.toByteArray()));
log.debug(fmtLog("ping success"));
}
} catch (Throwable t) {
log.warn(fmtLog("ping failed"), t);
} finally {
this.sleep(this.pingInterval * 1000);
}
}
}
protected synchronized void disconnect() {
try {
if (this.conn == null) {
return;
}
this.conn.close(1000, "client closed");
log.info(fmtLog("disconnected to %s", this.connUrl));
} finally {
this.conn = null;
this.connUrl = null;
this.connId = null;
this.serviceId = null;
}
}
protected void reconnect() {
this.isReconnecting = true;
try {
log.info("start reconnecting...");
// 首次重连随机抖动
if (this.reconnectNonce > 0) {
Random rand = new Random();
int nonce = rand.nextInt(this.reconnectNonce * 1000);
this.sleep(nonce);
}
// 重连
if (this.reconnectCount >= 0) {
for (int i = 0; i < this.reconnectCount; i++) {
if (this.conn != null) {
return;
}
if (tryConnect(i)) {
return;
}
this.sleep(this.reconnectInterval * 1000);
}
throw new ServerUnreachableException(String.format("unable to connect to the server after trying %d times", this.reconnectCount));
} else {
int i = 0;
while (true) {
if (this.conn != null) {
return;
}
if (tryConnect(i)) {
return;
}
this.sleep(this.reconnectInterval * 1000);
i++;
}
}
} finally {
this.isReconnecting = false;
}
}
private boolean tryConnect(int cnt) {
cnt++;
String time;
switch (cnt) {
case 1:
time = cnt + "st";
break;
case 2:
time = cnt + "nd";
break;
case 3:
time = cnt + "rd";
break;
default:
time = cnt + "th";
break;
}
log.info(fmtLog("trying to reconnect for the %s time", time));
try {
connect();
return true;
} catch (ClientException e) {
log.error(e.toString());
throw e;
} catch (Throwable t) {
log.error(t.toString());
return false;
}
}
private String getConnUrl() throws IOException {
String body = String.format("{\"AppID\": \"%s\", \"AppSecret\": \"%s\"}", this.appId, this.appSecret);
Request request = new Request.Builder()
.url(this.domain + GEN_ENDPOINT_URI)
.addHeader("locale", "zh")
.post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), body))
.build();
try (Response response = this.httpClient.newCall(request).execute()) {
if (response.code() != 200 || response.body() == null) {
throw new ServerException(response.code(), "system busy");
}
EndpointResp resp = Jsons.DEFAULT.fromJson(response.body().string(), EndpointResp.class);
if (resp.getCode() == OK) {
// do nothing
} else if (resp.getCode() == SYSTEM_BUSY) {
throw new ServerException(resp.getCode(), "system busy");
} else if (resp.getCode() == INTERNAL_ERROR) {
throw new ServerException(resp.getCode(), resp.getMsg());
} else {
throw new ClientException(resp.getCode(), resp.getMsg());
}
Endpoint data = resp.getData();
if (data.getClientConfig() != null) {
this.configure(data.getClientConfig());
}
return data.getUrl();
}
}
private synchronized void connect() throws IOException {
if (this.conn != null) {
return;
}
this.connUrl = this.getConnUrl();
HttpUrl u = parseUrl(this.connUrl);
this.connId = u.queryParameter(DEVICE_ID);
this.serviceId = u.queryParameter(SERVICE_ID);
Request request = new Request.Builder()
.url(connUrl)
.build();
this.httpClient.newWebSocket(request, new Listener(this));
}
protected void handleMessage(byte[] msg) {
try {
Pbbp2.Frame frame = Pbbp2.Frame.parseFrom(msg);
FrameType ft = FrameType.of(frame.getMethod());
switch (ft) {
case CONTROL:
handleControlFrame(frame);
break;
case DATA:
handleDataFrame(frame);
break;
}
} catch (Exception e) {
log.error(fmtLog("handle message failed"), e);
}
}
private void handleControlFrame(Pbbp2.Frame frame) {
List<Pbbp2.Header> hs = frame.getHeadersList();
MessageType mt = MessageType.of(getString(hs, HEADER_TYPE));
switch (mt) {
case PING:
return;
case PONG:
log.debug(fmtLog("receive pong"));
if (!frame.hasPayload()) {
return;
}
ClientConfig conf = Jsons.DEFAULT.fromJson(frame.getPayload().toStringUtf8(), ClientConfig.class);
configure(conf);
break;
}
}
private void handleDataFrame(Pbbp2.Frame frame) {
List<Pbbp2.Header> hs = frame.getHeadersList();
String msgId = getString(hs, HEADER_MESSAGE_ID);
String traceId = getString(hs, HEADER_TRACE_ID);
Integer sum = getInteger(hs, HEADER_SUM);
Integer seq = getInteger(hs, HEADER_SEQ);
String type = getString(hs, HEADER_TYPE);
byte[] pl = frame.getPayload().toByteArray();
if (sum > 1) {
// 合包
pl = combine(msgId, sum, seq, pl);
if (pl == null) {
return;
}
}
MessageType mt = MessageType.of(type);
log.debug(fmtLog("receive message, message_type: %s, message_id: %s, trace_id: %s, payload: %s",
mt.getName(), msgId, traceId, new String(pl, StandardCharsets.UTF_8)));
com.lark.oapi.ws.model.Response response = new com.lark.oapi.ws.model.Response(200);
long start = System.currentTimeMillis();
try {
switch (mt) {
case EVENT:
this.eventHandler.doWithoutValidation(pl);
break;
case CARD:
return;
}
} catch (Throwable e) {
log.error(fmtLog("handle message failed, message_type: %s, message_id: %s, trace_id: %s,",
mt.getName(), msgId, traceId), e);
response = new com.lark.oapi.ws.model.Response(500);
}
long end = System.currentTimeMillis();
byte[] resp = Jsons.DEFAULT.toJson(response).getBytes(StandardCharsets.UTF_8);
byte[] bs = frame.toBuilder()
.setPayload(ByteString.copyFrom(resp))
.addHeaders(Pbbp2.Header.newBuilder()
.setKey(HEADER_BIZ_RT)
.setValue(String.valueOf(end - start))
.build())
.build()
.toByteArray();
this.conn.send(com.lark.oapi.okio.ByteString.of(bs));
}
private byte[] combine(String msgId, int sum, int seq, byte[] bs) {
byte[][] val = this.cache.getIfPresent(msgId);
if (val == null) {
byte[][] buf = new byte[sum][];
buf[seq] = bs;
this.cache.put(msgId, buf);
return null;
}
val[seq] = bs;
ByteArrayOutputStream pl = new ByteArrayOutputStream();
for (byte[] v : val) {
if (v == null) {
this.cache.put(msgId, val);
return null;
}
try {
pl.write(v);
} catch (IOException ignored) {
}
}
return pl.toByteArray();
}
private void configure(ClientConfig conf) {
this.reconnectCount = conf.getReconnectCount();
this.reconnectInterval = conf.getReconnectInterval();
this.reconnectNonce = conf.getReconnectNonce();
this.pingInterval = conf.getPingInterval();
}
private HttpUrl parseUrl(String url) {
String httpUrl = url.replace("wss://", "https://").replace("ws://", "https://");
HttpUrl u = HttpUrl.parse(httpUrl);
if (u == null) {
throw new ServerException(500, "connect url is invalid");
}
return u;
}
private String getString(List<Pbbp2.Header> headers, String key) {
return headers.stream()
.filter(o -> o.getKey().equals(key))
.findFirst()
.map(Pbbp2.Header::getValue)
.orElseThrow(() -> new HeaderNotFoundException(key));
}
private Integer getInteger(List<Pbbp2.Header> headers, String key) {
return headers.stream()
.filter(o -> o.getKey().equals(key))
.findFirst()
.map(Pbbp2.Header::getValue)
.map(Integer::parseInt)
.orElseThrow(() -> new HeaderNotFoundException(key));
}
protected String fmtLog(String format, Object... args) {
String log = String.format(format, args);
if (this.connId != null) {
log += String.format(" [conn_id=%s]", this.connId);
}
return log;
}
private Pbbp2.Frame newPingFrame(int serviceId) {
return Pbbp2.Frame.newBuilder()
.setService(serviceId)
.setMethod(FrameType.CONTROL.getCode())
.addHeaders(Pbbp2.Header.newBuilder()
.setKey(HEADER_TYPE)
.setValue(MessageType.PING.getName())
.build())
.setSeqID(0)
.setLogID(0)
.build();
}
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ignored) {
}
}
public static class Builder {
private String appId;
private String appSecret;
private EventDispatcher eventHandler;
private Boolean autoReconnect;
private String domain;
public Builder(String appId, String appSecret) {
this.appId = appId;
this.appSecret = appSecret;
}
public Builder eventHandler(EventDispatcher eventHandler) {
this.eventHandler = eventHandler;
return this;
}
public Builder autoReconnect(Boolean autoReconnect) {
this.autoReconnect = autoReconnect;
return this;
}
public Builder domain(String domain) {
this.domain = domain;
return this;
}
public Client build() {
return new Client(this);
}
}
}