package project.UserClient;

import project.common.*;
import java.io.*;
import java.net.*;
import javax.net.ssl.*;

public class ClientConnection
{
	SSLSocketFactory sockfactory;
	SSLSocket sock;
	BufferedIO io;

	public ClientConnection()
	{
		System.out.println("Creating SSLSocketFactory...");
		sockfactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
	}

	public BufferedIO newConnection(String server, int port)
	{
		BufferedInputStream sockIn;
		PrintStream sockOut;

		try{
			sock = (SSLSocket)sockfactory.createSocket(server, port);
			sockIn = new BufferedInputStream(sock.getInputStream());
			sockOut  = new PrintStream(sock.getOutputStream());
		} catch (IOException e) {
			System.out.println("Error: " + e.getMessage());
			return null;
		}
			
		io = new BufferedIO(sockIn, sockOut);
		String s = io.readln();
		if(s == null)
			return null;
		System.out.println(s);
		return io;
	}

	public void terminateConnection()
	{
		try {
			io.println("QUIT");
			io.readln();
			sock.close();
		} catch (IOException e) {
			System.out.println("Error: " + e.getMessage());
		}			
	}
}
