Search in sources :

Example 1 with TransportConnectLimitException

use of io.transport.core.exception.TransportConnectLimitException in project transporter by wang4ever.

the class AbstractChannelMessageHandler method processAccpetConnect.

/**
 * 新建/注册客户端连接处理
 *
 * @param ctx
 * @param msg
 */
protected void processAccpetConnect(ChannelHandlerContext ctx, ConnectMessage msg) {
    // 1.0 Check device logged in.
    if (this.client == null)
        throw new TransportException("No new socket channel(client is null).");
    else {
        String cliDeviceId = this.client.getDeviceInfo().getDeviceId();
        String reqDeviceId = msg.getDeviceInfo().getDeviceId();
        // 同一deviceId单点登录控制:当前channel已登录或当前deviceId已有已登录的channel(重复创建了channel)
        if (!StringUtils.isEmpty(cliDeviceId) || this.repository.contains(reqDeviceId))
            throw new TransportConnectLimitException("Already loggedin. deviceId(client)=" + cliDeviceId + ", deviceId(req)=" + reqDeviceId);
    }
    // 
    // 1.1 Check parameter.
    msg.validation();
    // 
    // 1.2 Check of appId connections limit.
    this.accpetValidation(msg.getAppId());
    // 
    // 2.1 Authentication appId/secret.
    this.login(msg);
    this.client.setDeviceInfo(msg.getDeviceInfo());
    this.repository.putClient(msg.getAppId(), this.client);
    if (logger.isInfoEnabled())
        logger.info("新建客户端. {}, {}", ctx.channel(), this.client.getDeviceInfo().getDeviceId());
    // 
    // 3.1 Echo result.
    ConnectRespMessage resp = new ConnectRespMessage();
    // 3.2 Get active cluster node info.
    Map<String, NodeInfo> nodes = this.clusterService.clusterNodes(true, null);
    if (nodes != null) {
        for (NodeInfo node : nodes.values()) {
            try {
                boolean isWsCh = TransportProcessors.isWSChannel(ctx.channel());
                if (isWsCh)
                    resp.getHostAndPorts().add(node.getHost() + ":" + node.getWsPort());
                else
                    resp.getHostAndPorts().add(node.getHost() + ":" + node.getRpcPort());
            } catch (Exception e) {
                logger.error("返回节点信息错误.", e);
            }
        }
    }
    if (logger.isInfoEnabled())
        logger.info("节点列表. {}", resp.getHostAndPorts());
    this.echoWrite(ctx, resp);
}
Also used : TransportConnectLimitException(io.transport.core.exception.TransportConnectLimitException) NodeInfo(io.transport.common.bean.NodeInfo) ConnectRespMessage(io.transport.core.protocol.message.internal.ConnectRespMessage) TransportException(io.transport.core.exception.TransportException) TransportAuthenticationException(io.transport.core.exception.TransportAuthenticationException) TransportConnectLimitException(io.transport.core.exception.TransportConnectLimitException) TransportException(io.transport.core.exception.TransportException)

Example 2 with TransportConnectLimitException

use of io.transport.core.exception.TransportConnectLimitException in project transporter by wang4ever.

the class AbstractChannelMessageHandler method channelRegistered.

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    int backlog = TransportProcessors.isWSChannel(ctx.channel()) ? config.getWsConfig().getBacklog() : config.getRpcConfig().getBacklog();
    int curr = this.repository.size();
    if (curr >= backlog)
        throw new TransportConnectLimitException("Too many connections, the current connections " + curr);
    if (logger.isInfoEnabled())
        logger.info("新建连接. channel={}", ctx.channel());
    this.client = new Client((SocketChannel) ctx.channel());
    if (logger.isDebugEnabled())
        logger.debug("Reserved channel {}", this.client.getChannel());
    super.channelRegistered(ctx);
}
Also used : TransportConnectLimitException(io.transport.core.exception.TransportConnectLimitException) SocketChannel(io.netty.channel.socket.SocketChannel) Client(io.transport.core.repository.Client)

Example 3 with TransportConnectLimitException

use of io.transport.core.exception.TransportConnectLimitException in project transporter by wang4ever.

the class AbstractChannelMessageHandler method accpetValidation.

/**
 * 检查AppID连接限制数量
 *
 * @param appId
 */
private void accpetValidation(String appId) {
    // 1.1 Get appId connects default.
    int appIdDeviceConnects = this.config.getCtlAppIdDeviceConnectsDefault();
    // 1.2 Get current appId connects config limit.
    String appIdDeviceConnectsTxt = this.jedisService.get(config.getCtlAppIdDeviceConnectsPKey());
    if (!StringUtils.isEmpty(appIdDeviceConnectsTxt))
        appIdDeviceConnects = Integer.parseInt(appIdDeviceConnectsTxt);
    // 1.3 Get appId groups.
    String key0 = config.getCtlAppIdGroupsPKey() + appId;
    Set<String> groupIds = this.jedisService.getSet(key0);
    // 1.4 Get groupId sub deviceIds all.
    final Set<String> deviceIds = Sets.newHashSet();
    if (groupIds != null) {
        for (String groupId : groupIds) {
            String key1 = config.getCtlGroupIdDevicesPKey() + groupId;
            Set<String> deviceIds0 = this.jedisService.getSet(key1);
            if (deviceIds0 != null) {
                for (String deviceId : deviceIds0) // 同时在内存channel池也存在,才能确定是有效连接.
                if (this.repository.contains(deviceId))
                    deviceIds.add(deviceId);
            }
        }
    }
    if (logger.isInfoEnabled())
        logger.info("AppId连接信息. appId={}, groups={}, connected(devices)={}", appId, groupIds, deviceIds);
    // 2.1 Check the number of appId connections limit.
    if (deviceIds != null && deviceIds.size() > appIdDeviceConnects)
        throw new TransportConnectLimitException("Connect over limit, more than " + appIdDeviceConnects + ", already connected is " + deviceIds.size());
}
Also used : TransportConnectLimitException(io.transport.core.exception.TransportConnectLimitException)

Aggregations

TransportConnectLimitException (io.transport.core.exception.TransportConnectLimitException)3 SocketChannel (io.netty.channel.socket.SocketChannel)1 NodeInfo (io.transport.common.bean.NodeInfo)1 TransportAuthenticationException (io.transport.core.exception.TransportAuthenticationException)1 TransportException (io.transport.core.exception.TransportException)1 ConnectRespMessage (io.transport.core.protocol.message.internal.ConnectRespMessage)1 Client (io.transport.core.repository.Client)1