package com.yichao.woo.netty.in.action.ch13;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.MessageToMessageEncoder;
import java.net.InetSocketAddress;
import java.util.List;
import static io.netty.util.CharsetUtil.UTF_8;
/**
* Created by Yichao-Woo.
*/
public class LogEventEncoder extends MessageToMessageEncoder<LogEvent> {
private final InetSocketAddress remoteAddress;
public LogEventEncoder(InetSocketAddress remoteAddress) {
this.remoteAddress = remoteAddress;
}
@Override
protected void encode(ChannelHandlerContext ctx, LogEvent msg, List<Object> out) throws Exception {
ByteBuf buf = ctx.alloc().buffer();
buf.writeBytes(msg.getLogfile().getBytes(UTF_8));
buf.writeByte(LogEvent.SEPARATOR);
buf.writeBytes(msg.getMsg().getBytes(UTF_8));
out.add(new DatagramPacket(buf, remoteAddress));
}
}