/*
 *****************************************************************************
 *                                                                           *
 *              IMPINJ CONFIDENTIAL AND PROPRIETARY                          *
 *                                                                           *
 * This source code is the sole property of Impinj, Inc. Reproduction or     *
 * utilization of this source code in whole or in part is forbidden without  *
 * the prior written consent of Impinj, Inc.                                 *
 *                                                                           *
 * (c) Copyright Impinj, Inc. 2014. All rights reserved.                     *
 *                                                                           *
 *****************************************************************************/

package com.impinj.stp;

import java.io.*;
import java.net.*;

 public class TcpIpCommunication {
    private InputStream inputStream = null;
    private OutputStream outputStream = null;
    private Socket socket = null;
    private byte[] testBuf = null;
    private boolean jUnitTest = false;
    private int ATTEMPTS = 10;
    protected TcpIpCommunication(String ipOrHost, int timeoutMS) throws IOException {
        if (!ipOrHost.equals(Device.JUNIT_TEST_TAG)) {
            // TCP/IP socket interface
            socket = new Socket();
            SocketAddress socketAddress = new InetSocketAddress(ipOrHost, Device.PORT);
            socket.connect(socketAddress, timeoutMS);
            socket.setTcpNoDelay(true);
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
        }
        else {
            File pipeFile = new File("TestPipe");
            try {
                jUnitTest = true;
                outputStream = new FileOutputStream(pipeFile);
                inputStream = new FileInputStream(pipeFile);
            }catch(FileNotFoundException e){
                e.printStackTrace();
            }
        }
    }
    protected boolean isjUnitTest() {
        return jUnitTest;
    }
    protected Socket getSocket() {
        return socket;
    }
   /**
     * Closes connection between Speedway and the STP application
     */
   protected void Close() {
       try {
             if (outputStream != null) {
                 outputStream.close();
                 outputStream = null;
             }
             if (inputStream != null) {
                 inputStream.close();
                 inputStream = null;
             }
             if (socket != null) {
                 socket.close();
                 socket = null;
             }
        } catch (IOException e) {
             e.printStackTrace();
        }
   }
   protected void write(byte buf[]) throws IOException {
       outputStream.write(buf);
   }
   protected byte[] read(int length) throws IOException {
       byte buf[] = new byte[length];
       int attempt = ATTEMPTS;
       int off = 0;
       int bytesRead = inputStream.read(buf, off, length);
       while (bytesRead < length && attempt-- > 0) {
             off += bytesRead;
             bytesRead += inputStream.read(buf, off, length-off);
       }
       if (attempt == 0 || bytesRead != length)
           throw new IOException("GetNextToHost: Unable to read payload!!");
       return buf;
   }
}