All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iteaj.iot.client.protocol.ClientSocketProtocol Maven / Gradle / Ivy

package com.iteaj.iot.client.protocol;

import cn.hutool.core.util.ClassUtil;
import com.iteaj.iot.FrameworkManager;
import com.iteaj.iot.ProtocolException;
import com.iteaj.iot.ProtocolType;
import com.iteaj.iot.client.*;
import com.iteaj.iot.client.component.SingleTcpClientComponent;
import com.iteaj.iot.client.component.SocketClientComponent;
import com.iteaj.iot.protocol.socket.AbstractSocketProtocol;
import io.netty.channel.socket.DatagramPacket;

/**
 * create time: 2021/8/6
 *
 * @author iteaj
 * @since 1.0
 */
public abstract class ClientSocketProtocol extends AbstractSocketProtocol implements ClientProtocol {

    /**
     * udp协议报文
     */
    private DatagramPacket packet;

    /**
     * 用来获取客户端
     * @see SocketClient
     * @see #getIotClient()
     */
    private ClientConnectProperties clientKey;

    private ClientProtocolHandle defaultProtocolHandle; // 默认协议处理器

    @Override
    public C requestMessage() {
        return super.requestMessage();
    }

    @Override
    public C responseMessage() {
        return super.responseMessage();
    }

    @Override
    public abstract ProtocolType protocolType();

    /**
     * 返回客户端
     * @return
     */
    @Override
    public SocketClient getIotClient() {
        ClientComponent clientComponent = FrameworkManager.getClientComponent(this.getMessageClass());
        if(clientComponent instanceof SingleTcpClientComponent && getClientKey() != null) {
            throw new ProtocolException("组件["+clientComponent.getName()+"]属于单客户端组件, 不支持使用方法 #request(host, port)");
        }

        Object clientKey = this.getClientKey() != null ? this.getClientKey() : clientComponent.getConfig();
        if(clientKey == null) {
            throw new ClientProtocolException("未找到可用客户端");
        }

        IotClient socketClient = clientComponent.getClient(clientKey);
        if(socketClient instanceof SocketClient) {
            return (SocketClient) socketClient;
        } else {
            /**
             * 防止同一个connectKey建立多个连接
             * @see ClientConnectProperties#connectKey() 声明是同一个连接
             * @see com.iteaj.iot.client.component.SimpleMultiClientManager#addClient(Object, IotClient) 此处会阻止同一个connectKey添加多次
             */
            synchronized (clientComponent) {
                socketClient = clientComponent.getClient(clientKey);
                if(socketClient != null) {
                    return (SocketClient) socketClient;
                }

                return this.createSocketClient((SocketClientComponent) clientComponent, (ClientConnectProperties)clientKey);
            }
        }
    }

    /**
     * 如果客户端不存在是否创建客户端
     * @param component
     * @param clientKey
     * @return
     */
    protected SocketClient createSocketClient(SocketClientComponent component, ClientConnectProperties clientKey) {
        return component.createNewClientAndConnect(clientKey);
    }

    protected Class getMessageClass() {
        if(requestMessage() != null) {
            return (Class) requestMessage().getClass();
        } else {
            return (Class) ClassUtil.getTypeArgument(getClass());
        }
    }

    protected String getMessageId() {
        ClientMessage message = requestMessage();
        return message.getMessageId();
    }

    public ClientProtocolHandle getDefaultProtocolHandle() {
        if(defaultProtocolHandle == null) {
            defaultProtocolHandle = (ClientProtocolHandle) FrameworkManager.getProtocolHandle(getClass());
        }

        return defaultProtocolHandle;
    }

    public DatagramPacket getPacket() {
        return packet;
    }

    public void setPacket(DatagramPacket packet) {
        this.packet = packet;
    }

    public ClientConnectProperties getClientKey() {
        return clientKey;
    }

    public ClientSocketProtocol setClientKey(ClientConnectProperties clientKey) {
        this.clientKey = clientKey;
        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy