ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/framework/heartbeat/HeartBeat.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.framework.heartbeat; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import com.baidu.duer.dcs.http.HttpConfig; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import com.baidu.duer.dcs.http.HttpRequestInterface; |
| TMBOY | 45:2aa9f933c8d2 | 20 | import com.baidu.duer.dcs.http.callback.ResponseCallback; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import com.baidu.duer.dcs.util.LogUtil; |
| TMBOY | 45:2aa9f933c8d2 | 22 | |
| TMBOY | 45:2aa9f933c8d2 | 23 | import java.util.Timer; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import java.util.TimerTask; |
| TMBOY | 45:2aa9f933c8d2 | 25 | |
| TMBOY | 45:2aa9f933c8d2 | 26 | import okhttp3.Call; |
| TMBOY | 45:2aa9f933c8d2 | 27 | import okhttp3.Response; |
| TMBOY | 45:2aa9f933c8d2 | 28 | |
| TMBOY | 45:2aa9f933c8d2 | 29 | /** |
| TMBOY | 45:2aa9f933c8d2 | 30 | * 用于检测Directives 是否正常 |
| TMBOY | 45:2aa9f933c8d2 | 31 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 32 | * Created by guxiuzhong@baidu.com on 2017/6/17. |
| TMBOY | 45:2aa9f933c8d2 | 33 | */ |
| TMBOY | 45:2aa9f933c8d2 | 34 | public class HeartBeat { |
| TMBOY | 45:2aa9f933c8d2 | 35 | private static final String TAG = HeartBeat.class.getSimpleName(); |
| TMBOY | 45:2aa9f933c8d2 | 36 | // 设置ping成功后的(ping成功即代表directive请求成功了)请求的时间间隔 |
| TMBOY | 45:2aa9f933c8d2 | 37 | // 修改成270s原因:国内网关保持一个连接时间一般都是300s |
| TMBOY | 45:2aa9f933c8d2 | 38 | private static final long PING_TIME_SUCCEED = 30 * 1000; |
| TMBOY | 45:2aa9f933c8d2 | 39 | // 设置ping失败后的(ping失败即代表directive请求失败了)请求的时间间隔 |
| TMBOY | 45:2aa9f933c8d2 | 40 | private static final long PING_TIME_FAILED = 5 * 1000; |
| TMBOY | 45:2aa9f933c8d2 | 41 | private final HttpRequestInterface httpRequest; |
| TMBOY | 45:2aa9f933c8d2 | 42 | private Timer timer; |
| TMBOY | 45:2aa9f933c8d2 | 43 | private PingTask pingTask; |
| TMBOY | 45:2aa9f933c8d2 | 44 | private IHeartbeatListener listener; |
| TMBOY | 45:2aa9f933c8d2 | 45 | // 连接状态 |
| TMBOY | 45:2aa9f933c8d2 | 46 | private ConnectState connectState; |
| TMBOY | 45:2aa9f933c8d2 | 47 | |
| TMBOY | 45:2aa9f933c8d2 | 48 | private enum ConnectState { |
| TMBOY | 45:2aa9f933c8d2 | 49 | CONNECTED, |
| TMBOY | 45:2aa9f933c8d2 | 50 | UNCONNECTED |
| TMBOY | 45:2aa9f933c8d2 | 51 | } |
| TMBOY | 45:2aa9f933c8d2 | 52 | |
| TMBOY | 45:2aa9f933c8d2 | 53 | public HeartBeat(HttpRequestInterface httpRequest) { |
| TMBOY | 45:2aa9f933c8d2 | 54 | this.httpRequest = httpRequest; |
| TMBOY | 45:2aa9f933c8d2 | 55 | timer = new Timer(); |
| TMBOY | 45:2aa9f933c8d2 | 56 | } |
| TMBOY | 45:2aa9f933c8d2 | 57 | |
| TMBOY | 45:2aa9f933c8d2 | 58 | /** |
| TMBOY | 45:2aa9f933c8d2 | 59 | * 如果Directives连接成功了,调用此方法进行探测 |
| TMBOY | 45:2aa9f933c8d2 | 60 | */ |
| TMBOY | 45:2aa9f933c8d2 | 61 | public void startNormalPing() { |
| TMBOY | 45:2aa9f933c8d2 | 62 | connectState = ConnectState.CONNECTED; |
| TMBOY | 45:2aa9f933c8d2 | 63 | startPing(PING_TIME_SUCCEED, PING_TIME_SUCCEED); |
| TMBOY | 45:2aa9f933c8d2 | 64 | } |
| TMBOY | 45:2aa9f933c8d2 | 65 | |
| TMBOY | 45:2aa9f933c8d2 | 66 | /** |
| TMBOY | 45:2aa9f933c8d2 | 67 | * 如果Directives连接异常了,调用此方法进行探测 |
| TMBOY | 45:2aa9f933c8d2 | 68 | */ |
| TMBOY | 45:2aa9f933c8d2 | 69 | public void startExceptionalPing() { |
| TMBOY | 45:2aa9f933c8d2 | 70 | connectState = ConnectState.UNCONNECTED; |
| TMBOY | 45:2aa9f933c8d2 | 71 | startPing(PING_TIME_FAILED, PING_TIME_FAILED); |
| TMBOY | 45:2aa9f933c8d2 | 72 | } |
| TMBOY | 45:2aa9f933c8d2 | 73 | |
| TMBOY | 45:2aa9f933c8d2 | 74 | /** |
| TMBOY | 45:2aa9f933c8d2 | 75 | * 如果Events连接异常了,调用此方法进行探测 |
| TMBOY | 45:2aa9f933c8d2 | 76 | */ |
| TMBOY | 45:2aa9f933c8d2 | 77 | public void startImmediatePing() { |
| TMBOY | 45:2aa9f933c8d2 | 78 | connectState = ConnectState.UNCONNECTED; |
| TMBOY | 45:2aa9f933c8d2 | 79 | startPing(0, PING_TIME_FAILED); |
| TMBOY | 45:2aa9f933c8d2 | 80 | } |
| TMBOY | 45:2aa9f933c8d2 | 81 | |
| TMBOY | 45:2aa9f933c8d2 | 82 | private void fireOnConnect() { |
| TMBOY | 45:2aa9f933c8d2 | 83 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 84 | listener.onStartConnect(); |
| TMBOY | 45:2aa9f933c8d2 | 85 | } |
| TMBOY | 45:2aa9f933c8d2 | 86 | } |
| TMBOY | 45:2aa9f933c8d2 | 87 | |
| TMBOY | 45:2aa9f933c8d2 | 88 | private void startPing(long delay, long timeInterval) { |
| TMBOY | 45:2aa9f933c8d2 | 89 | if (pingTask != null) { |
| TMBOY | 45:2aa9f933c8d2 | 90 | pingTask.cancel(); |
| TMBOY | 45:2aa9f933c8d2 | 91 | } |
| TMBOY | 45:2aa9f933c8d2 | 92 | pingTask = new PingTask(); |
| TMBOY | 45:2aa9f933c8d2 | 93 | if (timer != null) { |
| TMBOY | 45:2aa9f933c8d2 | 94 | timer.schedule(pingTask, delay, timeInterval); |
| TMBOY | 45:2aa9f933c8d2 | 95 | } |
| TMBOY | 45:2aa9f933c8d2 | 96 | } |
| TMBOY | 45:2aa9f933c8d2 | 97 | |
| TMBOY | 45:2aa9f933c8d2 | 98 | /** |
| TMBOY | 45:2aa9f933c8d2 | 99 | * 停止ping探测,释放资源 |
| TMBOY | 45:2aa9f933c8d2 | 100 | */ |
| TMBOY | 45:2aa9f933c8d2 | 101 | public void release() { |
| TMBOY | 45:2aa9f933c8d2 | 102 | if (pingTask != null) { |
| TMBOY | 45:2aa9f933c8d2 | 103 | pingTask.cancel(); |
| TMBOY | 45:2aa9f933c8d2 | 104 | pingTask = null; |
| TMBOY | 45:2aa9f933c8d2 | 105 | } |
| TMBOY | 45:2aa9f933c8d2 | 106 | |
| TMBOY | 45:2aa9f933c8d2 | 107 | timer.cancel(); |
| TMBOY | 45:2aa9f933c8d2 | 108 | timer = null; |
| TMBOY | 45:2aa9f933c8d2 | 109 | httpRequest.cancelRequest(HttpConfig.HTTP_PING_TAG); |
| TMBOY | 45:2aa9f933c8d2 | 110 | } |
| TMBOY | 45:2aa9f933c8d2 | 111 | |
| TMBOY | 45:2aa9f933c8d2 | 112 | private void ping() { |
| TMBOY | 45:2aa9f933c8d2 | 113 | httpRequest.cancelRequest(HttpConfig.HTTP_PING_TAG); |
| TMBOY | 45:2aa9f933c8d2 | 114 | httpRequest.doGetPingAsync(null, new ResponseCallback() { |
| TMBOY | 45:2aa9f933c8d2 | 115 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 116 | public void onError(Call call, Exception e, int id) { |
| TMBOY | 45:2aa9f933c8d2 | 117 | super.onError(call, e, id); |
| TMBOY | 45:2aa9f933c8d2 | 118 | LogUtil.d(TAG, "ping onError"); |
| TMBOY | 45:2aa9f933c8d2 | 119 | connectState = ConnectState.UNCONNECTED; |
| TMBOY | 45:2aa9f933c8d2 | 120 | fireOnConnect(); |
| TMBOY | 45:2aa9f933c8d2 | 121 | } |
| TMBOY | 45:2aa9f933c8d2 | 122 | |
| TMBOY | 45:2aa9f933c8d2 | 123 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 124 | public void onResponse(Response response, int id) { |
| TMBOY | 45:2aa9f933c8d2 | 125 | super.onResponse(response, id); |
| TMBOY | 45:2aa9f933c8d2 | 126 | LogUtil.d(TAG, "ping onResponse, code :" + response.code()); |
| TMBOY | 45:2aa9f933c8d2 | 127 | if (response.code() == 200 || response.code() == 204) { |
| TMBOY | 45:2aa9f933c8d2 | 128 | if (connectState == ConnectState.UNCONNECTED) { |
| TMBOY | 45:2aa9f933c8d2 | 129 | fireOnConnect(); |
| TMBOY | 45:2aa9f933c8d2 | 130 | } |
| TMBOY | 45:2aa9f933c8d2 | 131 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 132 | connectState = ConnectState.UNCONNECTED; |
| TMBOY | 45:2aa9f933c8d2 | 133 | fireOnConnect(); |
| TMBOY | 45:2aa9f933c8d2 | 134 | } |
| TMBOY | 45:2aa9f933c8d2 | 135 | } |
| TMBOY | 45:2aa9f933c8d2 | 136 | }); |
| TMBOY | 45:2aa9f933c8d2 | 137 | } |
| TMBOY | 45:2aa9f933c8d2 | 138 | |
| TMBOY | 45:2aa9f933c8d2 | 139 | public void setHeartbeatListener(IHeartbeatListener listener) { |
| TMBOY | 45:2aa9f933c8d2 | 140 | this.listener = listener; |
| TMBOY | 45:2aa9f933c8d2 | 141 | } |
| TMBOY | 45:2aa9f933c8d2 | 142 | |
| TMBOY | 45:2aa9f933c8d2 | 143 | /** |
| TMBOY | 45:2aa9f933c8d2 | 144 | * ping 回调接口 |
| TMBOY | 45:2aa9f933c8d2 | 145 | */ |
| TMBOY | 45:2aa9f933c8d2 | 146 | public interface IHeartbeatListener { |
| TMBOY | 45:2aa9f933c8d2 | 147 | /** |
| TMBOY | 45:2aa9f933c8d2 | 148 | * 需要建立长连接时回调 |
| TMBOY | 45:2aa9f933c8d2 | 149 | */ |
| TMBOY | 45:2aa9f933c8d2 | 150 | void onStartConnect(); |
| TMBOY | 45:2aa9f933c8d2 | 151 | } |
| TMBOY | 45:2aa9f933c8d2 | 152 | |
| TMBOY | 45:2aa9f933c8d2 | 153 | private final class PingTask extends TimerTask { |
| TMBOY | 45:2aa9f933c8d2 | 154 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 155 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 156 | ping(); |
| TMBOY | 45:2aa9f933c8d2 | 157 | } |
| TMBOY | 45:2aa9f933c8d2 | 158 | } |
| TMBOY | 45:2aa9f933c8d2 | 159 | } |
