| | |
| | | import com.ard.utils.util.GisUtils; |
| | | import io.netty.buffer.ByteBuf; |
| | | import io.netty.buffer.EmptyByteBuf; |
| | | import io.netty.channel.ChannelHandler; |
| | | import io.netty.channel.ChannelHandlerContext; |
| | | import io.netty.channel.ChannelId; |
| | | import io.netty.channel.ChannelInboundHandlerAdapter; |
| | | import io.netty.channel.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | |
| | |
| | | import java.net.InetSocketAddress; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.ScheduledFuture; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import static com.ard.utils.util.ByteUtils.byteToBitString; |
| | | import static com.ard.utils.util.ByteUtils.toLittleEndian; |
| | | |
| | | @Slf4j(topic = "netty") |
| | | @ChannelHandler.Sharable |
| | | public class BootNettyChannelInboundHandlerAdapter extends ChannelInboundHandlerAdapter { |
| | | public class BootNettyChannelInboundHandlerAdapter extends SimpleChannelInboundHandler<ByteBuf> { |
| | | |
| | | /** |
| | | * 从服务端收到新的数据时,这个方法会在收到消息时被调用 |
| | | */ |
| | | @Override |
| | | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception, IOException { |
| | | if(msg == null){ |
| | | return; |
| | | } |
| | | |
| | | //System.out.println("channelRead:read msg:"+msg1.toString()); |
| | | //BootNettyClientChannel bootNettyClientChannel = BootNettyClientChannelCache.get("clientId:"+ctx.channel().id().toString()); |
| | | //if(bootNettyClientChannel != null){ |
| | | // System.out.println("to do"); |
| | | // bootNettyClientChannel.setLast_data(msg1.toString()); |
| | | //} |
| | | protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) { |
| | | InetSocketAddress inSocket = (InetSocketAddress) ctx.channel().remoteAddress(); |
| | | String host = inSocket.getAddress().getHostAddress(); |
| | | int port = inSocket.getPort(); |
| | | ArdEquipRadar ardEquipRadar = ClientInitialize.tureConnectMap.get(host+":"+port); |
| | | // msg转Buf |
| | | ByteBuf buf = (ByteBuf) msg; |
| | | ArdEquipRadar ardEquipRadar = BootNettyClientChannelCache.getRadar(host + ":" + port); |
| | | if (ardEquipRadar != null) { |
| | | // 创建缓冲中字节数的字节数组 |
| | | byte[] byteArray = new byte[buf.readableBytes()]; |
| | | byte[] byteArray = new byte[msg.readableBytes()]; |
| | | // 写入数组 |
| | | buf.readBytes(byteArray); |
| | | msg.readBytes(byteArray); |
| | | // 处理接收到的消息 |
| | | byte[] bytes = MessageParsing.receiveCompletePacket(byteArray); |
| | | if (bytes != null) { |
| | | processData(ardEquipRadar, bytes); |
| | | } |
| | | //回应服务端 |
| | | //ctx.write("I got server message thanks server!"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public void channelReadComplete(ChannelHandlerContext ctx) throws IOException { |
| | | System.out.println("channelReadComplete"); |
| | | //System.out.println("channelReadComplete"); |
| | | ctx.flush(); |
| | | } |
| | | |
| | |
| | | super.channelActive(ctx); |
| | | // 客户端与服务端 建立连接 |
| | | InetSocketAddress inSocket = (InetSocketAddress) ctx.channel().remoteAddress(); |
| | | String clientIp = inSocket.getAddress().getHostAddress(); |
| | | String host = inSocket.getAddress().getHostAddress(); |
| | | int port = inSocket.getPort(); |
| | | log.debug("连接成功:【"+clientIp+":"+port+"】"); |
| | | log.debug("连接成功:【" + host + ":" + port + "】"); |
| | | } |
| | | |
| | | /** |
| | |
| | | String host = ipSocket.getHostString(); |
| | | log.error("与设备" + host + ":" + port + "连接断开!"); |
| | | // 重连 |
| | | BootNettyClientThread thread = new BootNettyClientThread(host,port); |
| | | ArdEquipRadar ardEquipRadar = BootNettyClientChannelCache.getRadar(host + ":" + port); |
| | | if (ardEquipRadar != null) { |
| | | BootNettyClientThread thread = new BootNettyClientThread(ardEquipRadar); |
| | | thread.start(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 解析报警数据 |
| | | */ |