hxb
2022-09-08 2a01ef5e49422cca49bc7476fc1b8be8c8556561
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
package com.hdl.sdk.link.core.connect;
 
 
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.hdl.sdk.link.bean.LinkRoomBean;
import com.hdl.sdk.link.common.config.TopicConstant;
import com.hdl.sdk.link.common.event.EventDispatcher;
import com.hdl.sdk.link.common.event.EventListener;
import com.hdl.sdk.link.common.utils.IdUtils;
import com.hdl.sdk.link.common.utils.LogUtils;
import com.hdl.sdk.link.common.utils.TextUtils;
import com.hdl.sdk.link.common.utils.ThreadToolUtils;
import com.hdl.sdk.link.core.bean.LinkRequest;
import com.hdl.sdk.link.core.bean.LinkResponse;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
import com.hdl.sdk.link.core.bean.response.BaseLocalResponse;
import com.hdl.sdk.link.core.utils.EncryptUtil;
import com.hdl.sdk.link.core.utils.LinkResponseUtils;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
 
 
import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * Created by Tong on 2021/11/11.
 */
public class HDLConnectHelper {
 
    private static final Long DEF_SEND_TIMEOUT = 8000L;
    private static final int DEF_MAX_RETRY = 1;//最大重发数
    private static final int DEF_SEND_ONE = 1;
    private static final int TCP_PORT = 8586;
    private static final int UDP_PORT = 8585;
 
    private final Long sendAwaitTime;
    private final int maxRetry;
 
    /**
     * 是否tcp发送类型
     */
    private boolean isTcp;
    /**
     * 发送的目标IP
     */
    private String ipAddress;
    /**
     * 发送的目标地址
     */
    private int port;
    private final LinkRequest linkRequest;
    private final EventListener eventListener;
 
    private final AtomicInteger sendNumber = new AtomicInteger(0);
 
    private final AtomicBoolean isSend = new AtomicBoolean(false);
 
    private HdlSocketListener listener;
 
    private ScheduledExecutorService sendThread;
 
    private String observeTopic;
 
    public interface HdlSocketListener {
        void onSucceed(Object msg);
 
        void onFailure();
    }
 
    /**
     * 发送UDP或者TCP数据
     *
     * @param sendAwaitTime 每次发送等待时间
     * @param maxRetry      重试次数
     * @param ipAddress     发送目标IP
     * @param port          发送目标端口
     * @param linkRequest   发送对象
     * @param observeTopic  接收主题
     * @param listener      回调
     * @param isTcp         是否TCP
     */
    public HDLConnectHelper(Long sendAwaitTime, int maxRetry, String ipAddress, int port,
                            LinkRequest linkRequest, String observeTopic, HdlSocketListener listener, boolean isTcp) {
        this.sendAwaitTime = sendAwaitTime;
        this.maxRetry = maxRetry;
        this.ipAddress = ipAddress;
        this.port = port;
        this.linkRequest = linkRequest;
        this.observeTopic = observeTopic;
        this.listener = listener;
        this.isTcp = isTcp;
 
        eventListener = new EventListener() {
            @Override
            public void onMessage(Object msg) {
                try {
                    //移除监听
                    removeListener();
                    isSend.set(true);
                    if (sendThread != null) {
                        sendThread.shutdownNow();
                    }
 
                    if (listener != null && msg instanceof LinkResponse) {
                        LinkResponse linkResponse = (LinkResponse) msg;
                        JsonObject jsonObject = new JsonParser().parse(linkResponse.getData()).getAsJsonObject();
 
                        String code = null;
                        if (jsonObject.get("code") != null) {
                            code = jsonObject.get("code").getAsString();
                        }
                        /**
                         * 可能返回code属性可能没有   没有的话直接成功  有的话只有200才会成功
                         */
                        if (code == null || code.equals("200") || code.equals("0")) {
                            listener.onSucceed(msg);
                            return;
                        }
                    }
                } catch (Exception e) {
 
                }
                //上面没有正常执行,回调失败
                notifyFailure();
            }
        };
        //注册监听
        registerListener();
    }
 
    /**
     * 按照指定次数发,回调
     *
     * @param maxRetry     重试次数
     * @param ipAddress    发送目标IP
     * @param port         发送目标端口
     * @param linkRequest  发送对象
     * @param observeTopic 接收主题
     * @param listener     回调
     * @param isTcp        是否TCP
     */
    public HDLConnectHelper(int maxRetry, String ipAddress, int port,
                            LinkRequest linkRequest, String observeTopic, HdlSocketListener listener, boolean isTcp) {
        this(DEF_SEND_TIMEOUT, maxRetry, ipAddress, port, linkRequest, observeTopic, listener, isTcp);
    }
 
    /**
     * 按照指定次数发,回调
     *
     * @param maxRetry     重试次数
     * @param ipAddress    发送目标IP
     * @param linkRequest  发送对象
     * @param observeTopic 接收主题
     * @param listener     回调
     * @param isTcp        是否TCP
     */
    public HDLConnectHelper(int maxRetry, String ipAddress,
                            LinkRequest linkRequest, String observeTopic, HdlSocketListener listener, boolean isTcp) {
        this(maxRetry, ipAddress, isTcp ? TCP_PORT : UDP_PORT, linkRequest, observeTopic, listener, isTcp);
    }
 
    /**
     * 按照指定次数发,不回调
     *
     * @param maxRetry    重试次数
     * @param ipAddress   发送目标IP
     * @param linkRequest 发送对象
     * @param isTcp       是否TCP
     */
    public HDLConnectHelper(int maxRetry, String ipAddress,
                            LinkRequest linkRequest, boolean isTcp) {
        this(maxRetry, ipAddress, linkRequest, null, null, isTcp);
    }
 
    /**
     * 按照默认重发机制发送
     *
     * @param ipAddress    发送目标IP
     * @param port         发送目标端口
     * @param linkRequest  发送对象
     * @param observeTopic 接收主题
     * @param listener     回调
     * @param isTcp        是否TCP
     */
    public HDLConnectHelper(String ipAddress, int port,
                            LinkRequest linkRequest, String observeTopic, HdlSocketListener listener, boolean isTcp) {
        this(DEF_MAX_RETRY, ipAddress, port, linkRequest, observeTopic, listener, isTcp);
    }
 
    /**
     * 默认端口发送
     *
     * @param ipAddress    发送目标IP
     * @param linkRequest  发送对象
     * @param observeTopic 接收主题
     * @param listener     回调
     * @param isTcp        是否TCP
     */
    public HDLConnectHelper(String ipAddress,
                            LinkRequest linkRequest, String observeTopic, HdlSocketListener listener, boolean isTcp) {
        this(DEF_SEND_TIMEOUT, DEF_MAX_RETRY, ipAddress, isTcp ? TCP_PORT : UDP_PORT, linkRequest, observeTopic, listener, isTcp);
    }
 
    /**
     * 发送一次
     *
     * @param ipAddress   发送目标IP
     * @param linkRequest 发送对象
     * @param isTcp       是否TCP
     */
    public HDLConnectHelper(String ipAddress, LinkRequest linkRequest, boolean isTcp) {
        this(DEF_SEND_TIMEOUT, DEF_SEND_ONE, ipAddress, isTcp ? TCP_PORT : UDP_PORT, linkRequest, null, null, isTcp);
    }
 
    /**
     * 注册监听
     */
    private void registerListener() {
        if (!TextUtils.isEmpty(observeTopic) && null != listener) {
            EventDispatcher.getInstance().register(observeTopic, eventListener);
        }
    }
 
    /**
     * 移除监听
     */
    private void removeListener() {
        if (!TextUtils.isEmpty(observeTopic)) {
            EventDispatcher.getInstance().remove(observeTopic, eventListener);
        }
    }
 
    public void send() {
        getSendThread().scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                //发送次数小于重发次数
                if ((sendNumber.get() < maxRetry)) {
                    try {
                        //还没有收到回复,再发送
                        if (!isSend.get()) {
                            sendNumber.set(sendNumber.get() + 1);
                            if(!linkRequest.getTopic().endsWith("heartbeat")) {//心跳主题数据过多,过滤下
                                LogUtils.i("发送数据:\r\n" + new String(linkRequest.getSendBytes()));
                            }
                            //如是tcp
                            if (isTcp) {
                                HDLTcpConnect.getTcpSocketBoot(ipAddress).sendMsg(EncryptUtil.getEncryBytes(linkRequest));
                            } else {
                                //如果是udp
                                HDLUdpConnect.getInstance().getUdpBoot().sendMsg(ipAddress, port, EncryptUtil.getEncryBytes(linkRequest));
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    //超出重发次数并没有收到回复
                    if (!isSend.get()) {
                        notifyFailure();
                    }
                }
            }
        }, 0, sendAwaitTime, TimeUnit.MILLISECONDS);
        //initialdelay - 首次执行的延迟时间 0
        //delay - 一次执行终止和下一次执行开始之间的延迟
    }
 
 
    /**
     * 获取发送线程
     *
     * @return 返回获取到的线程
     */
    private ScheduledExecutorService getSendThread() {
        if (sendThread == null) {
            sendThread = ThreadToolUtils.getInstance().newScheduledThreadPool(1);
        }
        return sendThread;
    }
 
 
    /**
     * 发送失败
     */
    private void notifyFailure() {
        //移除监听
        removeListener();
        if (sendThread != null) {
            sendThread.shutdownNow();
            sendThread = null;
        }
        if (listener != null) {
            listener.onFailure();
            listener = null;
        }
    }
}