package serverHandlers;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class TimeClientHandler extends ChannelInboundHandlerAdapter {
	
	private static final Logger LOG = LogManager.getLogger(TimeClientHandler.class);
	
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
    	ByteBuf in = (ByteBuf) msg;
		String requestMsg  =in.toString(StandardCharsets.UTF_8 );
		
		String response = handleClientRequest(requestMsg);
		
		
		ctx.write(Unpooled.copiedBuffer(response+"\r\n", StandardCharsets.UTF_8));
		ctx.flush(); // (2)
    }

    private String handleClientRequest(String requestMsg) {

//		LOG.info("handleClientRequest:"+requestMsg);
		
		if(requestMsg.contains("OK")){
			//add the ip:port to the group member list;
			
			
//			String[] arr = requestMsg.split(":");
//			
//			InetSocketAddress addr = new InetSocketAddress(arr[1].trim(), Integer.parseInt(arr[2].trim()));
//			server.addMemberToList(addr);
			LOG.info("Client rreceived OK!!");
			return "";
		}
		
		
		return "...";
		
	
	}

	@Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}
@@ -269,16 +269,102 @@
 pooled;%0A
+import io.netty.channel.ChannelFuture;%0Aimport io.netty.channel.ChannelFutureListener;%0A
 import i
@@ -612,21 +612,223 @@
 lass);%0A%09
-%0A    
+private ByteBuf content;%0A%09private ChannelHandlerContext ctx;%0A%09%0A%09@Override%0A%09public void channelActive(ChannelHandlerContext ctx) throws Exception %7B%0A%09%09%0A%09%09this.ctx = ctx;%0A%09%09sendMessage(%22Join me!!%22);%0A%09%09%0A%09%7D%0A%09%0A%09%0A%09
 @Overrid
@@ -1781,9 +1781,533 @@
 ;%0A    %7D%0A
+%09%0A%09%0A%09%0A%09private void sendMessage(String msg) %7B%0A%09%09// TODO Auto-generated method stub%0A%09%09ctx.writeAndFlush(Unpooled.copiedBuffer(msg+%22%5Cr%5Cn%22, StandardCharsets.UTF_8)).addListener(listener1);%0A%09%7D%0A%09%0A%09private final ChannelFutureListener listener1 = new ChannelFutureListener()%7B%0A%0A%09%09public void operationComplete(ChannelFuture future) throws Exception %7B%0A%09%09%09if(future.isSuccess())%7B%0A%09%09%09%09LOG.info(%22in listener1. op complete success%22);%0A%09%09%09%7D%0A%09%09%09else%7B%0A%09%09%09%09future.cause().printStackTrace();%0A%09%09%09%09future.channel().close();%0A%09%09%09%7D%0A%09%09%09%0A%09%09%7D%0A%09%09%0A%09%7D; %0A
 %7D

fix 2