添加资源http-dns实现

This commit is contained in:
tongchenfei
2020-12-24 20:30:54 +08:00
parent 7653f28a54
commit a96dcf19e0
20 changed files with 338 additions and 103 deletions

View File

@@ -0,0 +1,26 @@
package com.mogo.httpdns.mogo;
import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.mogo.httpdns.mogo.test", appContext.getPackageName());
}
}

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mogo.httpdns.mogo">
</manifest>

View File

@@ -0,0 +1,123 @@
package com.mogo.httpdns.mogo;
import android.content.Context;
import android.text.TextUtils;
import android.util.ArrayMap;
import androidx.annotation.Nullable;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.mogo.commons.network.Utils;
import com.mogo.httpdns.HttpDnsConst;
import com.mogo.httpdns.IHttpDnsCallback;
import com.mogo.httpdns.IHttpDnsTtlCallback;
import com.mogo.httpdns.IMogoHttpDns;
import com.mogo.httpdnshelper.sdk.HttpDnsHelper;
import com.mogo.httpdnshelper.sdk.bean.HttpDnsSimpleLocation;
import com.mogo.httpdnshelper.sdk.listener.IHttpDnsConfig;
import com.mogo.httpdnshelper.sdk.listener.OnAddressChangedListener;
import com.mogo.map.location.MogoLocation;
import com.mogo.module.common.MogoApisHandler;
import com.mogo.utils.network.HttpDns;
import org.jetbrains.annotations.NotNull;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static com.mogo.httpdns.HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_HTTP;
@Route( path = HttpDnsConst.PATH )
public class MogoHttpDns implements IMogoHttpDns, HttpDns,OnAddressChangedListener{
private final HttpDnsHelper httpDnsHelper = new HttpDnsHelper();
@Nullable
@Override
public HttpDns dns() {
return this;
}
@Override
public String getCachedHttpDnsIps(String host, int type) {
return httpDnsHelper.getHttpDnsCachedAddress(type, host);
}
@Override
public void getHttpDnsIp(String host, int type, boolean useCache, IHttpDnsCallback callback) {
if(useCache) {
String address = httpDnsHelper.getHttpDnsCachedAddress(type, host);
if (address != null) {
callback.onParsed(address);
}else{
callback.onParsed(httpDnsHelper.getHttpDnsAddress(type, host));
}
}else {
callback.onParsed(httpDnsHelper.getHttpDnsAddress(type, host));
}
}
private final Map<String, IHttpDnsTtlCallback> ttlCallbackMap = new ArrayMap<>();
@Override
public void addHttpDnsTtlCallback(String host, int type, IHttpDnsTtlCallback callback) {
ttlCallbackMap.put(type + "-" + host, callback);
}
@Override
public void removeHttpDnsTtlCallback(String host, int type) {
ttlCallbackMap.remove(type + "-" + host);
}
@Override
public void init(final Context context) {
httpDnsHelper.init(context, new IHttpDnsConfig() {
@NotNull
@Override
public HttpDnsSimpleLocation getCurrentLocation() {
MogoLocation last = MogoApisHandler.getInstance().getApis().getMapServiceApi().getSingletonLocationClient(context).getLastKnowLocation();
if(last!=null){
return new HttpDnsSimpleLocation(last.getCityCode(), last.getLatitude(), last.getLongitude());
}
return null;
}
@Override
public boolean showDebugLog() {
return true;
}
@NotNull
@Override
public String getSn() {
return Utils.getSn();
}
});
httpDnsHelper.setAddressChangedListener(this);
}
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
String cacheIp = httpDnsHelper.getHttpDnsCachedAddress(HTTP_DNS_ADDRESS_TYPE_HTTP, hostname);
if ( cacheIp == null || TextUtils.isEmpty( cacheIp ) ) {
return Collections.emptyList();
}
String[] info = cacheIp.split(":");
if (info.length > 1) {
return Arrays.asList( InetAddress.getAllByName( info[0]) );
}else {
return Arrays.asList(InetAddress.getAllByName(cacheIp));
}
}
@Override
public void onAddressChanged(@org.jetbrains.annotations.Nullable Map<String, String> map) {
if (map != null) {
for (String key : map.keySet()) {
ttlCallbackMap.get(key).onTtl();
}
}
}
}

View File

@@ -0,0 +1,17 @@
package com.mogo.httpdns.mogo;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}