package com.wdkl.ncs.android.middleware.udp;

import java.util.concurrent.CountDownLatch;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;

@ChannelHandler.Sharable
public class UdpClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {

    //使用此应用为同步回调
    private CountDownLatch latch;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
        String response = msg.content().toString(CharsetUtil.UTF_8);
        System.out.println(response);
        /*
        jsonObject.put("public_ip", tcpServerDTO.getPublicIp());
        jsonObject.put("local_ip", tcpServerDTO.getLocalIp());
        jsonObject.put("tcp_port", tcpServerDTO.getTcpPort());
        jsonObject.put("reader_idle_time", tcpServerDTO.getReaderIdleTime());
         */
//        JsonResponse jsonResponse = JSON.parseObject(response, JsonResponse.class);
//        if (jsonResponse.success){
            UdpClient.result = response;
            latchCountDown();
//        }
        ctx.close();
    }

    public void latchCountDown(){
        latch.countDown();
    }

    public void resetLatch(CountDownLatch latch){
        this.latch = latch;
    }

}
