Netty 框架學習 —— UDP 廣播

低吟不作語發表於2021-07-08

UDP 廣播

面向連線的傳輸(如 TCP)管理兩個網路端點之間的連線的建立,在連線的生命週期的有序和可靠的訊息傳輸,以及最後,連線的有序終止。相比之下,類似 UDP 的無連線協議中則沒有持久化連線的概念,此外,UDP 也沒有 TCP 的糾錯機制。但 UDP 的效能比 TCP 要好很多,適合那些能夠處理或者忍受訊息丟失的應用程式

目前為止,我們所有的例子都是採用一種叫作單播的傳輸模式,定義為傳送訊息給一個由唯一地址所標識的單一的網路目的地。面向連線的協議和無連線協議都支援這種模式

UDP 提供了向多個接收者傳送訊息的額外傳輸模式:

  • 多播:傳輸到一個預定義的主機組
  • 廣播:傳輸到網路(子網)上的所有主機

本章示例將通過傳送能夠被同一個網路中的所有主機接收的訊息來演示 UDP 廣播的使用


UDP 示例應用程式

我們的程式將開啟一個檔案,隨後通過 UDP 把每一行作為一個訊息廣播到一個指定的埠。而接收方只需簡單地在指定埠上啟動一個監聽程式,便可以建立一個事件監視器來接受訊息。本次示例以日誌檔案處理程式為例

1. 訊息 POJO:LogEvent

在這個應用程式中,我們將會把訊息作為事件處理,並且由於該資料來自於日誌檔案,所以我們將它稱為 LogEvent

public class LogEvent {

    public static final byte SEPARATOR = (byte) ':';
    private final InetSocketAddress source;
    private final String logfile;
    private final String msg;
    private final long received;

    public LogEvent(String logfile, String msg) {
        this(null, logfile, msg, -1);
    }

    public LogEvent(InetSocketAddress source, String logfile, String msg, long received) {
        this.source = source;
        this.logfile = logfile;
        this.msg = msg;
        this.received = received;
    }

    public InetSocketAddress getSource() {
        return source;
    }

    public String getLogfile() {
        return logfile;
    }

    public String getMsg() {
        return msg;
    }

    public long getReceived() {
        return received;
    }
}

2. 編寫廣播者

Netty 的 DatagramPacket 是一個簡單的訊息容器,DatagramChannel 實現和遠端節點的通訊,要將 LogEvent 訊息轉換為 DatagramPacket,我們需要一個編碼器

下述是編碼器的程式碼實現

public class LogEventEncoder extends MessageToMessageEncoder<LogEvent> {

    private final InetSocketAddress remoteAddress;

    public LogEventEncoder(InetSocketAddress remoteAddress) {
        this.remoteAddress = remoteAddress;
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, LogEvent logEvent, List<Object> out) throws Exception {
        byte[] file = logEvent.getLogfile().getBytes(StandardCharsets.UTF_8);
        byte[] msg = logEvent.getMsg().getBytes(StandardCharsets.UTF_8);
        ByteBuf buf = ctx.alloc().buffer(file.length + msg.length + 1);
        buf.writeBytes(file);
        buf.writeByte(LogEvent.SEPARATOR);
        buf.writeBytes(msg);
        out.add(new DatagramPacket(buf, remoteAddress));
    }
}

接下來準備引導該伺服器,包括設定 ChannelOption,以及在 ChannelPipeline 中安裝所需的 ChannelHandler,這部分通過主類 LogEventBroadcaster 完成

public class LogEventBroadcaster {

    private final EventLoopGroup group;
    private final Bootstrap bootstrap;
    private final File file;

    public LogEventBroadcaster(InetSocketAddress address, File file) {
        group = new NioEventLoopGroup();
        bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioDatagramChannel.class)
                .option(ChannelOption.SO_BROADCAST, true)
                .handler(new LogEventEncoder(address));
        this.file = file;
    }

    public void run() throws Exception {
        // 繫結 Channel
        Channel ch = bootstrap.bind(0).sync().channel();
        long pointer = 0;
        for (; ; ) {
            long len = file.length();
            if (len < pointer) {
                // 將檔案指標指向檔案的最後一個位元組
                pointer = len;
            } else if (len > pointer) {
                RandomAccessFile raf = new RandomAccessFile(file, "r");
                // 設定當前檔案指標
                raf.seek(pointer);
                String line;
                while ((line = raf.readLine()) != null) {
                    ch.writeAndFlush(new LogEvent(null, line, file.getAbsolutePath(), -1));
                }
                pointer = raf.getFilePointer();
                raf.close();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.interrupted();
                break;
            }
        }
    }

    public void stop() {
        group.shutdownGracefully();
    }

    public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            throw new InterruptedException();
        }
        LogEventBroadcaster broadcaster = new LogEventBroadcaster(new InetSocketAddress("255.255.255.255",
                Integer.parseInt(args[0])), new File(args[1]));
        try {
            broadcaster.run();
        }
        finally {
            broadcaster.stop();
        }
    }
}

3. 編寫監視器

編寫一個稱為 LogEventMonitor 的消費者程式,它的作用包括:

  • 接收由 LogEventBroadcaster 廣播的 UDP DatagramPacket
  • 解碼為 LogEvent 訊息
  • 處理 LogEvent 訊息

和之前一樣,解碼器 LogEventDecoder 負責將傳入的 DatagramPacket 解碼為 LogEvent 訊息

public class LogEventDecoder extends MessageToMessageDecoder<DatagramPacket> {

    @Override
    protected void decode(ChannelHandlerContext ctx, DatagramPacket datagramPacket, List<Object> out) throws Exception {
        ByteBuf data = datagramPacket.content();
        int idx = data.indexOf(0, data.readableBytes(), LogEvent.SEPARATOR);
        String filename = data.slice(0, idx).toString(CharsetUtil.UTF_8);
        String logMsg = data.slice(idx + 1, data.readableBytes()).toString(CharsetUtil.UTF_8);
        LogEvent event = new LogEvent(datagramPacket.sender(), logMsg, filename, System.currentTimeMillis());
        out.add(event);
    }
}

建立一個處理 LogEvent 的 ChannelHandler

public class LogEventHandler extends SimpleChannelInboundHandler<LogEvent> {

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void messageReceived(ChannelHandlerContext ctx, LogEvent event) throws Exception {
        StringBuilder builder = new StringBuilder();
        builder.append(event.getReceived());
        builder.append("[");
        builder.append(event.getSource().toString());
        builder.append("] [");
        builder.append(event.getLogfile());
        builder.append("] : ");
        builder.append(event.getMsg());
        System.out.println(builder.toString());
    }
}

現在需要將 LogEventDecoder 和 LogEventHandler 安裝到 ChannelPipeline 中,下述程式碼展示瞭如何通過 LogEventMonitor 主類來做到這一點

public class LogEventMonitor {

    private final EventLoopGroup group;
    private final Bootstrap bootstrap;

    public LogEventMonitor(InetSocketAddress address) {
        group = new NioEventLoopGroup();
        bootstrap = new Bootstrap();
        bootstrap.group(group)
                .channel(NioDatagramChannel.class)
                .option(ChannelOption.SO_BROADCAST, true)
                .handler(new ChannelInitializer<Channel>() {

                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new LogEventDecoder());
                        pipeline.addLast(new LogEventHandler());
                    }
                })
                .localAddress(address);
    }

    public Channel bind() {
        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>");
        }
        LogEventMonitor monitor = new LogEventMonitor(new InetSocketAddress(Integer.parseInt(args[0])));
        try {
            Channel channel = monitor.bind();
            channel.closeFuture().sync();
        }
        finally {
            monitor.stop();
        }
    }
}

相關文章