/*
 * COPYRIGHT (c) 1998-1999 Jan-Henrik Haukeland, http://www.tildeslash.com/
 */


package ts.mail;

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

import ts.mail.MailModel;
import ts.config.Constant;

/** 
 * This class is an immutable abstract mail server. It could
 * represent a MTA (Mail Transport Agent) or a server for retrieving
 * mail (e.g. POP3 server). The class is an abstract frontend to a
 * Mail server and implements general properties for such a server.
 *
 *
 * @author  Jan-Henrik Haukeland &lt;hauk@tildeslash.com&gt;
 * @version $Id: AbstractMailServer.java,v 1.9 2001/08/13 12:48:32 hauk Exp $ 
 */
public abstract class  AbstractMailServer
{

  public static final int  SO_TIMEOUT= Constant.MAILSERVER_TIMEOUT;


  // ---------------------- protected members ---------------------- //

  /** Mail server host Internet address */
  protected InetAddress myMailServer;
  
  /** Mail server port number */
  protected int         myMailServerPortNumber;



  /** A socket for connecting to the Mail Server */
  private   Socket      socket;



  // ----------------------- Constructors ----------------------- //
 
  /**
   * Initializes the object with values for the mail server
   *
   * @param aHost - The mail server host. The value can either be a 
   *                machine name, such as "mail.online.no", or a string
   *                representing its IP address, such as "193.212.1.34".
   *
   * @param aPort - The server portnumber 
   *
   * @exception UnknownHostException  Is thrown if no IP address 
   *                                  for the host could be found
   */
  public AbstractMailServer(String aHost, int aPort) 
      throws UnknownHostException
  {
    myMailServer= InetAddress.getByName(aHost);
    myMailServerPortNumber= aPort;
  }


  // ------------------------- Get methods ------------------------ //


  /**
   * @return Returns the mail servers IP address as "%d.%d.%d.%d"
   */
  public String getMailServer()
  {
    return myMailServer.getHostAddress();
  }


  /**
   * @return Returns the mail servers hostname
   */
  public String getMailServerName()
  {
    return myMailServer.getHostName();
  }


  /**
   * @return  Returns the mail server's port number
   */
  public int getMailServerPortNumber()
  {
    return myMailServerPortNumber;
  }


  // --------------------------------------------------------------- //



  /**
   * Connects to the server and return a connected socket 
   * @return a connected socket
   */
  protected Socket openConnection() throws IOException 
  {
    socket= new Socket(myMailServer, myMailServerPortNumber);
    socket.setSoTimeout(SO_TIMEOUT);
    socket.setTcpNoDelay(true);
    return socket;
  }



  
  /**
   * Close the connection to the mail server
   */
  protected void closeConnection() throws IOException 
  {
    socket.close();
  }




  /**
   * Returns an inputstream from the connected socket
   * @return an inputstream from the mail server
   * @exception IOException if not connected
   */
  protected InputStream getInputStream() throws IOException
  {
    if ( socket == null ) 
	throw new IOException("Not connected to mail server");
    return socket.getInputStream();
  }




  /**
   * Returns an outputstream to the mail server for writing
   * @return an outputstream to the mail server
   * @exception IOException if not connected
   */
  protected OutputStream getOutputStream() throws IOException
  {
    if ( socket == null ) 
	throw new IOException("Not connected to mail server");
    return socket.getOutputStream();
  }


}
