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;
}
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);
}
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();
}
}));
}
}
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);
}
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;
}
Aggregations