mPaaS baseline 10.1.68 and later supports the Mobile Scheduling Center (MDC). The MDC supports two connection types: native AAR and component-based (Portal&Bundle).
Prerequisites
If you use the native AAR connection type, complete the steps in Add mPaaS to your project.
If you use the component-based (Portal&Bundle) connection type, follow the Component-based connection flow.
Add the Mobile Scheduling SDK dependency
mPaaS supports the following two connection types. You can select one based on your project requirements.
Native AAR
Use Component Management (AAR) to install the Mobile Scheduling component in your project. For more information, see AAR component management.
Component-based (Portal&Bundle)
In the Portal and Bundle projects, install the Mobile Scheduling component using Component Management. For more information, see Connection flow.
Use the Mobile Scheduling SDK
Add AndroidManifest configurations
To use the SDK, add the group (dg) and host information for the MDC to the AndroidManifest file.
<!--Set the group name for the Mobile Scheduling Center-->
<meta-data
android:name="amdc.dg"
android:value="${MDC_DG}" />
<!--Set the host to the service domain name of the Mobile Scheduling Center-->
<meta-data
android:name="amdc.host"
android:value="${IP}" />
<!--Optional. A backup IP address used when the local DNS cache is polluted for the first time.-->
<meta-data android:name="amdc.backup"
android:value="${IP}" />Call the Mobile Scheduling Center API
The MPMDC class provides all APIs for the MDC. To call the MDC API, follow these steps:
After the mPaaS framework is initialized, you can call the
initmethod inApplication.onCreateto initialize DMC.void init(Context context);For example:
@Override public void onCreate() { super.onCreate(); MP.init(this, MPInitParam.obtain().setCallback(new MPInitParam.MPCallback() { @Override public void onInit() { MPMDC.init(App.this); } }) ); }Retrieve IP information.
MPHttpIp getIpsByHost(String host); // Get the IP information for the corresponding host. List<MPHttpIp> getAll(); // Get all IP configuration information for the Mobile Scheduling Center. class MPHttpIp { public MPHttpIpEntry[] ipEntries; // IP list public String host; // host public String ip; // Local IP } class MPHttpIpEntry { public static final int IP_TYPE_V4 = 4; public static final int IP_TYPE_V6 = 6; public String ip; // IP public int port; // Port public int ipType; // IP type, v4/v6 }Set the UID. The MDC uses the UID to differentiate user data and enable the grayscale feature.
MPLogger.setUserId(xxxx);
Enable the MDC feature for RPC
RPC uses the MDC configurations and provides APIs for your service. You can use the mPaaS switch configuration to dynamically control the MDC and IPv6 features.
When the MDC switch is enabled, RPC uses the MDC configurations. By default, this switch is enabled.
MPRpc.openMDC(isOpen);When the IPv6 switch is disabled, RPC filters out IPv6 addresses from the MDC and uses only IPv4 addresses. When the switch is enabled, both IPv4 and IPv6 addresses are used. By default, this switch is disabled.
MPRpc.openIPV6(isOpen);
Example of connecting to MDC separately
Establish a socket connection using an IP address
MPHttpIp httpIp = MPHttpIp.getIpsByHost(String host);
MPHttpIpEntry[] ipEntries = httpdnsIP.ipEntries;
String ips = new String[ipEntries.length];
for (int i = 0; i < httpdnsIPEntries.length; i++) {
ipArr[i] = httpdnsIPEntries[i].ip;
}
// Establish a socket connection.
// You can set the index for selectAddressIndex based on request failures.
Socket newSocket = new Socket(Proxy.NO_PROXY);
newSocket.connect(new InetSocketAddress(ips[selectAddressIndex], port));
// After establishing a connection using an IP address, you must also establish an SSL connection to access HTTPS.
// For SSL connections, see the sample SSL connection code below.
// httpUrlConnection and okhttp also require setting sslSocketFactory to handle HTTPS.
// To get the socketFactory, you can also see the code below. This is standard template code that you can adapt to your project.
// Use httpUrlConnection
URL url = new URL("http://ips[selectAddressIndex]/index.jsp");
// If it is HTTPS, you need to set this.
HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
// Use okhttp
Request request = new Request.Builder()
.get()
.url("http://ips[selectAddressIndex]/index.jsp")
.build();
OkHttpClient client = new OkHttpClient.Builder()
// If it is HTTPS, you need to set this.
.sslSocketFactory(sslSocketFactory).build();
Call call = client.newCall(request);Establish a connection using SSL
// Get socketFactory
SSLSocketFactory sslSocketFactory = createZSSLContext().getSocketFactory();
// SSL handshake
if (sslSocketFactory != null) {
SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, host, port, true);
sslSocket.setUseClientMode(true);
sslSocket.startHandshake();
Log.d(LOGTAG, "tls socket ssl handshake suc");
sslSocket.setSoTimeout(0);
return sslSocket;
}
public static final SSLContext createZSSLContext() throws NoSuchAlgorithmException, KeyManagementException {
final SSLContext sslContext = SSLContext.getInstance("TLS");
final X509KeyManager x509KeyManagers[] = {createDefaultKeyManager()};
final X509TrustManager x509TrustManagers[] = {new X509TrustManagerWrapper(createDefaultTrustManager())};
sslContext.init(x509KeyManagers, x509TrustManagers, new SecureRandom());
return sslContext;
}
public static final X509KeyManager createDefaultKeyManager() throws KeyManagementException {
try {
String defaultAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
KeyManagerFactory kmf = KeyManagerFactory.getInstance(defaultAlgorithm);
kmf.init(null, null);
return findX509KeyManager(kmf.getKeyManagers());
} catch (NoSuchAlgorithmException e) {
throw new KeyManagementException(e);
} catch (UnrecoverableKeyException e) {
throw new KeyManagementException(e);
} catch (KeyStoreException e) {
throw new KeyManagementException(e);
}
}
private static final X509KeyManager findX509KeyManager(KeyManager[] kms) throws KeyManagementException {
for (KeyManager km : kms) {
if (km instanceof X509KeyManager) {
return (X509KeyManager)km;
}
}
throw new KeyManagementException("Failed to find an X509KeyManager in "+ Arrays.toString(kms));
}
public static final X509TrustManager createDefaultTrustManager() throws KeyManagementException {
try {
String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(defaultAlgorithm);
tmf.init((KeyStore)null);
return findX509TrustManager(tmf.getTrustManagers());
} catch (NoSuchAlgorithmException e) {
throw new KeyManagementException(e);
} catch (KeyStoreException e) {
throw new KeyManagementException(e);
}
}
private static final X509TrustManager findX509TrustManager(TrustManager tms[]) throws KeyManagementException {
for (TrustManager tm : tms) {
if (tm instanceof X509TrustManager) {
return (X509TrustManager) tm;
}
}
throw new KeyManagementException("Failed to find an X509TrustManager in "+Arrays.toString(tms));
}
private static final class X509TrustManagerWrapper implements X509TrustManager {
private final X509TrustManager x509TrustManager;
public X509TrustManagerWrapper(X509TrustManager x509TrustManager) {
this.x509TrustManager = x509TrustManager;
}
@Override
public final void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
x509TrustManager.checkClientTrusted(chain, authType);
}
@Override
public final void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
int size = 0;
if (chain != null) {
size = chain.length;
}
try {
x509TrustManager.checkServerTrusted(chain, authType);
} catch (Throwable e) {
if (e instanceof CertificateException) {
throw (CertificateException) e;
} else {
throw new CertificateException(e);
}
}
}
@Override
public final X509Certificate[] getAcceptedIssuers() {
X509Certificate[] acceptedIssuers = x509TrustManager.getAcceptedIssuers();
return acceptedIssuers;
}
}