package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.logging.Level;


public class Server extends Thread {

	static protected ServerSocket ss;
	
	protected Socket socket;
	protected InputStream in;
	protected OutputStream out;
	
	public Server(Socket socket, int timeout) throws IOException {
		this.socket = socket;
		this.socket.setTcpNoDelay(true);
		this.socket.setSoTimeout(timeout);
		// this.socket.setSoLinger(true, timeout);

		InputStream tmpIn = socket.getInputStream();
		OutputStream tmpOut = socket.getOutputStream();
		
		tmpIn.read();

        byte[] data = new byte[4]; // Integer.SIZE / 8
        int type = tmpIn.read();
        tmpIn.read(data);
        // ByteBuffer buffer = ByteBuffer.allocate(data.length);
        //buffer.put(data).flip();
		tmpOut.write(0);
		tmpOut.flush();
		
		this.in = new BufferedInputStream(socket.getInputStream());
		this.out = new BufferedOutputStream(socket.getOutputStream());
	}

	public static void main(String[] args) {
		System.out.println("Server started");

	    String ip = "127.0.0.1";
	    if (args.length > 0)
	    	ip = args[0];
	    
	    int port = 44142;
	    if (args.length > 1)
	    	port = Integer.parseInt(args[1]);
	    
	    int timeout = 3000;
	    if (args.length > 2)
	    	timeout = Integer.parseInt(args[2]);
	    
		Socket socket = null;

		try {
			ss = new ServerSocket(port, 50, InetAddress.getByName(ip));
			socket  = ss.accept();
			Server server = new Server(socket, timeout);
			server.start();
			server.join();
	    } catch (Throwable e) {
			e.printStackTrace();
		}
	    finally {
	    	if (socket != null) {
	    		try {
	    			socket.close();
	    		} catch (IOException e) {
	    			e.printStackTrace();
	    		}
	    		socket = null;
	    	}
	    }
	    System.out.println("Server terminated");
	    System.exit(0);
	}
	
	@Override
	public void run() {
		byte[] data = new byte[21725];
		Arrays.fill(data, "*".getBytes()[0]);
		try {
			//send data at once
			//out.write(data);
			
			//send data little by little
			for (int i = 0; i < (21725-16)/2 ; i++) {
			  out.write(data, 0, 2);
			}
			
			out.flush();

			out.write(data, 0, 2);
			out.write(data, 0, 9);
			out.write(data, 0, 5);

			out.flush();

			out.close();
			out = null;
			
			System.out.println("zzz...");
			Thread.sleep(4 * 60 * 1000);
			System.out.println("wakeup.");
			
			if (out != null) {
				out.close();
				out = null;
			}
			if (in != null) {
				in.close();
				in = null;
			}
			if (socket != null) {
				socket.close();
				socket = null;
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
