zsh_root
2024-01-02 7b595546af704983dbafcd0d385c8768ddacefc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package Netty;
 
import java.net.InetSocketAddress;
import java.util.Vector;
 
import BaoWen.Dell_55AA01;
import BaoWen.Dell_Baowen;
import BaoWen.Urt_7000_port;
import Frame.Tool;
import Method.BaoWenShow;
import Method.GetNowTime;
import Method.SaveFIleInTxt;
import PbuliClass.Systems;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.ResourceLeakDetector;
import monitorGas.ReadGas;
import rtkanchor.Dell_jizhan;
import rtkanchor.Jizhan;
import rtkanchor.Rtkac_manage;
import rtkanchor.SendMessage;
 
 
/*
˵Ã÷
1. ÎÒÃÇ×Ô¶¨ÒåÒ»¸öHandler ÐèÒª¼Ì³ÐNETTY  ¹æ¶¨ºÃµÄij¸öHandlerAdapter(¹æ·¶)
2. ÕâʱÎÒÃÇ×Ô¶¨ÒåÒ»¸öHandler , ²ÅÄܳÆÎªÒ»¸öhandler
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
 
 
    //ËùÓÐÔÚÏß»ùÕ¾µÄ¼¯ºÏ
    static Vector<String> tagidonlie = new Vector<String>();
 
    String tport = Systems.getTcpreceiveport();//ϵͳÉèÖô¦TCP½ÓÊÕ¶Ë¿Ú;
    StringBuffer message = new StringBuffer();
    static String ip = null;
    Tool tool = new Tool();
 
    //¶ÁÈ¡Êý¾Ýʵ¼Ê(ÕâÀïÎÒÃÇ¿ÉÒÔ¶ÁÈ¡¿Í»§¶Ë·¢Ë͵ÄÏûÏ¢)
    /*
    1. ChannelHandlerContext ctx:ÉÏÏÂÎĶÔÏó, º¬ÓР¹ÜµÀpipeline , Í¨µÀchannel, µØÖ·
    2. Object msg: ¾ÍÊǿͻ§¶Ë·¢Ë͵ÄÊý¾Ý Ä¬ÈÏObject
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Channel channel = ctx.channel();
        @SuppressWarnings("unused")
        ChannelPipeline pipeline = ctx.pipeline(); //±¾ÖÊÊÇÒ»¸öË«ÏòÁ´½Ó, ³öÕ¾ÈëÕ¾
        ByteBuf buf = (ByteBuf) msg;//½« msg ×ª³ÉÒ»¸ö ByteBuf
//        String message1=buf.toString(CharsetUtil.UTF_8);//ASCLL¸ñʽ±¨ÎÄ
//        System.out.println(message1);
        byte[] bytes = new byte[buf.readableBytes()];
        buf.getBytes(buf.readerIndex(), bytes);
        buf.readBytes(bytes);
        InetSocketAddress insocket = (InetSocketAddress) channel.remoteAddress();
        ip = insocket.getAddress().getHostAddress();
        String datas = Bytes2HexString(bytes);
        String message1=new String(bytes, 0, bytes.length);
        Dell_55AA01.dell_gui();
 
        if (message1.startsWith("55BB,HeightModify")) {
            Tool.HeightModify(message1);
 
        }else if (datas.startsWith("010314")){
            ReadGas.dell_gas2_data_new(ip,datas);
        }
 
        SaveFIleInTxt.save_baowen(datas);
        String port = String.valueOf(insocket.getPort());//É豸µÄ¶Ë¿Ú
        InetSocketAddress insocket2 = (InetSocketAddress) channel.localAddress();
        String port1 = String.valueOf(insocket2.getPort());//·þÎñÆ÷µÄ¶Ë¿Ú
        String message = "TCPÀ´×Ô" + port1 + "," + ip + ":" + port + "," + datas;
        String trim = message1.trim();
        String s = trim.toUpperCase();
        //55AA¿ªÍ·Êý¾Ý
        if (datas.toUpperCase().startsWith("55AA")) {
            Dell_Baowen.intsert(ip.toString(), datas.toUpperCase(), GetNowTime.timestamp2(), insocket.getPort());
        }else {
            Urt_7000_port.dellmessage(s, s, ip, insocket.getPort(), GetNowTime.timestamp2());
            BaoWenShow.ok("2", "ԭʼÊý¾Ý", s, ip, "´¦Àíԭʼ±¨ÎÄ");
            BaoWenShow.tcpshow(message, message);
        }
        if (port1.equals("tport")) {
 
        } else if (!Rtkac_manage.isPeizhijizhan()) {//µ±»ù×¼Õ¾²»ÔÚÅäÖÃģʽµÄʱºò
            Jizhan jz = Dell_jizhan.get_2port_jizhan(port1);//ͨ¹ýÊý¾ÝÉÏÀ´µÄ¶Ë¿ÚÕÒµ½»ùÕ¾
            if (jz != null) {
                SendMessage.sendmessage_to_urt(jz, datas);//½«²î·ÖÊý¾Ý·¢×߸øµÄÒÆ¶¯ÖÕ¶Ë
                jz.setAddtime(GetNowTime.now());
                jz.setBaoliu2(port);
                jz.setBaoliu1(ip);
                jz.setState("ÔÚÏß");
                int num = jz.getNum() + 1;
                if (num == 65535) {
                    num = 0;
                }
                jz.setNum(num);
            }
        }
 
        ReferenceCountUtil.release(buf);
        ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
 
 
    }
 
    //Êý¾Ý¶ÁÈ¡Íê±Ï
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
 
    }
 
    //´¦ÀíÒì³£, Ò»°ãÊÇÐèÒª¹Ø±ÕͨµÀ
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
 
    @SuppressWarnings("unused")
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
 
        if (evt instanceof IdleStateEvent) {
 
            //½«  evt ÏòÏÂתÐÍ IdleStateEvent
            IdleStateEvent event = (IdleStateEvent) evt;
            String eventType = null;
            switch (event.state()) {
                case READER_IDLE:
                    eventType = "¶Á¿ÕÏÐ";
                    break;
                case WRITER_IDLE:
                    eventType = "д¿ÕÏÐ";
                    break;
                case ALL_IDLE:
                    eventType = "¶Áд¿ÕÏÐ";
                    break;
            }
 
            InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
            StringBuffer ip = new StringBuffer(insocket.getAddress().getHostAddress());
 
 
            //Èç¹û·¢Éú¿ÕÏУ¬ÎÒÃǹرÕͨµÀ
            // ctx.channel().close();
        }
    }
 
    public static String getIp() {
        return ip;
    }
 
    /**
     * ½«×Ö½ÚÊý×éתΪ16½øÖÆ×Ö·û´®
     */
    public static String Bytes2HexString(byte[] b) {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }
 
    public String convertByteBufToString(ByteBuf buf) {
        String str;
        if (buf.hasArray()) { // ´¦Àí¶Ñ»º³åÇø
            str = new String(buf.array(), buf.arrayOffset() + buf.readerIndex(), buf.readableBytes());
        } else { // ´¦ÀíÖ±½Ó»º³åÇøÒÔ¼°¸´ºÏ»º³åÇø
            byte[] bytes = new byte[buf.readableBytes()];
            buf.getBytes(buf.readerIndex(), bytes);
            str = new String(bytes, 0, buf.readableBytes());
        }
        return str;
    }
 
 
}