package roart.common.communication.model;

import java.lang.reflect.Array;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

import roart.common.util.JsonUtil;
import roart.common.util.MathUtil;

public abstract class Communication {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    private String service;
    protected ObjectMapper mapper;
    protected Class myclass;
    private String returnService;
    protected String myname;
    protected boolean send;
    protected boolean receive;
    protected boolean sendreceive;
    protected String connection;
    public Communication(String myname, Class myclass, String service, ObjectMapper mapper, boolean send, boolean receive, boolean sendreceive, String connection) {
        this.myclass = myclass;
        this.service = service;
        this.mapper = mapper;
        this.myname = myname;
        this.send = send;
        this.receive = receive;
        this.sendreceive = sendreceive;
        this.connection = connection;
        if (sendreceive) {
            try {
                this.returnService = service + InetAddress.getLocalHost().getHostAddress() + System.currentTimeMillis();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public ObjectMapper getMapper() {
        return mapper;
    }

    public void setMapper(ObjectMapper mapper) {
        this.mapper = mapper;
    }

    public Class getMyclass() {
        return myclass;
    }

    public void setMyclass(Class myclass) {
        this.myclass = myclass;
    }

    public String getReturnService() {
        return returnService;
    }

    public void setReturnService(String returnService) {
        this.returnService = returnService;
    }

    public String getMyname() {
        return myname;
    }

    public void setMyname(String myname) {
        this.myname = myname;
    }

    protected String getSendService() {
        return service;
    }
    
    protected String getReceiveService() {
        if (returnService != null) {
            return returnService;            
        } else {
            return service;
        }
    }
        
    public abstract void send(String s);

    public void send(Object o) {
        System.out.println("sendchn " + getSendService());
        System.out.println("sendchn " + JsonUtil.convert(o));
        send(JsonUtil.convert(o, mapper));
    }

    public abstract String[] receiveString();

    public <T> T[] sendReceive2(Object param) {
        System.out.println("xxyy00");
        long time = System.currentTimeMillis();
        send(param);
        T[] r = receive();
        log.info("Rq time {}s for {} ", MathUtil.round((double) (System.currentTimeMillis() - time) / 1000, 1), service);
        //destroy();
        System.out.println("xxyy"+r);
        System.out.println("xxyy"+r.getClass().getName());
        return r;
    }

    public <T> T[] sendReceive(Object param) {
        System.out.println("xxyyzz00");
        long time = System.currentTimeMillis();
        send(param);
        T[] r = receive();
        log.info("Rq time {}s for {} ", MathUtil.round((double) (System.currentTimeMillis() - time) / 1000, 1), service);
        destroyTmp();
        System.out.println("xxyy"+r);
        System.out.println("xxyy"+r.getClass().getName());
        return r;
    }

    protected void destroyTmp() {
    }

    public <T> T[] receive() {
        System.out.println("recvchn " + getReceiveService());
        Class<T> aclass = myclass;
        String[] receives = receiveString();
        //T[] ts = new T[receives.length];
        T[] ts = (T[]) Array.newInstance(myclass, receives.length);
        int count = 0;
        for (String aReceive : receives) {
            System.out.println("t0"+aReceive);
            T t;
            if (aclass == String.class) {
                t = (T) aReceive;
            } else {
                t = JsonUtil.convertnostrip(aReceive, aclass);
            }
            System.out.println("t"+t);
            ts[count++] = t;
        }
        return ts;
    }

    public abstract void destroy();
}

@@ -2596,39 +2596,35 @@
     
-System.out.println(%22sendchn %22 +
+log.info(%22Send service %7B%7D%22,
  get
@@ -2643,70 +2643,8 @@
 ));%0A
-        System.out.println(%22sendchn %22 + JsonUtil.convert(o));%0A
     
@@ -2788,46 +2788,8 @@
 ) %7B%0A
-        System.out.println(%22xxyy00%22);%0A
     
@@ -2909,32 +2909,40 @@
 time %7B%7Ds for %7B%7D 
+class %7B%7D
 %22, MathUtil.roun
@@ -3011,126 +3011,53 @@
 vice
-);%0A        //destroy();%0A        System.out.println(%22xxyy%22+r);%0A        System.out.println(%22xxyy%22+r.getClass().getName()
+, r.getClass().getName());%0A        //destroy(
 );%0A 
@@ -3131,48 +3131,8 @@
 ) %7B%0A
-        System.out.println(%22xxyyzz00%22);%0A
     
@@ -3260,16 +3260,24 @@
  for %7B%7D 
+class %7B%7D
 %22, MathU
@@ -3354,127 +3354,54 @@
 vice
-);%0A        destroyTmp();%0A        System.out.println(%22xxyy%22+r);%0A        System.out.println(%22xxyy%22+r.getClass().getName()
+, r.getClass().getName());%0A        destroyTmp(
 );%0A 
@@ -3508,39 +3508,38 @@
     
-System.out.println(%22recvchn %22 +
+log.info(%22Receive service %7B%7D%22,
  get
@@ -3818,55 +3818,8 @@
 ) %7B%0A
-            System.out.println(%22t0%22+aReceive);%0A
     
@@ -4009,47 +4009,8 @@
   %7D%0A
-            System.out.println(%22t%22+t);%0A
     

Remove noise for communication layer (I232).