add new func in MogoHttpDns

This commit is contained in:
zhongchao
2021-07-28 16:08:37 +08:00
parent b9b8a863cc
commit 1775c5bdd7
5 changed files with 92 additions and 6 deletions

View File

@@ -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;

View File

@@ -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<String, String>? = null
private val ttlCallbackMap: MutableMap<String, MutableList<IHttpDnsTtlCallback>> =
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")

View File

@@ -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")

View File

@@ -0,0 +1,8 @@
package com.mogo.cloud.httpdns.listener
/**
* dns ttl 通知
*/
interface IHttpDnsTtlCallback {
fun onTtl()
}

View File

@@ -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<String, String>?
/**
* 监听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()
}