|
|
@@ -1,22 +1,28 @@
|
|
|
package cn.vbdsm.hj212.modbus.server;
|
|
|
|
|
|
-import cn.vbdsm.hj212.modbus.coder.MyEncoder;
|
|
|
+import cn.vbdsm.hj212.modbus.coder.CodeEncoder;
|
|
|
import cn.vbdsm.hj212.modbus.config.ClientAttributeKey;
|
|
|
+import cn.vbdsm.hj212.modbus.handler.ClientHeartbeatHandler;
|
|
|
import cn.vbdsm.hj212.modbus.handler.TcpClientHandler;
|
|
|
import cn.vbdsm.hj212.modbus.ws.data.TerminalData;
|
|
|
import io.netty.bootstrap.Bootstrap;
|
|
|
+import io.netty.buffer.ByteBuf;
|
|
|
+import io.netty.buffer.Unpooled;
|
|
|
import io.netty.channel.*;
|
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
-import io.netty.channel.socket.SocketChannel;
|
|
|
+//import io.netty.channel.socket.SocketChannel;
|
|
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
|
|
|
|
|
import lombok.Getter;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.scheduling.support.CronTrigger;
|
|
|
|
|
|
+import java.io.*;
|
|
|
import java.net.InetSocketAddress;
|
|
|
-import java.util.Scanner;
|
|
|
+import java.net.Socket;
|
|
|
+import java.nio.ByteBuffer;
|
|
|
+import java.nio.channels.SocketChannel;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
@@ -44,24 +50,27 @@ public class TcpSocketClient {
|
|
|
this.host = host;
|
|
|
this.port = port;
|
|
|
this.terminalData = terminalData;
|
|
|
- init();
|
|
|
+ //init();
|
|
|
}
|
|
|
|
|
|
- public void init(){
|
|
|
- EventLoopGroup group = new NioEventLoopGroup();
|
|
|
- this.bootstrap = new Bootstrap();
|
|
|
- bootstrap.group(group).channel(NioSocketChannel.class)
|
|
|
- .option(ChannelOption.TCP_NODELAY, true)
|
|
|
- .handler(new ChannelInitializer<SocketChannel>() {
|
|
|
- @Override
|
|
|
- protected void initChannel(SocketChannel ch) throws Exception {
|
|
|
- ChannelPipeline pipeline = ch.pipeline();
|
|
|
- pipeline.addLast("encoder",new MyEncoder());
|
|
|
- pipeline.addLast("handler",new TcpClientHandler());
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- }
|
|
|
+// public void init(){
|
|
|
+// EventLoopGroup group = new NioEventLoopGroup(1);
|
|
|
+// this.bootstrap = new Bootstrap();
|
|
|
+// bootstrap.group(group).channel(NioSocketChannel.class)
|
|
|
+// .option(ChannelOption.TCP_NODELAY, true)
|
|
|
+// .option(ChannelOption.SO_KEEPALIVE, true)
|
|
|
+// .option(ChannelOption.SO_SNDBUF, 1024 * 1024)
|
|
|
+// .handler(new ChannelInitializer<SocketChannel>() {
|
|
|
+// @Override
|
|
|
+// protected void initChannel(SocketChannel ch) throws Exception {
|
|
|
+// ChannelPipeline pipeline = ch.pipeline();
|
|
|
+// pipeline.addLast("encoder",new CodeEncoder());
|
|
|
+// pipeline.addLast("handler",new TcpClientHandler());
|
|
|
+// pipeline.addLast("heartbeat",new ClientHeartbeatHandler());
|
|
|
+// }
|
|
|
+// });
|
|
|
+//
|
|
|
+// }
|
|
|
|
|
|
|
|
|
public void Connect(){
|
|
|
@@ -73,40 +82,117 @@ public class TcpSocketClient {
|
|
|
this.channel = f.sync().channel();
|
|
|
this.channel.attr(ClientAttributeKey.terminalKey).set(terminalData.getTerminalId());
|
|
|
isConnect = true;
|
|
|
- //断线重连
|
|
|
- f.addListener(new ChannelFutureListener() {
|
|
|
+ this.channel.eventLoop().schedule(new Runnable() {
|
|
|
@Override
|
|
|
- public void operationComplete(ChannelFuture channelFuture) throws Exception {
|
|
|
- if (!channelFuture.isSuccess()) {
|
|
|
- isConnect = false;
|
|
|
- final EventLoop loop = channelFuture.channel().eventLoop();
|
|
|
- loop.schedule(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- log.info("设备号:"+terminalData.getTerminalId()+" not connect service");
|
|
|
- Connect();
|
|
|
- }
|
|
|
- }, 15, TimeUnit.SECONDS);
|
|
|
- } else {
|
|
|
- isConnect = true;
|
|
|
- channel = channelFuture.channel();
|
|
|
- System.out.println("设备号:"+terminalData.getTerminalId()+" connected!");
|
|
|
- }
|
|
|
+ public void run() {
|
|
|
+ channel.writeAndFlush("数据读取完成!");
|
|
|
}
|
|
|
- });
|
|
|
+ }, 5, TimeUnit.MINUTES);
|
|
|
+ //断线重连
|
|
|
+// f.addListener(new ChannelFutureListener() {
|
|
|
+// @Override
|
|
|
+// public void operationComplete(ChannelFuture channelFuture) throws Exception {
|
|
|
+// if (!channelFuture.isSuccess()) {
|
|
|
+// isConnect = false;
|
|
|
+// final EventLoop loop = channelFuture.channel().eventLoop();
|
|
|
+// loop.schedule(new Runnable() {
|
|
|
+// @Override
|
|
|
+// public void run() {
|
|
|
+// log.info("设备号:"+terminalData.getTerminalId()+" not connect service");
|
|
|
+// Connect();
|
|
|
+// }
|
|
|
+// }, 15, TimeUnit.SECONDS);
|
|
|
+// } else {
|
|
|
+// isConnect = true;
|
|
|
+// channel = channelFuture.channel();
|
|
|
+// System.out.println("设备号:"+terminalData.getTerminalId()+" connected!");
|
|
|
+// }
|
|
|
+// }
|
|
|
+// });
|
|
|
} catch (InterruptedException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ log.error("->服务端连接失败...");
|
|
|
+ }finally {
|
|
|
+ //释放线程组资源
|
|
|
+ //group.shutdownGracefully();
|
|
|
}
|
|
|
//释放线程组资源
|
|
|
//group.shutdownGracefully();
|
|
|
}
|
|
|
|
|
|
- public void sendData(String msg){
|
|
|
+// public void sendData(String msg){
|
|
|
+// try {
|
|
|
+// this.channel.writeAndFlush(msg);
|
|
|
+// } catch (Exception e) {
|
|
|
+// throw new RuntimeException(e);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ public void sendData(String msg) {
|
|
|
+// try {
|
|
|
+// Socket socket = new Socket(this.host, this.port);
|
|
|
+// log.info("Connected to server...");
|
|
|
+// InputStream input = socket.getInputStream();
|
|
|
+// OutputStream output = socket.getOutputStream();
|
|
|
+// //PrintWriter writer = new PrintWriter(output, true);
|
|
|
+// //BufferedReader reader = new BufferedReader(new InputStreamReader(input));
|
|
|
+// //writer.println(msg);
|
|
|
+// output.write(msg.getBytes(StandardCharsets.UTF_8));
|
|
|
+// InputStreamReader in = new InputStreamReader(input, StandardCharsets.UTF_8);
|
|
|
+// //获取输入流里面数据并存储数据
|
|
|
+// byte[] b = new byte[1024];
|
|
|
+// StringBuilder sb = new StringBuilder() ;
|
|
|
+// String s;
|
|
|
+// if (in.read() != -1) {
|
|
|
+// s = new String(b);
|
|
|
+// //System.out.println(s);
|
|
|
+// sb.append(s);
|
|
|
+// }
|
|
|
+// log.info("来自服务器的数据:" + sb.toString());
|
|
|
+// output.flush();
|
|
|
+// output.close();
|
|
|
+// in.close();
|
|
|
+// socket.close();
|
|
|
+// } catch (IOException e) {
|
|
|
+// log.error("->服务端发送失败!!,{}",e.getMessage());
|
|
|
+// }
|
|
|
+ SocketChannel socketChannel = null;
|
|
|
try {
|
|
|
- this.channel.writeAndFlush(msg);
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ socketChannel = SocketChannel.open();
|
|
|
+ socketChannel.connect(new InetSocketAddress(this.host, this.port));
|
|
|
+ // 写入数据到socket
|
|
|
+ ByteBuffer buffer = ByteBuffer.allocate(2048);
|
|
|
+ buffer.put(msg.getBytes(StandardCharsets.UTF_8));
|
|
|
+ buffer.flip();
|
|
|
+ while (buffer.hasRemaining()) {
|
|
|
+ socketChannel.write(buffer);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 从socket读取数据
|
|
|
+ buffer.clear();
|
|
|
+ if (socketChannel.read(buffer) > 0) {
|
|
|
+ buffer.flip();
|
|
|
+ ByteBuf byteBuf = Unpooled.wrappedBuffer(buffer);
|
|
|
+ // 在这里处理你的ByteBuf数据
|
|
|
+ byte[] bytes = new byte[byteBuf.readableBytes()];
|
|
|
+ byteBuf.readBytes(bytes);
|
|
|
+ log.info("设备:{},接收到客户端数据{}",this.terminalData.getTerminalId(),new String(bytes));
|
|
|
+ buffer.clear();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("->服务端发送失败!!,{}",e.getMessage());
|
|
|
+ }finally {
|
|
|
+ if (socketChannel != null) {
|
|
|
+ try {
|
|
|
+ socketChannel.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("->服务端发送失败!!,关闭channel失败!{}",e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|