#include "servaSessionManager.h" #include "../common/serverMySQL_dataBinder.h" #include "../../components/base/basicEventHeap.h" #define LOG( X ) m_pLog->Log( "SSMN", this, X ) #ifdef DEBUG #define LOG_ERROR( X ) {\ char _tBfrErrMSG2w34[256];\ sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ m_pLog->Log( "SSMN", this, _LOG_ERROR, _tBfrErrMSG2w34 );\ } #else #define LOG_ERROR( X ) m_pLog->Log( "SSMN", this, _LOG_ERROR, X ); #endif servaSessionManager *servaSessionManager::_instance = NULL; servaSessionManager *servaSessionManager::getInstance( config_setting_t * Settings ){ if( _instance == NULL ){ _instance = new servaSessionManager( Settings ); }; return _instance; }; servaSessionManager::servaSessionManager( config_setting_t * Settings ){ m_pLog = basicLogCollection::getInstance(); m_pDBC = (serverDataBinder*) servaMySQLDBC::getInstance(); m_bRepIsConnected = false; //Read System (loalfile) Settings m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "SESSON_MNGR" ); #ifdef DEBUG LOG("Getting Settings ..."); #endif config_setting_t * pSetting; int tVal; const char * tBuf; char tBuffer[128]; /* if(!config_setting_lookup_int(m_pSettings, "SESSION_TIMEOUT", &tVal )){ pSetting = config_setting_add(m_pSettings, "SESSION_TIMEOUT", CONFIG_TYPE_INT); m_nSessionTO = DEFAULT_SESSION_TIMEOUT; config_setting_set_int( pSetting, m_nSessionTO ); basicSettingsProvider::getInstance()->compromise(); LOG("Session Timeout not set - using default"); } else { m_nSessionTO = tVal; sprintf( tBuffer, "Session Timeout Read From Settings: %d sec.", m_nSessionTO ); LOG( tBuffer ); }; */ m_nSessionTO = m_pDBC->getSessionTimeOut(); if(!config_setting_lookup_string(m_pSettings, "REPORTS_DIR_NAME", &tBuf )){ pSetting = config_setting_add(m_pSettings, "REPORTS_DIR_NAME", CONFIG_TYPE_STRING); sprintf( m_pDbReportBase, "data/reports" ); config_setting_set_string( pSetting, m_pDbReportBase ); basicSettingsProvider::getInstance()->compromise(); LOG("Reports Storage not set - using default"); } else { sprintf( m_pDbReportBase, tBuf ); }; if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_USER", &tBuf )){ pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_USER", CONFIG_TYPE_STRING); sprintf( web_access_user, "www-data" ); config_setting_set_string( pSetting, web_access_user ); basicSettingsProvider::getInstance()->compromise(); LOG("Web Access User not set - using default"); } else { sprintf( web_access_user, tBuf ); }; if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_GROUP", &tBuf )){ pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_GROUP", CONFIG_TYPE_STRING); sprintf( web_access_group, "www-data" ); config_setting_set_string( pSetting, web_access_group ); basicSettingsProvider::getInstance()->compromise(); LOG("Web Access Group not set - using default"); } else { sprintf( web_access_group, tBuf ); }; basicSettingsProvider::getInstance()->validate(); //if( !fileExist( DB_SHMEM_POINT )) { // bRecreateMemStructures = true; // mkdir( DB_SHMEM_POINT, STORAGE_DIR_MODE );// S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH ); // chown( DB_SHMEM_POINT, getUserUid(web_access_user), getUserGid(web_access_group) ); //To make Accessed from PHP // chmod( DB_SHMEM_POINT, STORAGE_DIR_MODE ); //}; }; servaSessionManager::~servaSessionManager(){ //Close all Ongoing Session since Application goes to die m_pDBC->closeAllOnGoingSessions(); }; uint64_t servaSessionManager::addEvent( uint64_t tack_id, uint64_t module_id, ARGUS_DATA_EVENT *ptr ){ uint64_t rV = 0; if( SESSION_DATA *sp = updateSession( tack_id, module_id ) ) { if( addEvent( sp, ptr ) ) rV = sp->db_id; }; return rV; }; bool servaSessionManager::addEvent( uint64_t sid, ARGUS_DATA_EVENT *ptr ){ if( SESSION_DATA *sp = validateSession( sid ) ) { return addEvent( sp, ptr ); }; return false; }; bool servaSessionManager::addEvent( SESSION_DATA *sp, ARGUS_DATA_EVENT *ptr ){ if ( ptr == NULL ) return false; if ( sp == NULL ) return false; if ( sp->m_pDataReport ) { char tQBuf[1024]; switch ( ptr->TYPE ) { case EVT_LRF_END_SESSION_TO: case EVT_LRF_END_SESSION_MA: //No Technical events return false; sprintf( tQBuf, "INSERT INTO `events`(`session_id`,`module_id`,`ts`,`ts_ms`,`evt_type`) VALUES(%" PRIu64 ",%" PRIu64 ",%d,%d,%d)", sp->db_id, ptr->MODULE_ID, ptr->TS, ptr->MS, ptr->TYPE ); break; case EVT_LRF_START_SESSION: //No Technical events return false; sprintf( tQBuf, "INSERT INTO `events`(`session_id`,`module_id`,`ts`,`ts_ms`,`evt_type`) VALUES(%" PRIu64 ",%" PRIu64 ",%d,%d,%d)", sp->db_id, ptr->MODULE_ID, ptr->TS, ptr->MS, ptr->TYPE ); break; case EVT_FLT_LATF_CHK: case EVT_FLT_LATF_UP_DETEC: case EVT_FLT_LATF_BT_DETEC: case EVT_FLT_LATF_TRP_DET: case EVT_FLT_LATF_TRP_UPDT: case EVT_FLT_LATF_SUSP_START: case EVT_FLT_LATF_SUSP_NAK: case EVT_FLT_LATF_REGULAR: case EVT_FLT_LATF_DISABLED: case EVT_FLT_LATF_SUSP_DET: case EVT_FLT_LATF_DIFF_DET: case EVT_FLT_LATF_OP_DISABLED: case EVT_FLT_LATF_OP_TD: case EVT_FLT_LATF_OP_TG: case EVT_FLT_LATF_OP_EXP: case EVT_OP_ACK_MASK: case EVT_OP_NACK_MASK: case EVT_OP_ON_MASK: case EVT_OP_OFF_MASK: case EVT_CONNECTED: case EVT_DISCONNECTED: case EVT_COMMUNICATION_JAMED: case EVT_STUDY: case EVT_BUSY: case EVT_FLIP: case EVT_READY: case EVT_ERR_LRF_NO_IRQ: case EVT_ERR_LRF_NO_ZERO: case EVT_ERR_LRF_ANGLE: case EVT_ERR_LRF_VOLTAGE: case EVT_ERR_LRF_BROKEN: case EVT_LRF_VIS_CHANGED: case EVT_ERR_OVERFLOW: case EVT_SST_OVERFLOW: case EVT_VIDEO_FPS_DEGRADED: case EVT_VIDEO_FPS_RAISED:{ //No Technical events return false; sprintf( tQBuf, "INSERT INTO `events`(`session_id`,`module_id`,`ts`,`ts_ms`,`evt_type`) VALUES(%" PRIu64 ",%" PRIu64 ",%d,%d,%d)", sp->db_id, ptr->MODULE_ID, ptr->TS, ptr->MS, ptr->TYPE ); break; }; case EVT_ROI_ACTIVE: case EVT_TARGET_3D_WHITE: case EVT_TARGET_3D_BLACK:{ updateSessionStat( sp->db_id ); sprintf( tQBuf, "INSERT INTO `events`(`session_id`,`module_id`,`ts`,`ts_ms`,`evt_type`,`azimuth`,`elevation`,`range`,`area`)" \ " VALUES(%" PRIu64 ",%" PRIu64 ",%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.2f)", sp->db_id, ptr->MODULE_ID, ptr->TS, ptr->MS, ptr->TYPE, ptr->AZIMUTH / 1000.0f, ptr->ELEVATION / 1000.0f, ptr->RANGE / 1000.0f, ptr->AREA / 100.0f ); break; }; case EVT_TARGET_2D_WHITE: case EVT_TARGET_2D_BLACK:{ updateSessionStat( sp->db_id ); sprintf( tQBuf, "INSERT INTO `events`(`session_id`,`module_id`,`ts`,`ts_ms`,`evt_type`,`azimuth`,`range`,`area`)" \ " VALUES(%" PRIu64 ",%" PRIu64 ",%d,%d,%d,%0.2f,%0.2f,%0.2f)", sp->db_id, ptr->MODULE_ID, ptr->TS, ptr->MS, ptr->TYPE, ptr->AZIMUTH / 1000.0f, ptr->RANGE / 1000.0f, ptr->AREA / 100.0f ); break; }; default: return false; }; #ifdef DEBUG_EXTRA LOG( tQBuf ); #endif return sp->m_pDataReport->SilentQuery( tQBuf ); }; return false; }; bool servaSessionManager::addVideo( DBC_RPC_MEDIA_INFO_EX *ptr ){//uint32_t module_id, uint32_t ts, uint16_t tsms, CSTR uuid, CSTR fmt, uint8_t fps ){ if ( ptr == NULL ){ LOG_ERROR( "ExtInfo ptr is null!"); return false; }; char tB[64]; sprintf( tB, "Session id: %" PRIu64, ptr->session_id ); LOG( tB ); // At Time of Video Addition the Session May be expired already!! // No neet to Validate session! bool bExpiredSession = false; SESSION_DATA *sp = validateSession( ptr->session_id ); if ( sp == NULL ) { LOG("Session Allredy Closed"); sp = reopenSession( ptr->session_id ); if ( sp == NULL ) { LOG_ERROR( "Session Data ptr is null!"); return false; }; bExpiredSession = true; }; if ( sp->m_pDataReport ) { char tName[MAX_PATH]; sprintf(tName, "%s/%s.%s", sp->reportVaultPath, ptr->uuid, ptr->fmt ); chown( tName, getUserUid(web_access_user), getUserGid(web_access_group) ); chmod( tName, DB_FILE_MODE ); updateSessionStat( ptr->session_id, false ); char tQBuf[CTEXT]; sprintf( tQBuf, "INSERT INTO `videos`(`session_id`,`module_id`,`uuid`,`ts`,`ts_ms`,`ext`,`fps`) VALUES(%" PRIu64 ", %d,'%s',%d,%d,'%s',%d)", ptr->session_id, ptr->module_id, ptr->uuid, ptr->ts, ptr->tms, ptr->fmt, ptr->fps ); #ifdef DEBUG LOG( tQBuf ); #endif bool rv = sp->m_pDataReport->SilentQuery( tQBuf ); if( bExpiredSession ) dropSession( ptr->session_id ); return rv; } else { LOG_ERROR( "Report DB is closed!"); }; return false; }; bool servaSessionManager::create(){ //Reset All Ongoing Session since Application was restarted (crashed?) return m_pDBC->closeAllOnGoingSessions(); }; CSTR servaSessionManager::getReportDir( uint64_t track_id, uint32_t ts ){ char retVal[MAX_PATH]; REPORT_DIRINFO tRDF; fillRDF( ts, tRDF ); sprintf( retVal, "%s/%" PRIu64 "/%s/%s/%s", m_pDbReportBase, track_id, tRDF.YEAR, tRDF.MONTH, tRDF.DAY ); #ifdef DEBUG_EXTRA LOG( retVal ); #endif return strdup( retVal ); }; CSTR servaSessionManager::getSessionBase( uint64_t sid ){ CSTR rData = NULL; SESSION_DATA *sp = validateSession( sid ); if ( sp ) return strdup( sp->reportVaultPath ); //TODO: Get Session Base from Database then (not open/close session) sp = reopenSession( sid ); if ( sp ) { rData = strdup( sp->reportVaultPath ); dropSession( sid ); }; return rData; }; SESSION_DATA *servaSessionManager::reopenSession( uint64_t session_id ){ SESSION_DATA * tData = NULL; // No Session -> Create one tData = m_pDBC->getSessionData( session_id ); if ( tData ) { //Reopen just for fun :) tData->skeep_validation = true; // Create History Database if not exists char tBase[MAX_PATH]; sprintf( tBase, "%s/%d", m_pDbReportBase, tData->track_id ); if( fileExist( tBase ) == 0 ){ if (!createAccessibleDir( tBase, STORAGE_DIR_MODE, getUserUid(web_access_user), getUserGid(web_access_group))){ LOG_ERROR("Cannot create Base Report Dir!"); #ifdef DEBUG char tB[1024]; sprintf( tB, "Dir: %s, UID[%s]: %d GID[%s]: %d", tBase, web_access_user, getUserUid(web_access_user) ,web_access_group, getUserGid(web_access_group) ); LOG( tB ); #endif free( tData ); return NULL; }; }; REPORT_DIRINFO tRDF; fillRDF( tData->sts, tRDF ); if( checkRDFDir( tBase, tRDF, true, getUserUid(web_access_user), getUserGid(web_access_group), STORAGE_DIR_MODE ) ){ #ifdef DEBUG LOG("Connect to Report Database RS"); #endif sprintf( tData->reportVaultPath, "%s/%s/%s/%s", tBase, tRDF.YEAR, tRDF.MONTH, tRDF.DAY ); //LOG( tData->reportVaultPath ); char tName[MAX_PATH]; sprintf( tName, "%s/report.db", tData->reportVaultPath ); if( fileExist( tName ) ){ //Allready have report file tData->m_pDataReport = new SQLite_Connector(); if ( tData->m_pDataReport ) { if( !tData->m_pDataReport->Connect( tName, 0, true) ) { LOG_ERROR("Cannot Connect to RDB!"); delete( tData->m_pDataReport ); free( tData ); tData = NULL; }; } else { LOG_ERROR("Cannot Create Connector to RDB!"); free( tData ); tData = NULL; }; } else { #ifdef DEBUG LOG("Create New Report Database"); #endif //Create new report file tData->m_pDataReport = new SQLite_Connector(); if ( tData->m_pDataReport ) { if( tData->m_pDataReport->Connect( tName, 0, true) ) { chown( tName, getUserUid(web_access_user), getUserGid(web_access_group) ); chmod( tName, DB_FILE_MODE ); tData->m_pDataReport->SilentQuery("CREATE TABLE IF NOT EXISTS \"events\" ( \"id\" INTEGER NOT NULL, \"session_id\" INTEGER NOT NULL, \"module_id\" INTEGER NOT NULL, \"ts\" INTEGER NOT NULL, \"ts_ms\" INTEGER NOT NULL, \"evt_type\" INTEGER NOT NULL, \"azimuth\" REAL, \"elevation\" REAL, \"range\" REAL, \"area\" REAL, PRIMARY KEY(\"id\" AUTOINCREMENT) )"); tData->m_pDataReport->SilentQuery("CREATE TABLE IF NOT EXISTS \"videos\" ( \"id\" INTEGER NOT NULL, \"session_id\" INTEGER NOT NULL, \"module_id\" INTEGER NOT NULL, \"uuid\" TEXT NOT NULL UNIQUE, \"ts\" INTEGER NOT NULL, \"ts_ms\" INTEGER NOT NULL, \"ext\" TEXT NOT NULL, \"fps\" INTEGER NOT NULL, PRIMARY KEY(\"id\" AUTOINCREMENT) )"); } else { LOG_ERROR("Cannot Connect to RDB!"); delete( tData->m_pDataReport ); free( tData ); tData = NULL; }; } else { LOG_ERROR("Cannot Create Connector to RDB!"); free( tData ); tData = NULL; }; }; } else { LOG_ERROR("The report Vault Dir can not be accessed!"); free( tData ); tData = NULL; }; }; if ( tData ){ LOG("Assign Session To Vector!"); m_mSessionProtector.lock(); m_mSessions[tData->track_id] = tData; m_mSessionProtector.unlock(); }; return tData; }; SESSION_DATA *servaSessionManager::updateSession( uint64_t tack_id, uint64_t module_id ){ SESSION_DATA * tData = NULL; try { m_mSessionProtector.lock(); tData = m_mSessions.at( tack_id ); m_mSessionProtector.unlock(); #ifdef DEBUG LOG("Have Opened Session"); #endif } catch ( ... ) { m_mSessionProtector.unlock(); // No Session -> Create one tData = (SESSION_DATA*) malloc( sizeof(SESSION_DATA) ); if ( tData ) { memset( tData, 0, sizeof(SESSION_DATA) ); uint64_t cms = getMilliseconds(); tData->lts = tData->sts = cms/1000; tData->sms = cms%1000; tData->track_id = tack_id; getUniqID_UUID( tData->uuid ); // Create History Database if not exists char tBase[MAX_PATH]; sprintf( tBase, "%s/%" PRIu64, m_pDbReportBase, tack_id ); if( fileExist( tBase ) == 0 ){ if (!createAccessibleDir( tBase, STORAGE_DIR_MODE, getUserUid(web_access_user), getUserGid(web_access_group))){ LOG_ERROR("Cannot create Base Report Dir!"); #ifdef DEBUG char tB[1024]; sprintf( tB, "Dir: %s, UID[%s]: %d GID[%s]: %d", tBase, web_access_user, getUserUid(web_access_user) ,web_access_group, getUserGid(web_access_group) ); LOG( tB ); #endif free( tData ); return NULL; }; }; REPORT_DIRINFO tRDF; fillRDF( tData->sts, tRDF ); if( checkRDFDir( tBase, tRDF, true, getUserUid(web_access_user), getUserGid(web_access_group), STORAGE_DIR_MODE ) ){ #ifdef DEBUG LOG("Connect to Report Database US"); #endif sprintf( tData->reportVaultPath, "%s/%s/%s/%s", tBase, tRDF.YEAR, tRDF.MONTH, tRDF.DAY ); LOG( tData->reportVaultPath ); char tName[MAX_PATH]; sprintf( tName, "%s/report.db", tData->reportVaultPath ); if( fileExist( tName ) ){ //Allready have report file tData->m_pDataReport = new SQLite_Connector(); if ( tData->m_pDataReport ) { if( !tData->m_pDataReport->Connect( tName, 0, true) ) { LOG_ERROR("Cannot Connect to RDB!"); delete( tData->m_pDataReport ); free( tData ); tData = NULL; }; } else { LOG_ERROR("Cannot Create Connector to RDB!"); free( tData ); tData = NULL; }; } else { #ifdef DEBUG LOG("Create New Report Database"); #endif //Create new report file tData->m_pDataReport = new SQLite_Connector(); if ( tData->m_pDataReport ) { if( tData->m_pDataReport->Connect( tName, 0, true) ) { chown( tName, getUserUid(web_access_user), getUserGid(web_access_group) ); chmod( tName, DB_FILE_MODE ); tData->m_pDataReport->SilentQuery("CREATE TABLE IF NOT EXISTS \"events\" ( \"id\" INTEGER NOT NULL, \"session_id\" INTEGER NOT NULL, \"module_id\" INTEGER NOT NULL, \"ts\" INTEGER NOT NULL, \"ts_ms\" INTEGER NOT NULL, \"evt_type\" INTEGER NOT NULL, \"azimuth\" REAL, \"elevation\" REAL, \"range\" REAL, \"area\" REAL, PRIMARY KEY(\"id\" AUTOINCREMENT) )"); tData->m_pDataReport->SilentQuery("CREATE TABLE IF NOT EXISTS \"videos\" ( \"id\" INTEGER NOT NULL, \"session_id\" INTEGER NOT NULL, \"module_id\" INTEGER NOT NULL, \"uuid\" TEXT NOT NULL UNIQUE, \"ts\" INTEGER NOT NULL, \"ts_ms\" INTEGER NOT NULL, \"ext\" TEXT NOT NULL, \"fps\" INTEGER NOT NULL, PRIMARY KEY(\"id\" AUTOINCREMENT) )"); } else { LOG_ERROR("Cannot Connect to RDB!"); delete( tData->m_pDataReport ); free( tData ); tData = NULL; }; } else { LOG_ERROR("Cannot Create Connector to RDB!"); free( tData ); tData = NULL; }; }; } else { LOG_ERROR("The report Vault Dir can not be accessed!"); free( tData ); tData = NULL; }; } else { LOG_ERROR("Cannot Allocate Session Data [malloc]!"); }; if ( tData ) { //LOG("All Seems to be good!"); tData->db_id = m_pDBC->sessionCreate( tData->track_id, tData->sts, tData->sms, tData->uuid, 'A' ); if ( tData->db_id == 0 ) { LOG_ERROR("Cannot Create Session!"); delete( tData->m_pDataReport ); free( tData ); tData = NULL; } else { m_mSessionProtector.lock(); m_mSessions[tack_id] = tData; m_mSessionProtector.unlock(); //Propogate Event ->Session Start EVENT_CONTAINER *tEvt = createBasicEvent( tack_id, EVT_CLASS_SYSTEM, EVT_SRV_SESSION_STARTED ); if ( tEvt ) basicEventHeap::getInstance()->broadcast( tEvt ); }; }; }; return tData; }; bool servaSessionManager::updateSessionStat( uint64_t session_id, bool evt ){ if( SESSION_DATA *tD = validateSession( session_id ) ){ tD->lts = time( NULL ); if ( evt ) { m_pDBC->sessionStatAddEvt( session_id ); } else { m_pDBC->sessionStatAddVid( session_id ); }; return true; }; return false; }; bool servaSessionManager::validate(){ uint32_t cts = time( NULL ); M_SESSIONS::iterator dE; bool bMustDelete = false; m_mSessionProtector.lock(); for( M_SESSIONS::iterator idx = m_mSessions.begin(); idx != m_mSessions.end(); idx++ ){ if ( (*idx).second ) { #ifdef DEBUG_EXTRA char tB[128]; sprintf( tB, "Validation %d %d %d", cts, (*idx).second->lts, m_nSessionTO ); LOG( tB ); #endif if ( !(*idx).second->skeep_validation ) { if (cts - (*idx).second->lts > m_nSessionTO ) { // Expire Session //m_pDBC->updateSessionStatus( (*idx).second->db_id, 1 ); m_pDBC->sessionClose( (*idx).second->db_id ); // Set Session to remove dE = idx; bMustDelete = true; //Propogate Event Session End EVENT_CONTAINER *tEvt = createBasicEvent( (*idx).second->track_id, EVT_CLASS_SYSTEM, EVT_SRV_SESSION_ENDED ); if ( tEvt ) basicEventHeap::getInstance()->broadcast( tEvt ); }; }; }; }; if ( bMustDelete ) { // Remove Session delete( (*dE).second->m_pDataReport ); free( (*dE).second ); (*dE).second = NULL; m_mSessions.erase( dE ); }; m_mSessionProtector.unlock(); return true; }; SESSION_DATA *servaSessionManager::validateSession( uint64_t sid ){ for( M_SESSIONS::iterator idx = m_mSessions.begin(); idx != m_mSessions.end(); idx++ ) if( (*idx).second->db_id == sid ) return (*idx).second; return NULL; }; bool servaSessionManager::dropSession( uint64_t session_id ){ #ifdef DEBUG LOG("Drop Session!"); #endif for( M_SESSIONS::iterator idx = m_mSessions.begin(); idx != m_mSessions.end(); idx++ ){ if ( (*idx).second ) if ((*idx).second->db_id == session_id) return dropSession( idx ); }; return false; }; bool servaSessionManager::dropSession( M_SESSIONS::iterator idx ){ m_mSessionProtector.lock(); delete( (*idx).second->m_pDataReport ); free( (*idx).second ); (*idx).second = NULL; m_mSessions.erase( idx ); m_mSessionProtector.unlock(); return true; }; bool servaSessionManager::eraseSession( uint64_t session_id ){ //Erase just not important sessions // In case The session is Opened! #ifdef DEBUG_EXTRA LOG("Eraze Session!"); #endif dropSession( session_id ); SESSION_DATA * tData = m_pDBC->getSessionData( session_id ); if ( tData ) { char tBase[MAX_PATH]; sprintf( tBase, "%s/%d", m_pDbReportBase, tData->track_id ); if( fileExist( tBase ) ){ REPORT_DIRINFO tRDF; fillRDF( tData->sts, tRDF ); sprintf( tData->reportVaultPath, "%s/%s/%s/%s", tBase, tRDF.YEAR, tRDF.MONTH, tRDF.DAY ); #ifdef DEBUG_EXTRA LOG( tData->reportVaultPath ); #endif if( fileExist( tData->reportVaultPath ) ){ char tName[MAX_PATH]; sprintf( tName, "%s/report.db", tData->reportVaultPath ); if( fileExist( tName ) ){ bool must_unlink = true; tData->m_pDataReport = new SQLite_Connector(); if ( tData->m_pDataReport ) { if( tData->m_pDataReport->Connect( tName, 0, true) ) { char tB[1024]; sprintf( tB, "DELETE FROM `events` WHERE `session_id`=%"PRIu64 , session_id ); tData->m_pDataReport->SilentQuery( tB ); sprintf( tB, "SELECT `uuid`, `ext` FROM `videos` WHERE `session_id`=%"PRIu64 , session_id ); AAssoc *result = tData->m_pDataReport->Query( tB ); if( result != NULL ) { if( result->size() > 0 ) { Assoc * cArr; for ( uint16_t idx = 0; idx < result->size(); idx++ ) { Assoc::iterator it; char fN[MAX_PATH], uA[65], uX[6]; cArr = result->at(idx); it = cArr->find("uuid"); if( it != cArr->end() ){ strncpy( uA, it->second.c_str(), 64 ); uA[64]=(char)0; }; it = cArr->find("ext"); if( it != cArr->end() ){ strncpy( uX, it->second.c_str(), 5 ); uX[5]=(char)0; }; sprintf( fN,"%s/%s.%s", tData->reportVaultPath, uA, uX ); if( fileExist( fN ) ) { #ifdef DEBUG_EXTRA LOG("Deleting ..."); #endif unlink( fN ); #ifdef DEBUG_EXTRA LOG("Deleted!"); } else { LOG("Not Exists!"); #endif }; }; }; deleteResult( result ); }; sprintf( tB, "DELETE FROM `videos` WHERE `session_id`=%"PRIu64 , session_id ); tData->m_pDataReport->SilentQuery( tB ); //Check if still have othes Sessions sprintf( tB, "SELECT count( `id` ) as CNT FROM `events`" ); result = tData->m_pDataReport->Query( tB ); if( result!= NULL ) { if( result->size() > 0 ) { Assoc * cArr = result->at(0); Assoc::iterator it = cArr->find("CNT"); if( it != cArr->end() ) if ( atoi( it->second.c_str() ) > 0 ) must_unlink = false; }; deleteResult( result ); }; }; delete( tData->m_pDataReport ); // close database }; if ( must_unlink ){ #ifdef DEBUG_EXTRA LOG("Remove Database!"); LOG( tName ); #endif unlink( tName ); char tCMD[MAX_PATH]; sprintf( tCMD, "rm -R -f %s", tData->reportVaultPath ); #ifdef DEBUG_EXTRA LOG("Remove Folder!"); LOG( tCMD ); #endif system( tCMD ); }; } else { LOG_ERROR("Report Database Not Found!"); }; }; }; free( tData ); //Delete Session Record m_pDBC->deleteSession( session_id ); }; return true; };