Search in sources :

Example 1 with TransportException

use of io.transport.core.exception.TransportException 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 TransportException

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

the class ConnectMessage method validation.

public void validation() {
    boolean ret = (StringUtils.isEmpty(this.getAppId()));
    if (ret)
        throw new TransportException("'appId'不允许为空.");
    this.getDeviceInfo().validation();
}
Also used : TransportException(io.transport.core.exception.TransportException)

Example 3 with TransportException

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

the class ByteBufs method coverFixedBuf.

/**
 * 扩充定长字节数组缓冲区
 *
 * @param orgin
 *            参数
 * @param totalLen
 *            总长度
 * @return
 * @sine
 */
public static byte[] coverFixedBuf(byte[] orgin, int totalLen) {
    if (totalLen < orgin.length)
        throw new TransportException("The buffer array 'orgin' is over long and can only be 'totalLen'.");
    byte[] buf = new byte[totalLen];
    System.arraycopy(orgin, 0, buf, 0, orgin.length);
    return buf;
}
Also used : TransportException(io.transport.core.exception.TransportException)

Example 4 with TransportException

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

the class PushTopicHandler method execute.

@Override
public void execute(String topic, byte[] payload) {
    if (logger.isDebugEnabled())
        logger.debug("Push handler on message: topic={}, payload={}", topic, ByteBufs.toString(payload));
    try {
        // 当使用MQTT broker转发时,topic包含clientId的信息(topic -> toDeviceId).
        String toDeviceId = TopicType.getSubTopicAfter(topic);
        TransportMessage message = TransportProcessors.build(payload);
        // toDeviceId -> actorName
        String actorName = this.config.getCtlDeviceIdActorPkey() + toDeviceId;
        this.actorManager.actorTell(actorName, message);
        if (logger.isDebugEnabled())
            logger.debug("MQTT.device消息传送(Actor). actorName={}", actorName);
    } catch (Exception e) {
        if (e instanceof TransportException)
            logger.error("MQTT.device消费传送(Actor)失败. {}", ExceptionUtils.getRootCauseMessage(e));
        else
            logger.error("MQTT.device消费传送(Actor)异常. ", e);
    }
}
Also used : TransportException(io.transport.core.exception.TransportException) TransportException(io.transport.core.exception.TransportException) TransportMessage(io.transport.core.protocol.message.internal.TransportMessage)

Example 5 with TransportException

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

the class TransportMessageHandler method processDeviceRegisted.

/**
 * 注册前端设备连接认证信息处理
 *
 * @param ctx
 * @param msg
 */
private void processDeviceRegisted(ChannelHandlerContext ctx, DeviceRegistMessage msg) {
    // 1.1 Check deviceId.
    for (Object deviceId : msg.getClientDeviceIds()) {
        if (ByteBufs.toBytes(String.valueOf(deviceId)).length != DeviceInfo.ID_LEN)
            throw new TransportException("Protocol error, parameter 'deviceId' length can only be " + DeviceInfo.ID_LEN);
    }
    // 1.2 保存注册前端设备连接认证信息Redis
    for (Object deviceId : msg.getClientDeviceIds()) this.jedisService.set(String.valueOf(deviceId), msg.getAppId(), msg.getExpired());
    if (logger.isInfoEnabled())
        logger.info("新注册认证设备: msg={}", JSON.toJSONString(msg));
    // 1.3 Echo客户端
    DeviceRegistRespMessage resp = new DeviceRegistRespMessage();
    this.echoWrite(ctx, resp);
    if (logger.isDebugEnabled())
        logger.debug("Echo message. {}", resp);
}
Also used : DeviceRegistRespMessage(io.transport.core.protocol.message.internal.DeviceRegistRespMessage) TransportException(io.transport.core.exception.TransportException)

Aggregations

TransportException (io.transport.core.exception.TransportException)8 Client (io.transport.core.repository.Client)3 TransportMessage (io.transport.core.protocol.message.internal.TransportMessage)2 NodeInfo (io.transport.common.bean.NodeInfo)1 TransportAuthenticationException (io.transport.core.exception.TransportAuthenticationException)1 TransportConnectLimitException (io.transport.core.exception.TransportConnectLimitException)1 ConnectRespMessage (io.transport.core.protocol.message.internal.ConnectRespMessage)1 DeviceRegistRespMessage (io.transport.core.protocol.message.internal.DeviceRegistRespMessage)1