package client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientRunner implements Runnable {
	long sleep;
	private String host;
	private Integer port;
	private boolean skip;

	public ClientRunner(String host, Integer port, long sleep, boolean skip) {
		this.sleep = sleep;
		this.host = host;
		this.port = port;
		this.skip = skip;
	}

	@Override
	public void run() {
		try {
			Socket socket = new Socket(host, port);

			BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);

			out.println("WRK");
			String work = in.readLine();
			System.out.println("Sleep: "+sleep+", skip: "+skip+", work: "+work);

			socket.close();
			if (!skip) {
				Thread.sleep(sleep);

				socket = new Socket(host, port);

				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);

				out.println("RES");
				out.println(work);

				socket.close();
			}
		} catch (Exception e) {
			System.out.println(e);
		}

	}

}
