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.

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