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.

834 lines
35 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. LOG("Session is New");
  517. if ( tData->db_id == 0 ) {
  518. DBC_IS_MODULE_DATA *tMData = m_pDBC->getModuleData( module_id );
  519. if ( tMData ) {
  520. DBC_IS_TRACK_DATA *tTData = m_pDBC->getTrackData( tack_id );
  521. if ( tTData ) {
  522. if ( tTData->side == 'U') {
  523. if (tMData->side == 'F') {
  524. tData->src = 'L';
  525. } else {
  526. tData->src = 'R';
  527. };
  528. } else {
  529. if (tMData->side == 'F') {
  530. tData->src = 'R';
  531. } else {
  532. tData->src = 'L';
  533. };
  534. };
  535. free(tTData);
  536. };
  537. free(tMData);
  538. };
  539. char tB[128];
  540. LOG("All Seems to be good!");
  541. sprintf( tB, "Session Source is: [%c]", tData->src );
  542. LOG( tB );
  543. tData->db_id = m_pDBC->sessionCreate( tData->track_id, tData->sts, tData->sms, tData->uuid, tData->src );
  544. if ( tData->db_id == 0 ) {
  545. LOG_ERROR("Cannot Create Session!");
  546. delete( tData->m_pDataReport );
  547. free( tData );
  548. tData = NULL;
  549. } else {
  550. LOG("Session Registered at Database");
  551. //Propogate Event ->Session Start
  552. SERVA_SESSION_EVT_DATA *tSData = (SERVA_SESSION_EVT_DATA*) malloc( sizeof(SERVA_SESSION_EVT_DATA));
  553. if ( tSData ) {
  554. tSData->side = tData->src;
  555. tSData->track_id = tData->track_id;
  556. EVENT_CONTAINER *tEvt = createBasicEvent( tack_id, EVT_CLASS_SYSTEM, EVT_SRV_SESSION_STARTED, NULL, NULL, tSData );
  557. if ( tEvt ) basicEventHeap::getInstance()->broadcast( tEvt );
  558. };
  559. // Put Session into collection
  560. m_mSessionProtector.lock();
  561. m_mSessions[tack_id] = tData;
  562. m_mSessionProtector.unlock();
  563. };
  564. } else {
  565. LOG("Session is Old");
  566. char cSRC = ' ';
  567. DBC_IS_MODULE_DATA *tMData = m_pDBC->getModuleData( module_id );
  568. if ( tMData ) {
  569. DBC_IS_TRACK_DATA *tTData = m_pDBC->getTrackData( tack_id );
  570. if ( tTData ) {
  571. if (tTData->side == 'U') {
  572. if (tMData->side == 'F') {
  573. cSRC = 'L';
  574. } else {
  575. cSRC = 'R';
  576. };
  577. } else {
  578. if (tMData->side == 'F') {
  579. cSRC = 'R';
  580. } else {
  581. cSRC = 'L';
  582. };
  583. };
  584. free(tTData);
  585. };
  586. free(tMData);
  587. };
  588. if ( cSRC != ' ' ) {
  589. LOG("Have side info");
  590. if ( ( tData->src != 'B' ) && ( tData->src != cSRC ) ) {
  591. LOG("This is additional side!");
  592. tData->src = 'B'; // Change Status to 'B' = 'Both' (R+L)
  593. m_pDBC->updateSessionSource( tData->db_id, tData->src );
  594. // Create Event for other side
  595. // sending EVT_SRV_SESSION_STARTED - > to signalize SCADA
  596. // that another side of platform also become active
  597. SERVA_SESSION_EVT_DATA *tSData = (SERVA_SESSION_EVT_DATA*) malloc( sizeof(SERVA_SESSION_EVT_DATA));
  598. if ( tSData ) {
  599. tSData->side = tData->src;
  600. tSData->track_id = tData->track_id;
  601. EVENT_CONTAINER *tEvt = createBasicEvent( tack_id, EVT_CLASS_SYSTEM, EVT_SRV_SESSION_STARTED, NULL, NULL, tSData );
  602. if ( tEvt ) basicEventHeap::getInstance()->broadcast( tEvt );
  603. };
  604. };
  605. };
  606. };
  607. };
  608. };
  609. return tData;
  610. };
  611. bool servaSessionManager::updateSessionStat( uint64_t session_id, bool evt ){
  612. if( SESSION_DATA *tD = validateSession( session_id ) ){
  613. tD->lts = time( NULL );
  614. if ( evt ) {
  615. m_pDBC->sessionStatAddEvt( session_id );
  616. } else {
  617. m_pDBC->sessionStatAddVid( session_id );
  618. };
  619. return true;
  620. };
  621. return false;
  622. };
  623. bool servaSessionManager::validate(){
  624. uint32_t cts = time( NULL );
  625. M_SESSIONS::iterator dE;
  626. bool bMustDelete = false;
  627. m_mSessionProtector.lock();
  628. for( M_SESSIONS::iterator idx = m_mSessions.begin(); idx != m_mSessions.end(); idx++ ){
  629. if ( (*idx).second ) {
  630. #ifdef DEBUG_EXTRA
  631. char tB[128];
  632. sprintf( tB, "Validation %d %d %d", cts, (*idx).second->lts, m_nSessionTO );
  633. LOG( tB );
  634. #endif
  635. if ( !(*idx).second->skeep_validation ) {
  636. if (cts - (*idx).second->lts > m_nSessionTO ) {
  637. // Expire Session
  638. //m_pDBC->updateSessionStatus( (*idx).second->db_id, 1 );
  639. m_pDBC->sessionClose( (*idx).second->db_id );
  640. // Set Session to remove
  641. dE = idx;
  642. bMustDelete = true;
  643. //Propogate Event Session End
  644. EVENT_CONTAINER *tEvt = createBasicEvent( (*idx).second->track_id, EVT_CLASS_SYSTEM, EVT_SRV_SESSION_ENDED );
  645. if ( tEvt ) basicEventHeap::getInstance()->broadcast( tEvt );
  646. };
  647. };
  648. };
  649. };
  650. if ( bMustDelete ) {
  651. // Remove Session
  652. delete( (*dE).second->m_pDataReport );
  653. free( (*dE).second );
  654. (*dE).second = NULL;
  655. m_mSessions.erase( dE );
  656. };
  657. m_mSessionProtector.unlock();
  658. return true;
  659. };
  660. SESSION_DATA *servaSessionManager::validateSession( uint64_t sid ){
  661. for( M_SESSIONS::iterator idx = m_mSessions.begin(); idx != m_mSessions.end(); idx++ )
  662. if( (*idx).second->db_id == sid ) return (*idx).second;
  663. return NULL;
  664. };
  665. bool servaSessionManager::dropSession( uint64_t session_id ){
  666. #ifdef DEBUG
  667. LOG("Drop Session!");
  668. #endif
  669. for( M_SESSIONS::iterator idx = m_mSessions.begin(); idx != m_mSessions.end(); idx++ ){
  670. if ( (*idx).second ) if ((*idx).second->db_id == session_id) return dropSession( idx );
  671. };
  672. return false;
  673. };
  674. bool servaSessionManager::dropSession( M_SESSIONS::iterator idx ){
  675. m_mSessionProtector.lock();
  676. delete( (*idx).second->m_pDataReport );
  677. free( (*idx).second );
  678. (*idx).second = NULL;
  679. m_mSessions.erase( idx );
  680. m_mSessionProtector.unlock();
  681. return true;
  682. };
  683. bool servaSessionManager::eraseSession( uint64_t session_id ){
  684. // Erase just not important sessions
  685. // In case The session is Opened!
  686. #ifdef DEBUG
  687. LOG("Eraze Session!");
  688. #endif
  689. dropSession( session_id );
  690. SESSION_DATA * tData = m_pDBC->getSessionData( session_id );
  691. if ( tData ) {
  692. char tBase[MAX_PATH];
  693. sprintf( tBase, "%s/%d", m_pDbReportBase, tData->track_id );
  694. if( fileExist( tBase ) ){
  695. REPORT_DIRINFO tRDF;
  696. fillRDF( tData->sts, tRDF );
  697. sprintf( tData->reportVaultPath, "%s/%s/%s/%s", tBase, tRDF.YEAR, tRDF.MONTH, tRDF.DAY );
  698. #ifdef DEBUG_EXTRA
  699. LOG( tData->reportVaultPath );
  700. #endif
  701. if( fileExist( tData->reportVaultPath ) ){
  702. char tName[MAX_PATH];
  703. sprintf( tName, "%s/report.db", tData->reportVaultPath );
  704. if( fileExist( tName ) ){
  705. bool must_unlink = true;
  706. tData->m_pDataReport = new SQLite_Connector();
  707. if ( tData->m_pDataReport ) {
  708. if( tData->m_pDataReport->Connect( tName, 0, true) ) {
  709. char tB[1024];
  710. sprintf( tB, "DELETE FROM `events` WHERE `session_id`=%"PRIu64 , session_id );
  711. tData->m_pDataReport->SilentQuery( tB );
  712. sprintf( tB, "SELECT `uuid`, `ext` FROM `videos` WHERE `session_id`=%"PRIu64 , session_id );
  713. AAssoc *result = tData->m_pDataReport->Query( tB );
  714. if( result != NULL ) {
  715. if( result->size() > 0 ) {
  716. Assoc * cArr;
  717. for ( uint16_t idx = 0; idx < result->size(); idx++ ) {
  718. Assoc::iterator it;
  719. char fN[MAX_PATH], uA[65], uX[6];
  720. cArr = result->at(idx);
  721. it = cArr->find("uuid");
  722. if( it != cArr->end() ){
  723. strncpy( uA, it->second.c_str(), 64 );
  724. uA[64]=(char)0;
  725. };
  726. it = cArr->find("ext");
  727. if( it != cArr->end() ){
  728. strncpy( uX, it->second.c_str(), 5 );
  729. uX[5]=(char)0;
  730. };
  731. sprintf( fN,"%s/%s.%s", tData->reportVaultPath, uA, uX );
  732. if( fileExist( fN ) ) {
  733. #ifdef DEBUG_EXTRA
  734. LOG("Deleting ...");
  735. #endif
  736. unlink( fN );
  737. #ifdef DEBUG_EXTRA
  738. LOG("Deleted!");
  739. } else {
  740. LOG("Not Exists!");
  741. #endif
  742. };
  743. };
  744. };
  745. deleteResult( result );
  746. };
  747. sprintf( tB, "DELETE FROM `videos` WHERE `session_id`=%"PRIu64 , session_id );
  748. tData->m_pDataReport->SilentQuery( tB );
  749. //Check if still have othes Sessions
  750. sprintf( tB, "SELECT count( `id` ) as CNT FROM `events`" );
  751. result = tData->m_pDataReport->Query( tB );
  752. if( result!= NULL ) {
  753. if( result->size() > 0 ) {
  754. Assoc * cArr = result->at(0);
  755. Assoc::iterator it = cArr->find("CNT");
  756. if( it != cArr->end() ) if ( atoi( it->second.c_str() ) > 0 ) must_unlink = false;
  757. };
  758. deleteResult( result );
  759. };
  760. };
  761. delete( tData->m_pDataReport ); // close database
  762. };
  763. if ( must_unlink ){
  764. #ifdef DEBUG_EXTRA
  765. LOG("Remove Database!");
  766. LOG( tName );
  767. #endif
  768. unlink( tName );
  769. char tCMD[MAX_PATH];
  770. sprintf( tCMD, "rm -R -f %s", tData->reportVaultPath );
  771. #ifdef DEBUG_EXTRA
  772. LOG("Remove Folder!");
  773. LOG( tCMD );
  774. #endif
  775. system( tCMD );
  776. };
  777. } else {
  778. LOG_ERROR("Report Database Not Found!");
  779. };
  780. };
  781. };
  782. free( tData );
  783. //Delete Session Record
  784. m_pDBC->deleteSession( session_id );
  785. };
  786. return true;
  787. };