//package com.xinyingpower.microphoto.request;
//
//import android.annotation.SuppressLint;
//
//import com.dowse.base.log.ByteTools;
//
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.io.ObjectOutputStream;
//import java.util.concurrent.TimeUnit;
//
//import io.netty.buffer.ByteBuf;
//import io.netty.buffer.Unpooled;
//import io.netty.channel.ChannelHandler;
//import io.netty.channel.ChannelHandlerContext;
//import io.netty.channel.EventLoop;
//import io.netty.channel.SimpleChannelInboundHandler;
//import io.netty.channel.socket.DatagramPacket;
//import io.netty.handler.codec.MessageToByteEncoder;
//import io.netty.handler.timeout.IdleStateEvent;
//
//@ChannelHandler.Sharable
//public class ReconnectHandler extends SimpleChannelInboundHandler<Object> {
//    private int retries = 0;
//    private RetryPolicy mRetryPolicy;
//    private NettyChatClient mNettyChatClient;
//
//    public ReconnectHandler(NettyChatClient nettyChatClient) {
//        this.mNettyChatClient = nettyChatClient;
//    }
//
//    @Override
//    public void channelActive(ChannelHandlerContext ctx) throws Exception {
////        LogUtil.e("Successfully established a connection to the server.");
//        retries = 0;
//        ctx.fireChannelActive();
//    }
//
//    @SuppressLint("DefaultLocale")
//    @Override
//    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
//        if (retries == 0) {
////            LogUtil.e("Lost the TCP connection with the server.");
//            ctx.close();
//        }
//        boolean allowRetry = getRetryPolicy().allowRetry(retries);
//        if (allowRetry) {
//            long sleepTimeMs = getRetryPolicy().getSleepTimeMs(retries);
////            LogUtil.e(String.format("Try to reconnect to the server after %dms. Retry count: %d.", sleepTimeMs, ++retries));
//            final EventLoop eventLoop = ctx.channel().eventLoop();
//            eventLoop.schedule(() -> {
//                System.out.println("Reconnecting ...");
//                mNettyChatClient.connect();
//            }, sleepTimeMs, TimeUnit.MILLISECONDS);
//        }
//        ctx.fireChannelInactive();
//    }
//
//    /**
//     * 心跳监测
//     *
//     * @param ctx
//     * @param evt
//     * @throws Exception
//     */
//    @Override
//    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
//        if (evt instanceof IdleStateEvent) {
//            IdleStateEvent event = (IdleStateEvent) evt;
//            String eventType = null;
//            switch (event.state()) {
//                case READER_IDLE:
//                    ctx.close();
//                    eventType = "读空闲";
//                    break;
//                case WRITER_IDLE:
//                    eventType = "写空闲";
//                    break;
//                case ALL_IDLE:
//                    eventType = "读写空闲";
//                    break;
//                default:
//            }
////            LogUtil.e(ctx.channel().remoteAddress() + " " + eventType);
//        }
//    }
//
//
//    /**
//     * 收到消息后调用
//     */
//
//    @Override
//    public void channelRead0(ChannelHandlerContext ctx, Object message) {
////        LogUtil.e("======================");
////        LogUtil.e("收到消息" + message);
//
//
//        byte[] message1 = (byte[]) message;
//        bytesW(message1);
//
////        ByteBuf in = (ByteBuf) message;
////        StringBuffer msgObj = new StringBuffer();
////        for (int i = in.readerIndex(); i < in.writerIndex(); i++) {
////            String hexString = Integer.toHexString((int) in.getByte(i));
////            if (hexString.length() == 0) {
////                hexString = "00";
////            }
////            hexString = hexString.toUpperCase().replace("FFFFFF", "");
////            msgObj.append(hexString.length() == 1 ? "0" + hexString : hexString);
////        }
////        System.out.println(msgObj.toString().replace(" ", ""));
////        bytesW(((String) message).getBytes());
////        System.out.println("d423432432f");
//
//
//            try(ByteArrayOutputStream bos = new ByteArrayOutputStream();
//                ObjectOutputStream oos = new ObjectOutputStream(bos)) {
//                oos.writeObject(message);
//                oos.flush();
//                bytesW(bos.toByteArray());
//
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            } finally {
//
//            }
//        }
//
//
//    private RetryPolicy getRetryPolicy() {
//        if (this.mRetryPolicy == null) {
//            this.mRetryPolicy = mNettyChatClient.getRetryPolicy();
//        }
//        return this.mRetryPolicy;
//    }
//
//    public void decode(ByteBuf buf) {
//        try {
//            byte[] b = new byte[buf.readableBytes()];
//            buf.readBytes(b);
//
//            String curMsg = new String(b, "UTF-8").trim();
////            Log.i("Log", "curMsg == " + curMsg);
//            System.out.println(curMsg);
//
//            //数据处理的业务逻辑(根据与后端开发人员约定的规则对数据进行处理)
//            //...
//
//        } catch (Exception e) {
////            Log.i("netty exception", e.toString());
//        }
//    }
//
//    public static void bytesW( byte[] data) {
//        if (data == null || data.length <= 0) {
//            return;
//        }
//        String str = " " + ByteTools.byteToHexStr(data);
//        if (str != null && str.length() > 400) {
//            str = str.substring(0, 400) + " ....";
//        }
//        System.out.println(str);
//    }
//}