//   Course Number: COSC 650 Data Comm/Networks
//      Class Name: ClientConnection
//         Authors: Brian Hoffman
//                  Derek Waby
//                  Kypros Ioannou
//                  Harsha V. Reddy          
//     Description: This class encapsulates all of the information that the
//                  server needs to know about its connection to the client.
//                  It keeps track of the socket and the input and output streams
//                  and provides several convenient methods for accessing them.

// Java Core Packages
import java.io.*;
import java.net.*;

public class ServerConnection
{ 
    private Socket connection;
    private ObjectOutputStream output;
    private ObjectInputStream input;
    
    /**
     * ------------------------------------------------------------------------
     * constructor - takes the socket generated by the ServerSocket accept 
     *               method and creates an object to represent the connection
     *               and it streams
     * ------------------------------------------------------------------------
     */
    public ServerConnection(Socket inClient) throws IOException
    {
        connection = inClient;
        
        // get the input and output streams and flush header from output
        output = new ObjectOutputStream(connection.getOutputStream());
        output.flush();
        input = new ObjectInputStream(connection.getInputStream());
   }
    
    /* ------------------------------------------------------------------------
     * getMethods - methods that allow the user to fetch information about the
     *              connection
     *-------------------------------------------------------------------------
     */
    public Socket getSocket() { return connection; }
    public ObjectOutputStream getObjOutput() { return output; } 
    public ObjectInputStream getObjInput() { return input; }
    
    /**
     * ------------------------------------------------------------------------
     * sendObject - uses the encapsulated ObjectOutputStream to send an object 
     *              to the client. This is typically a ShamirMsg object.
     * ------------------------------------------------------------------------
     */
    public synchronized void sendObject(Object msg) throws IOException
    {
        output.writeObject(msg);
    }
    
    /**
     * ------------------------------------------------------------------------
     * readObject - blocking call that reads an object from the ObjectInputStream
     * ------------------------------------------------------------------------
     */
    public Object readObject() throws IOException, ClassNotFoundException
    {
        return input.readObject();
    }
    
    /**
     * ------------------------------------------------------------------------
     * close - closes the connectoion and handles any errors that occur
     * ------------------------------------------------------------------------
     */
    public void close()
    {
        try
        {
            output.close();
            input.close();
            connection.close();
        }
        catch(IOException ioException)
        {
            ioException.printStackTrace();
        }   
    }
}