use of io.transport.sdk.Configuration in project transporter by wang4ever.
the class TransportProgramTools method execute.
/**
* 执行并发请求 <br/>
* 启动一个线程推送消息.
*
* @param hostAndPort
* 服务器地址
* @param tName
* 并发线程名
* @param count
* 每个线程发送消息数
* @throws Exception
*/
private static void execute(String hostAndPort, String tName, int count) throws Exception {
// initialize.
Configuration config = new Configuration(appId, appSecret, groupId, DeviceType.FRONTEND, MyReceiveTextHandler.class);
config.setHostAndPorts(hostAndPort);
// Maximum can only be 32 byte.
config.setDeviceId(UUID.randomUUID().toString().replaceAll("-", ""));
// Join to server.
TransportClient client = new TransportClient(config).join();
// build.destroy(); // destroy instance.
// For test the registered WS client.
// System.out.println("准备注册WS连接设备... threadName=" + tName);
// client.registered(24 * 60 * 60, "111111111122222222223333333333ab");
// Definition message.
String payload = "{\"testKey\":\"test1\", \"TestData\":\"发送给浏览器的测试JSON报文ABCDEFG123456\"}";
int len = payload.getBytes(CharsetUtil.UTF_8).length;
// Loop sending.
for (int c = 0; c < count; c++) {
// client.unicast(toDeviceId, payload);
System.out.println("准备发送测试报文至浏览器 i=" + c + ", threadName=" + tName + ", len=" + len);
client.groupcast(toGroupId, payload);
// Random wait.
Thread.sleep(random.nextInt(1000));
}
System.out.println("测试执行结束. threadName=" + tName);
Thread.sleep(1000L);
System.exit(0);
}
use of io.transport.sdk.Configuration in project transporter by wang4ever.
the class HashRoutingLoadBalancer method main.
public static void main(String[] args) {
// 初始化
Configuration config = new Configuration(null, null, null, null);
List<HostAndPort> hostAndPorts = new ArrayList<>();
HostAndPort hap0 = new HostAndPort("192.168.1.100", 10030);
HostAndPort hap1 = new HostAndPort("192.168.1.101", 10030);
HostAndPort hap2 = new HostAndPort("192.168.1.102", 10030);
HostAndPort hap3 = new HostAndPort("192.168.1.103", 10030);
HostAndPort hap4 = new HostAndPort("192.168.1.104", 10030);
HostAndPort hap5 = new HostAndPort("192.168.1.105", 10030);
HostAndPort hap6 = new HostAndPort("192.168.1.106", 10030);
hostAndPorts.add(hap0);
hostAndPorts.add(hap1);
hostAndPorts.add(hap2);
hostAndPorts.add(hap3);
hostAndPorts.add(hap4);
hostAndPorts.add(hap5);
hostAndPorts.add(hap6);
config.setHostAndPorts(hostAndPorts);
HashRoutingLoadBalancer routingBalancer = new HashRoutingLoadBalancer(config);
// 模拟失败
routingBalancer.onConnectFailed(hap0);
routingBalancer.onConnectFailed(hap1);
routingBalancer.onConnectFailed(hap2);
routingBalancer.onConnectFailed(hap3);
routingBalancer.onConnectFailed(hap5);
routingBalancer.onConnectFailed(hap4);
routingBalancer.onConnectFailed(hap6);
System.out.println(routingBalancer.isRetryNode(hap0));
System.out.println(routingBalancer.determineCurrentLookupNode());
// 模拟重连
int crc16 = 23, nodes = 10;
int mod = crc16 % nodes;
int index = mod & (nodes - 1);
System.out.println(index);
}
use of io.transport.sdk.Configuration in project transporter by wang4ever.
the class MyTransportProgram method main.
public static void main(String[] args) throws Exception {
// initialize.
Configuration config = new Configuration(appId, appSecret, groupId, DeviceType.FRONTEND, MyReceiveTextHandler.class);
// build join to server.
TransportClients client = TransportClients.getInstance().build(config).join();
// build.destroy(); // destroy instance.
System.out.println("准备注册WS连接设备...");
client.registered(24 * 60 * 60, "111111111122222222223333333333ab");
Thread.sleep(2 * 1000L);
String payload = "{\"testKey\":\"test1\", \"TestData\":\"发送给浏览器的测试JSON报文ABCDEFG123456\"}";
long flag = 0l, count = 1;
while (flag < count) {
++flag;
// client.unicast(toDeviceId, payload);
System.out.println("准备发送测试报文到浏览器 ,len=" + payload.getBytes(CharsetUtil.UTF_8).length);
client.groupcast(toGroupId, payload);
Thread.sleep(1 * 1000L);
}
Thread.sleep(15 * 60 * 1000L);
System.exit(0);
}
Aggregations