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.

684 lines
28 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
1 year ago
​
​
​
​
​
​
1 year ago
​
​
​
1 year ago
​
​
​
​
​
​
​
​
​
1 year ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
2 years ago
​
2 years ago
​
​
2 years ago
​
​
​
​
​
2 years ago
​
​
​
​
  1. #include "servaSessionManager.h"
  2. #include "../common/serverMySQL_dataBinder.h"
  3. #define LOG( X ) m_pLog->Log( "SSMN", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG2w34[256];\
  7. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "SSMN", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "SSMN", this, _LOG_ERROR, X );
  12. #endif
  13. servaSessionManager *servaSessionManager::_instance = NULL;
  14. servaSessionManager *servaSessionManager::getInstance( config_setting_t * Settings ){
  15. if( _instance == NULL ){
  16. _instance = new servaSessionManager( Settings );
  17. };
  18. return _instance;
  19. };
  20. servaSessionManager::servaSessionManager( config_setting_t * Settings ){
  21. m_pLog = basicLogCollection::getInstance();
  22. m_pDBC = (serverDataBinder*) servaMySQLDBC::getInstance();
  23. m_bRepIsConnected = false;
  24. //Read System (loalfile) Settings
  25. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "SESSON_MNGR" );
  26. #ifdef DEBUG
  27. LOG("Getting Settings ...");
  28. #endif
  29. config_setting_t * pSetting;
  30. int tVal;
  31. const char * tBuf;
  32. char tBuffer[128];
  33. if(!config_setting_lookup_int(m_pSettings, "SESSION_TIMEOUT", &tVal )){
  34. pSetting = config_setting_add(m_pSettings, "SESSION_TIMEOUT", CONFIG_TYPE_INT);
  35. m_nSessionTO = DEFAULT_SESSION_TIMEOUT;
  36. config_setting_set_int( pSetting, m_nSessionTO );
  37. basicSettingsProvider::getInstance()->compromise();
  38. LOG("Session Timeout not set - using default");
  39. } else {
  40. m_nSessionTO = tVal;
  41. sprintf( tBuffer, "Session Timeout Read From Settings: %d sec.", m_nSessionTO );
  42. LOG( tBuffer );
  43. };
  44. if(!config_setting_lookup_string(m_pSettings, "REPORTS_DIR_NAME", &tBuf )){
  45. pSetting = config_setting_add(m_pSettings, "REPORTS_DIR_NAME", CONFIG_TYPE_STRING);
  46. sprintf( m_pDbReportBase, "data/reports" );
  47. config_setting_set_string( pSetting, m_pDbReportBase );
  48. basicSettingsProvider::getInstance()->compromise();
  49. LOG("Reports Storage not set - using default");
  50. } else {
  51. sprintf( m_pDbReportBase, tBuf );
  52. };
  53. if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_USER", &tBuf )){
  54. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_USER", CONFIG_TYPE_STRING);
  55. sprintf( web_access_user, "www-data" );
  56. config_setting_set_string( pSetting, web_access_user );
  57. basicSettingsProvider::getInstance()->compromise();
  58. LOG("Web Access User not set - using default");
  59. } else {
  60. sprintf( web_access_user, tBuf );
  61. };
  62. if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_GROUP", &tBuf )){
  63. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_GROUP", CONFIG_TYPE_STRING);
  64. sprintf( web_access_group, "www-data" );
  65. config_setting_set_string( pSetting, web_access_group );
  66. basicSettingsProvider::getInstance()->compromise();
  67. LOG("Web Access Group not set - using default");
  68. } else {
  69. sprintf( web_access_group, tBuf );
  70. };
  71. basicSettingsProvider::getInstance()->validate();
  72. //if( !fileExist( DB_SHMEM_POINT )) {
  73. // bRecreateMemStructures = true;
  74. // mkdir( DB_SHMEM_POINT, STORAGE_DIR_MODE );// S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
  75. // chown( DB_SHMEM_POINT, getUserUid(web_access_user), getUserGid(web_access_group) ); //To make Accessed from PHP
  76. // chmod( DB_SHMEM_POINT, STORAGE_DIR_MODE );
  77. //};
  78. };
  79. servaSessionManager::~servaSessionManager(){
  80. //Close all Ongoing Session since Application goes to die
  81. m_pDBC->closeAllOnGoingSessions();
  82. };
  83. uint64_t servaSessionManager::addEvent( uint64_t tack_id, uint64_t module_id, ARGUS_DATA_EVENT *ptr ){
  84. uint64_t rV = 0;
  85. if( SESSION_DATA *sp = updateSession( tack_id, module_id ) ) {
  86. if( addEvent( sp, ptr ) ) rV = sp->db_id;
  87. };
  88. return rV;
  89. };
  90. bool servaSessionManager::addEvent( uint64_t sid, ARGUS_DATA_EVENT *ptr ){
  91. if( SESSION_DATA *sp = validateSession( sid ) ) {
  92. return addEvent( sp, ptr );
  93. };
  94. return false;
  95. };
  96. bool servaSessionManager::addEvent( SESSION_DATA *sp, ARGUS_DATA_EVENT *ptr ){
  97. if ( ptr == NULL ) return false;
  98. if ( sp == NULL ) return false;
  99. if ( sp->m_pDataReport ) {
  100. char tQBuf[1024];
  101. switch ( ptr->TYPE ) {
  102. case EVT_LRF_END_SESSION_TO:
  103. case EVT_LRF_END_SESSION_MA:
  104. //No Technical events
  105. return false;
  106. sprintf( tQBuf, "INSERT INTO `events`(`session_id`,`module_id`,`ts`,`ts_ms`,`evt_type`) VALUES(%" PRIu64 ",%" PRIu64 ",%d,%d,%d)",
  107. sp->db_id,
  108. ptr->MODULE_ID,
  109. ptr->TS,
  110. ptr->MS,
  111. ptr->TYPE
  112. );
  113. break;
  114. case EVT_LRF_START_SESSION:
  115. //No Technical events
  116. return false;
  117. sprintf( tQBuf, "INSERT INTO `events`(`session_id`,`module_id`,`ts`,`ts_ms`,`evt_type`) VALUES(%" PRIu64 ",%" PRIu64 ",%d,%d,%d)",
  118. sp->db_id,
  119. ptr->MODULE_ID,
  120. ptr->TS,
  121. ptr->MS,
  122. ptr->TYPE
  123. );
  124. break;
  125. case EVT_FLT_LATF_CHK:
  126. case EVT_FLT_LATF_UP_DETEC:
  127. case EVT_FLT_LATF_BT_DETEC:
  128. case EVT_FLT_LATF_TRP_DET:
  129. case EVT_FLT_LATF_TRP_UPDT:
  130. case EVT_FLT_LATF_SUSP_START:
  131. case EVT_FLT_LATF_SUSP_NAK:
  132. case EVT_FLT_LATF_REGULAR:
  133. case EVT_FLT_LATF_DISABLED:
  134. case EVT_FLT_LATF_SUSP_DET:
  135. case EVT_FLT_LATF_DIFF_DET:
  136. case EVT_FLT_LATF_OP_DISABLED:
  137. case EVT_FLT_LATF_OP_TD:
  138. case EVT_FLT_LATF_OP_TG:
  139. case EVT_FLT_LATF_OP_EXP:
  140. case EVT_OP_ACK_MASK:
  141. case EVT_OP_NACK_MASK:
  142. case EVT_OP_ON_MASK:
  143. case EVT_OP_OFF_MASK:
  144. case EVT_CONNECTED:
  145. case EVT_DISCONNECTED:
  146. case EVT_COMMUNICATION_JAMED:
  147. case EVT_STUDY:
  148. case EVT_BUSY:
  149. case EVT_FLIP:
  150. case EVT_READY:
  151. case EVT_ERR_LRF_NO_IRQ:
  152. case EVT_ERR_LRF_NO_ZERO:
  153. case EVT_ERR_LRF_ANGLE:
  154. case EVT_ERR_LRF_VOLTAGE:
  155. case EVT_ERR_LRF_BROKEN:
  156. case EVT_LRF_VIS_CHANGED:
  157. case EVT_ERR_OVERFLOW:
  158. case EVT_SST_OVERFLOW:
  159. case EVT_VIDEO_FPS_DEGRADED:
  160. case EVT_VIDEO_FPS_RAISED:{
  161. //No Technical events
  162. return false;
  163. sprintf( tQBuf, "INSERT INTO `events`(`session_id`,`module_id`,`ts`,`ts_ms`,`evt_type`) VALUES(%" PRIu64 ",%" PRIu64 ",%d,%d,%d)",
  164. sp->db_id,
  165. ptr->MODULE_ID,
  166. ptr->TS,
  167. ptr->MS,
  168. ptr->TYPE
  169. );
  170. break;
  171. };
  172. case EVT_ROI_ACTIVE:
  173. case EVT_TARGET_3D_WHITE:
  174. case EVT_TARGET_3D_BLACK:{
  175. updateSessionStat( sp->db_id );
  176. sprintf( tQBuf, "INSERT INTO `events`(`session_id`,`module_id`,`ts`,`ts_ms`,`evt_type`,`azimuth`,`elevation`,`range`,`area`)" \
  177. " VALUES(%" PRIu64 ",%" PRIu64 ",%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.2f)",
  178. sp->db_id,
  179. ptr->MODULE_ID,
  180. ptr->TS,
  181. ptr->MS,
  182. ptr->TYPE,
  183. ptr->AZIMUTH / 1000.0f,
  184. ptr->ELEVATION / 1000.0f,
  185. ptr->RANGE / 1000.0f,
  186. ptr->AREA / 100.0f
  187. );
  188. break;
  189. };
  190. case EVT_TARGET_2D_WHITE:
  191. case EVT_TARGET_2D_BLACK:{
  192. updateSessionStat( sp->db_id );
  193. sprintf( tQBuf, "INSERT INTO `events`(`session_id`,`module_id`,`ts`,`ts_ms`,`evt_type`,`azimuth`,`range`,`area`)" \
  194. " VALUES(%" PRIu64 ",%" PRIu64 ",%d,%d,%d,%0.2f,%0.2f,%0.2f)",
  195. sp->db_id,
  196. ptr->MODULE_ID,
  197. ptr->TS,
  198. ptr->MS,
  199. ptr->TYPE,
  200. ptr->AZIMUTH / 1000.0f,
  201. ptr->RANGE / 1000.0f,
  202. ptr->AREA / 100.0f
  203. );
  204. break;
  205. };
  206. default: return false;
  207. };
  208. #ifdef DEBUG_EXTRA
  209. LOG( tQBuf );
  210. #endif
  211. return sp->m_pDataReport->SilentQuery( tQBuf );
  212. };
  213. return false;
  214. };
  215. 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 ){
  216. if ( ptr == NULL ){
  217. LOG_ERROR( "ExtInfo ptr is null!");
  218. return false;
  219. };
  220. char tB[64];
  221. sprintf( tB, "Session id: %" PRIu64, ptr->session_id );
  222. LOG( tB );
  223. // At Time of Video Addition the Session May be expired already!!
  224. // No neet to Validate session!
  225. bool bExpiredSession = false;
  226. SESSION_DATA *sp = validateSession( ptr->session_id );
  227. if ( sp == NULL ) {
  228. sp = reopenSession( ptr->session_id );
  229. if ( sp == NULL ) {
  230. LOG_ERROR( "Session Data ptr is null!");
  231. return false;
  232. };
  233. bExpiredSession = true;
  234. };
  235. if ( sp->m_pDataReport ) {
  236. char tName[MAX_PATH];
  237. sprintf(tName, "%s/%s.%s", sp->reportVaultPath, ptr->uuid, ptr->fmt );
  238. chown( tName, getUserUid(web_access_user), getUserGid(web_access_group) );
  239. chmod( tName, DB_FILE_MODE );
  240. updateSessionStat( ptr->session_id, false );
  241. char tQBuf[CTEXT];
  242. sprintf( tQBuf, "INSERT INTO `videos`(`session_id`,`module_id`,`uuid`,`ts`,`ts_ms`,`ext`,`fps`) VALUES(%" PRIu64 ", %d,'%s',%d,%d,'%s',%d)",
  243. ptr->session_id,
  244. ptr->module_id,
  245. ptr->uuid,
  246. ptr->ts,
  247. ptr->tms,
  248. ptr->fmt,
  249. ptr->fps
  250. );
  251. #ifdef DEBUG_EXTRA
  252. LOG( tQBuf );
  253. #endif
  254. bool rv = sp->m_pDataReport->SilentQuery( tQBuf );
  255. if( bExpiredSession ){
  256. delete( sp->m_pDataReport );
  257. free( sp );
  258. };
  259. return rv;
  260. } else {
  261. LOG_ERROR( "Report DB is closed!");
  262. };
  263. return false;
  264. };
  265. bool servaSessionManager::create(){
  266. //Reset All Ongoing Session since Application was restarted (crashed?)
  267. return m_pDBC->closeAllOnGoingSessions();
  268. };
  269. CSTR servaSessionManager::getReportDir( uint64_t track_id, uint32_t ts ){
  270. char retVal[MAX_PATH];
  271. REPORT_DIRINFO tRDF;
  272. fillRDF( ts, tRDF );
  273. sprintf( retVal, "%s/%" PRIu64 "/%s/%s/%s", m_pDbReportBase, track_id, tRDF.YEAR, tRDF.MONTH, tRDF.DAY );
  274. LOG( retVal );
  275. return strdup( retVal );
  276. };
  277. CSTR servaSessionManager::getSessionBase( uint64_t sid ){
  278. SESSION_DATA *sp = validateSession( sid );
  279. if ( sp ) return sp->reportVaultPath;
  280. return NULL;
  281. };
  282. SESSION_DATA *servaSessionManager::reopenSession( uint64_t session_id ){
  283. SESSION_DATA * tData = NULL;
  284. // No Session -> Create one
  285. tData = m_pDBC->getSessionData( session_id );
  286. if ( tData ) {
  287. //Reopen just for fun :)
  288. tData->skeep_validation = true;
  289. // Create History Database if not exists
  290. char tBase[MAX_PATH];
  291. sprintf( tBase, "%s/%d", m_pDbReportBase, tData->track_id );
  292. if( fileExist( tBase ) == 0 ){
  293. if (!createAccessibleDir( tBase, STORAGE_DIR_MODE, getUserUid(web_access_user), getUserGid(web_access_group))){
  294. LOG_ERROR("Cannot create Base Report Dir!");
  295. #ifdef DEBUG
  296. char tB[1024];
  297. 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) );
  298. LOG( tB );
  299. #endif
  300. free( tData );
  301. return NULL;
  302. };
  303. };
  304. REPORT_DIRINFO tRDF;
  305. fillRDF( tData->sts, tRDF );
  306. if( checkRDFDir( tBase, tRDF, true, getUserUid(web_access_user), getUserGid(web_access_group), STORAGE_DIR_MODE ) ){
  307. #ifdef DEBUG
  308. LOG("Connect to Report Database RS");
  309. #endif
  310. sprintf( tData->reportVaultPath, "%s/%s/%s/%s", tBase, tRDF.YEAR, tRDF.MONTH, tRDF.DAY );
  311. //LOG( tData->reportVaultPath );
  312. char tName[MAX_PATH];
  313. sprintf( tName, "%s/report.db", tData->reportVaultPath );
  314. if( fileExist( tName ) ){
  315. //Allready have report file
  316. tData->m_pDataReport = new SQLite_Connector();
  317. if ( tData->m_pDataReport ) {
  318. if( !tData->m_pDataReport->Connect( tName, 0, true) ) {
  319. LOG_ERROR("Cannot Connect to RDB!");
  320. delete( tData->m_pDataReport );
  321. free( tData );
  322. tData = NULL;
  323. };
  324. } else {
  325. LOG_ERROR("Cannot Create Connector to RDB!");
  326. free( tData );
  327. tData = NULL;
  328. };
  329. } else {
  330. #ifdef DEBUG
  331. LOG("Create New Report Database");
  332. #endif
  333. //Create new report file
  334. tData->m_pDataReport = new SQLite_Connector();
  335. if ( tData->m_pDataReport ) {
  336. if( tData->m_pDataReport->Connect( tName, 0, true) ) {
  337. chown( tName, getUserUid(web_access_user), getUserGid(web_access_group) );
  338. chmod( tName, DB_FILE_MODE );
  339. 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) )");
  340. 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) )");
  341. } else {
  342. LOG_ERROR("Cannot Connect to RDB!");
  343. delete( tData->m_pDataReport );
  344. free( tData );
  345. tData = NULL;
  346. };
  347. } else {
  348. LOG_ERROR("Cannot Create Connector to RDB!");
  349. free( tData );
  350. tData = NULL;
  351. };
  352. };
  353. } else {
  354. LOG_ERROR("The report Vault Dir can not be accessed!");
  355. free( tData );
  356. tData = NULL;
  357. };
  358. };
  359. if ( tData ){
  360. LOG("Assign Session To Vector!");
  361. m_mSessions[tData->track_id] = tData;
  362. };
  363. return tData;
  364. };
  365. SESSION_DATA *servaSessionManager::updateSession( uint64_t tack_id, uint64_t module_id ){
  366. SESSION_DATA * tData = NULL;
  367. try {
  368. tData = m_mSessions.at( tack_id );
  369. #ifdef DEBUG
  370. LOG("Have Opened Session");
  371. #endif
  372. } catch ( ... ) {
  373. // No Session -> Create one
  374. tData = (SESSION_DATA*) malloc( sizeof(SESSION_DATA) );
  375. if ( tData ) {
  376. memset( tData, 0, sizeof(SESSION_DATA) );
  377. uint64_t cms = getMilliseconds();
  378. tData->lts = tData->sts = cms/1000;
  379. tData->sms = cms%1000;
  380. tData->track_id = tack_id;
  381. getUniqID_UUID( tData->uuid );
  382. // Create History Database if not exists
  383. char tBase[MAX_PATH];
  384. sprintf( tBase, "%s/%" PRIu64, m_pDbReportBase, tack_id );
  385. if( fileExist( tBase ) == 0 ){
  386. if (!createAccessibleDir( tBase, STORAGE_DIR_MODE, getUserUid(web_access_user), getUserGid(web_access_group))){
  387. LOG_ERROR("Cannot create Base Report Dir!");
  388. #ifdef DEBUG
  389. char tB[1024];
  390. 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) );
  391. LOG( tB );
  392. #endif
  393. free( tData );
  394. return NULL;
  395. };
  396. };
  397. REPORT_DIRINFO tRDF;
  398. fillRDF( tData->sts, tRDF );
  399. if( checkRDFDir( tBase, tRDF, true, getUserUid(web_access_user), getUserGid(web_access_group), STORAGE_DIR_MODE ) ){
  400. #ifdef DEBUG
  401. LOG("Connect to Report Database US");
  402. #endif
  403. sprintf( tData->reportVaultPath, "%s/%s/%s/%s", tBase, tRDF.YEAR, tRDF.MONTH, tRDF.DAY );
  404. LOG( tData->reportVaultPath );
  405. char tName[MAX_PATH];
  406. sprintf( tName, "%s/report.db", tData->reportVaultPath );
  407. if( fileExist( tName ) ){
  408. //Allready have report file
  409. tData->m_pDataReport = new SQLite_Connector();
  410. if ( tData->m_pDataReport ) {
  411. if( !tData->m_pDataReport->Connect( tName, 0, true) ) {
  412. LOG_ERROR("Cannot Connect to RDB!");
  413. delete( tData->m_pDataReport );
  414. free( tData );
  415. tData = NULL;
  416. };
  417. } else {
  418. LOG_ERROR("Cannot Create Connector to RDB!");
  419. free( tData );
  420. tData = NULL;
  421. };
  422. } else {
  423. #ifdef DEBUG
  424. LOG("Create New Report Database");
  425. #endif
  426. //Create new report file
  427. tData->m_pDataReport = new SQLite_Connector();
  428. if ( tData->m_pDataReport ) {
  429. if( tData->m_pDataReport->Connect( tName, 0, true) ) {
  430. chown( tName, getUserUid(web_access_user), getUserGid(web_access_group) );
  431. chmod( tName, DB_FILE_MODE );
  432. 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) )");
  433. 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) )");
  434. } else {
  435. LOG_ERROR("Cannot Connect to RDB!");
  436. delete( tData->m_pDataReport );
  437. free( tData );
  438. tData = NULL;
  439. };
  440. } else {
  441. LOG_ERROR("Cannot Create Connector to RDB!");
  442. free( tData );
  443. tData = NULL;
  444. };
  445. };
  446. } else {
  447. LOG_ERROR("The report Vault Dir can not be accessed!");
  448. free( tData );
  449. tData = NULL;
  450. };
  451. } else {
  452. LOG_ERROR("Cannot Allocate Session Data [malloc]!");
  453. };
  454. if ( tData ) {
  455. //LOG("All Seems to be good!");
  456. tData->db_id = m_pDBC->sessionCreate( tData->track_id, tData->sts, tData->sms, tData->uuid, 'A' );
  457. if ( tData->db_id == 0 ) {
  458. LOG_ERROR("Cannot Create Session!");
  459. delete( tData->m_pDataReport );
  460. free( tData );
  461. tData = NULL;
  462. } else m_mSessions[tack_id] = tData;
  463. };
  464. };
  465. return tData;
  466. };
  467. bool servaSessionManager::updateSessionStat( uint64_t session_id, bool evt ){
  468. if( SESSION_DATA *tD = validateSession( session_id ) ){
  469. tD->lts = time( NULL );
  470. if ( evt ) {
  471. m_pDBC->sessionStatAddEvt( session_id );
  472. } else {
  473. m_pDBC->sessionStatAddVid( session_id );
  474. };
  475. return true;
  476. };
  477. return false;
  478. };
  479. bool servaSessionManager::validate(){
  480. uint32_t cts = time( NULL );
  481. M_SESSIONS::iterator dE;
  482. bool bMustDelete = false;
  483. for( M_SESSIONS::iterator idx = m_mSessions.begin(); idx != m_mSessions.end(); idx++ ){
  484. if ( (*idx).second ) {
  485. #ifdef DEBUG_EXTRA
  486. char tB[128];
  487. sprintf( tB, "Validation %d %d %d", cts, (*idx).second->lts, m_nSessionTO );
  488. LOG( tB );
  489. #endif
  490. if ( !(*idx).second->skeep_validation ) {
  491. if (cts - (*idx).second->lts > m_nSessionTO) {
  492. // Expire Session
  493. //m_pDBC->updateSessionStatus( (*idx).second->db_id, 1 );
  494. m_pDBC->sessionClose( (*idx).second->db_id );
  495. // Set Session to remove
  496. dE = idx;
  497. bMustDelete = true;
  498. };
  499. };
  500. };
  501. };
  502. if ( bMustDelete ) {
  503. // Remove Session
  504. delete( (*dE).second->m_pDataReport );
  505. free( (*dE).second );
  506. (*dE).second = NULL;
  507. m_mSessions.erase( dE );
  508. };
  509. return true;
  510. };
  511. SESSION_DATA *servaSessionManager::validateSession( uint64_t sid ){
  512. for( M_SESSIONS::iterator idx = m_mSessions.begin(); idx != m_mSessions.end(); idx++ )
  513. if( (*idx).second->db_id == sid ) return (*idx).second;
  514. return NULL;
  515. };
  516. bool servaSessionManager::dropSession( uint64_t session_id ){
  517. M_SESSIONS::iterator dE;
  518. bool bMustDelete = false;
  519. #ifdef DEBUG_EXTRA
  520. LOG("Drop Session!");
  521. #endif
  522. for( M_SESSIONS::iterator idx = m_mSessions.begin(); idx != m_mSessions.end(); idx++ ){
  523. if ( (*idx).second ) {
  524. if( (*idx).second->db_id == session_id ) {
  525. bMustDelete = true;
  526. break;
  527. };
  528. };
  529. };
  530. if ( bMustDelete ) {
  531. // Remove Session
  532. delete( (*dE).second->m_pDataReport );
  533. free( (*dE).second );
  534. (*dE).second = NULL;
  535. m_mSessions.erase( dE );
  536. return true;
  537. };
  538. return false;
  539. };
  540. bool servaSessionManager::eraseSession( uint64_t session_id ){
  541. //Erase just not important sessions
  542. // In case The session is Opened!
  543. #ifdef DEBUG_EXTRA
  544. LOG("Eraze Session!");
  545. #endif
  546. dropSession( session_id );
  547. SESSION_DATA * tData = m_pDBC->getSessionData( session_id );
  548. if ( tData ) {
  549. char tBase[MAX_PATH];
  550. sprintf( tBase, "%s/%d", m_pDbReportBase, tData->track_id );
  551. if( fileExist( tBase ) ){
  552. REPORT_DIRINFO tRDF;
  553. fillRDF( tData->sts, tRDF );
  554. sprintf( tData->reportVaultPath, "%s/%s/%s/%s", tBase, tRDF.YEAR, tRDF.MONTH, tRDF.DAY );
  555. #ifdef DEBUG_EXTRA
  556. LOG( tData->reportVaultPath );
  557. #endif
  558. if( fileExist( tData->reportVaultPath ) ){
  559. char tName[MAX_PATH];
  560. sprintf( tName, "%s/report.db", tData->reportVaultPath );
  561. if( fileExist( tName ) ){
  562. bool must_unlink = true;
  563. tData->m_pDataReport = new SQLite_Connector();
  564. if ( tData->m_pDataReport ) {
  565. if( tData->m_pDataReport->Connect( tName, 0, true) ) {
  566. char tB[1024];
  567. sprintf( tB, "DELETE FROM `events` WHERE `session_id`=%"PRIu64 , session_id );
  568. tData->m_pDataReport->SilentQuery( tB );
  569. sprintf( tB, "SELECT `uuid`, `ext` FROM `videos` WHERE `session_id`=%"PRIu64 , session_id );
  570. AAssoc *result = tData->m_pDataReport->Query( tB );
  571. if( result != NULL ) {
  572. if( result->size() > 0 ) {
  573. Assoc * cArr;
  574. for ( uint16_t idx = 0; idx < result->size(); idx++ ) {
  575. Assoc::iterator it;
  576. char fN[MAX_PATH], uA[65], uX[6];
  577. cArr = result->at(idx);
  578. it = cArr->find("uuid");
  579. if( it != cArr->end() ){
  580. strncpy( uA, it->second.c_str(), 64 );
  581. uA[64]=(char)0;
  582. };
  583. it = cArr->find("ext");
  584. if( it != cArr->end() ){
  585. strncpy( uX, it->second.c_str(), 5 );
  586. uX[5]=(char)0;
  587. };
  588. sprintf( fN,"%s/%s.%s", tData->reportVaultPath, uA, uX );
  589. if( fileExist( fN ) ) {
  590. #ifdef DEBUG_EXTRA
  591. LOG("Deleting ...");
  592. #endif
  593. unlink( fN );
  594. #ifdef DEBUG_EXTRA
  595. LOG("Deleted!");
  596. } else {
  597. LOG("Not Exists!");
  598. #endif
  599. };
  600. };
  601. };
  602. deleteResult( result );
  603. };
  604. sprintf( tB, "DELETE FROM `videos` WHERE `session_id`=%"PRIu64 , session_id );
  605. tData->m_pDataReport->SilentQuery( tB );
  606. //Check if still have othes Sessions
  607. sprintf( tB, "SELECT count( `id` ) as CNT FROM `events`" );
  608. result = tData->m_pDataReport->Query( tB );
  609. if( result!= NULL ) {
  610. if( result->size() > 0 ) {
  611. Assoc * cArr = result->at(0);
  612. Assoc::iterator it = cArr->find("CNT");
  613. if( it != cArr->end() ) if ( atoi( it->second.c_str() ) > 0 ) must_unlink = false;
  614. };
  615. deleteResult( result );
  616. };
  617. };
  618. delete( tData->m_pDataReport ); // close database
  619. };
  620. if ( must_unlink ){
  621. #ifdef DEBUG_EXTRA
  622. LOG("Remove Database!");
  623. LOG( tName );
  624. #endif
  625. unlink( tName );
  626. char tCMD[MAX_PATH];
  627. sprintf( tCMD, "rm -R -f %s", tData->reportVaultPath );
  628. #ifdef DEBUG_EXTRA
  629. LOG("Remove Folder!");
  630. LOG( tCMD );
  631. #endif
  632. system( tCMD );
  633. };
  634. } else {
  635. LOG_ERROR("Report Database Not Found!");
  636. };
  637. };
  638. };
  639. free( tData );
  640. //Delete Session Record
  641. m_pDBC->deleteSession( session_id );
  642. };
  643. return true;
  644. };