package learner;

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

public class SutWrapper {
	private Socket sock;
	private PrintWriter sockout;
	private BufferedReader sockin;

	public SutWrapper() {
		try {
			sock = new Socket("localhost", 7892);
			sock.setTcpNoDelay(true);  // remove unnecesarry delay in socket communication!
			sockout = new PrintWriter(sock.getOutputStream(), true);
			sockin = new BufferedReader(new InputStreamReader(sock.getInputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String sendInput(String input) {
		try {	
			System.out.println("SutWrapper sending: " + input);

			// Send input to SUT
			sockout.println(input);
			sockout.flush();

			// Receive output from SUT
			String concreteOutput = sockin.readLine();
			
			return concreteOutput;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void sendReset() {
		System.out.println("SutWrapper sending: reset");
		sockout.println("reset");
		sockout.flush();
	}
	
	public void sendConnect() {
		System.out.println("SutWrapper sending: connect");
		sockout.println("connect");
		sockout.flush();
	}
	
	public String getATC() {
		try {	
			System.out.println("SutWrapper sending: getATC");

			// Send input to SUT
			sockout.println("getATC");
			sockout.flush();

			// Receive output from SUT
			String concreteCounter = sockin.readLine();
			System.out.println("concreteCounter in SUTWrapper: " + concreteCounter);
			return concreteCounter;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public void close() {
		try {
			sockin.close();
			sockout.close();
			sock.close();
		} catch (IOException ex) {
			
		}
	}
}
