/*<<<***************************************************************************
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:
<18/08/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/
#include "stdafx.h"

#include "xbService.h"
#include "xbMsg.h"

#pragma warning(push)
#pragma warning(disable: 4311)
xbService::xbService(LPCSTR pLabel,const xbEngines &engUsed)
:xbEngine(xbEngineStart_Manual,pLabel,engUsed)
{
	m_bUserStart = false;
}

xbService::~xbService()
{
	if (m_arrLock.GetLength() > 0)
	{
		xbRelOutput("xbService("+xbString(m_pLabel)+")::~xbService() : Not stopped correctly ("+xbString(m_arrLock.GetLength())+" Instance(s) using it, state="+xbString(m_bStarted?"started":"stopped")+")");
		m_arrLock.Clear();
	}
	Destroy();
}

void	xbService::UseLock(sIxbBase *pUser)
{
	CheckReady();

	xbSectLock lck(&m_pCenter->m_sectStartStop);
	bool tostart = m_arrLock.IsEmpty();
	
	if (getLockInfo(pUser)>=0)
		xbThrowEsys("xbService("+xbString(m_pLabel)+")::UseLock() : pUser already in locker list");

	int no = m_arrLock.Add(xbServiceUserInfo(pUser));
	if (tostart && !m_bStarted)
	{
		try
		{
			Start();
			m_bUserStart = true;
		}
		catch(...)
		{
			m_arrLock.Clear();
			throw;
		}
	}
	else
	{
		try
		{
			onLockListChange(no);
		}
		catch(...)
		{
			m_arrLock.Delete(no);
			throw;
		}
	}
}

void	xbService::UseUnlock(sIxbBase *pUser)
{
	eProtectBegin();

	CheckReady();

	xbSectLock lck(&m_pCenter->m_sectStartStop);

	int no = getLockInfo(pUser);
	if (no < 0) 
		xbThrowEsys("xbService("+xbString(m_pLabel)+")::UseUnlock(sIxbBase *pUser="
		+xbString((xbNptr)pUser,xbStringNum_hexa)+") : pUser not found !!");

	m_arrLock.Delete(no);

	if (m_arrLock.IsEmpty() && m_bUserStart && m_bStarted)
	{
		int cnt = 0;
		while(!TryStop())
		{
			if (++cnt > xbENGINE_TRYCNT)
				xbThrowEsys("xbService("+xbString(m_pLabel)+")::UseUnlock() : timeout, can't stop this engine !");
			lck.Unlock();
			if (!xbDoEvents(NULL)) 
				SleepEx(xbENGINE_TRYSLEEP,xbWAITSLEEP_ALERTABLE);
			lck.Lock(&m_pCenter->m_sectStartStop);
			if (!m_arrLock.IsEmpty() || !m_bUserStart) break;
		}
	}
	else
	{
		if (m_bStarted) onLockListChange(0);
	}

	eProtectEnd();
}

void	xbService::UpdateLock(sIxbBase *pUser)
{
	CheckReady();

	xbSectLock lck(&m_pCenter->m_sectStartStop);

	int no = getLockInfo(pUser);
	if (no < 0) 
		xbThrowEsys("xbService("+xbString(m_pLabel)+")::UseUnlock(sIxbBase *pUser="
		+xbString((xbNptr)pUser,xbStringNum_hexa)+") : pUser not found !!");

	if (m_bStarted) onLockListChange(no);
}

int xbService::getLockInfo(sIxbBase *pUser)
{
	if (!pUser) 
		return -1;

	xbServiceUserInfo	*ptr = m_arrLock.Begin();
	xbServiceUserInfo	*pend = m_arrLock.End();
	int			no = 0;

	while(ptr < pend)
	{
		if (ptr->m_pUser == pUser) return no;
		ptr++;
		no++;
	}

	return -1;
}
#pragma warning(pop)
