目录结构
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();
}
}
}