From 1775c5bdd745ea1bd22d451aaf78302ec7f0ebe8 Mon Sep 17 00:00:00 2001 From: zhongchao Date: Wed, 28 Jul 2021 16:08:37 +0800 Subject: [PATCH 1/7] add new func in MogoHttpDns --- .../java/com/mogo/cloud/MoGoApplication.java | 1 + .../com/mogo/cloud/httpdns/HttpDnsHelper.kt | 41 ++++++++++++++++--- .../mogo/cloud/httpdns/MogoHttpDnsClient.kt | 22 ++++++++++ .../httpdns/listener/IHttpDnsTtlCallback.kt | 8 ++++ .../cloud/httpdns/listener/IMogoHttpDns.kt | 26 ++++++++++++ 5 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/listener/IHttpDnsTtlCallback.kt diff --git a/app/src/main/java/com/mogo/cloud/MoGoApplication.java b/app/src/main/java/com/mogo/cloud/MoGoApplication.java index 84e53e8..c1814fa 100644 --- a/app/src/main/java/com/mogo/cloud/MoGoApplication.java +++ b/app/src/main/java/com/mogo/cloud/MoGoApplication.java @@ -6,6 +6,7 @@ import android.support.multidex.MultiDexApplication; import android.util.Log; import com.auto.zhidao.logsdk.CrashSystem; +import com.mogo.cloud.httpdns.MogoHttpDnsClient; import com.mogo.cloud.httpdns.MogoHttpDnsConfig; import com.mogo.cloud.httpdns.bean.HttpDnsSimpleLocation; import com.mogo.cloud.httpdns.listener.IHttpDnsCurrentLocation; diff --git a/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/HttpDnsHelper.kt b/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/HttpDnsHelper.kt index 917d86a..1804a83 100644 --- a/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/HttpDnsHelper.kt +++ b/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/HttpDnsHelper.kt @@ -4,13 +4,13 @@ import android.os.Handler import android.os.HandlerThread import android.os.Message import android.util.ArrayMap +import com.mogo.cloud.httpdns.listener.IHttpDnsTtlCallback import com.mogo.cloud.httpdns.listener.IMogoHttpDns import com.mogo.cloud.httpdns.listener.OnAddressChangedListener import com.mogo.cloud.httpdns.util.ApiManager import com.mogo.cloud.httpdns.util.L import com.mogo.cloud.httpdns.util.NetWorkUtil import java.util.* -import kotlin.collections.ArrayList /** * 1. 本地每15min查询一次 @@ -18,7 +18,8 @@ import kotlin.collections.ArrayList * * @author tongchenfei */ -internal class HttpDnsHelper(private val builder: MogoHttpDnsConfig) : Handler.Callback,IMogoHttpDns { +internal class HttpDnsHelper(private val builder: MogoHttpDnsConfig) : Handler.Callback, + IMogoHttpDns { companion object { const val HTTP_DNS_TYPE_HTTP = 0 const val HTTP_DNS_TYPE_WS = 1 @@ -34,8 +35,10 @@ internal class HttpDnsHelper(private val builder: MogoHttpDnsConfig) : Handler.C private val workThread = HandlerThread("mogo-http-dns-work-thread") private val handler: Handler private var addressMap: Map? = null + private val ttlCallbackMap: MutableMap> = + mutableMapOf() - var addressChangedListener: OnAddressChangedListener? = null + private var addressChangedListener: OnAddressChangedListener? = null private val apiManager: ApiManager private var isInit = false @@ -65,7 +68,6 @@ internal class HttpDnsHelper(private val builder: MogoHttpDnsConfig) : Handler.C } private fun getHttpDnsAddressFromNet() { - val nAddress = apiManager.requestHttpDns(builder.getCurrentLocation()!!) if (addressChangedListener == null) { L.d(TAG, "addressChangeList is null") @@ -86,15 +88,31 @@ internal class HttpDnsHelper(private val builder: MogoHttpDnsConfig) : Handler.C private val requestLock = Any() + override fun syncGetHttpDns(host: String, type: Int, useCache: Boolean): String? { + L.d(TAG, "syncGetHttpDns $type-$host") + return if (useCache) { + val address: String? = getHttpDnsCachedAddress(type, host) + address ?: getHttpDnsAddress(type, host) + } else { + getHttpDnsAddress(type, host) + } + } + override fun getHttpDnsAddress(type: Int, _host: String): String? { val host = _host.toLowerCase(Locale.getDefault()) if (isInit) { requestCache.add("$type-$host") synchronized(requestLock) { - L.d(TAG, "getHttpDnsAddress: $type-$host \n thread: ${Thread.currentThread().name} isRequest: $isRequest") + L.d( + TAG, + "getHttpDnsAddress: $type-$host \n thread: ${Thread.currentThread().name} isRequest: $isRequest" + ) if (!isRequest) { isRequest = true - L.d(TAG, "prepare to get http dns from net thread: ${Thread.currentThread().name}") + L.d( + TAG, + "prepare to get http dns from net thread: ${Thread.currentThread().name}" + ) getHttpDnsAddressFromNet() } } @@ -134,6 +152,17 @@ internal class HttpDnsHelper(private val builder: MogoHttpDnsConfig) : Handler.C return addressMap } + override fun addHttpDnsTtlCallback(host: String, type: Int, callback: IHttpDnsTtlCallback) { + if (ttlCallbackMap["$type-$host"] == null) { + ttlCallbackMap["$type-$host"] = mutableListOf() + } + ttlCallbackMap["$type-$host"]!!.add(callback) + } + + override fun removeHttpDnsTtlCallback(host: String, type: Int) { + ttlCallbackMap.remove("$type-$host") + } + override fun handleMessage(msg: Message): Boolean { if (msg.what == MSG_REQUEST_IP_PORT) { L.d(TAG, "http dns loop check") diff --git a/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/MogoHttpDnsClient.kt b/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/MogoHttpDnsClient.kt index a118de1..c5feafe 100644 --- a/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/MogoHttpDnsClient.kt +++ b/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/MogoHttpDnsClient.kt @@ -1,5 +1,6 @@ package com.mogo.cloud.httpdns +import com.mogo.cloud.httpdns.listener.IHttpDnsTtlCallback import com.mogo.cloud.httpdns.listener.IMogoHttpDns @@ -20,6 +21,13 @@ object MogoHttpDnsClient : IMogoHttpDns { return getHttpDnsCachedAddress(type, _host) ?: return getHttpDnsAddress(type, _host) } + override fun syncGetHttpDns(host: String, type: Int, useCache: Boolean): String? { + if (httpDnsHelper == null) { + throw IllegalStateException("MogoHttpDnsClient init error") + } + return httpDnsHelper!!.syncGetHttpDns(host, type, useCache) + } + override fun getHttpDnsAddress(type: Int, _host: String): String? { if (httpDnsHelper == null) { throw IllegalStateException("MogoHttpDnsClient init error") @@ -41,6 +49,20 @@ object MogoHttpDnsClient : IMogoHttpDns { return httpDnsHelper!!.getAllAddress() } + override fun addHttpDnsTtlCallback(host: String, type: Int, callback: IHttpDnsTtlCallback) { + if (httpDnsHelper == null) { + throw IllegalStateException("MogoHttpDnsClient init error") + } + return httpDnsHelper!!.addHttpDnsTtlCallback(host, type, callback) + } + + override fun removeHttpDnsTtlCallback(host: String, type: Int) { + if (httpDnsHelper == null) { + throw IllegalStateException("MogoHttpDnsClient init error") + } + return httpDnsHelper!!.removeHttpDnsTtlCallback(host, type) + } + override fun release() { if (httpDnsHelper == null) { throw IllegalStateException("MogoHttpDnsClient init error") diff --git a/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/listener/IHttpDnsTtlCallback.kt b/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/listener/IHttpDnsTtlCallback.kt new file mode 100644 index 0000000..7b55b76 --- /dev/null +++ b/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/listener/IHttpDnsTtlCallback.kt @@ -0,0 +1,8 @@ +package com.mogo.cloud.httpdns.listener + +/** + * dns ttl 通知 + */ +interface IHttpDnsTtlCallback { + fun onTtl() +} \ No newline at end of file diff --git a/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/listener/IMogoHttpDns.kt b/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/listener/IMogoHttpDns.kt index 75e45bc..78be437 100644 --- a/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/listener/IMogoHttpDns.kt +++ b/foudations/mogo-httpdns/src/main/java/com/mogo/cloud/httpdns/listener/IMogoHttpDns.kt @@ -3,6 +3,17 @@ package com.mogo.cloud.httpdns.listener import com.mogo.cloud.httpdns.HttpDnsHelper.Companion.HTTP_DNS_TYPE_HTTP interface IMogoHttpDns { + + /** + * 同步进行dns解析,无需回调 + * @param host + * @param type + * @param useCache + * + * @return ip:port + */ + fun syncGetHttpDns(host: String, type: Int, useCache: Boolean): String? + /** * 根据类型和host获取IP,直接通过网络请求获取全部路由表 * 同时多线程多次请求会忽略部分网络请求,一定程度减少接口请求次数 @@ -34,5 +45,20 @@ interface IMogoHttpDns { */ fun getAllAddress(): Map? + /** + * 监听ttl回调 + * @param host + * @param type + * @param callback + */ + fun addHttpDnsTtlCallback(host: String, type: Int, callback: IHttpDnsTtlCallback) + + /** + * 注销ttl回调 + * @param host + * @param type + */ + fun removeHttpDnsTtlCallback(host: String, type: Int) + fun release() } \ No newline at end of file From 8b97d4c2f6a83202b25cca9088ed7df34dd3b023 Mon Sep 17 00:00:00 2001 From: zhongchao Date: Wed, 28 Jul 2021 17:20:33 +0800 Subject: [PATCH 2/7] update all version and wait to public --- .idea/gradle.xml | 3 +-- .idea/misc.xml | 2 +- .idea/runConfigurations.xml | 13 ------------- gradle.properties | 22 +++++++++++----------- 4 files changed, 13 insertions(+), 27 deletions(-) delete mode 100644 .idea/runConfigurations.xml diff --git a/.idea/gradle.xml b/.idea/gradle.xml index e98503b..4347ce7 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -4,7 +4,7 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index 2887110..e936494 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -13,7 +13,7 @@