1 package net.anyflow.lannister.server;
2
3 import java.util.List;
4
5 import io.netty.buffer.ByteBuf;
6 import io.netty.channel.ChannelHandlerContext;
7 import io.netty.handler.codec.MessageToMessageCodec;
8 import net.anyflow.lannister.Statistics;
9 import net.anyflow.lannister.Statistics.Criterion;
10
11 public class ByteCounterCodec extends MessageToMessageCodec<ByteBuf, ByteBuf> {
12 @SuppressWarnings("unused")
13 private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ByteCounterCodec.class);
14
15 @Override
16 protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
17 Statistics.INSTANCE.add(Criterion.BYTE_SENT, msg.retain().readableBytes());
18 Statistics.INSTANCE.add(Criterion.MESSAGES_SENT, 1);
19
20 out.add(msg);
21 }
22
23 @Override
24 protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
25 Statistics.INSTANCE.add(Criterion.BYTE_RECEIVED, msg.retain().readableBytes());
26 Statistics.INSTANCE.add(Criterion.MESSAGES_RECEIVED, 1);
27
28 out.add(msg);
29 }
30 }