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

com.iteaj.iot.client.component.SocketClientComponent Maven / Gradle / Ivy

package com.iteaj.iot.client.component;

import com.iteaj.iot.client.*;
import com.iteaj.iot.*;
import com.iteaj.iot.config.ConnectProperties;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.List;

/**
 * 基于socket的客户端组件 主要tcp, upd等
 * @param 
 */
public abstract class SocketClientComponent extends ProtocolFactory
        implements ClientComponent, IotProtocolFactory, InitializingBean {

    /**
     * 默认客户端
     * @see #getConfig()
     */
    private IotClient iotClient;
    private Class messageClass;
    private IotThreadManager threadManager;
    private ClientConnectProperties config;
    private MultiClientManager clientManager;
    protected Logger logger = LoggerFactory.getLogger(getClass());

    public SocketClientComponent() {
        this(null);
    }

    /**
     * @param config 默认客户端
     */
    public SocketClientComponent(ClientConnectProperties config) {
        this(config, new SimpleMultiClientManager(null));
    }

    public SocketClientComponent(ClientConnectProperties config, MultiClientManager clientManager) {
        this.config = config;
        this.clientManager = clientManager;
        this.setDelegation(createProtocolTimeoutStorage());

        if(this.clientManager.getClientComponent() == null) {
            this.clientManager.setClientComponent(this);
        }
    }

    @Override
    public void init(Object ...args) {
        if(getConfig() != null) {
            this.getClient().init(args[0]);
        }
    }

    @Override
    public void connect() {
        if(this.getConfig() != null) {
            this.getClient().connect((future) -> {
                final ChannelFuture channelFuture = (ChannelFuture) future;
                channelFuture.addListener(future1 -> {
                    if(future1.isSuccess()) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("默认客户端({}) 连接服务器成功 - 远程主机 {}:{} - 客户端标识:{}"
                                    , getClientComponent().getName(), this.config.getHost(), this.config.getPort(), getConfig().connectKey());
                        }
                    }
                });
            }, 3000);
        }
    }

    @Override
    public void addClient(Object clientKey, IotClient value) {
        this.clientManager.addClient(clientKey, value);
    }

    @Override
    public IotClient getClient() {
        if(this.iotClient != null) {
            return this.iotClient;
        } else {
            if(this.getConfig() != null) {
                return this.iotClient = createNewClient(this.config);
            }
        }

        return null;
    }

    @Override
    public List clients() {
        return clientManager.clients();
    }

    @Override
    public IotClient getClient(Object clientKey) {
        return clientManager.getClient(clientKey);
    }

    @Override
    public IotClient removeClient(Object clientKey) {
        return clientManager.removeClient(clientKey);
    }

    @Override
    public void setClientComponent(ClientComponent component) {
        this.clientManager.setClientComponent(component);
    }

    @Override
    public Class getMessageClass() {
        if(this.messageClass == null) {
            // @since 2.3.0 只解析一次报文类
            this.messageClass = ClientComponent.super.getMessageClass();
        }

        return this.messageClass;
    }

    /**
     * 创建一个新客户端
     * @param config
     * @return
     */
    public abstract SocketClient createNewClient(ClientConnectProperties config);

    /**
     * 创建新的客户端并连接
     * @param config
     */
    public SocketClient createNewClientAndConnect(ClientConnectProperties config) {
        SocketClient newClient = createNewClient(config);
        EventLoopGroup workerGroup = threadManager.getWorkerGroup();
        newClient.init((NioEventLoopGroup) workerGroup);
        // 创建连接
        newClient.connect((future) -> {
            final ChannelFuture channelFuture = (ChannelFuture) future;
            channelFuture.addListener(future1 -> {
                if(future1.isSuccess() && logger.isDebugEnabled()) {
                    logger.debug("客户端({}) 连接服务器成功 - 远程主机 {}:{} - 客户端标识:{}"
                            , getClientComponent().getName(), this.config.getHost(), this.config.getPort(), getConfig().connectKey());
                }
            });
        }, 3000).syncUninterruptibly();
        return newClient;
    }

    @Override
    public IotProtocolFactory protocolFactory() {
        return this;
    }

    public MultiClientManager getClientManager() {
        return clientManager;
    }

    protected ProtocolTimeoutStorage createProtocolTimeoutStorage() {
        return new ProtocolTimeoutStorage(getName());
    }

    public ClientConnectProperties getConfig() {
        return config;
    }

    public void setConfig(ClientConnectProperties config) {
        this.config = config;
    }

    @Autowired
    protected void setThreadManager(IotThreadManager threadManager) {
        this.threadManager = threadManager;
    }

    @Override
    public void afterPropertiesSet() throws Exception { }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy