package ProtocolLayer;

/**
  * Abstract FTP connection. FTPCon will include FTPCon name, remote site initial
  * directory, FTP server host name, FTP server host port(derault 21), login
  * user name/ password, local machine initial path for get operation,
  * file list to get/put, FTP mode and error message.<P>
  * Field description :<BR>
  * <B>String _id</B> : Since JATLite ftp is assumed to be used by software agents,
  * unique FTP connection name will be necessary. For instance, if an agent
  * publishes some data to FTP server A and to FTP server B, the agent needs to
  * identify different FTP connection using _id.<BR>
  * <B>String _remotePath</B> : Remote path is an initial directory where you want to
  * send/save data to a remote file system. Change directory command will be
  * passed to FTP server if you set this parameter. For instnce, if an agent wants to
  * put data to a file /home/myName/AgentData , remote path will be /home/myName.<BR>
  * <B>String _host</B> : FTP server host name, e.g. cdr.stanford.edu<BR>
  * <B>int _port</B> : FTP server port number. Dafault is 21, which usually works. Change
  * port number only if your FTP server has different, special port number<BR>
  * <B>String _userName, _password</B>  Valid user name and password for FTP server where
  * an agent wants to save/retrieve data. _userName can be "anonymous".<BR>
  * <B>String _localPath</B> : Initial local directory where an agent wants to save retrieved
  * data. If an agent wants to save data as a file /home/myName/fileRetrieved, then
  * _localPath is /home/myName.<BR>
  * <B>String[] files</B> : Files to save or to retrieve. Note that files should include
  * only the file names and should not include any directory information. Directory
  * information should be set by _remotePath and _localPath.<BR>
  * <B>int _mode</B> : Various mode parameters. Modes can be combined using bitwise OR(|).
  * FTPCon initially assumes PUT(e.g. save data to FTP server) mode. If GET(retrieve
  * from remote FTP site) mode, ISGETMODE should be set. If APPEND mode, APPEND should
  * be set. If you want to create a directory to FTP site, CREATEDIR should be set.
  * If you want to save/retrieve ASCII, ASCIITYPE should be set. If an agent is an applet,
  * ISAPPLET should be set.<BR>
	* <B>String _errorMsg </B> : Error message buffer if FTP error occurs.
	*/

/*
* Copyright (c) 1996,1997,1998 <A HREF="http://cdr.stanford.edu/">Center for Design Research</A>, Stanford University.
* All rights reserved.<p>
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You can obtain the GNU General Public License at
* http://www.gnu.org/copyleft/gpl.html or by writing to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* This software is bound by the terms and conditions listed in the
* attached <a href="../LICENSE">LICENSE file</A>.
*  @author <A HREF="http://cdr.stanford.edu/html/people/jeon-bio.html>Heecheol Jeon</A>
*  @version Java(tm) 1.1
*/

public class FTPCon  {

	/**
	  * Unique FTPCon name.
	  */
	protected String _id;

	/**
	  * FTP site initial directory
	  */
	protected String _remotePath;

	/**
	  * FTP server host name, e.g. cdr.stanford.edu
	  */
	protected String _host;

	/**
	  * If you did not setup a special FTP port, you need not to set port number. 21 is assumed
	  */
	protected int _port;

	/**
	  * Login user name for FTP server
	  */
	protected String _userName;

	/**
	  * Login password for FTP server
	  */
	protected String _password;

	/**
	  * Local directory for GET operation
	  */
	protected String _localPath;

	/**
	  * Files to GET or PUT
	  */
	protected String[] _files;

	/**
	  * Various mode definition. Initially, PUT, not append, not create directory,
	  * binary transfer and stand alone will be assumed. If an agent has different
	  * mode when transfers data, mode should be set using OR(|).
	  */
	protected int _mode;  // true for get, default

	/**
	  * Error message buffer, if error occurs during transaction
	  */
	protected String _errorMsg = "";

	/**
	  * Set GET(retrieve file) mode
	  */
	public static final int ISGETMODE = 1;

	/**
	  * Set append mode
	  */
	public static final int APPEND = 2;

	/**
	  * Create directory for remote site, if not exist
	  */
	public static final int CREATEDIR = 4;

	/**
	 * ASCII transfer mode
	 */
	public static final int ASCIITYPE = 8; // true for ascii.

	/**
	  * Applet data transfer mode
	  */
	public static final int ISAPPLET = 16;

	/**
	  * Default constructor.
	  */
	public FTPCon()   {
		_id = "";
		_remotePath = "";
		_host = "";
		_port = 21;
		_userName = "";
		_password = "";
		_localPath = "";
		_files = null;
		_mode = 0;
	}

	/**
	  * Constructor from FTP connection information
	  * @param id FTP connection name
	  * @param remotePath FTP site initial directory
	  * @param host FTP server host name
	  * @param port 21. Otherwiser, you need to set
	  * @param userName FTP server login user name
	  * @param password FTP server login password
	  * @param localPath Local machine initial directory
	  * @param files Files to put or get
	  * @param mode Mode definition
	  */
	public FTPCon(String id,
							  String remotePath,
								String host,
								int port,
								String userName,
								String password,
								String localPath,
								String[] files,
								int mode)  {
		if(id == null)  {
			_id = "";
		}
		else  {
			_id = id;
		}

		if(remotePath == null)  {
			_remotePath = "";
		}
		else  {
			_remotePath = remotePath;
		}

		if(host == null)  {
			_host = "";
		}
		else  {
			_host = host;
		}

		_port = port;

		if(userName == null)  {
			_userName = "";
		}
		else  {
			_userName = userName;
		}

		if(password == null)  {
			_password = "";
		}
		else  {
			_password = password;
		}

		if(localPath == null)  {
			_localPath = "";
		}
		else {
			_localPath = localPath;
		}

		_files = files;
		_mode = mode;
	}

	/**
	  * Set FTPCon name
	  * @param id FTPCon name
	  */
	public void setID(String id)  {
		_id = id;
	}

	/**
	  * Get FTPCon name
	  * @return FTPCon name
	  */
	public String getID()  {
		return _id;
	}

	/**
	  * Set FTP site initial path
	  * @param path FTPCon initial path
	  */
	public void setRemotePath(String path)  {
		_remotePath = path;
	}

	/**
		* Get FTP site initial path
		* @return Remote ftp site initial path, set for this FPTCon
	  */
	public String getRemotePath() {
		return _remotePath;
	}

	/**
	  * Set FTP server host name
	  * @param host FTP server host name
	  */

	public void setHost(String host)  {
		_host = host;
	}

	/**
	  * Get FTP host internet address
	  * @return FTP server host name.
	  */
	public String getHost()  {
		return _host;
	}

	/**
	  * Set FTP server port
	  * @param port Port number
	  */
	public void setPort(int port)  {
		_port = port;
	}

	/**
	  * Get current FTP server port
	  * @return FTP server port
	  */
	public int getPort()  {
		return _port;
	}

	/**
	  * Set FTP server user login name
	  * @param username FTP server user login name
	  */
	public void setUserName(String username)  {
		_userName = username;
	}

	/**
	  * Get FTP server login user name
	  * @return login user name
	  */
	public String getUserName()  {
		return _userName;
	}

	/**
	  * Set FTP server login password
	  * @param password FTP server login password
	  */
	public void setPassword(String password)  {
		_password = password;
	}

	/**
 	  * Get FTP server login password
	  * @return login password
	  */
	public String getPassword()  {
		return _password;
	}

	/**
	  * Set local directory for GET operation
	  * @param path Local machine initial path
	  */
	public void setLocalPath(String path)  {
		_localPath = path;
	}

	/**
	  * Get current local path.
	  * @return Current local path for GET operation
	  */
	public String getLocalPath()  {
		return _localPath;
	}

	/**
	  * Set file list to PUT or GET
	  * @param files File name list
	  */
	public void setFiles(String[] files)  {
		_files = files;
	}

	/**
	  * Get current files to PUT or GET
	  * @return file list
	  */
	public String[] getFiles()  {
		return _files;
	}

	/**
	  * Set FTPCon mode
	  * @param mode FTPCon mode
	  */
	public void setMode(int mode)  {
		_mode = mode;
	}

	/**
	  * Get current mode
	  * @return current mode
	  */
	public int getMode()  {
		return _mode;
	}

	/**
	  * See if current mode is GET
	  * @return true if current mode is GET
	  */
	public boolean isGetMode()  {
		if((_mode & ISGETMODE) != 0)  return true;
		return false;
	}

	/**
	  * Make a readable string
	  * @return FTPCon description
	  */
	public String toString()  {
		String retstr = "Remote Path :" + _remotePath + "\r\n";
		retstr = retstr + "Host :" + _host + "\r\n";
		retstr = retstr + "Port :" + Integer.toString(_port) + "\r\n";
		retstr = retstr + "User name:" + _userName + "\r\n";
		retstr = retstr + "Password:" + _password + "\r\n";
		retstr = retstr + "Local path:" + _localPath + "\r\n";
		retstr = retstr + "Files:";
		for(int i=0; i < _files.length; i++)  {
			retstr = retstr + _files[i] + " ";
		}
		retstr = retstr + "\r\n" + "Mode:";
		if((_mode & ISAPPLET) != 0)  {
				retstr = retstr + "Applet ";
		}

		if((_mode & ISGETMODE) != 0)  {
			retstr = retstr + "Get ";
		}
		else  {
			retstr = retstr + "Put ";
		}

		if((_mode & APPEND) != 0) {
			retstr = retstr + "Append ";
		}

		if((_mode & CREATEDIR) != 0)  {
			retstr = retstr + "CreateDir";
		}

		retstr = retstr + "\r\n";

		if(_errorMsg != null)  {
			retstr = retstr + "ErrorMessage " + _errorMsg + "\r\n";
		}

		return retstr;
	}

	/**
	  * Get string list for FTPCon
	  * @return String list - _id,_remotePath,_host,_port,_userName,_password,_localPath,
	  *  "("file1 file2 ...")", _mode and _errorMsg.
	  */
	public String[] toStringList()   {
		String[] list = new String[10];
		list[0] = _id;
		list[1] = _remotePath;
		list[2] = _host;
		list[3] = Integer.toString(_port);
		list[4] = _userName;
		list[5] = _password;
		list[6] = _localPath;
		list[8] = Integer.toString(_mode);
		list[9] = _errorMsg;
		list[7] = "(";
		if(_files != null)  {
			for(int i=0; i < _files.length; i++)  {
				list[7] = list[7] + _files[i] + " ";
			}
		}
		list[7] = list[7] + ")";
		return list;
	}

	/**
	  * Set error message if error occurs
	  * @param err Error string
	  */
	public void setErrorMessage(String err)  {
		_errorMsg = err;
	}

	/**
	  * Get error message
	  * @return Error message.
	  */
	public String getErrorMessage()  {
		return _errorMsg;
	}

}
