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
package Netty;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import rtkanchor.Dell_jizhan;
import rtkanchor.Dell_urt;
 
import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import Method.BaoWenShow;
import Method.GetNowTime;
import PbuliClass.Systems;
 
public class GroupChatServerHandler extends SimpleChannelInboundHandler<String> {
    static String tport=Systems.getTcpreceiveport();//ϵͳÉèÖô¦TCP½ÓÊÕ¶Ë¿Ú
    //¶¨ÒåÒ»¸öchannle ×飬¹ÜÀíËùÓеÄchannel
    //GlobalEventExecutor.INSTANCE) ÊÇÈ«¾ÖµÄʼþÖ´ÐÐÆ÷£¬ÊÇÒ»¸öµ¥Àý
    public static ChannelGroup  channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
 
    //handlerAdded ±íʾÁ¬½Ó½¨Á¢£¬Ò»µ©Á¬½Ó£¬µÚÒ»¸ö±»Ö´ÐÐ
    //½«µ±Ç°channel ¼ÓÈëµ½  channelGroup
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
 
        Channel channel = ctx.channel();
        //½«¸Ã¿Í»§¼ÓÈëÁÄÌìµÄÐÅÏ¢ÍÆË͸øÆäËüÔÚÏߵĿͻ§¶Ë
        /*
        ¸Ã·½·¨»á½« channelGroup ÖÐËùÓеÄchannel ±éÀú£¬²¢·¢ËÍ ÏûÏ¢£¬ÎÒÃDz»ÐèÒª×Ô¼º±éÀú
         */
        channelGroup.add(channel); 
 
    }
 
    //¶Ï¿ªÁ¬½Ó, ½«xx¿Í»§À뿪ÐÅÏ¢ÍÆË͸øµ±Ç°ÔÚÏߵĿͻ§
    //    @Override
    //    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    //
    //        Channel channel = ctx.channel();
    //        channelGroup.writeAndFlush("[¿Í»§¶Ë]" + channel.remoteAddress() + " À뿪ÁË\n");
    //        System.out.println("channelGroup size" + channelGroup.size());
    //
    //    }
 
    //±íʾchannel ´¦Óڻ״̬, Ìáʾ xxÉÏÏß
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
        String ip=insocket.getAddress().getHostAddress();            
        String port=String.valueOf(insocket.getPort());    
        InetSocketAddress insocket2 = (InetSocketAddress)ctx.channel().localAddress();
        String port1=String.valueOf(insocket2.getPort());
        String message=port1+","+ip+":"+port+",ÉÏÏßÁË,"+GetNowTime.now();
        BaoWenShow.tcpshow(message,message);
        if(port1.equals(tport)) {
            Dell_urt.add_urt(ip, port);
        }else {
            Dell_jizhan.alertiport(port1, ip, port);
        }
 
    }
 
    //±íʾchannel ´¦ÓÚ²»»î¶¯×´Ì¬, Ìáʾ xxÀëÏßÁË
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
        InetSocketAddress insocket2 = (InetSocketAddress)ctx.channel().localAddress();
        String ip=insocket.getAddress().getHostAddress();
        String port1=String.valueOf(insocket2.getPort());
        String message=port1+","+ip+", ÀëÏßÁË,"+GetNowTime.now();
        BaoWenShow.tcpshow(message,message);
        Dell_urt.delete(ip);
        if(Dell_jizhan.get_2ip_jizhan(ip) !=null) {
            Dell_jizhan.get_2ip_jizhan(ip).setState("ÀëÏß");
        }
        
    }
 
    //¶ÁÈ¡Êý¾Ý
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
 
        //»ñÈ¡µ½µ±Ç°channel
        Channel channel = ctx.channel();
        //ÕâʱÎÒÃDZéÀúchannelGroup, ¸ù¾Ý²»Í¬µÄÇé¿ö£¬»ØËͲ»Í¬µÄÏûÏ¢
 
        channelGroup.forEach(ch -> {
            if(channel != ch) { //²»Êǵ±Ç°µÄchannel,ת·¢ÏûÏ¢
                ch.writeAndFlush("[¿Í»§]" + channel.remoteAddress() + " ·¢ËÍÁËÏûÏ¢" + msg + "\n");
            }else {//»ØÏÔ×Ô¼º·¢Ë͵ÄÏûÏ¢¸ø×Ô¼º
                ch.writeAndFlush("[×Ô¼º]·¢ËÍÁËÏûÏ¢" + msg + "\n");
            }
        });
    }
 
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //¹Ø±ÕͨµÀ
        ctx.close();
    }
 
    /**»ñȡij¸öÉÏÏßµÄÉ豸¶ÔÏó*/
    public static  Channel get_chanel (String ip) {
        Channel channel=null;
        Iterator<Channel> it=GroupChatServerHandler.channelGroup.iterator();
        while(it.hasNext()) {
            Channel channel1=it.next();
            InetSocketAddress insocket = (InetSocketAddress) channel1.remoteAddress();
            String ip1=insocket.getAddress().getHostAddress();            
            if(ip1.equals(ip)) {
                channel=channel1;
                break;
            }
        }
        return channel;
    }
 
}