/***************************************************************************
                   Service Class Header ( Config.h )
                -----------------------------------------

	begin      : 2013/02/15
	copyright  : (C) 2013 Solbox Inc.
	author     : Development 1 Team
					- 2013/02/15 - 1st dadamin
	email      : dev1@solbox.com
	version    : 3.2.0

	CopyRight(C) 2005 Solbox Inc. All Rights reserved.
	Redistribution and use in source and binary forms, with or with out
	modification, are not permitted in outside of Solbox Inc.
***************************************************************************/
#ifndef __SERVICE_H__
#define __SERVICE_H__

class CServiceInfo
{
public:
	CServiceInfo() 
		: m_port(0), m_type(CServiceInfo::UNSET), m_enableSSL(false) {}
	CServiceInfo(const CServiceInfo &info)
		: m_port(info.m_port), m_type(info.m_type), m_enableSSL(info.m_enableSSL) {}

	~CServiceInfo() {}

	void PrintInfo() 
	{
		cout << m_port << "," << m_type << "," << m_enableSSL << endl;
	};

	enum SERVICE_TYPE {
		UNSET	=  -1,
		XML		=	0,
		SDK		=	1,
		ETC		=	2,
		STR		=	3
	};
public:
	int					m_port;
	enum SERVICE_TYPE	m_type;
	bool				m_enableSSL;

};

class CService 
{
public:
	CService();
	CService(CServiceInfo&  info);
	virtual ~CService();

	void SetInfo(CServiceInfo&  info)		{ m_info = info; }

	int		GetPort()				{ return m_info.m_port; }
	bool	EnableSSL()				{ return m_info.m_enableSSL; }

	int		init();
	int		Start();
	void	Stop();

protected:	
	void *entry();
	virtual int RunClient(int nClientfd, SSL *ssl = NULL) = 0;
	
	int			TCPListen();
	SSL_CTX *	initializeCTX();
	SSL*		CerateSSL(int nClientfd);

	int			Accept();
	int			Acceptv6();

	pthread_t m_threadid;
	bool m_done;

	bool m_ipv6;
	int m_listenfd;
	SSL_CTX * m_ctx;

	CServiceInfo	m_info;	
private:	
	static void *_entry_func(void *arg);
};

class CXMLService : public CService
{
public:
	CXMLService();
	CXMLService(CServiceInfo& info) ;

	virtual ~CXMLService();

protected:
	virtual int RunClient(int nClientfd, SSL *ssl = NULL);

private:
};

class CSDKService : public CService
{
public:
	CSDKService();
	CSDKService(CServiceInfo& info) ;

	virtual ~CSDKService();

protected:
	virtual int RunClient(int nClientfd, SSL *ssl = NULL);

private:

};

class CEtcService : public CService
{
public:
	CEtcService();
	CEtcService(CServiceInfo& info);
	
	virtual ~CEtcService();

protected:
	virtual int RunClient(int nClientfd, SSL *ssl = NULL);

private:

};

class CStrService : public CService
{
public:
	CStrService();
	CStrService(CServiceInfo& info);

	virtual ~CStrService();

protected:
	virtual int RunClient(int nClientfd, SSL *ssl = NULL);

private:

};

// Abstract Factory returning a Service
class CSericeFactory 
{
public:
	CService* CreateSerice(CServiceInfo &info);
};
#endif // __SERVICE_H__
