You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

129 lines
2.7 KiB

#include "basicEventHeap.h"
#include "basicEventHandler.h"
#define LOG(X) m_pLog->Log("basicEventHeap", this, X)
#define LOG_ERROR(X) m_pLog->Log("basicEventHeap", this, _LOG_ERROR, X)
basicEventHeap * basicEventHeap::_instance = 0;
/**
* Returns the single instance of the object.
*/
basicEventHeap * basicEventHeap::getInstance()
{
if (0 == _instance)
{
_instance = new basicEventHeap;
};
return _instance;
}
void basicEventHeap::addHandler(basicEventHandler * handler, bool atBottom)
{
m_pHandlerMutex->lock();
if( atBottom ) {
m_vHandlers.push_back( handler );
} else {
m_vHandlers.insert( m_vHandlers.begin(), handler );
};
m_pHandlerMutex->unlock();
}
void basicEventHeap::removeHandler(basicEventHandler * handler)
{
m_pHandlerMutex->lock();
for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx != m_vHandlers.end(); idx++)
{
if ((*idx) == handler)
{
m_vHandlers.erase(idx);
break;
};
};
m_pHandlerMutex->unlock();
}
void basicEventHeap::moveHandlerBottom( basicEventHandler * handler ){
m_pHandlerMutex->lock();
for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx != m_vHandlers.end(); idx++)
{
if ((*idx) == handler)
{
m_vHandlers.erase(idx);
break;
};
};
m_vHandlers.push_back( handler );
m_pHandlerMutex->unlock();
}
void basicEventHeap::moveHandlerTop( basicEventHandler * handler ){
m_pHandlerMutex->lock();
for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx != m_vHandlers.end(); idx++)
{
if ((*idx) == handler)
{
m_vHandlers.erase(idx);
break;
};
};
m_vHandlers.insert( m_vHandlers.begin(), handler );
m_pHandlerMutex->unlock();
}
void basicEventHeap::m_fThreadProc(void)
{
basicEventHandler * mpHandler = NULL;
EVENT_CONTAINER * mpEvent = NULL;
m_bThreadStatus = TH_WORKING;
pthread_setname_np( pthread_self(), "EventHeap" );
while (m_bThreadStatus < TH_ENDED)
{
if (m_bThreadStatus == TH_WORKING)
{
mpEvent = NULL;
m_pDispatchingMutex->lock();
if (! m_vEventQueue.empty())
{
mpEvent = m_vEventQueue.front();
m_vEventQueue.pop();
};
m_pDispatchingMutex->unlock();
if (mpEvent != NULL)
{
m_pHandlerMutex->lock();
for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx < m_vHandlers.end(); idx++)
{
mpHandler = (*idx);
if (mpHandler != NULL)
{
if (! mpHandler->dispatchEvent(mpEvent))
{
break; // Stop Propogation if false
}
}
};
m_pHandlerMutex->unlock();
utilizeBasicEvent(mpEvent);
}
else
{
SLEEP(1);
}
};
};
}
void basicEventHeap::broadcast(EVENT_CONTAINER * Evt)
{
if (Evt)
{
m_pDispatchingMutex->lock();
m_vEventQueue.push(Evt);
m_pDispatchingMutex->unlock();
};
}
#undef LOG
#undef LOG_ERROR