package com.zipcoder.server;


import java.io.IOException;
import java.net.Socket;

public class RequestHandler {
    private final Socket clientSocket;


    public RequestHandler(Socket clientSocket){
        this.clientSocket = clientSocket;
    }

    public void run() {
        try {
            handleRequest();
        } catch (Exception e) {
            Logger.error("Cannot handle request because " + e.getMessage(), e);
        } finally {
            closeSocket();
        }
    }

    private void closeSocket() {
        if (clientSocket != null){
            try {
                clientSocket.close();
            } catch (IOException e) {
                Logger.error("Cannot close socket because " + e.getMessage(), e);
            }
        }
    }

    private void handleRequest() throws IOException {
        ResponseHello response = new ResponseHello(this.clientSocket.getOutputStream());
        response.writeResponse();
    }
}
