Netty实战的第13章UDP的server

目录结构

LogEvent

package com.xmg.nnetty.udp;
importjava.net.InetSocketAddress;
public class LogEvent {
    public static final byte SEPARATOR =  (byte)':';
    private final InetSocketAddress address;
    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 address,  long received, String logfile2, String msg2) {
        this.address = address;
        this.received = received;
        this.logfile = logfile2;
        this.msg = msg2;
    }
    public static byte getSeparator() {
        return SEPARATOR;
    }
    public InetSocketAddress getAddress() {
        return address;
    }
    public String getLogfile() {
        return logfile;
    }
    public String getMsg() {
        return msg;
    }
    public long getReceived() {
        return received;
    }
    
}

LogEventEncoder

package com.xmg.nnetty.udp;

import java.net.InetSocketAddress;
import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.MessageToMessageEncoder;

public class LogEventEncoder extends MessageToMessageEncoder<LogEvent>{

    private final InetSocketAddress address;
    
    public LogEventEncoder(InetSocketAddress address) {
        this.address = address;
    }
    
    @Override
    protected void encode(ChannelHandlerContext ctx, LogEvent logEvent, List<Object> out) throws Exception {
        byte[] file = logEvent.getLogfile().getBytes();
        byte[] msg = logEvent.getMsg().getBytes();
        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,address));
        System.out.println("编码成功");
    }

}

LogEventBroadcaster

package com.xmg.nnetty.udp;

import java.io.File;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

public class LogEventBroadcaster {

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

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

    public void run() throws Exception {
        Channel ch = strap.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, -1, file.getAbsolutePath(), line));
                }
                pointer = raf.getFilePointer();
                raf.close();
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                Thread.interrupted();
                break;
            }
        }
    }

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

    public static void main(String[] args) throws Exception {
        LogEventBroadcaster broadcaster = new LogEventBroadcaster(
                new InetSocketAddress("255.255.255.255", 9001),
                new File("D:/a.txt"));
        try {
            broadcaster.run();
        }
        finally {
            broadcaster.stop();
        }
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值