package com.pacoapp.paco.net.tesp;

import com.pacoapp.paco.net.tesp.message.TespMessage;

import java.io.*;
import java.net.Socket;
import java.util.logging.Logger;

public class TespMessageSocket<R extends TespMessage, S extends TespMessage> {
    private static final int defaultTimeoutMs = 5000;

    public static final Logger log = Logger.getLogger(TespMessageSocket.class.getName());

    private final TespCodec tespCodec = TespCodec.getInstance();

    private final Socket socket;
    private final InputStream socketInputStream;
    private final OutputStream socketOutputStream;

    private final int timeoutMs;
    private final boolean isAsync = false;

    public TespMessageSocket(Socket socket) throws IOException {
        this(socket, defaultTimeoutMs);
    }

    public TespMessageSocket(Socket socket, int timeout) throws IOException {
        this.socket = socket;
        socketOutputStream = socket.getOutputStream();
        socketInputStream = socket.getInputStream();

        this.timeoutMs = timeout;
    }

    public void add(S message) throws IOException {
      socketOutputStream.write(tespCodec.encode(message));
      socketOutputStream.flush();


        // byte[] encodedMessage = tespCodec.encode(message);
        // System.out.println("encodedmessage size: " + encodedMessage.length);
        // try {
        //     Socket socket2 = new Socket("127.0.0.1", 31415);
        //     OutputStream outputStream = socket2.getOutputStream();
        //     outputStream.write(encodedMessage);
        //     outputStream.flush();
        //     outputStream.close();
        //     socket2.close();
        //     System.out.println("Done writing to socket");
        // } catch (IOException e) {
        //     log.info("Caught exception in add(message): " +e.getMessage());
        //     e.printStackTrace();
        //     throw e;
        // }
//
//        byte[] buf = new byte[1024];
//        int cnt=0;
//        int tmp = 0;
//        while (( tmp = socketInputStream.read(buf)) > 0) {
//            cnt += tmp;
//        }
//        System.out.println("received back bytes: " + cnt);
    }

    public void close() {
        log.info("Closing socket");
        try {
            socketOutputStream.flush();
            socket.close();
        } catch (IOException e) {
            log.warning("Exception closing TespMessageSocket socket: " + e.getMessage());
        }
    }

    public boolean isBroken() {
        return !socket.isConnected();
    }
}

@@ -1181,956 +1181,8 @@
 ();%0A
-%0A%0A        // byte%5B%5D encodedMessage = tespCodec.encode(message);%0A        // System.out.println(%22encodedmessage size: %22 + encodedMessage.length);%0A        // try %7B%0A        //     Socket socket2 = new Socket(%22127.0.0.1%22, 31415);%0A        //     OutputStream outputStream = socket2.getOutputStream();%0A        //     outputStream.write(encodedMessage);%0A        //     outputStream.flush();%0A        //     outputStream.close();%0A        //     socket2.close();%0A        //     System.out.println(%22Done writing to socket%22);%0A        // %7D catch (IOException e) %7B%0A        //     log.info(%22Caught exception in add(message): %22 +e.getMessage());%0A        //     e.printStackTrace();%0A        //     throw e;%0A        // %7D%0A//%0A//        byte%5B%5D buf = new byte%5B1024%5D;%0A//        int cnt=0;%0A//        int tmp = 0;%0A//        while (( tmp = socketInputStream.read(buf)) %3E 0) %7B%0A//            cnt += tmp;%0A//        %7D%0A//        System.out.println(%22received back bytes: %22 + cnt);%0A
     

remove commented code