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.

680 lines
28 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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
​
​
​
​
​
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. SESSION_DATA *sp = validateSession( ptr->session_id );
  226. if ( sp == NULL ) {
  227. sp = reopenSession( ptr->session_id );
  228. if ( sp == NULL ) {
  229. LOG_ERROR( "Session Data ptr is null!");
  230. return false;
  231. };
  232. };
  233. if ( sp->m_pDataReport ) {
  234. char tName[MAX_PATH];
  235. sprintf(tName, "%s/%s.%s", sp->reportVaultPath, ptr->uuid, ptr->fmt );
  236. chown( tName, getUserUid(web_access_user), getUserGid(web_access_group) );
  237. chmod( tName, DB_FILE_MODE );
  238. updateSessionStat( ptr->session_id, false );
  239. char tQBuf[1024];
  240. sprintf( tQBuf, "INSERT INTO `videos`(`session_id`,`module_id`,`uuid`,`ts`,`ts_ms`,`ext`,`fps`) VALUES(%" PRIu64 ", %d,'%s',%d,%d,'%s',%d)",
  241. ptr->session_id,
  242. ptr->module_id,
  243. ptr->uuid,
  244. ptr->ts,
  245. ptr->tms,
  246. ptr->fmt,
  247. ptr->fps
  248. );
  249. #ifdef DEBUG_EXTRA
  250. LOG( tQBuf );
  251. #endif
  252. bool rv = sp->m_pDataReport->SilentQuery( tQBuf );
  253. if (sp->skeep_validation ) dropSession( ptr->session_id );
  254. return rv;
  255. } else {
  256. LOG_ERROR( "Report DB is closed!");
  257. };
  258. return false;
  259. };
  260. bool servaSessionManager::create(){
  261. //Reset All Ongoing Session since Application was restarted (crashed?)
  262. return m_pDBC->closeAllOnGoingSessions();
  263. };
  264. CSTR servaSessionManager::getReportDir( uint64_t track_id, uint32_t ts ){
  265. char retVal[MAX_PATH];
  266. REPORT_DIRINFO tRDF;
  267. fillRDF( ts, tRDF );
  268. sprintf( retVal, "%s/%" PRIu64 "/%s/%s/%s", m_pDbReportBase, track_id, tRDF.YEAR, tRDF.MONTH, tRDF.DAY );
  269. LOG( retVal );
  270. return strdup( retVal );
  271. };
  272. CSTR servaSessionManager::getSessionBase( uint64_t sid ){
  273. SESSION_DATA *sp = validateSession( sid );
  274. if ( sp ) return sp->reportVaultPath;
  275. return NULL;
  276. };
  277. SESSION_DATA *servaSessionManager::reopenSession( uint64_t session_id ){
  278. SESSION_DATA * tData = NULL;
  279. // No Session -> Create one
  280. tData = m_pDBC->getSessionData( session_id );
  281. if ( tData ) {
  282. //Reopen just for fun :)
  283. tData->skeep_validation = true;
  284. // Create History Database if not exists
  285. char tBase[MAX_PATH];
  286. sprintf( tBase, "%s/%d", m_pDbReportBase, tData->track_id );
  287. if( fileExist( tBase ) == 0 ){
  288. if (!createAccessibleDir( tBase, STORAGE_DIR_MODE, getUserUid(web_access_user), getUserGid(web_access_group))){
  289. LOG_ERROR("Cannot create Base Report Dir!");
  290. #ifdef DEBUG
  291. char tB[1024];
  292. 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) );
  293. LOG( tB );
  294. #endif
  295. free( tData );
  296. return NULL;
  297. };
  298. };
  299. REPORT_DIRINFO tRDF;
  300. fillRDF( tData->sts, tRDF );
  301. if( checkRDFDir( tBase, tRDF, true, getUserUid(web_access_user), getUserGid(web_access_group), STORAGE_DIR_MODE ) ){
  302. #ifdef DEBUG
  303. LOG("Connect to Report Database RS");
  304. #endif
  305. sprintf( tData->reportVaultPath, "%s/%s/%s/%s", tBase, tRDF.YEAR, tRDF.MONTH, tRDF.DAY );
  306. //LOG( tData->reportVaultPath );
  307. char tName[MAX_PATH];
  308. sprintf( tName, "%s/report.db", tData->reportVaultPath );
  309. if( fileExist( tName ) ){
  310. //Allready have report file
  311. tData->m_pDataReport = new SQLite_Connector();
  312. if ( tData->m_pDataReport ) {
  313. if( !tData->m_pDataReport->Connect( tName, 0, true) ) {
  314. LOG_ERROR("Cannot Connect to RDB!");
  315. delete( tData->m_pDataReport );
  316. free( tData );
  317. tData = NULL;
  318. };
  319. } else {
  320. LOG_ERROR("Cannot Create Connector to RDB!");
  321. free( tData );
  322. tData = NULL;
  323. };
  324. } else {
  325. #ifdef DEBUG
  326. LOG("Create New Report Database");
  327. #endif
  328. //Create new report file
  329. tData->m_pDataReport = new SQLite_Connector();
  330. if ( tData->m_pDataReport ) {
  331. if( tData->m_pDataReport->Connect( tName, 0, true) ) {
  332. chown( tName, getUserUid(web_access_user), getUserGid(web_access_group) );
  333. chmod( tName, DB_FILE_MODE );
  334. 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) )");
  335. 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) )");
  336. } else {
  337. LOG_ERROR("Cannot Connect to RDB!");
  338. delete( tData->m_pDataReport );
  339. free( tData );
  340. tData = NULL;
  341. };
  342. } else {
  343. LOG_ERROR("Cannot Create Connector to RDB!");
  344. free( tData );
  345. tData = NULL;
  346. };
  347. };
  348. } else {
  349. LOG_ERROR("The report Vault Dir can not be accessed!");
  350. free( tData );
  351. tData = NULL;
  352. };
  353. };
  354. if ( tData ){
  355. LOG("Assign Session To Vector!");
  356. m_mSessions[tData->track_id] = tData;
  357. };
  358. return tData;
  359. };
  360. SESSION_DATA *servaSessionManager::updateSession( uint64_t tack_id, uint64_t module_id ){
  361. SESSION_DATA * tData = NULL;
  362. try {
  363. tData = m_mSessions.at( tack_id );
  364. #ifdef DEBUG
  365. LOG("Have Opened Session");
  366. #endif
  367. } catch ( ... ) {
  368. // No Session -> Create one
  369. tData = (SESSION_DATA*) malloc( sizeof(SESSION_DATA) );
  370. if ( tData ) {
  371. memset( tData, 0, sizeof(SESSION_DATA) );
  372. uint64_t cms = getMilliseconds();
  373. tData->lts = tData->sts = cms/1000;
  374. tData->sms = cms%1000;
  375. tData->track_id = tack_id;
  376. getUniqID_UUID( tData->uuid );
  377. // Create History Database if not exists
  378. char tBase[MAX_PATH];
  379. sprintf( tBase, "%s/%" PRIu64, m_pDbReportBase, tack_id );
  380. if( fileExist( tBase ) == 0 ){
  381. if (!createAccessibleDir( tBase, STORAGE_DIR_MODE, getUserUid(web_access_user), getUserGid(web_access_group))){
  382. LOG_ERROR("Cannot create Base Report Dir!");
  383. #ifdef DEBUG
  384. char tB[1024];
  385. 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) );
  386. LOG( tB );
  387. #endif
  388. free( tData );
  389. return NULL;
  390. };
  391. };
  392. REPORT_DIRINFO tRDF;
  393. fillRDF( tData->sts, tRDF );
  394. if( checkRDFDir( tBase, tRDF, true, getUserUid(web_access_user), getUserGid(web_access_group), STORAGE_DIR_MODE ) ){
  395. #ifdef DEBUG
  396. LOG("Connect to Report Database US");
  397. #endif
  398. sprintf( tData->reportVaultPath, "%s/%s/%s/%s", tBase, tRDF.YEAR, tRDF.MONTH, tRDF.DAY );
  399. LOG( tData->reportVaultPath );
  400. char tName[MAX_PATH];
  401. sprintf( tName, "%s/report.db", tData->reportVaultPath );
  402. if( fileExist( tName ) ){
  403. //Allready have report file
  404. tData->m_pDataReport = new SQLite_Connector();
  405. if ( tData->m_pDataReport ) {
  406. if( !tData->m_pDataReport->Connect( tName, 0, true) ) {
  407. LOG_ERROR("Cannot Connect to RDB!");
  408. delete( tData->m_pDataReport );
  409. free( tData );
  410. tData = NULL;
  411. };
  412. } else {
  413. LOG_ERROR("Cannot Create Connector to RDB!");
  414. free( tData );
  415. tData = NULL;
  416. };
  417. } else {
  418. #ifdef DEBUG
  419. LOG("Create New Report Database");
  420. #endif
  421. //Create new report file
  422. tData->m_pDataReport = new SQLite_Connector();
  423. if ( tData->m_pDataReport ) {
  424. if( tData->m_pDataReport->Connect( tName, 0, true) ) {
  425. chown( tName, getUserUid(web_access_user), getUserGid(web_access_group) );
  426. chmod( tName, DB_FILE_MODE );
  427. 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) )");
  428. 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) )");
  429. } else {
  430. LOG_ERROR("Cannot Connect to RDB!");
  431. delete( tData->m_pDataReport );
  432. free( tData );
  433. tData = NULL;
  434. };
  435. } else {
  436. LOG_ERROR("Cannot Create Connector to RDB!");
  437. free( tData );
  438. tData = NULL;
  439. };
  440. };
  441. } else {
  442. LOG_ERROR("The report Vault Dir can not be accessed!");
  443. free( tData );
  444. tData = NULL;
  445. };
  446. } else {
  447. LOG_ERROR("Cannot Allocate Session Data [malloc]!");
  448. };
  449. if ( tData ) {
  450. //LOG("All Seems to be good!");
  451. tData->db_id = m_pDBC->sessionCreate( tData->track_id, tData->sts, tData->sms, tData->uuid, 'A' );
  452. if ( tData->db_id == 0 ) {
  453. LOG_ERROR("Cannot Create Session!");
  454. delete( tData->m_pDataReport );
  455. free( tData );
  456. tData = NULL;
  457. } else m_mSessions[tack_id] = tData;
  458. };
  459. };
  460. return tData;
  461. };
  462. bool servaSessionManager::updateSessionStat( uint64_t session_id, bool evt ){
  463. if( SESSION_DATA *tD = validateSession( session_id ) ){
  464. tD->lts = time( NULL );
  465. if ( evt ) {
  466. m_pDBC->sessionStatAddEvt( session_id );
  467. } else {
  468. m_pDBC->sessionStatAddVid( session_id );
  469. };
  470. return true;
  471. };
  472. return false;
  473. };
  474. bool servaSessionManager::validate(){
  475. uint32_t cts = time( NULL );
  476. M_SESSIONS::iterator dE;
  477. bool bMustDelete = false;
  478. for( M_SESSIONS::iterator idx = m_mSessions.begin(); idx != m_mSessions.end(); idx++ ){
  479. if ( (*idx).second ) {
  480. #ifdef DEBUG_EXTRA
  481. char tB[128];
  482. sprintf( tB, "Validation %d %d %d", cts, (*idx).second->lts, m_nSessionTO );
  483. LOG( tB );
  484. #endif
  485. if ( !(*idx).second->skeep_validation ) {
  486. if (cts - (*idx).second->lts > m_nSessionTO) {
  487. // Expire Session
  488. //m_pDBC->updateSessionStatus( (*idx).second->db_id, 1 );
  489. m_pDBC->sessionClose( (*idx).second->db_id );
  490. // Set Session to remove
  491. dE = idx;
  492. bMustDelete = true;
  493. };
  494. };
  495. };
  496. };
  497. if ( bMustDelete ) {
  498. // Remove Session
  499. delete( (*dE).second->m_pDataReport );
  500. free( (*dE).second );
  501. (*dE).second = NULL;
  502. m_mSessions.erase( dE );
  503. };
  504. return true;
  505. };
  506. SESSION_DATA *servaSessionManager::validateSession( uint64_t sid ){
  507. for( M_SESSIONS::iterator idx = m_mSessions.begin(); idx != m_mSessions.end(); idx++ )
  508. if( (*idx).second->db_id == sid ) return (*idx).second;
  509. return NULL;
  510. };
  511. bool servaSessionManager::dropSession( uint64_t session_id ){
  512. M_SESSIONS::iterator dE;
  513. bool bMustDelete = false;
  514. #ifdef DEBUG_EXTRA
  515. LOG("Drop Session!");
  516. #endif
  517. for( M_SESSIONS::iterator idx = m_mSessions.begin(); idx != m_mSessions.end(); idx++ ){
  518. if ( (*idx).second ) {
  519. if( (*idx).second->db_id == session_id ) {
  520. bMustDelete = true;
  521. break;
  522. };
  523. };
  524. };
  525. if ( bMustDelete ) {
  526. // Remove Session
  527. delete( (*dE).second->m_pDataReport );
  528. free( (*dE).second );
  529. (*dE).second = NULL;
  530. m_mSessions.erase( dE );
  531. return true;
  532. };
  533. return false;
  534. };
  535. bool servaSessionManager::eraseSession( uint64_t session_id ){
  536. //Erase just not important sessions
  537. // In case The session is Opened!
  538. #ifdef DEBUG_EXTRA
  539. LOG("Eraze Session!");
  540. #endif
  541. dropSession( session_id );
  542. SESSION_DATA * tData = m_pDBC->getSessionData( session_id );
  543. if ( tData ) {
  544. char tBase[MAX_PATH];
  545. sprintf( tBase, "%s/%d", m_pDbReportBase, tData->track_id );
  546. if( fileExist( tBase ) ){
  547. REPORT_DIRINFO tRDF;
  548. fillRDF( tData->sts, tRDF );
  549. sprintf( tData->reportVaultPath, "%s/%s/%s/%s", tBase, tRDF.YEAR, tRDF.MONTH, tRDF.DAY );
  550. #ifdef DEBUG_EXTRA
  551. LOG( tData->reportVaultPath );
  552. #endif
  553. if( fileExist( tData->reportVaultPath ) ){
  554. char tName[MAX_PATH];
  555. sprintf( tName, "%s/report.db", tData->reportVaultPath );
  556. if( fileExist( tName ) ){
  557. bool must_unlink = true;
  558. tData->m_pDataReport = new SQLite_Connector();
  559. if ( tData->m_pDataReport ) {
  560. if( tData->m_pDataReport->Connect( tName, 0, true) ) {
  561. char tB[1024];
  562. sprintf( tB, "DELETE FROM `events` WHERE `session_id`=%"PRIu64 , session_id );
  563. tData->m_pDataReport->SilentQuery( tB );
  564. sprintf( tB, "SELECT `uuid`, `ext` FROM `videos` WHERE `session_id`=%"PRIu64 , session_id );
  565. AAssoc *result = tData->m_pDataReport->Query( tB );
  566. if( result != NULL ) {
  567. if( result->size() > 0 ) {
  568. Assoc * cArr;
  569. for ( uint16_t idx = 0; idx < result->size(); idx++ ) {
  570. Assoc::iterator it;
  571. char fN[MAX_PATH], uA[65], uX[6];
  572. cArr = result->at(idx);
  573. it = cArr->find("uuid");
  574. if( it != cArr->end() ){
  575. strncpy( uA, it->second.c_str(), 64 );
  576. uA[64]=(char)0;
  577. };
  578. it = cArr->find("ext");
  579. if( it != cArr->end() ){
  580. strncpy( uX, it->second.c_str(), 5 );
  581. uX[5]=(char)0;
  582. };
  583. sprintf( fN,"%s/%s.%s", tData->reportVaultPath, uA, uX );
  584. if( fileExist( fN ) ) {
  585. #ifdef DEBUG_EXTRA
  586. LOG("Deleting ...");
  587. #endif
  588. unlink( fN );
  589. #ifdef DEBUG_EXTRA
  590. LOG("Deleted!");
  591. } else {
  592. LOG("Not Exists!");
  593. #endif
  594. };
  595. };
  596. };
  597. deleteResult( result );
  598. };
  599. sprintf( tB, "DELETE FROM `videos` WHERE `session_id`=%"PRIu64 , session_id );
  600. tData->m_pDataReport->SilentQuery( tB );
  601. //Check if still have othes Sessions
  602. sprintf( tB, "SELECT count( `id` ) as CNT FROM `events`" );
  603. result = tData->m_pDataReport->Query( tB );
  604. if( result!= NULL ) {
  605. if( result->size() > 0 ) {
  606. Assoc * cArr = result->at(0);
  607. Assoc::iterator it = cArr->find("CNT");
  608. if( it != cArr->end() ) if ( atoi( it->second.c_str() ) > 0 ) must_unlink = false;
  609. };
  610. deleteResult( result );
  611. };
  612. };
  613. delete( tData->m_pDataReport ); // close database
  614. };
  615. if ( must_unlink ){
  616. #ifdef DEBUG_EXTRA
  617. LOG("Remove Database!");
  618. LOG( tName );
  619. #endif
  620. unlink( tName );
  621. char tCMD[MAX_PATH];
  622. sprintf( tCMD, "rm -R -f %s", tData->reportVaultPath );
  623. #ifdef DEBUG_EXTRA
  624. LOG("Remove Folder!");
  625. LOG( tCMD );
  626. #endif
  627. system( tCMD );
  628. };
  629. } else {
  630. LOG_ERROR("Report Database Not Found!");
  631. };
  632. };
  633. };
  634. free( tData );
  635. //Delete Session Record
  636. m_pDBC->deleteSession( session_id );
  637. };
  638. return true;
  639. };