#ifndef _xbSERVICE_
#define _xbSERVICE_
/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#Title: xbService
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<01/06/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/

#ifndef _xbENGINE_
#	include "xbEngine.h"
#endif

#ifndef _xbPTR_
#	include "xbPtr.h"
#endif

#ifndef _xbLOCK_
#	include "xbLock.h"
#endif

#ifndef _xbTHREAD_
#	include "xbThread.h"
#endif

struct	xbEXPORT xbServiceUserInfo
{
	sIxbBase	*m_pUser;
	xbServiceUserInfo()
	{
		m_pUser = NULL;
	}
	xbServiceUserInfo(sIxbBase *pUser)
	{
		m_pUser = pUser;
	}
};

class xbEXPORT __declspec(uuid("{ED34120F-92F0-46c4-BDDD-F3700F04640B}")) 
	xbService:public xbEngine
{
public:
	xbService(LPCSTR pLabel,const xbEngines &engUsed = xbEngines());
	virtual ~xbService();
	virtual bool GetInterface(REFIID riid, void ** ppInterf)
	{
		xbImplInterface(xbService);
		return xbEngine::GetInterface(riid,ppInterf);
	}

	void	UseLock(sIxbBase *pUser);
	void	UseUnlock(sIxbBase *pUser);
	void	UpdateLock(sIxbBase *pUser);
private:
	virtual void onStart()
	{
		m_bUserStart = false;
		onStartService();
	}
	virtual bool onStop(bool bForce)
	{
		m_bUserStart = false;
		return onStopService(bForce);
	}
protected:
	virtual bool isLockedByMe() const = 0;
	virtual void onStartService() = 0;
	virtual bool onStopService(bool) = 0;
	virtual void onLockListChange(int noLock) = 0;

	int getLockInfo(sIxbBase *pUser);

	bool	m_bUserStart;
	xbArray<xbServiceUserInfo>	m_arrLock;
};

class xbEXPORT xbUseService:public xbUseEngineCenterExe
{
public:
	xbUseService()
	{
		m_pService = NULL;
	}
	xbUseService(xbService *pService)
	{
		m_pService = NULL;
		StartUse(pService);
	}
	~xbUseService()
	{
		eProtect(StopUse());
	}
	void StartUse(xbService *pService)
	{
		if (pService == m_pService) return;
		if (m_pService) StopUse();
		xbUseEngineCenterExe::StartUse(&g_xbEngineCenter,false);
		pService->UseLock(this);
		m_pService = pService;
	}
	void StopUse()
	{
		if (!m_pService) return;
		xbService	*pService = m_pService;
		m_pService = NULL;
		pService->UseUnlock(this);
		xbUseEngineCenterExe::StopUse();
	}
protected:
	void updateServiceParams()
	{
		if (!m_pService) return;
		m_pService->UpdateLock(this);
	}
	xbService	*m_pService;

	xbDclNoNewForObject(xbUseService);
};

interface __declspec(uuid("{093D102F-E416-4b5f-B937-2F8292611D1A}")) 
xbEXPORT IxbServiceNotify:public IxbObject
{
	virtual void onLockListChange(const xbServiceUserInfo *pUserInfo,int cLocker,int noLocker) = 0;
};

template <class object,class iobject = object> class xbTService : public xbService
{
public:
	xbTService(LPCSTR pLabel,const xbEngines &engUsed = xbEngines())
		:xbService(pLabel,engUsed)
	{
	}
	virtual ~xbTService()
	{
		if (m_arrLock.GetLength() > 0)
		{
			xbRelOutput("xbTService("+xbString(m_pLabel)+")::~xbTService() : Not stopped correctly ("+xbString(m_arrLock.GetLength())+" Instance(s) using it, state="+xbString(m_bStarted?"started":"stopped")+")");
			m_arrLock.Clear();
		}
		Destroy();
	}

	bool GetServiceObject(xbRefPtr<iobject> *pref)
	{
		if (!IsReady()) return false;
		xbSectLock lck(&m_pCenter->m_sectStartStop);
		if (!IsStarted(NULL)) return false;
		*pref = m_ptr;
		return true;
	}
protected:
	virtual void onStartService()
	{
		object *ptr = new object();
		try
		{
			ptr->Create();

			m_ptr = ptr;
		}
		catch(...)
		{
			ptr->Release();
			throw;
		}
		ptr->Release();

		try
		{
			onLockListChange(-1);
		}
		catch(...)
		{
			m_ptr.Empty();
			throw;
		}
	}
	virtual bool onStopService(bool bForce)
	{
		if (m_ptr.IsValid() && !m_ptr->AskDestroy(bForce))
			return false;
		m_ptr.Empty();
		return true;
	}
	virtual void onLockListChange(int noLock)
	{
		xbRefQIPtr<IxbServiceNotify>	ptr((xbObject*)m_ptr,false);

		if (!ptr) return;

		ptr->onLockListChange(m_arrLock.Begin(),m_arrLock.GetLength(),noLock);
	}
	virtual bool isLockedByMe() const {return false;}
	xbRefPtr<iobject>	m_ptr;
};

#endif
