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.

791 lines
33 KiB

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