|
|
#include "rgooseModuleFactory.h"
#include "rgooseModuleUSBRelay.h"
#include "rgooseModuleKafka.h"
#include "rgooseModuleFlyHunter.h"
#include "rgooseModuleSSDP.h"
#include "rgooseModuleOnvifObserver.h"
#include "rgooseModuleEmbedVideo.h"
#include "rgooseModuleModemManager.h"
#ifdef MEM_DEBUG
#include "../alg/generic/heapDebug.h"
#endif
#define LOG( X ) m_pLog->Log( "MFAC", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "MFAC", this, _LOG_ERROR, X )
argusModuleFactory * argusModuleFactory::_instance = NULL;
/**
* Returns the single instance of the object.*/argusModuleFactory* argusModuleFactory::getInstance( uint32_t ServerID ) { if( NULL == _instance) { if( ServerID ) { _instance = new argusModuleFactory( ServerID ); }; }; return _instance;};
argusModuleFactory::argusModuleFactory( uint32_t ServerID ):basicThread(){ m_nServerID = ServerID; m_bInitialValidatioonDone = false; m_pLog = basicLogCollection::getInstance(); m_pDBC = mojoDBC::getInstance(); init();};
argusModuleFactory::~argusModuleFactory(){ waitThreadEnd( true ); shutdownServices( true ); _instance = NULL;};
void argusModuleFactory::shutdownServices( bool force ){#ifdef DEBUG
LOG("shutdownServices");#endif
m_mModulesProtector.lock(); if( m_vKernelModules.size() > 0 ){ for( vectorKernelModules::iterator index = m_vKernelModules.begin(); index < m_vKernelModules.end(); index++ ){#ifdef DEBUG
char tB[128]; switch( (*index)->getType() ){ case ARGUS_MODULE_DCBUSB: sprintf( tB, "Stop Service of DCUSB Module" ); break; case ARGUS_MODULE_KAFKA: sprintf( tB, "Stop Service of KAFKA Module" ); break; case ARGUS_MODULE_GPS: sprintf( tB, "Stop Service of GPS Module" ); break; case ARGUS_MODULE_NETWORK: sprintf( tB, "Stop Service of Network Module" ); break; case ARGUS_MODULE_FLYHUNTER: sprintf( tB, "Stop Service of FlyHunter Module" ); break; case ARGUS_MODULE_SSDP_ANONCE: sprintf( tB, "Stop Service of SSDP Module" ); break; case ARGUS_MODULE_ONVIF_OBSERVER: sprintf( tB, "Stop Service of OnVif Observer Module" ); break; case ARGUS_MODULE_EMBED_VIDEO: sprintf( tB, "Stop Service of EmbedVideo Module" ); break; case ARGUS_MODULE_MODEM_MANAGER: sprintf( tB, "Stop Service of Modem Manager Module" ); break; default: sprintf( tB, "Stop Service of UNKNOWN[%d] Module", (*index)->getType() ); break; }; LOG( tB );#endif
(*index)->stopService( force );#ifdef DEBUG
LOG("Done");#endif
deleteModule( (*index) );#ifdef DEBUG
LOG("Module is Deleted");#endif
}; m_vKernelModules.clear(); }; m_mModulesProtector.unlock();#ifdef DEBUG
LOG("Done shutdownServices");#endif
};
bool argusModuleFactory::loadModules( uint32_t ServerID ){ DBC_INT_LIST *tList = NULL;
if(m_pDBC == NULL) return false; m_nServerID = ServerID; tList = m_pDBC->listModules( m_nServerID ); if( tList != NULL ) { if( tList->size() ) { for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) createModule( *idx ); tList->clear(); }#ifdef DEBUG
else { LOG("No Modules Defined"); }#endif
delete( tList ); }#ifdef DEBUG
else { LOG("No Modules Defined"); }#endif
return true;};
argusKernelModule *argusModuleFactory::createModule( int id ){ argusKernelModule *tVal = NULL; if( m_pDBC != NULL ){ DBC_MODULE_DATA *tData = NULL;
tData = m_pDBC->getModuleData( id ); if( tData!= NULL ) { switch( tData->type ) { case ARGUS_MODULE_DCBUSB:#ifdef DEBUG
LOG("Creating Dry Contact USB Module");#endif
tVal = new argusModuleUSBRelay( tData->module_id ); break; case ARGUS_MODULE_KAFKA:#ifdef DEBUG
LOG("Creating KAFKA Module");#endif
tVal = new argusModuleKafka( tData->module_id ); break; case ARGUS_MODULE_FLYHUNTER:#ifdef DEBUG
LOG("Creating Fly Hunter Module");#endif
tVal = new argusModuleFlyHunter( tData->module_id ); break; case ARGUS_MODULE_SSDP_ANONCE:#ifdef DEBUG
LOG("Creating SSDP Announcer Module");#endif
tVal = new argusModuleSSDPAnnouncer( tData->module_id ); break; case ARGUS_MODULE_ONVIF_OBSERVER:#ifdef DEBUG
LOG("Creating OnVif Observer Module");#endif
if ( m_pDBC->isLocal() ) { tVal = new argusModuleOnvifObserver(tData->module_id);#ifdef DEBUG
} else { LOG("Module is Disabled Since Remote Database!");#endif
}; break; case ARGUS_MODULE_EMBED_VIDEO:#ifdef DEBUG
LOG("Creating Embed Video Module");#endif
if ( m_pDBC->isLocal() ){ tVal = new argusModuleEmbedVideo( tData->module_id );#ifdef DEBUG
} else { LOG("Module is Disabled Since Remote Database!");#endif
}; break; case ARGUS_MODULE_MODEM_MANAGER:#ifdef DEBUG
LOG("Creating Modem Manager Module");#endif
tVal = new argusModuleModemManager( tData->module_id ); break; }; free(tData); } else { LOG_ERROR("No Module Data Fetched!"); }; } else { LOG_ERROR("No Database Object Assigned!"); }; if( tVal != NULL ){ tVal->startService(); m_mModulesProtector.lock(); m_vKernelModules.push_back( tVal ); m_mModulesProtector.unlock(); }; return tVal;};
void argusModuleFactory::deleteModule( argusKernelModule *M ){ if( M ) { switch( M->getType() ) { case ARGUS_MODULE_DCBUSB: delete( (argusModuleUSBRelay*) M ); break; case ARGUS_MODULE_KAFKA: delete( (argusModuleKafka*) M ); break; case ARGUS_MODULE_FLYHUNTER: delete( (argusModuleFlyHunter*) M ); break; case ARGUS_MODULE_SSDP_ANONCE: delete( (argusModuleSSDPAnnouncer*) M ); break; case ARGUS_MODULE_ONVIF_OBSERVER: delete( (argusModuleOnvifObserver*) M ); break; case ARGUS_MODULE_EMBED_VIDEO: delete( (argusModuleEmbedVideo*) M ); break; case ARGUS_MODULE_MODEM_MANAGER: delete( (argusModuleModemManager*) M ); break; }; };};
void argusModuleFactory::deleteModule( int id ){ m_mModulesProtector.lock(); if( m_vKernelModules.size() > id ) { deleteModule( m_vKernelModules.at( id ) ); m_vKernelModules.erase( m_vKernelModules.begin() + id ); m_vKernelModules.shrink_to_fit(); }; m_mModulesProtector.unlock();};
uint32_t argusModuleFactory::getModulesCount( ARGUS_MODULE_TYPE t ){ uint32_t cnt = 0; m_mModulesProtector.lock(); if( m_vKernelModules.size() ) { for( int i=0; i < m_vKernelModules.size(); i++ ) { if( m_vKernelModules.at(i)->getType() == t ) cnt++; }; }; m_mModulesProtector.unlock(); return cnt;};
uint32_t argusModuleFactory::fetchModules( ARGUS_MODULE_TYPE t, argusKernelModule**&RA ){ uint32_t cnt = getModulesCount( t ); RA = NULL; if( cnt > 0 ) { RA = ( argusKernelModule** ) calloc( cnt, sizeof( argusKernelModule* ) ); if( RA!= NULL ) { m_mModulesProtector.lock(); if( m_vKernelModules.size() ) { uint32_t idx = 0; for( int i=0; i < m_vKernelModules.size(); i++ ) { if( m_vKernelModules.at(i)->getType() == t ) { RA[idx] = m_vKernelModules.at(i); idx++; }; }; }; m_mModulesProtector.unlock(); } else cnt = 0; }; return cnt;};
argusKernelModule *argusModuleFactory::getModule( uint32_t id ){ argusKernelModule *RetVal = NULL; m_mModulesProtector.lock(); if( m_vKernelModules.size() ) { for( vectorKernelModules::iterator idx = m_vKernelModules.begin(); idx < m_vKernelModules.end(); idx++ ){ if( (*idx)->getId() == id ) { RetVal = (*idx); break; }; }; }; m_mModulesProtector.unlock(); return RetVal;};
argusKernelModule *argusModuleFactory::getModule( ARGUS_MODULE_TYPE t, uint32_t id ){ argusKernelModule *RetVal = NULL; m_mModulesProtector.lock(); if( m_vKernelModules.size() ) { for( vectorKernelModules::iterator idx = m_vKernelModules.begin(); idx < m_vKernelModules.end(); idx++ ){ if( id ){ if( ( (*idx)->getType() == t ) && ( (*idx)->getId() == id ) ){ RetVal = (*idx); break; }; } else { if( (*idx)->getType() == t ){ RetVal = (*idx); break; }; }; }; }; m_mModulesProtector.unlock(); return RetVal;};
bool argusModuleFactory::validate( void ){ return ( m_bThreadStatus < TH_ENDED );};
void argusModuleFactory::m_fThreadProc( void ){ if( m_pDBC == NULL) { m_bThreadStatus = TH_ENDED; } else { DBC_INT_LIST *tList = NULL;#ifdef MEM_DEBUG
size_t memStart, memEnd, memDiff; char MEM_MSG[256];#endif
m_bThreadStatus = TH_WORKING; while( m_bThreadStatus < TH_ENDED ) {#ifdef MEM_DEBUG
memStart = getCurrentRSS();#endif
m_mModulesProtector.lock(); if( m_vKernelModules.size() ) { DBC_INT_LIST tL; for( int cm = 0; cm < m_vKernelModules.size(); cm++ ) tL.push_back( m_vKernelModules.at(cm)->getId() ); tList = m_pDBC->listNewModules( m_nServerID, &tL ); tL.clear(); } else { tList = m_pDBC->listNewModules( m_nServerID, NULL ); }; m_mModulesProtector.unlock(); if( tList != NULL ) {#ifdef DEBUG
LOG("New Modules Exists!");#endif
for( int index=0; index< tList->size(); index++) createModule(tList->at(index)); tList->clear(); delete( tList ); };#ifdef MEM_DEBUG
memEnd = getCurrentRSS(); memDiff = memEnd - memStart; sprintf(MEM_MSG,"MEM START: %d MEM DIF1: %d", memStart, memDiff ); LOG( MEM_MSG );#endif
m_mModulesProtector.lock(); if( m_vKernelModules.size() ) { //Get Full List
//tList = NULL;
tList = m_pDBC->listModules( m_nServerID ); if( tList != NULL ) { int cm = 0; while( cm < m_vKernelModules.size() ) { //check if module removed
bool tRemoved = true; for( int lm = 0; lm < tList->size(); lm++ ) { if( tList->at(lm) == m_vKernelModules.at( cm )->getId() ) { tRemoved = false; break; }; }; if( !tRemoved ) { // validate module
m_vKernelModules.at( cm )->validate(); cm++; } else {#ifdef DEBUG
LOG("Delete Module!");#endif
deleteModule( cm ); }; }; tList->clear(); delete( tList ); } else {#ifdef DEBUG
LOG("All Modules Must be Deleted!");#endif
while( m_vKernelModules.size() ) deleteModule( 0 ); }; }#ifdef DEBUG_EXTRA
else { LOG("No Modules to Validate!"); }#endif
; m_mModulesProtector.unlock();#ifdef MEM_DEBUG
memEnd = getCurrentRSS(); memDiff = memEnd - memStart; sprintf(MEM_MSG,"MEM START: %d MEM DIF2: %d", memStart, memDiff ); LOG( MEM_MSG );#endif
if( !m_bInitialValidatioonDone ) m_bInitialValidatioonDone = true; for( int i = ( 5000 / DEFAULT_THREAD_SLEEP_TIME ); i && ( m_bThreadStatus < TH_ENDED ); i-- ) SLEEP( DEFAULT_THREAD_SLEEP_TIME ); }; };};//--enable-gpl --enable-libx264 --enable-nonfree --extra-ldflags=-latomic --enable-omx --enable-omx-rpi
//-rtsp_transport tcp
#undef LOG
#undef LOG_ERROR
|