package com.xuhao.didi.oksocket;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.xuhao.didi.core.iocore.interfaces.IPulseSendable;
import com.xuhao.didi.core.iocore.interfaces.ISendable;
import com.xuhao.didi.core.pojo.OriginalData;
import com.xuhao.didi.oksocket.adapter.LogAdapter;
import com.xuhao.didi.oksocket.btnview.Ripple;
import com.xuhao.didi.oksocket.data.DefaultSendBean;
import com.xuhao.didi.oksocket.data.HandShakeBean;
import com.xuhao.didi.oksocket.data.LogBean;
import com.xuhao.didi.oksocket.data.PulseBean;
import com.xuhao.didi.socket.client.impl.client.action.ActionDispatcher;
import com.xuhao.didi.socket.client.sdk.OkSocket;
import com.xuhao.didi.socket.client.sdk.client.ConnectionInfo;
import com.xuhao.didi.socket.client.sdk.client.OkSocketOptions;
import com.xuhao.didi.socket.client.sdk.client.action.SocketActionAdapter;
import com.xuhao.didi.socket.client.sdk.client.connection.IConnectionManager;
import com.xuhao.didi.socket.client.sdk.client.connection.NoneReconnect;
import com.xuhao.didi.socket.common.interfaces.utils.TextUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextTool;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.miscservices.pasteboard.PasteData;
import ohos.miscservices.pasteboard.SystemPasteboard;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
public class ComplexDemo extends Ability implements AbsButton.CheckedStateChangedListener,
Component.ClickedListener, Component.FocusChangedListener {
private ConnectionInfo mInfo;
private Ripple mConnect;
private IConnectionManager mManager;
private TextField mIpEt;
private TextField mPortEt;
private Ripple mRedirect;
private TextField mFrequencyEt;
private Ripple mSetFrequency;
private Ripple mMenualPulse;
private Ripple mClearLog;
private Checkbox mReconnectSwitch;
private ListContainer mSendList;
private ListContainer mReceList;
private Text mPortHint;
private Text mPortText;
private Text mEditHint;
private Text mIpText;
private Text mPulseHint;
private Text mPulseText;
private LogAdapter mSendLogAdapter;
private LogAdapter mReceLogAdapter;
private String mSelectColor = "#169E93";
private String mColorUn = "#7A7A7A";
private int maxsize = 15;
private int mSize = 11;
private TextUtile textUtils;
private SocketActionAdapter adapter = new SocketActionAdapter() {
@Override
public void onSocketConnectionSuccess(ConnectionInfo info, String action) {
logRece("连接成功(Connecting Successful)");
mManager.send(new HandShakeBean());
mConnect.setText("DisConnect");
initSwitch();
mManager.getPulseManager().setPulseSendable(new PulseBean());
mIpEt.setEnabled(true);
mPortEt.setEnabled(true);
textUtils.chickTextColor(ComplexDemo.this,mIpEt,mPortEt);
}
private void initSwitch() {
OkSocketOptions okSocketOptions = mManager.getOption();
mReconnectSwitch.setChecked(!(okSocketOptions.getReconnectionManager() instanceof NoneReconnect));
}
@Override
public void onSocketDisconnection(ConnectionInfo info, String action, Exception e) {
if (e != null) {
if (e instanceof RedirectException) {
logSend("正在重定向连接(Redirect Connecting)...");
mManager.switchConnectionInfo(((RedirectException) e).redirectInfo);
mManager.connect();
mIpEt.setEnabled(true);
mPortEt.setEnabled(true);
textUtils.chickTextColor(ComplexDemo.this,mIpEt,mPortEt);
} else {
logSend("异常断开(Disconnected with exception):" + e.getMessage());
mIpEt.setEnabled(false);
mPortEt.setEnabled(false);
textUtils.textInfo(mIpEt,mIpText,maxsize);
textUtils.textInfo(mPortEt,mPulseText,maxsize);
textUtils.chickColor(ComplexDemo.this,mIpEt,mPortEt);
}
} else {
logSend("正常断开(Disconnect Manually)");
mIpEt.setEnabled(false);
mPortEt.setEnabled(false);
textUtils.textInfo(mIpEt,mIpText,maxsize);
textUtils.textInfo(mPortEt,mPulseText,maxsize);
textUtils.chickColor(ComplexDemo.this,mIpEt,mPortEt);
}
mConnect.setText("Connect");
}
@Override
public void onSocketConnectionFailed(ConnectionInfo info, String action, Exception e) {
logSend("连接失败(Connecting Failed)");
mConnect.setText("Connect");
mIpEt.setEnabled(false);
mPortEt.setEnabled(false);
textUtils.textInfo(mIpEt,mIpText,maxsize);
textUtils.textInfo(mPortEt,mPulseText,maxsize);
textUtils.chickColor(ComplexDemo.this,mIpEt,mPortEt);
}
@Override
public void onSocketReadResponse(ConnectionInfo info, String action, OriginalData data) {
String str = new String(data.getBodyBytes(), Charset.forName("utf-8"));
JsonObject jsonObject = new JsonParser().parse(str).getAsJsonObject();
int cmd = jsonObject.get("cmd").getAsInt();
if (cmd == 54) { // 登陆成功
String handshake = jsonObject.get("handshake").getAsString();
logRece("握手成功! 握手信息(Handshake Success):" + handshake + ". 开始心跳(Start Heartbeat)..");
} else if (cmd == 57) { // 切换,重定向.(暂时无法演示,如有疑问请咨询github)
String ip = jsonObject.get("data").getAsString().split(":")[0];
int port = Integer.parseInt(jsonObject.get("data").getAsString().split(":")[1]);
ConnectionInfo redirectInfo = new ConnectionInfo(ip, port);
redirectInfo.setBackupInfo(mInfo.getBackupInfo());
mManager.getReconnectionManager().addIgnoreException(RedirectException.class);
mManager.disconnect(new RedirectException(redirectInfo));
} else if (cmd == 14) { // 心跳
logRece("收到心跳,喂狗成功(Heartbeat Received,Feed the Dog)");
mManager.getPulseManager().feed();
} else {
logRece(str);
}
}
@Override
public void onSocketWriteResponse(ConnectionInfo info, String action, ISendable data) {
byte[] bytes = data.parse();
bytes = Arrays.copyOfRange(bytes, 4, bytes.length);
String str = new String(bytes, Charset.forName("utf-8"));
JsonObject jsonObject = new JsonParser().parse(str).getAsJsonObject();
int cmd = jsonObject.get("cmd").getAsInt();
switch (cmd) {
case 54: {
String handshake = jsonObject.get("handshake").getAsString();
logSend("发送握手数据(Handshake Sending):" + handshake);
mManager.getPulseManager().pulse();
break;
}
default:
logSend(str);
}
}
@Override
public void onPulseSend(ConnectionInfo info, IPulseSendable data) {
byte[] bytes = data.parse();
bytes = Arrays.copyOfRange(bytes, 4, bytes.length);
String str = new Stri