package elect;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.Socket;


public class SocketConnection {

	private Socket socket;
	
	/**
	 * add by chenhaobin
	 * 2015-08-18
	 * @param client
	 */
	public void setSocket(Socket client){
		socket = client;
	}

	/**
	 * getClient 获取客户端
	 * 
	 * @return 客户端对象
	 */
	public static SocketConnection getConnection() {
		SocketConnection socketConnection = null;

		if (socketConnection == null) {
			socketConnection = new SocketConnection();
		}
		return socketConnection;
	}
	
	/**
	 * connect 连接服务端
	 *  @param ipAddress 服务端IP
	 * @param port 服务端口
	 * @return 连接成功：true 连接成功：false
	 */

	public boolean connect(String ipAddress, int port,int timeout) {
		//int SOCKET_TIMEOUT = 30 * 1000;

		try {
			socket = new Socket(ipAddress, port);
			//socket.setKeepAlive(true);
			socket.setSoTimeout(timeout);
			

			return true;
		} catch (Exception ex) {
			System.out.println("connect Exception" + ex.getMessage());
			return false;
		}
	}


	public byte[] read() {
		int SOCKET_BUFFERLENGTH = 0x10000;

		byte[] receiveData = null;

		try {
			InputStream inputStream = null;
			inputStream = socket.getInputStream();
			BufferedInputStream bufferedInputStream = new BufferedInputStream(
					inputStream);

			try {
				byte[] receiveBuffer = new byte[SOCKET_BUFFERLENGTH];
				int receiveLength = bufferedInputStream.read(receiveBuffer, 0,
						SOCKET_BUFFERLENGTH);

				if (receiveLength == -1) {
					System.out.println("The connection has been closed.");
				} else {
					receiveData = new byte[receiveLength];
					for (int i = 0; i < receiveLength; i++) {
						receiveData[i] = receiveBuffer[i];
					}
				}
			} catch (InterruptedIOException e) {
				e.printStackTrace();
			} catch (IOException e) {
				System.out.println("read InterruptedIOException" + e.getMessage());
				e.printStackTrace();
			}
		} catch (IOException e) {
			System.out.println("read IOException" + e.getMessage());
			e.printStackTrace();
		}

		return receiveData;
	}


	public boolean write(byte[] sendData) {
		if (sendData != null) {
			return write(sendData, 0, sendData.length);
		} else {
			return false;
		}

	}


	public boolean write(byte[] sendData, int start, int end) {
		boolean sendResult;

		if (sendData == null) {
			sendResult = false;
		} else if (sendData.length == 0) {
			sendResult = false;
		} else {
			try {
				OutputStream outputstream = null;
				outputstream = socket.getOutputStream();
				BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
						outputstream);

				try {
					bufferedOutputStream.write(sendData, start, end);
					bufferedOutputStream.flush();
					sendResult = true;
				} catch (IOException e) {
					e.printStackTrace();
					sendResult = false;
				}
			} catch (IOException e) {
				System.out.println("write IOException" + e.getMessage());
				e.printStackTrace();
				sendResult = false;
			}
		}

		return sendResult;
	}

	/**
	 * connect 关闭服务端
	 * @return 关闭成功：true 关闭成功：false
	 */

	public synchronized boolean disconnect() {
		try {
			if(socket != null){
				socket.close();
				return true;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}
}
