【Netty】UDP廣播事件

來源:互聯網
上載者:User

標籤:remote   monit   ann   readline   builder   oge   tcp   class   end   

一、前言

  前面學習了WebSocket協議,並且通過樣本講解了WebSocket的具體使用,接著學習如何使用不需連線的UDP來廣播事件。

二、UDP廣播事件

  2.1 UDP基礎

  連線導向的TCP協議管理端到端的串連,在串連生命週期中,發送的訊息會有序並且可靠地進行傳輸,最後串連有序地終止。然而,在無連線協定(如UDP)中,沒有持久串連的概念,每個訊息(UDP資料報)都是獨立的傳輸,此外,UDP沒有TCP的錯誤修正機制(即每個對等體會確認其接收到的分組,並且寄件者會重傳未確認的分組)。

  UDP的限制比TCP多,但是比TCP快很多,這是因為消除了握手和訊息管理的所有開銷,UDP非常適合處理或容忍訊息丟失的應用。

  2.2 UDP廣播

  迄今為止所有的樣本都使用了單播的傳輸模式,其被定義為將訊息發送到由唯一地址標識的單個網路目的地,有串連和不需連線的協議都支援這種模式,UDP為多個收件者發送訊息提供了額外的傳輸模式:

    · 組播--傳輸到定義的主機群組。

    · 廣播--傳輸到網路(或子網)上的所有主機。

  本章中的樣本將通過發送在同一網路上的所有主機接收的訊息來使用UDP廣播。

  2.3 UDP簡單樣本

  樣本將開啟一個檔案,並通過UDP將每一行廣播為指定連接埠。展示了應用的結構圖。

  

  2.4 LogEvent POJO

  在訊息應用中,訊息經常以POJO形式展現,LogEvent的POJO如下。  

public final 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, -1, logfile, msg);    }    public LogEvent(InetSocketAddress source, long received,        String logfile, String msg) {        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 getReceivedTimestamp() {        return received;    }}

  2.5 編寫broadcaster

  Netty提供了許多類來支援UDP應用程式,如Netty的DatagramPacket是DatagramChannel實現與遠程對等體進行通訊的簡單訊息容器,我們需要一個編碼器將EventLog訊息轉換為DatagramPackets,可以擴充Netty的MessageToMessageEncoder,LogEventEncoder的代碼如下。  

public class LogEventEncoder extends MessageToMessageEncoder<LogEvent> {    private final InetSocketAddress remoteAddress;    public LogEventEncoder(InetSocketAddress remoteAddress) {        this.remoteAddress = remoteAddress;    }    @Override    protected void encode(ChannelHandlerContext channelHandlerContext,        LogEvent logEvent, List<Object> out) throws Exception {        byte[] file = logEvent.getLogfile().getBytes(CharsetUtil.UTF_8);        byte[] msg = logEvent.getMsg().getBytes(CharsetUtil.UTF_8);        ByteBuf buf = channelHandlerContext.alloc()            .buffer(file.length + msg.length + 1);        buf.writeBytes(file);        buf.writeByte(LogEvent.SEPARATOR);        buf.writeBytes(msg);        out.add(new DatagramPacket(buf, remoteAddress));    }}

  完成編碼器後,即可以開始啟動服務端,其中服務端LogEventBroadcaster的代碼如下。  

public class LogEventBroadcaster {    private final Bootstrap bootstrap;    private final File file;    private final EventLoopGroup group;    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 IOException {        Channel ch = bootstrap.bind(0).syncUninterruptibly().channel();        System.out.println("LogEventBroadcaster running");        long pointer = 0;        for (;;) {            long len = file.length();            if (len < pointer) {                // file was reset                pointer = len;            } else if (len > pointer) {                // Content was added                RandomAccessFile raf = new RandomAccessFile(file, "r");                raf.seek(pointer);                String line;                while ((line = raf.readLine()) != null) {                    ch.writeAndFlush(new LogEvent(null, -1, file.getAbsolutePath(), line));                }                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 IllegalArgumentException();        }        LogEventBroadcaster broadcaster = new LogEventBroadcaster(new InetSocketAddress("255.255.255.255",                Integer.parseInt(args[0])), new File(args[1]));        try {            broadcaster.run();        } finally {            broadcaster.stop();        }    }}

  2.6 編寫monitor

  在應用中

    · 接收由LogEventBroadcaster廣播的UDP DatagramPackets。

    · 將其解碼為LogEvent。

    · 將LogEvent寫入輸出資料流System.out。

  展示LogEvent的流動。

  

  LogEventDecoder負責將傳入的DatagramPackets解碼為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 i = data.indexOf(0, data.readableBytes(), LogEvent.SEPARATOR);        String filename = data.slice(0, i).toString(CharsetUtil.UTF_8);        String logMsg =  data.slice(i + 1, data.readableBytes()).toString(CharsetUtil.UTF_8);        LogEvent event = new LogEvent(datagramPacket.recipient(), System.currentTimeMillis(),                filename,logMsg);        out.add(event);    }}

  而LogEventHandler用於處理LogEvent,其代碼如下。  

public class LogEventHandler extends SimpleChannelInboundHandler<LogEvent> {    @Override    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {        cause.printStackTrace();        ctx.close();    }    @Override    public void channelRead0(ChannelHandlerContext channelHandlerContext, LogEvent event) throws Exception {        StringBuilder builder = new StringBuilder();        builder.append(event.getReceivedTimestamp());        builder.append(" [");        builder.append(event.getSource().toString());        builder.append("] [");        builder.append(event.getLogfile());        builder.append("] : ");        builder.append(event.getMsg());        System.out.println(builder.toString());    }}

  LogEventMonitor用於將處理器添加至管道中,其代碼如下。  

public class LogEventMonitor {    private final Bootstrap bootstrap;    private final EventLoopGroup group;    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 channel) throws Exception {                        ChannelPipeline pipeline = channel.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();            System.out.println("LogEventMonitor running");            channel.closeFuture().await();        } finally {            monitor.stop();        }    }}

  運行LogEventBroadcaster和LogEventMonitor

三、總結

  本篇博文講解了UDP協議,以及其樣本,在實際應用中需要根據不同的應用情境選擇不同的協議,謝謝各位園友的觀看~

【Netty】UDP廣播事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.