Search in sources :

Example 6 with TransportException

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

the class TransportClients method build.

/**
 * 构建Transport应用程序
 *
 * @param config
 *            配置信息
 * @return
 * @throws TransportException
 */
public synchronized TransportClients build(Configuration config) throws TransportException {
    this.config = config;
    if (this.config == null)
        throw new TransportException("'config'参数为空.");
    this.config.validation();
    return this;
}
Also used : TransportException(io.transport.sdk.exception.TransportException)

Example 7 with TransportException

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

the class TransportClients method unicast.

/**
 * 单播发送消息(指定的点对点one-to-one)
 *
 * @param toDeviceId
 * @param payload
 * @return 通道对象(可用于控制同步发送方案: unicast(..).sync())
 * @throws TransportException
 */
public Future<Message> unicast(String toDeviceId, String payload) throws TransportException {
    if (StringUtil.isNullOrEmpty(toDeviceId) || StringUtil.isNullOrEmpty(payload))
        throw new TransportException("'toDeviceId/payload' is not allowed to be empty.");
    String fromDeviceId = Constants.processSerial;
    TransportMessage msg = new TransportMessage(fromDeviceId, toDeviceId, payload);
    return this.execute(msg, true);
}
Also used : TransportException(io.transport.sdk.exception.TransportException) TransportMessage(io.transport.sdk.protocol.message.internal.TransportMessage)

Example 8 with TransportException

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

the class TransportConnector method connect.

/**
 * 连接到服务器
 *
 * @param sync
 * @param callback
 */
public void connect(final boolean sync) {
    try {
        if (this.bootstrap == null)
            this.configure();
        // Get the current load balancing node.
        HostAndPort hap = this.config.getRoutingBalancer().determineCurrentLookupNode();
        config.getLogger().info("Connect to " + hap + " failed.");
        // Configuration completion, starting connection with server,
        // blocking by calling the sync synchronization method until the
        // connection is successful.
        ChannelFuture cf = this.bootstrap.connect(hap.getHost(), hap.getPort());
        // Reconnect listener.
        cf.addListener((ChannelFuture f) -> {
            if (!f.isSuccess()) {
                f.channel().eventLoop().schedule(() -> {
                    // Update connection failure (counter, for load balance
                    // calculation).
                    config.getRoutingBalancer().onConnectFailed(hap);
                    // Retry connection.
                    this.connect(sync);
                }, config.getReconnectDelay(), TimeUnit.SECONDS);
            } else {
                this.config.getLogger().info("Connected to " + hap);
                // Log on after the connection is successful.
                this.client.login();
            }
        });
        if (sync)
            this.channel = cf.sync().channel();
        else
            this.channel = cf.channel();
    } catch (Exception e) {
        throw new TransportException("Connection failed.", e);
    } finally {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                this.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }));
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HostAndPort(io.transport.sdk.Configuration.HostAndPort) TransportException(io.transport.sdk.exception.TransportException) IOException(java.io.IOException) TransportException(io.transport.sdk.exception.TransportException)

Example 9 with TransportException

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

the class HashRoutingLoadBalancer method getPreselectedHap.

/**
 * 统计获取预选节点
 */
private HostAndPort getPreselectedHap() {
    List<HostAndPort> preHaps = new ArrayList<>();
    for (HostAndPort hap0 : this.config.getHostAndPorts()) {
        if (// 是否可作为预选节点
        this.isRetryNode(hap0))
            preHaps.add(hap0);
    }
    int crc16 = CRC16.crc16Modbus(Constants.processSerial.getBytes());
    int size = preHaps.size();
    if (size == 0)
        throw new TransportException("All cluster nodes of the server failed.");
    int nodeIndex = crc16 % size & (size - 1);
    return preHaps.get(nodeIndex);
}
Also used : HostAndPort(io.transport.sdk.Configuration.HostAndPort) ArrayList(java.util.ArrayList) TransportException(io.transport.sdk.exception.TransportException)

Example 10 with TransportException

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

Aggregations

TransportException (io.transport.sdk.exception.TransportException)18 IOException (java.io.IOException)6 TransportMessage (io.transport.sdk.protocol.message.internal.TransportMessage)4 HostAndPort (io.transport.sdk.Configuration.HostAndPort)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 Channel (io.netty.channel.Channel)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 LoggingHandler (io.netty.handler.logging.LoggingHandler)2 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)2 TransportMessageDecoder (io.transport.sdk.protocol.codec.TransportMessageDecoder)2 TransportMessageEncoder (io.transport.sdk.protocol.codec.TransportMessageEncoder)2 DeviceRegistMessage (io.transport.sdk.protocol.message.internal.DeviceRegistMessage)2 TransportAuthenticationException (io.transport.sdk.exception.TransportAuthenticationException)1 TransportInitializeException (io.transport.sdk.exception.TransportInitializeException)1 ArrayList (java.util.ArrayList)1