add socket apk version
This commit is contained in:
@@ -9,15 +9,10 @@ import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mogo.cloud.GsonUtil;
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClient;
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
|
||||
import com.mogo.cloud.socket.internal.InternalSocketManager;
|
||||
import com.mogo.cloud.socket.third.ThirdSocketManager;
|
||||
import com.mogo.cloud.utils.logger.Logger;
|
||||
import com.zhidao.locupload.Platform;
|
||||
import com.zhidao.ptech.connsvr.commom.protocol.MogoCommon;
|
||||
import com.zhidao.ptech.connsvr.protocol.MogoConnsvr;
|
||||
import com.zhidao.socket.Callback;
|
||||
import com.zhidao.socket.CallbackManager;
|
||||
import com.zhidao.socket.Environment;
|
||||
import com.zhidao.socket.SocketClient;
|
||||
import com.zhidao.socket.SocketConfig;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
@@ -25,23 +20,18 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static com.mogo.cloud.httpdns.MogoHttpDnsConfig.HTTP_DNS_ENV_DEMO;
|
||||
import static com.mogo.cloud.httpdns.MogoHttpDnsConfig.HTTP_DNS_ENV_DEV;
|
||||
import static com.mogo.cloud.httpdns.MogoHttpDnsConfig.HTTP_DNS_ENV_QA;
|
||||
import static com.mogo.cloud.httpdns.MogoHttpDnsConfig.HTTP_DNS_ENV_RELEASE;
|
||||
import static com.mogo.cloud.socket.SocketServicesConstants.TAG;
|
||||
|
||||
|
||||
/**
|
||||
* Socket 长链核心服务
|
||||
*/
|
||||
public class SocketManager implements IMogoCloudSocketManager, Callback {
|
||||
public class SocketManager implements IMogoCloudSocketManager {
|
||||
|
||||
private static volatile SocketManager mInstance;
|
||||
private MoGoAiCloudClientConfig cloudClientConfig;
|
||||
|
||||
private SocketManager() {
|
||||
CallbackManager.getInstance().register(this);
|
||||
cloudClientConfig = MoGoAiCloudClient.getInstance().getAiCloudClientConfig();
|
||||
}
|
||||
|
||||
@@ -77,21 +67,18 @@ public class SocketManager implements IMogoCloudSocketManager, Callback {
|
||||
|
||||
@Override
|
||||
public void init(Context context) {
|
||||
SocketConfig.instance()
|
||||
.setAppContext(context.getApplicationContext())
|
||||
.setEnvironment(getEnvironment())
|
||||
.setClient(Platform.getClient(Platform.car))
|
||||
.setChannelId(SocketServicesConstants.SOCKET_CHANNEL_ID)
|
||||
.setOpenAnalytics(true)
|
||||
.setSn(cloudClientConfig.getThirdPartyDeviceId())
|
||||
.setToken(cloudClientConfig.getToken())
|
||||
.setAuthPubKey(cloudClientConfig.getAuthPubKey())
|
||||
.setDebug(cloudClientConfig.isShowDebugLog());
|
||||
SocketClient.getInstance().start(context);
|
||||
if (cloudClientConfig.isThirdLogin()) {
|
||||
ThirdSocketManager.getInstance().init(context);
|
||||
} else {
|
||||
InternalSocketManager.getInstance().init(context);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerOnMessageListener(int msgType, IMogoCloudSocketOnMessageListener listener) {
|
||||
if (listener == null) {
|
||||
return;
|
||||
}
|
||||
if (mListeners.containsKey(msgType)) {
|
||||
Logger.w(TAG, "msgType %d is exist.", msgType);
|
||||
return;
|
||||
@@ -120,12 +107,15 @@ public class SocketManager implements IMogoCloudSocketManager, Callback {
|
||||
public void sendMsg(String appId, int headerType, MsgBody body, IMogoCloudSocketMsgAckListener listener) {
|
||||
Logger.d(TAG, "sendMsg msgId : " + body.getMsgId());
|
||||
final byte[] pb = convertToPBBytes(body.getMsgType(), body.getContent());
|
||||
SocketClient.getInstance().sendData(appId, MogoCommon.Product.mogoBussiness.getNumber(), pb, headerType, true, body.getMsgId());
|
||||
if (cloudClientConfig.isThirdLogin()) {
|
||||
ThirdSocketManager.getInstance().sendMsg(appId, pb, headerType, true, body.getMsgId());
|
||||
} else {
|
||||
InternalSocketManager.getInstance().sendMsg(pb, headerType, true, body.getMsgId());
|
||||
}
|
||||
mAckListeners.put(body.getMsgId(), listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(@NonNull CallbackManager manager, @NonNull byte[] message, String appId, long msgId) {
|
||||
public void update(@NonNull byte[] message, long msgId) {
|
||||
try {
|
||||
MogoConnsvr.Payload payload = MogoConnsvr.Payload.parseFrom(message);
|
||||
int msgType = payload.getMsgType();
|
||||
@@ -166,8 +156,7 @@ public class SocketManager implements IMogoCloudSocketManager, Callback {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAck(@NonNull CallbackManager manager, @NonNull byte[] headerBytes, byte[] content) {
|
||||
public void onAck(@NonNull byte[] headerBytes) {
|
||||
try {
|
||||
MogoConnsvr.Header header = MogoConnsvr.Header.parseFrom(headerBytes);
|
||||
int msgType = header.getMsgType();
|
||||
@@ -184,24 +173,6 @@ public class SocketManager implements IMogoCloudSocketManager, Callback {
|
||||
}
|
||||
}
|
||||
|
||||
private Environment getEnvironment() {
|
||||
switch (cloudClientConfig.getNetMode()) {
|
||||
case HTTP_DNS_ENV_DEV:
|
||||
return Environment.dev;
|
||||
case HTTP_DNS_ENV_QA:
|
||||
case HTTP_DNS_ENV_DEMO:
|
||||
return Environment.qa;
|
||||
case HTTP_DNS_ENV_RELEASE:
|
||||
default:
|
||||
return Environment.release;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] objectToBytes(Object obj) {
|
||||
String jsonStr = GsonUtil.jsonFromObject(obj);
|
||||
return jsonStr.getBytes();
|
||||
}
|
||||
|
||||
private byte[] convertToPBBytes(int msgType, byte[] payloadBytes) {
|
||||
MogoConnsvr.Payload payloadData = MogoConnsvr.Payload.newBuilder()
|
||||
.setMsgType(msgType)
|
||||
@@ -211,6 +182,11 @@ public class SocketManager implements IMogoCloudSocketManager, Callback {
|
||||
|
||||
public synchronized void release() {
|
||||
mListeners.clear();
|
||||
if (cloudClientConfig.isThirdLogin()) {
|
||||
ThirdSocketManager.getInstance().release();
|
||||
} else {
|
||||
InternalSocketManager.getInstance().release();
|
||||
}
|
||||
cloudClientConfig = null;
|
||||
mInstance = null;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,15 @@ package com.mogo.cloud.socket;
|
||||
|
||||
import android.support.annotation.Keep;
|
||||
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClient;
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
|
||||
import com.zhidao.socket.Environment;
|
||||
|
||||
import static com.mogo.cloud.httpdns.MogoHttpDnsConfig.HTTP_DNS_ENV_DEMO;
|
||||
import static com.mogo.cloud.httpdns.MogoHttpDnsConfig.HTTP_DNS_ENV_DEV;
|
||||
import static com.mogo.cloud.httpdns.MogoHttpDnsConfig.HTTP_DNS_ENV_QA;
|
||||
import static com.mogo.cloud.httpdns.MogoHttpDnsConfig.HTTP_DNS_ENV_RELEASE;
|
||||
|
||||
public class SocketServicesConstants {
|
||||
|
||||
public static final String TAG = "MoGoAiCloud_Socket";
|
||||
@@ -12,4 +21,18 @@ public class SocketServicesConstants {
|
||||
*/
|
||||
@Keep
|
||||
public static final String SOCKET_CHANNEL_ID = "dataCrawler";
|
||||
|
||||
public static Environment getEnvironment() {
|
||||
MoGoAiCloudClientConfig cloudClientConfig = MoGoAiCloudClient.getInstance().getAiCloudClientConfig();
|
||||
switch (cloudClientConfig.getNetMode()) {
|
||||
case HTTP_DNS_ENV_DEV:
|
||||
return com.zhidao.socket.Environment.dev;
|
||||
case HTTP_DNS_ENV_QA:
|
||||
case HTTP_DNS_ENV_DEMO:
|
||||
return com.zhidao.socket.Environment.qa;
|
||||
case HTTP_DNS_ENV_RELEASE:
|
||||
default:
|
||||
return com.zhidao.socket.Environment.release;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.mogo.cloud.socket.internal;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClient;
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
|
||||
import com.mogo.cloud.socket.SocketManager;
|
||||
import com.mogo.cloud.utils.logger.Logger;
|
||||
import com.zhidao.ptech.connsvr.commom.protocol.MogoCommon;
|
||||
import com.zhidao.socketsdk.manager.OnSocketAckCallback;
|
||||
import com.zhidao.socketsdk.manager.OnSocketReceiveCallback;
|
||||
import com.zhidao.socketsdk.manager.SocketConnManager;
|
||||
import com.zhidao.socketsdk.manager.SocketConnManagerImpl;
|
||||
|
||||
import static com.mogo.cloud.socket.SocketServicesConstants.TAG;
|
||||
|
||||
public class InternalSocketManager implements OnSocketReceiveCallback, OnSocketAckCallback {
|
||||
|
||||
private static volatile InternalSocketManager mInstance;
|
||||
private static final int MSG_PRODUCT_LINE = MogoCommon.Product.mogoBussiness_VALUE;
|
||||
|
||||
private final MoGoAiCloudClientConfig cloudClientConfig;
|
||||
private SocketConnManager mSocketConnManager;
|
||||
|
||||
private InternalSocketManager() {
|
||||
cloudClientConfig = MoGoAiCloudClient.getInstance().getAiCloudClientConfig();
|
||||
}
|
||||
|
||||
public static InternalSocketManager getInstance() {
|
||||
if (mInstance == null) {
|
||||
synchronized (InternalSocketManager.class) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new InternalSocketManager();
|
||||
}
|
||||
}
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
public void init(Context context) {
|
||||
mSocketConnManager = SocketConnManagerImpl.getInstance(context);
|
||||
mSocketConnManager.addSocketMessageCallback(this);
|
||||
mSocketConnManager.addSocketAckCallback(this);
|
||||
if (TextUtils.isEmpty(cloudClientConfig.getSocketAppId())) {
|
||||
Logger.e(TAG, "需要初始化 Socket AppId");
|
||||
return;
|
||||
}
|
||||
mSocketConnManager.init(cloudClientConfig.getSocketAppId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAck(byte[] headerBytes, byte[] payload) {
|
||||
Logger.d(TAG, "InternalSocketManager update ack");
|
||||
SocketManager.getInstance().onAck(headerBytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(byte[] message) {
|
||||
Logger.d(TAG, "InternalSocketManager update message");
|
||||
SocketManager.getInstance().update(message, 0);
|
||||
}
|
||||
|
||||
public void sendMsg(byte[] pb, int headerType, boolean isAck, long msgId) {
|
||||
if (mSocketConnManager.isConnected()) {
|
||||
Logger.d(TAG, "isConnected.");
|
||||
mSocketConnManager.sendPayload(MSG_PRODUCT_LINE, pb, headerType, isAck, msgId);
|
||||
} else {
|
||||
Logger.e(TAG, "sendMsg error, connect is lost.");
|
||||
}
|
||||
}
|
||||
|
||||
public void release() {
|
||||
mSocketConnManager.onRelease();
|
||||
mSocketConnManager = null;
|
||||
mInstance = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.mogo.cloud.socket.third;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClient;
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
|
||||
import com.mogo.cloud.socket.SocketManager;
|
||||
import com.mogo.cloud.socket.SocketServicesConstants;
|
||||
import com.mogo.cloud.utils.logger.Logger;
|
||||
import com.zhidao.locupload.Platform;
|
||||
import com.zhidao.ptech.connsvr.commom.protocol.MogoCommon;
|
||||
import com.zhidao.socket.Callback;
|
||||
import com.zhidao.socket.CallbackManager;
|
||||
import com.zhidao.socket.SocketClient;
|
||||
import com.zhidao.socket.SocketConfig;
|
||||
|
||||
import static com.mogo.cloud.socket.SocketServicesConstants.TAG;
|
||||
import static com.mogo.cloud.socket.SocketServicesConstants.getEnvironment;
|
||||
|
||||
public class ThirdSocketManager implements Callback {
|
||||
|
||||
private static volatile ThirdSocketManager mInstance;
|
||||
private final MoGoAiCloudClientConfig cloudClientConfig;
|
||||
|
||||
private ThirdSocketManager() {
|
||||
CallbackManager.getInstance().register(this);
|
||||
cloudClientConfig = MoGoAiCloudClient.getInstance().getAiCloudClientConfig();
|
||||
}
|
||||
|
||||
public static ThirdSocketManager getInstance() {
|
||||
if (mInstance == null) {
|
||||
synchronized (ThirdSocketManager.class) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new ThirdSocketManager();
|
||||
}
|
||||
}
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
public void init(Context context) {
|
||||
SocketConfig.instance()
|
||||
.setAppContext(context.getApplicationContext())
|
||||
.setEnvironment(getEnvironment())
|
||||
.setClient(Platform.getClient(Platform.car))
|
||||
.setChannelId(SocketServicesConstants.SOCKET_CHANNEL_ID)
|
||||
.setOpenAnalytics(true)
|
||||
.setSn(cloudClientConfig.getThirdPartyDeviceId())
|
||||
.setToken(cloudClientConfig.getToken())
|
||||
.setAuthPubKey(cloudClientConfig.getAuthPubKey())
|
||||
.setDebug(cloudClientConfig.isShowDebugLog());
|
||||
SocketClient.getInstance().start(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(@NonNull CallbackManager manager, @NonNull byte[] message, String appId, long msgId) {
|
||||
Logger.d(TAG,"ThirdSocketManager update message");
|
||||
SocketManager.getInstance().update(message, msgId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAck(@NonNull CallbackManager manager, @NonNull byte[] header, byte[] content) {
|
||||
Logger.d(TAG,"ThirdSocketManager update ack");
|
||||
SocketManager.getInstance().onAck(header);
|
||||
}
|
||||
|
||||
public void sendMsg(String appId,
|
||||
byte[] payload,
|
||||
int headerType,
|
||||
boolean ack,
|
||||
long msgId) {
|
||||
SocketClient.getInstance().sendData(appId, MogoCommon.Product.mogoBussiness.getNumber(), payload, headerType, ack, msgId);
|
||||
|
||||
}
|
||||
|
||||
public synchronized void release() {
|
||||
SocketClient.getInstance().stop();
|
||||
mInstance = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user