import java.io.*;
import java.net.*;


/****************************************************************************
* Class Connector                                                           *
* It provides facilities for the GUI to send and receive messages from the  *
* IndiGolog control through a tcpip communication link.                     *
****************************************************************************/
public class Connector
{
    private String linkName;      // name of the communication link
    private String hostName;      // name of the host
    private int    portNum;       // port number

    private Socket         sckt;
    private PrintWriter    output;
    private BufferedReader input;
    private boolean        connected;


    /**
    * Constructor of class Connector
    */
    public Connector(String name, String host, int port)
    {
        linkName  = new String(name);
        hostName  = new String(host);
        portNum   = port;
        connected = false;
    }

    /**
    * It sends the input message to the IndiGolog control
    * Pre : connectedToIndi() == true
    */
    public void sendMessage(String message)
    {
        output.println("term(" + message + ").");
    }

    /**
    * It reads a message from the IndiGolog control. If there is no
    * incoming message, then it will return nil
    * Pre : connectedToIndi() == true
    */
    public String receiveMessage()
    {
        String message = null;

        try
        {
            /* check if there is any message from the IndiGolog control */
            message = input.readLine();
        }
        catch (IOException e)
        {
            System.err.println("Could not read message from socket");
        }

        return message;
    }

    /**
    * It creates a communication link to the IndiGolog control. Notice that
    * it will send its name as an acknowledgement to the IndiGolog control
    * after the connection is created successfully
    * Pre : connectedToIndi() == false
    */
    public void connectToIndi()
    {
        try
        {
            /* establish the connection to the IndiGolog program */
            sckt   = new Socket(hostName, portNum);
            input  = new BufferedReader(new InputStreamReader(sckt.getInputStream()));
            output = new PrintWriter(sckt.getOutputStream(), true);

            connected = true;

            /* acknowledge the IndiGolog control */
            sendMessage(linkName.toLowerCase());
        }
        catch (IOException e)
        {
            System.err.println("Failed to create the I/O stream");
        }
        catch (NullPointerException e)
        {
            System.err.println("Could not establish the connection");
        }
    }

    /**
    * It closes the communication link to the IndiGolog control
    * Pre : connectedToIndi() == true
    */
    public void disconnectFromIndi()
    {
        try
        {
            /* close the connection */
            input.close();
            output.close();
            sckt.close();
            connected = false;
        }
        catch (IOException e)
        {
            System.err.println("Could not close the connection");
        }
    }

    /**
    * It returns true if the communication link to the IndiGolog control
    * has been established
    */
    public boolean connectedToIndi()
    {
        return connected;
    }
}
