From 93f031deaf3e17b6de44dfd56d4515a1cfcb6044 Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期日, 12 十二月 2021 22:32:47 +0800 Subject: [PATCH] 默认发数据主网关 --- HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java | 185 ++++++++++++++++++++++++++++++++-------------- 1 files changed, 128 insertions(+), 57 deletions(-) diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java index 95ac400..ed94f66 100644 --- a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java +++ b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java @@ -1,6 +1,5 @@ package com.hdl.sdk.connect.socket; -import android.net.ipsec.ike.TunnelModeChildSessionParams; import android.text.TextUtils; import com.google.gson.Gson; @@ -12,6 +11,7 @@ import com.hdl.sdk.common.exception.HDLLinkException; import com.hdl.sdk.common.utils.LogUtils; import com.hdl.sdk.common.utils.ThreadToolUtils; +import com.hdl.sdk.connect.HDLLink; import com.hdl.sdk.connect.bean.response.AuthenticateResponse; import com.hdl.sdk.connect.bean.response.NetworkAccessBroadcastResponse; import com.hdl.sdk.connect.callback.BaseCallBack; @@ -30,15 +30,13 @@ import com.hdl.sdk.connect.bean.LinkResponse; import com.hdl.sdk.connect.protocol.LinkMessageDecoder; import com.hdl.sdk.connect.protocol.LinkMessageEncoder; -import com.hdl.sdk.socket.SocketBoot; -import com.hdl.sdk.socket.SocketOptions; import com.hdl.sdk.socket.client.UdpClient; import com.hdl.sdk.socket.codec.MessagePipeLine; -import com.hdl.sdk.socket.listener.SendListener; +import com.hdl.sdk.socket.udp.UdpSocketBoot; +import com.hdl.sdk.socket.udp.UdpSocketOptions; -import java.io.UnsupportedEncodingException; +import java.net.InetSocketAddress; import java.util.List; -import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -47,7 +45,7 @@ /** * Created by jlchen on 11/11/21. * - * @Description : HDLAuthSocket + * @Description : HDLAuthSocket 鐢变簬鍓嶆湡宸茬粡鍛藉悕濂斤紝涓嶅仛鏇存敼锛屽彲鐢ㄤ綔Udp鏈嶅姟绔娇鐢� */ public class HDLAuthSocket { private static final String TAG = "HDLAuth"; @@ -55,7 +53,10 @@ * udp榛樿绔彛 */ private static final int UDP_PORT = 8585; - private static SocketBoot updBoot; + /** + * 鍥犱负鑰冭檻鍒颁娇鐢ㄤ竴涓鍙o紝瑕佹敮鎸佹帴鏀跺缃戝叧鐨勬暟鎹紝鎵�浠ュ彧鍏佽浣跨敤涓�涓� + */ + private static UdpSocketBoot udpSocketBoot; // private EventListener authEvent; //鎼滅储缃戝叧 private EventListener searchGatewayEvent; @@ -71,6 +72,7 @@ private HDLAuthSocket() { initSearchGatewayEvent(); + initListenerGatewayEvent(); } // public interface CallBack extends BaseCallBack { @@ -87,28 +89,39 @@ synchronized (HDLLinkConfig.class) { if (instance == null) { instance = new HDLAuthSocket(); + //1.鍚姩Socket 寮�鍚洃鍚� + instance.getUdpBoot(); } } } return instance; } - private SocketOptions getUdpOptions() { - final SocketOptions options = new SocketOptions(); + private UdpSocketOptions getUdpOptions() { + final UdpSocketOptions options = new UdpSocketOptions(); final MessagePipeLine pipeLine = new MessagePipeLine(); pipeLine.add(new LinkMessageDecoder()); pipeLine.add(new LinkMessageEncoder()); options.setHandleMessage(pipeLine); - options.setEnabledHeartbeat(false); return options; } - private SocketBoot getUdpBoot(String ip) { - if (updBoot == null) { - updBoot = UdpClient.init(ip, UDP_PORT, getUdpOptions()); - updBoot.connect(); + + /** + * 鑾峰彇褰撳墠udp瀵硅薄锛屽鏋滀笉瀛樺湪灏卞垱寤� + * @return 杩斿洖褰撳墠瀵硅薄 + */ + private UdpSocketBoot getUdpBoot() { + try { + if (udpSocketBoot == null) { + udpSocketBoot = UdpClient.init(UDP_PORT, getUdpOptions()); + udpSocketBoot.bind(); + } + } catch (Exception e) { + return null; } - return updBoot; + + return udpSocketBoot; } /** @@ -119,8 +132,6 @@ */ public void startAuthenticateRequest(AuthenticateRequest request, HDLLinkCallBack callBack) { HDLLinkConfig.getInstance().clearConfig(); - //1.鍚姩Socket 寮�鍚洃鍚� - getUdpBoot(IpUtils.getBroadcastAddress()); //2.鏋勫缓鐩戝惉Listener // authEvent = //3.鐩戝惉缃戝叧骞挎挱鐨勫叆缃戞寚浠� @@ -166,24 +177,35 @@ String requestStr = gs.toJson(request); LinkRequest message = new LinkRequest(topic, requestStr); - HdlSocketHelper.send(getUdpBoot(ip), message, new HdlSocketHelper.HdlSocketListener() { + HdlSocketHelper.send(getUdpBoot(), InetSocketAddress.createUnresolved(ip,UDP_PORT), message, new HdlSocketHelper.HdlSocketListener() { @Override public void onSucceed(Object msg) { if (callBack == null) return; - AuthenticateResponse bean = getAuthenticateResponseBean(msg); - if (bean != null) { - String localSecret = bean.getAuth().getLocalSecret(); - String gatewayId = bean.getObjects().getGatewayID(); - String ipAddress = bean.getObjects().getIPAddress(); - //鍒ゆ柇缃戝叧鏄惁宸茬粡娉ㄥ唽鍒颁簯绔� - if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) { - //璁よ瘉澶辫触锛岀綉鍏虫湭娉ㄥ唽鍒颁簯绔� - callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED)); + try { + AuthenticateResponse bean = getAuthenticateResponseBean(msg); + if (bean != null) { + String localSecret = ""; + String gatewayId = ""; + String ipAddress = ""; + if(bean.getAuth() != null) { + localSecret = bean.getAuth().getLocalSecret(); + } + if(bean.getObjects() != null){ + gatewayId = bean.getObjects().getGatewayID(); + ipAddress = bean.getObjects().getIPAddress(); + } + //鍒ゆ柇缃戝叧鏄惁宸茬粡娉ㄥ唽鍒颁簯绔� + if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) { + //璁よ瘉澶辫触锛岀綉鍏虫湭娉ㄥ唽鍒颁簯绔� + callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED)); + } else { + HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress); + callBack.onSuccess("璁よ瘉鎴愬姛"); + } } else { - HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress); - callBack.onSuccess("璁よ瘉鎴愬姛"); + callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR)); } - } else { + } catch (Exception e) { callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR)); } } @@ -271,7 +293,7 @@ * @param callBack 鍥炶皟 */ public void searchGatewayMulticast(SearchGatewayCallBack callBack) { - searchGateway(HDLLinkConfig.getInstance().getGatewayId(), UDP_GROUP_IP, callBack); + searchGateway(HDLLinkConfig.getInstance().getGatewayId(), InetSocketAddress.createUnresolved(UDP_GROUP_IP,UDP_PORT), callBack); } /** @@ -281,10 +303,8 @@ */ public void searchGatewayBroadcast(SearchGatewayCallBack callBack) { String ip = IpUtils.getBroadcastAddress(); - searchGateway(HDLLinkConfig.getInstance().getGatewayId(), ip, callBack); + searchGateway(HDLLinkConfig.getInstance().getGatewayId(), InetSocketAddress.createUnresolved(ip,UDP_PORT), callBack); } - - /** @@ -301,7 +321,7 @@ GsonConvert.getGson().toJson(data)); String ip = IpUtils.getBroadcastAddress(); - HdlSocketHelper.send(getUdpBoot(ip), message, new HdlSocketHelper.HdlSocketListener() { + HdlSocketHelper.send(getUdpBoot(), InetSocketAddress.createUnresolved(ip,UDP_PORT), message, new HdlSocketHelper.HdlSocketListener() { @Override public void onSucceed(Object msg) { if (callBack == null) return; @@ -333,8 +353,8 @@ return; } LinkRequest message = new LinkRequest(topic, bodyStr); - String ip = IpUtils.getBroadcastAddress(); - HdlSocketHelper.send(getUdpBoot(ip), message, new HdlSocketHelper.HdlSocketListener() { + String ip = HDLLinkConfig.getInstance().getIpAddress(); IpUtils.getBroadcastAddress(); + HdlSocketHelper.send(getUdpBoot(), InetSocketAddress.createUnresolved(ip,UDP_PORT), message, new HdlSocketHelper.HdlSocketListener() { @Override public void onSucceed(Object msg) { if (callBack == null) return; @@ -350,6 +370,21 @@ ); } + /** + * 閫氱敤鍙戦�佹寚浠� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙� + * + * @param topic 鍙戦�佹暟鎹� + * @param bodyStr 鍥炲鐨勪富棰� + */ + public void udpSendMsg(String topic, String bodyStr) { + if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) { + LogUtils.e("udpSendMsg", "鍙傛暟涓嶈兘涓虹┖"); + return; + } + LinkRequest message = new LinkRequest(topic, bodyStr); + String ip = HDLLinkConfig.getInstance().getIpAddress();// IpUtils.getBroadcastAddress(); + getUdpBoot().sendMsg( InetSocketAddress.createUnresolved(ip,UDP_PORT),message.getSendBytes()); + } private GatewaySearchBean getGatewaySearchBean(Object msg) { GatewaySearchBean searchBean = null; @@ -399,13 +434,15 @@ /** * 缃戝叧鎼滅储鐩稿叧 */ - private static final int MAX_SEARCH_COUNT = 10;//鎬诲叡鎼滅储娴嬭瘯 - private final AtomicInteger searchGatewayCount = new AtomicInteger(0);; + private static final int MAX_SEARCH_COUNT = 10;//鎬诲叡鎼滅储娴嬭瘯 + private final AtomicInteger searchGatewayCount = new AtomicInteger(0); + ; private final AtomicBoolean isSearchGatewaySuccess = new AtomicBoolean(true); private String searchGatewayId = ""; private SearchGatewayCallBack mSearchGatewayCallBack; - private void initSearchGatewayEvent(){ - LogUtils.i("鎼滅储缃戝叧--","initSearchGatewayEvent"); + + private void initSearchGatewayEvent() { + LogUtils.i("鎼滅储缃戝叧", "initSearchGatewayEvent"); searchGatewayEvent = new EventListener() { @Override public void onMessage(Object msg) { @@ -413,7 +450,6 @@ if (msg instanceof LinkResponse) { LinkResponse linkResponse = (LinkResponse) msg; String data = linkResponse.getData(); - LogUtils.i("鎼滅储鍒扮綉鍏�->" + data); if (!TextUtils.isEmpty(data)) { final BaseLocalResponse<GatewaySearchBean> response = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewaySearchBean>>() { }.getType()); @@ -425,7 +461,7 @@ searchGatewayCount.set(11);//娆℃暟鏍囪 HDLLinkConfig.getInstance().setCurrentGateway(searchBean);//璁剧疆褰撳墠缃戝叧 HDLLinkConfig.getInstance().setLocalEncrypt(searchBean.isLocalEncrypt());//璁剧疆鏄惁鍔犲瘑 - if(mSearchGatewayCallBack != null){ + if (mSearchGatewayCallBack != null) { mSearchGatewayCallBack.onSuccess(searchBean); } } @@ -439,14 +475,48 @@ }; } + private void initListenerGatewayEvent() { + LogUtils.i( "鍒濆鍖栨案涔呯洃鍚綉鍏冲箍鎾簨浠讹紝涓嶇敤绉婚櫎姝や簨浠�"); + EventListener gatewayEvent = new EventListener() { + @Override + public void onMessage(Object msg) { + try { + if (msg instanceof LinkResponse) { + LinkResponse linkResponse = (LinkResponse) msg; + String data = linkResponse.getData(); + LogUtils.i("鎺ユ敹鍒扮綉鍏充俊鎭細" + data); + if (!TextUtils.isEmpty(data)) { + final BaseLocalResponse<GatewaySearchBean> response = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewaySearchBean>>() { + }.getType()); + GatewaySearchBean gateway = response.getObjects(); + if (gateway != null && !TextUtils.isEmpty(gateway.getGatewayId())) { + //涓荤綉鍏冲苟涓旀槸褰撳墠缁戝畾鐨勭綉鍏� + if ("true".equals(gateway.getMaster().toLowerCase()) && gateway.getGatewayId().equals(HDLLinkConfig.getInstance().getGatewayId())) { + HDLLinkConfig.getInstance().setCurrentGateway(gateway);//璁剧疆褰撳墠缃戝叧 + HDLLinkConfig.getInstance().setLocalEncrypt(gateway.isLocalEncrypt());//璁剧疆鏄惁鍔犲瘑 + HDLLinkConfig.getInstance().setIpAddress(gateway.getIp_address()); + //鏇存柊褰撳墠缃戝叧鐨勪俊鎭� + HDLLinkConfig.getInstance().reSaveConfig(); + } + } + } + } + } catch (Exception e) { + + } + } + }; + EventDispatcher.getInstance().registerIo(TopicConstant.GATEWAY_SEARCH_REPLY, gatewayEvent); + } + /** * 鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎锛屾悳绱㈠埌鍒欒繑鍥炴寚瀹氱殑缃戝叧瀵硅薄 * * @param gatewayId 缃戝叧id - * @param ip 鎺ユ敹鐩爣鐨刬p鍦板潃 + * @param desInetSocketAddress 鐩爣鐨勫湴鍧� * @param callBack 鍥炶皟 */ - public void searchGateway(String gatewayId, String ip, SearchGatewayCallBack callBack) { + public void searchGateway(String gatewayId, InetSocketAddress desInetSocketAddress, SearchGatewayCallBack callBack) { this.searchGatewayId = gatewayId; this.mSearchGatewayCallBack = callBack; //閲嶇疆鍙傛暟 @@ -463,22 +533,22 @@ new Thread(new Runnable() { @Override public void run() { - while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get()) ) { + while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get())) { try { //鎼滅储缃戝叧 searchGatewayCount.set(searchGatewayCount.get() + 1); - LogUtils.i("鎼滅储缃戝叧--","鎼滅储缃戝叧绗�"+searchGatewayCount.get()+"娆�"); - getUdpBoot(ip).sendMsg(message.getSendBytes()); + LogUtils.i("鎼滅储缃戝叧", "鎼滅储缃戝叧绗�" + searchGatewayCount.get() + "娆�"); + getUdpBoot().sendMsg(desInetSocketAddress,message.getSendBytes()); Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } } - if(!isSearchGatewaySuccess.get()){ + if (!isSearchGatewaySuccess.get()) { //鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃� callBackSearchGatewayTimeout(); - LogUtils.e("鎼滅储缃戝叧--","鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�"); + LogUtils.e("鎼滅储缃戝叧--", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�"); } } }).start(); @@ -488,27 +558,28 @@ /** * 娉ㄥ唽鎼滅储缃戝叧鐩戝惉 */ - private void registerSearchGatewayEvent(){ - LogUtils.i("鎼滅储缃戝叧--","娉ㄥ唽鎼滅储缃戝叧鐩戝惉"); + private void registerSearchGatewayEvent() { + LogUtils.i("鎼滅储缃戝叧", "娉ㄥ唽鎼滅储缃戝叧鐩戝惉"); EventDispatcher.getInstance().registerIo(TopicConstant.GATEWAY_SEARCH_REPLY, searchGatewayEvent); } + /** * 绉婚櫎鎼滅储缃戝叧鐩戝惉 */ - private void removeSearchGatewayEvent(){ - LogUtils.i("鎼滅储缃戝叧--","绉婚櫎鎼滅储缃戝叧鐩戝惉"); + private void removeSearchGatewayEvent() { + LogUtils.i("鎼滅储缃戝叧", "绉婚櫎鎼滅储缃戝叧鐩戝惉"); EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_SEARCH_REPLY, searchGatewayEvent); } /** * 鍥炶皟鎼滅储缃戝叧瓒呮椂 */ - private void callBackSearchGatewayTimeout(){ + private void callBackSearchGatewayTimeout() { removeSearchGatewayEvent(); ThreadToolUtils.getInstance().runOnUiThread(new Runnable() { @Override public void run() { - if(mSearchGatewayCallBack != null){ + if (mSearchGatewayCallBack != null) { mSearchGatewayCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEARCH_GATEWAY_TIMEOUT_ERROR)); } } -- Gitblit v1.8.0