package com.ld.igds.io.sample.lt;
|
|
import com.ld.igds.util.BytesUtil;
|
import com.ld.io.api.InvokeResult;
|
import io.netty.bootstrap.Bootstrap;
|
import io.netty.buffer.Unpooled;
|
import io.netty.channel.*;
|
import io.netty.channel.oio.OioEventLoopGroup;
|
import io.netty.channel.socket.SocketChannel;
|
import io.netty.channel.socket.oio.OioSocketChannel;
|
import io.netty.handler.codec.bytes.ByteArrayDecoder;
|
import io.netty.handler.codec.bytes.ByteArrayEncoder;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 扦样机 服务,平台作为客户端主动连接 扦样机
|
*
|
* @author: andy.jia
|
* @description:
|
* @version:
|
*/
|
public class LtCheckClientEngine implements Runnable {
|
|
private String host;
|
private int port;
|
// public ChannelFuture channelFuture;
|
public Channel channel;
|
|
/**
|
* 系统默认扦样机的状态信息
|
*/
|
//public static LtCHeckMac CHECK_MAC = new LtCHeckMac();
|
public static Map<String ,LtCHeckMac> CHECK_MAC = new HashMap<>();
|
LtCheckClientEngine(String host, int port) {
|
this.host = host;
|
this.port = port;
|
}
|
|
public void start() {
|
Thread thread = new Thread(this);
|
thread.start();
|
}
|
|
@Override
|
public void run() {
|
try {
|
startRun();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public void startRun() throws Exception {
|
EventLoopGroup group = new OioEventLoopGroup();
|
Bootstrap b = new Bootstrap();
|
// b.option(ChannelOption.SO_KEEPALIVE, true);
|
b.group(group).channel(OioSocketChannel.class)
|
.handler(new ChannelInitializer<SocketChannel>() {
|
@Override
|
protected void initChannel(SocketChannel ch)
|
throws Exception {
|
ChannelPipeline p = ch.pipeline();
|
// 字符串解码 和 编码
|
p.addLast("decoder", new ByteArrayDecoder());
|
p.addLast("encoder", new ByteArrayEncoder());
|
// 自己的逻辑Handler
|
p.addLast("handler", new ClientHandler());
|
}
|
});
|
|
// 发起异步连接请求,绑定连接端口和host信息
|
ChannelFuture channelFuture = b.connect(host, port);
|
this.channel = channelFuture.channel();
|
// channelFuture.channel().closeFuture().sync();
|
|
channelFuture.addListener(new ChannelFutureListener() {
|
@Override
|
public void operationComplete(ChannelFuture arg0) throws Exception {
|
if (channelFuture.isSuccess()) {
|
System.out.println("连接服务器成功");
|
} else {
|
System.out.println("连接服务器失败");
|
channelFuture.cause().printStackTrace();
|
group.shutdownGracefully(); // 关闭线程组
|
}
|
}
|
});
|
}
|
|
public InvokeResult send(byte[] array) throws InterruptedException {
|
if (null == channel) {
|
return InvokeResult.SOCKET_NOT_CREATE;
|
}
|
if (!channel.isActive()) {
|
return InvokeResult.CHANNEL_CLOSED;
|
}
|
channel.writeAndFlush(Unpooled.copiedBuffer(array)).sync();
|
|
return InvokeResult.SUCCESS;
|
}
|
|
public static void main(String[] args) {
|
try {
|
|
String msg = "000000000006010600070012";
|
LtCheckClientEngine test = new LtCheckClientEngine(
|
"192.168.1.5", 502);
|
|
test.start();
|
|
msg = "000000000006010300000007";
|
|
// Thread.sleep(3000L);
|
|
test.send(BytesUtil.hexStrToBytes(msg));
|
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
|
public Channel getChannel() {
|
return channel;
|
}
|
|
}
|