Search in sources :

Example 1 with Client

use of io.transport.core.repository.Client 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 2 with Client

use of io.transport.core.repository.Client in project transporter by wang4ever.

the class TransportProcessors method sentGroupMsg.

/**
 * 向group.channel多个通道发送消息
 *
 * @param repository
 *            channel通道库
 * @param toGroupId
 *            客户端ID(标识唯一通道)
 * @param buf
 *            transport二进制byte消息
 */
public static void sentGroupMsg(ChannelRepository repository, TransportMessage warp) {
    String toGroupId = warp.getToGroupId();
    Collection<Client> clients = repository.getClients(toGroupId);
    if (clients == null || clients.isEmpty())
        throw new TransportException("'" + toGroupId + "' without online devices.");
    // 2.0 遍历发送
    for (Client c : clients) sent(c, warp);
}
Also used : Client(io.transport.core.repository.Client) TransportException(io.transport.core.exception.TransportException)

Example 3 with Client

use of io.transport.core.repository.Client in project transporter by wang4ever.

the class TransportProcessors method sentMsg.

/**
 * 向channel通道发送消息
 *
 * @param repository
 *            channel通道库
 * @param toDeviceId
 *            客户端ID(标识唯一通道)
 * @param buf
 *            transport二进制byte消息
 */
public static void sentMsg(ChannelRepository repository, TransportMessage warp) {
    String toDeviceId = warp.getToDeviceId();
    Client c = repository.getClient(toDeviceId);
    if (c == null)
        throw new TransportException("Device '" + toDeviceId + "' offline.");
    // 1.0 构建消息并发送
    sent(c, warp);
}
Also used : Client(io.transport.core.repository.Client) TransportException(io.transport.core.exception.TransportException)

Example 4 with Client

use of io.transport.core.repository.Client in project transporter by wang4ever.

the class NettyChannelMessageService method send.

@Override
public String send(String clientId, String msg) {
    Client c = this.channelRepository.getClient(clientId);
    // BinaryWebSocketFrame frame = new
    // BinaryWebSocketFrame(ByteBufs.toByteBuf(msg));
    TextWebSocketFrame frame = new TextWebSocketFrame(ByteBufs.toByteBuf(msg));
    c.getChannel().writeAndFlush(frame);
    return null;
}
Also used : TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) Client(io.transport.core.repository.Client)

Example 5 with Client

use of io.transport.core.repository.Client in project transporter by wang4ever.

the class PushGroupTopicHandler method execute.

@Override
public void execute(String topic, byte[] payload) {
    if (logger.isDebugEnabled())
        logger.debug("Push.group handler on message: topic={}, payload={}", topic, ByteBufs.toString(payload));
    try {
        // 当使用MQTT broker转发时,topic包含clientId的信息(topic -> toGroupId).
        String toGroupId = TopicType.getSubTopicAfter(topic);
        TransportMessage message = TransportProcessors.build(payload);
        // 发送给toGroupId下所有deviceId
        Collection<Client> clients = this.channelRepository.getClients(toGroupId);
        if (clients != null && !clients.isEmpty()) {
            for (Client c : clients) {
                // toDeviceId -> actorName
                String actorName = this.config.getCtlDeviceIdActorPkey() + c.getDeviceInfo().getDeviceId();
                this.actorManager.actorTell(actorName, message);
                if (logger.isDebugEnabled())
                    logger.debug("MQTT.group消息传送(Actor). actorName={}", actorName);
            }
        } else
            throw new TransportException("toGroupId=" + toGroupId + " clients is null.");
    } catch (Exception e) {
        if (e instanceof TransportException)
            logger.error("MQTT.group消费传送(Actor)失败. {}", ExceptionUtils.getRootCauseMessage(e));
        else
            logger.error("MQTT.group消费传送(Actor)异常. ", e);
    }
}
Also used : Client(io.transport.core.repository.Client) TransportException(io.transport.core.exception.TransportException) TransportException(io.transport.core.exception.TransportException) TransportMessage(io.transport.core.protocol.message.internal.TransportMessage)

Aggregations

Client (io.transport.core.repository.Client)5 TransportException (io.transport.core.exception.TransportException)3 SocketChannel (io.netty.channel.socket.SocketChannel)1 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)1 TransportConnectLimitException (io.transport.core.exception.TransportConnectLimitException)1 TransportMessage (io.transport.core.protocol.message.internal.TransportMessage)1