package io.sunfly.push.client;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.AttributeKey;
import io.sunfly.push.model.Notification;
import io.sunfly.push.wan.message.PushNotification;

public class ClientHandler extends ChannelInboundHandlerAdapter {
    public static final AttributeKey<String> AK_DEVICE_ID = AttributeKey.newInstance("deviceId");

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
            throws Exception {
        if (evt instanceof IdleStateEvent) {
            ByteBuf buf = ctx.alloc().ioBuffer(2, 2);
            buf.writeShort(2);

            ctx.writeAndFlush(buf);
        }
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        if (msg instanceof PushNotification) {
            PushNotification pn = (PushNotification)msg;
            Notification notification = pn.getNotification();
            System.out.format("deviceId = %s, topic = %s, create time = %s, content = %s\n",
                    ctx.channel().attr(AK_DEVICE_ID).get(), notification.getTopic(),
                    notification.getCreateTime(), notification.getContent());
        } else {
            System.err.println("Unknown message: " + msg.getClass().getSimpleName());
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        cause.printStackTrace();
    }
}

@@ -336,16 +336,74 @@
 ation;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 public c
@@ -460,16 +460,104 @@
 apter %7B%0A
+    private static final Logger logger = LoggerFactory.getLogger(ClientHandler.class);%0A%0A
     publ
@@ -1244,25 +1244,19 @@
     
-System.out.format
+logger.info
 (%22de
@@ -1264,18 +1264,18 @@
 iceId = 
-%25s
+%7B%7D
 , topic 
@@ -1280,18 +1280,18 @@
 c = 
-%25s
+%7B%7D
 , create
  tim
@@ -1290,18 +1290,18 @@
 eate
- 
+_
 time = 
-%25s
+%7B%7D
 , co
@@ -1312,12 +1312,10 @@
 t = 
-%25s%5Cn
+%7B%7D
 %22,%0A 
@@ -1509,25 +1509,18 @@
     
-System.err.printl
+logger.war
 n(%22U
@@ -1722,30 +1722,45 @@
     
-cause.printStackTrace(
+logger.warn(%22Exception caught%22, cause
 );%0A 

use slf4j to log info instead of System.out