content: package com.lgp.netty.netty_ch8;
  
  import io.netty.bootstrap.Bootstrap;
  import io.netty.channel.*;
  import io.netty.channel.nio.NioEventLoopGroup;
  import io.netty.channel.socket.nio.NioDatagramChannel;
  
  import java.net.InetSocketAddress;
  
  /**
   * @AUTHOR lgp
   * @DATE 2019/1/3 14:55
   * @DESCRIPTION
   **/
  public class LogEventMonitor {
  
      private final Bootstrap bootstrap;
      private final EventLoopGroup group;
  
      public LogEventMonitor(InetSocketAddress address) {
          group = new NioEventLoopGroup();
          bootstrap = new Bootstrap();
          //1 引导 NioDatagramChannel。设置 SO_BROADCAST socket 选项。
          bootstrap.group(group)
                  .channel(NioDatagramChannel.class)
                  .option(ChannelOption.SO_BROADCAST, true)
                  .handler(new ChannelInitializer<Channel>() {
                      @Override
                      protected void initChannel(Channel channel) throws Exception {
                          ChannelPipeline pipeline = channel.pipeline();
                          //2 添加 ChannelHandler 到 ChannelPipeline
                          pipeline.addLast(new LogEventDecoder());
                          pipeline.addLast(new LogEventHandler());
                      }
                  }).localAddress(address);
  
      }
  
      public Channel bind() {
          //3 绑定的通道。注意,在使用 DatagramChannel 是没有连接，因为这些 无连接
          return bootstrap.bind().syncUninterruptibly().channel();
      }
  
      public void stop() {
          group.shutdownGracefully();
      }
  
      public static void main(String[] args) throws Exception {
  //        if (args.length != 1) {
  //            throw new IllegalArgumentException("Usage: LogEventMonitor <port>");
  //        }
          //4 构建一个新的 LogEventMonitor
  //        LogEventMonitor monitor = new LogEventMonitor(new InetSocketAddress(Integer.parseInt(args[0])));
          LogEventMonitor monitor = new LogEventMonitor(new InetSocketAddress(44444));
          try {
              Channel channel = monitor.bind();
              System.out.println("LogEventMonitor running");
  
              channel.closeFuture().await();
          } finally {
              monitor.stop();
          }
      }
  }
  