/*************************************************************************\
* "$RCSfile: Ftp.h,v $"
***************************************************************************
* Information by CVS or VSS:
*     Last check in on: $Date: 2013-06-20 14:49:18 $
*     by                $Author: goebel $
*     Revision        : $Revision: 1.7 $
*
* CVS: $Source: /Daten/Projekte/CVS/Lib/GBIT_Lib/include/Ftp.h,v $
**************************************************************************/
/*! \file Ftp.h
*	\brief Ftp Server Header
*	\author (c) 2004 - 2011 IMAGO Technologies GmbH
*
*	$Revision: 1.7 $
*	$Date: 2013-06-20 14:49:18 $
**************************************************************************/

//!@cond SHOWALL

#ifndef _FTP_H_
#define _FTP_H_

#include <std.h>
#include <log.h>
#include <sem.h>
#include <stdio.h>
#include <string.h>
#include "../include/socket.h"

#define BUFFER_SIZE    512
#define MAX_USERS       32
#define MAX_SESSIONS    32
#define MAX_PASV_PORTS 256

// Set the following Log Handles to get Debug or Error Messages
extern far LOG_Handle FTP_LogDebug;
extern far LOG_Handle FTP_LogError;

class CUser;
class CSession;

class Ftp
{
	private:
		// General member variables
		HANDLE      m_ListeningThread;
		char        m_ListenName[20];
		HANDLE      m_FileIoThread;
		char        m_FileIoName[20];
//		HANDLE      m_TimerThread;
//		HANDLE      m_StopServerThread;
		SOCKET      m_ListeningSock;

		// General Parameters
		int         m_ListeningPort;
		int         m_MaxConnections;
		int         priority;

		// Advanced Options
		bool        m_UsePasvUrl;
		int         m_MinPasvPort;
		int         m_MaxPasvPort;
		char        m_PasvUrl[BUFFER_SIZE];

		// Other member variables
		bool        m_IsRunning;
		int         nbPasvPortsInUse;
		int         m_PasvPortsInUse[MAX_PASV_PORTS];
		int         nbSessions;
		CSession    *m_Sessions[MAX_SESSIONS];
		int         nbUsers;
		CUser       *m_Users[MAX_USERS];
		SOCKET      m_PasvSock;
		int         m_PasvPort;

	public:
		// Construction / Destruction
		Ftp(int Priority=-1, int ListeningPort=21);
		~Ftp();

		bool        m_ShouldStop;
		HANDLE      hBuf;
		SEM_Handle  SemPasvConnect;
		bool        m_useIoTask;
		HANDLE      m_hMbxFileIO;

		// inline functions
		bool        isRunning() {return m_IsRunning;}
		int         getNbUsers() {return nbUsers;}
		int         getNbSessions() {return nbSessions;}
		int         getNbPasvPortsInUse() {return nbPasvPortsInUse;}
		bool        usePasvUrl () {return m_UsePasvUrl;}
		char*       getPasvUrl() {return m_PasvUrl;}

		SOCKET      getListeningSock() {return m_ListeningSock;}
		void        setListeningSock(SOCKET s) {m_ListeningSock = s;}
		void        setListeningThread(HANDLE hThread) {m_ListeningThread = hThread;}

		void        setUsePasvUrl(bool b_) {m_UsePasvUrl = b_;}
		void        setPasvUrl(char* pu) {strcpy(m_PasvUrl, pu);}
		SOCKET      getPasvSock() {return m_PasvSock;}
		int         getPasvPort() {return m_PasvPort;}
		SEM_Handle  getPasvSem() {return SemPasvConnect;}

		// Settings
		void        setListeningPort(int port_);
		int         getListeningPort();
		void        setMaxConnections(int n_);
		int         getMaxConnections();
		bool        setPasvPortRange(int min, int max);
		void        setPriority(int prio);
		int         getPriority();

		// User functions
		int         addUser(char *login, char *password, int simConn = 5, int timeOut = 120);
		bool        deleteUser(int index_);
		int         findUser(char *user_);
		bool        getUserProperties(int index_, char*, char*, int*, int*);
		CUser*      getUser(int index_) {return m_Users[index_];}
		int         getUserNbSessions(int);
		int         getNbConnections();
		int         getUserMaxConnections(int);
		int         addVirtualDir(char* login, char *real, char *virt, char *perm);

		// Daemon Control functions
		bool        start();
		bool        stop();

		// Session functions
		int         addSession(CSession *s);
		bool        removeSession(int n);
};

#endif

//!@endcond
