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.

480 lines
18 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
12 months ago
​
12 months ago
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Dr.Frucht Systems Ltd.
  3. // Copyright � 2000-2022. All Rights Reserved.
  4. // Version 1.0 11/19/2022
  5. // Last modification: 11/19/2022
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: mojoKernel.cpp
  9. // AUTHOR: gagua
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // LRF_Kernel.cpp
  13. //----------------------------------------------------------------------------
  14. //
  15. #include "servaKernel.h"
  16. #include "../data/json/json_tool.h"
  17. #include <strings.h>
  18. #include <sys/time.h>
  19. #include <time.h>
  20. #include <json/value.h>
  21. #include <json/writer.h>
  22. #include <json/reader.h>
  23. //#include "../common/kernelEventFilterAPF.h"
  24. //#include "../common/kernelEventFilterTrain.h"
  25. //#include "../common/kernelModuleEmbedVideo.h"
  26. #include "../common/serverMySQL_dataBinder.h"
  27. #include "servaModBusManager.h"
  28. #define LOG( X ) kernelPrototype::m_pLog->Log( "KRNL", this, X )
  29. #ifdef DEBUG
  30. #define LOG_ERROR( X ) {\
  31. char _tBfrErrMSG234[256];\
  32. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  33. kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\
  34. }
  35. #else
  36. #define LOG_ERROR( X ) kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
  37. #endif
  38. servaKernel *servaKernel::_instance = NULL;
  39. servaKernel *servaKernel::getInstance( config_setting_t *ptr ){
  40. if ( _instance == NULL ) {
  41. _instance = new servaKernel( ptr );
  42. };
  43. return _instance;
  44. };
  45. servaKernel::servaKernel( config_setting_t *ptr ):kernelPrototype( ptr ){
  46. //m_pEventHeap = basicEventHeap::getInstance();
  47. // memset( &m_sSysInfo, 0, sizeof( DBC_SYS_LOAD_INFO ));
  48. //m_pShadow = NULL;
  49. //m_pSystemAnchor = NULL;
  50. // m_bSessionStarted = false;
  51. // m_bWorkWithRemoteServer = false;
  52. // m_bKeepLocalReport = true;
  53. // m_bMediaMode = 0;
  54. m_pLifeMonitor = servaLifeMonitoring::getInstance();
  55. m_pSM = NULL;
  56. m_pRPC = NULL;//??
  57. m_pModBus = NULL;
  58. memset( &m_sServaConfig, 0, sizeof( DBC_SERVA_CONFIG ));
  59. setWM( WM_NONE );
  60. };
  61. servaKernel::~servaKernel(){
  62. WaitFinish( true );
  63. //stopSession( false );
  64. //while(1){
  65. // char tB[200];
  66. // sprintf( tB, "[%d] Seconds Till System Restart", timeout );
  67. // LOG( "BopBop!" );
  68. // SLEEP(1000);
  69. //};
  70. if ( m_pModBus ) {
  71. delete( (servaModBusManager *) m_pModBus );
  72. };
  73. #ifdef LINUX_KERNEL_WATCHDOG
  74. LOG("Disable WD!");
  75. if( m_iWatchdogFd > -1 ) {
  76. char tB[64];
  77. sprintf( tB, "-1\n" );
  78. //ToDo: Make Error Handling here!
  79. write( m_iWatchdogFd, &tB, strlen( tB ) );
  80. };
  81. LOG("WatchDog turned Off!");
  82. #endif
  83. _instance = NULL;
  84. };
  85. bool servaKernel::create(){
  86. if( m_pSettings != NULL ) {
  87. config_setting_t * pSetting;
  88. int tVal;
  89. const char * tBuf;
  90. char tBuffer[128];
  91. LOG("Getting Settings ...");
  92. /*if( !config_setting_lookup_string( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){
  93. char * tVar;
  94. char * tTS;
  95. tTS = getTStr(NULL);
  96. tVar = MD5String(tTS);
  97. free(tTS);
  98. sprintf( m_sIdentity, tVar );
  99. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  100. config_setting_set_string( pSetting, tVar );
  101. free( tVar );
  102. basicSettingsProvider::getInstance()->compromise();
  103. LOG("Server Identity not defined - generating new!");
  104. } else {
  105. sprintf( m_sIdentity, tBuf );
  106. sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf );
  107. LOG( tBuffer );
  108. };
  109. if( !config_setting_lookup_int( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, &tVal ) ){
  110. tVal = m_nInstanceID = 1;
  111. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT );
  112. config_setting_set_int( pSetting, tVal );
  113. basicSettingsProvider::getInstance()->compromise();
  114. LOG("Site ID not defined - using [1] as number!");
  115. } else {
  116. m_nInstanceID = tVal;
  117. sprintf( tBuffer, "App Instance ID Read From Settings: %d", m_nInstanceID );
  118. LOG( tBuffer );
  119. };*/
  120. basicSettingsProvider::getInstance()->validate();
  121. } else {
  122. LOG_ERROR("Failed Read Config Settings!");
  123. return false;
  124. };
  125. LOG("Register Server ... ");
  126. registerServA();
  127. m_pSM = servaSessionManager::getInstance( m_pSettings );
  128. if ( m_pSM ){
  129. cleanUpMess();
  130. if ( m_pSM->create() ) {
  131. LOG("Session Manager Created");
  132. servaModBusManager * tModBus = servaModBusManager::getInstance();
  133. if( tModBus ){
  134. LOG("Have ModBus Instance");
  135. if( tModBus->create() ){
  136. LOG("Modbus Created");
  137. m_pModBus = (void *) tModBus;
  138. } else delete( tModBus );
  139. };
  140. /* Test Composer */
  141. servaVideoComposer *tVC = new servaVideoComposer( 49 );
  142. if ( tVC ) {
  143. tVC->compose();
  144. };
  145. m_pRPC = servaRemoteClient::getInstance(this);
  146. if ( m_pRPC ) {
  147. if( m_pRPC->start() ) return init();
  148. };
  149. };
  150. };
  151. return false;
  152. };
  153. void servaKernel::m_fThreadProc( void ){
  154. time_t tTimeTSeconds = 0;
  155. time_t cTimeTSeconds = 0;
  156. m_bThreadStatus = TH_WORKING;
  157. LOG("ServA Started!");
  158. while ( m_bThreadStatus < TH_ENDED ) {
  159. if( DBC_RPC_WEB_CMD * tCMD = m_pDBC->fetchWebCmd( 0 ) ) {
  160. m_pDBC->updateWebCmdStatus( tCMD->id, DBC_CMD_STARTED );
  161. switch( tCMD->cmd ){
  162. case CMD_KRNL_RELOAD:{
  163. LOG("Reload ServA Node!");
  164. m_bThreadStatus = TH_ENDED;
  165. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  166. break;
  167. };
  168. case CMD_KRNL_RESTART:{
  169. LOG("Restart ServA Node!");
  170. m_bThreadStatus = TH_ENDED;
  171. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  172. break;
  173. };
  174. case CMD_KRNL_FACTORY_RESET:{
  175. LOG("Restart ServA -> Factory Reset!");
  176. m_bThreadStatus = TH_ENDED;
  177. setExitCause( EXIT_CODE_FACTORY_RESET );
  178. break;
  179. };
  180. case CMD_NODES_RELOAD:{
  181. //if( m_pNodeFactory != NULL ) {
  182. // m_pNodeFactory->reloadNodes();
  183. //};
  184. break;
  185. };
  186. case CMD_NODE_RELOAD:{
  187. uint32_t tNodeID = atoi( tCMD->param );
  188. //if( m_pNodeFactory != NULL ) {
  189. // m_pNodeFactory->reloadNode( tNodeID );
  190. //};
  191. break;
  192. };
  193. case CMD_KRNL_PNP:{
  194. //if( m_pHardwareManager!= NULL ) {
  195. // LOG_ERROR( "Restart and Detect HardWare!" );
  196. // // Must be shutdowned all service that uses hardware prior to detection!
  197. // m_bThreadStatus = TH_ENDED;
  198. // setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  199. //};
  200. break;
  201. };
  202. case CMD_KRNL_UPDATE_IDENT:{
  203. //LOG("Restart Mojo -> Update Ident!");
  204. //sprintf( m_sIdentity, tCMD->PARAM );
  205. //config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
  206. //config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  207. //config_setting_set_string( pSetting, tCMD->PARAM );
  208. //basicSettingsProvider::getInstance()->compromise();
  209. //basicSettingsProvider::getInstance()->validate();
  210. //m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
  211. m_bThreadStatus = TH_ENDED;
  212. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  213. break;
  214. };
  215. case CMD_KRNL_REPLACE_DATABASE:{
  216. LOG("Restart Mojo -> Replace Database!");
  217. //CSTR tNAME = m_pDBC->getDataParam( 0 );
  218. //CSTR tUSER = m_pDBC->getDataParam( 1 );
  219. //CSTR tGROUP = m_pDBC->getDataParam( 2 );
  220. //if ( tNAME ) {
  221. // char tB[256];
  222. // sprintf( tB,"./data/%s.db", tCMD->PARAM );
  223. // if ( fileExist( tB )) {
  224. // LOG("Restart Mojo -> Close Database!");
  225. //Closing Database
  226. // delete( m_pDBC );
  227. // m_pDBC = NULL;
  228. // LOG("Restart Mojo -> Delete Database!");
  229. // //unlink( tNAME );
  230. // LOG("Restart Mojo -> Copy Database!");
  231. // fileCopy( tB, tNAME );
  232. // LOG("Restart Mojo -> Chown Database!");
  233. // ch_chown( tNAME, tUSER, tGROUP );
  234. // LOG("Restart Mojo -> Delete Old Database!");
  235. // unlink( tB );
  236. // m_bThreadStatus = TH_ENDED;
  237. // LOG("Restart Mojo -> Exit!");
  238. // setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
  239. // m_bThreadStatus = TH_ENDED;
  240. // } else {
  241. // LOG_ERROR("New Database Does not Exists!");
  242. // };
  243. //free( tNAME );
  244. //free( tUSER );
  245. //free( tGROUP );
  246. //};
  247. break;
  248. };
  249. case CMD_KRNL_SYNC_TIME:{
  250. uint32_t uts = atoi( tCMD->param );
  251. if( uts ){
  252. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  253. time_t ts = (time_t)uts;
  254. if( stime( &ts ) == 0 ){
  255. // Restert Server;
  256. m_bThreadStatus = TH_ENDED;
  257. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  258. };
  259. #else
  260. timespec ts;
  261. ts.tv_sec = (time_t)uts;
  262. ts.tv_nsec = 0;
  263. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  264. // Restert Server;
  265. m_bThreadStatus = TH_ENDED;
  266. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  267. };
  268. #endif
  269. };
  270. break;
  271. };
  272. case CMD_KRNL_SYSTEM_TASK:{
  273. break;
  274. };
  275. case CMD_KRNL_PURGE_SESSION:{
  276. if ( tCMD->aux_id ) {
  277. if (m_pSM) m_pSM->eraseSession( tCMD->aux_id );
  278. };
  279. break;
  280. };
  281. case CMD_KRNL_SET_SESSION_HZL:{
  282. uint8_t tLVL = atoi( tCMD->param );
  283. if ( tCMD->aux_id ) {
  284. LOG("Life Session Hazard Level Updated");
  285. /* Actual DataBase chenge done in WebApp Here we just making internal Event */
  286. SESSION_RATE_DATA *tData = (SESSION_RATE_DATA *) malloc( sizeof( SESSION_RATE_DATA ) );
  287. if ( tData ) {
  288. tData->level = tLVL;
  289. tData->track_id = tCMD->aux_id;
  290. EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_SESSION_RATED, NULL, NULL, tData );
  291. if ( tEvt ) {
  292. basicEventHeap::getInstance()->broadcast( tEvt );
  293. } else {
  294. free( tData );
  295. };
  296. };
  297. };
  298. break;
  299. };
  300. case CMD_KRNL_TEST_MODBUS:{
  301. MODBUS_TEST_DATA *tData = (MODBUS_TEST_DATA *) malloc( sizeof( MODBUS_TEST_DATA ) );
  302. if ( tData ) {
  303. tData->platform_id = tCMD->aux_id;
  304. tData->c_OutBits = atoi( tCMD->param );
  305. EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_TEST_MODBUS, NULL, NULL, tData );
  306. if ( tEvt ) {
  307. basicEventHeap::getInstance()->broadcast( tEvt );
  308. } else {
  309. free( tData );
  310. };
  311. };
  312. break;
  313. };
  314. };
  315. m_pDBC->updateWebCmdStatus( tCMD->id, DBC_CMD_READY_TO_CLEAN );
  316. free( tCMD );
  317. };
  318. cTimeTSeconds = time( NULL );
  319. if( cTimeTSeconds != tTimeTSeconds ){ //Each Second
  320. tTimeTSeconds = cTimeTSeconds;
  321. #ifdef LINUX_KERNEL_WATCHDOG
  322. if( m_iWatchdogFd > -1 ) {
  323. char tB[64];
  324. sprintf( tB, "2000\n" );
  325. //ToDo: Make Error Handling here!
  326. write( m_iWatchdogFd, &tB, strlen( tB ) );
  327. };
  328. #endif
  329. m_pDBC->cleanWebCmd();
  330. updateStatus();
  331. if ( m_vEncodersPool.size() ) {
  332. for ( V_MEDIA_ENCODERS::iterator idx = m_vEncodersPool.begin(); idx < m_vEncodersPool.end(); ) {
  333. if ( (*idx)->getStatus() == ENCODER_FINISHED ) {
  334. handleEncodedVideo( *idx );
  335. m_vEncodersPool.erase( idx );
  336. } else idx++;
  337. };
  338. };
  339. if ( m_sServaConfig.encoderLimit ) {
  340. if (m_vEncodersPool.size() < m_sServaConfig.encoderLimit) {
  341. if (uint64_t new_id = m_pDBC->getRecentMediaId()) {
  342. LOG("New Encoder!");
  343. if ( processVideoFile( new_id ) ) {
  344. updateEncodeStatus( new_id, MEDIA_ENCODE_PROGRESS, 0 );
  345. } else {
  346. LOG_ERROR("Cannot Encode Queued Video -> Removing Task!");
  347. m_pDBC->removeRemoteMediaInfo( new_id );
  348. };
  349. };
  350. };
  351. } else LOG_ERROR("Video Encoders Disabled??");
  352. if ( m_pSM ) m_pSM->validate();
  353. if ( m_pLifeMonitor ) m_pLifeMonitor->validate();
  354. if ( m_pModBus ) ((servaModBusManager *)m_pModBus)->validate();
  355. //m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  356. if ( m_pVideoComposer == NULL ) {
  357. uint64_t tSID = m_pDBC->getSessionWithUnmergedVideo( m_sServaConfig.composerProceedDelay );
  358. if ( tSID ) {
  359. m_pVideoComposer = new servaVideoComposer( tSID );
  360. if ( m_pVideoComposer ) {
  361. if( !m_pVideoComposer->compose() ){
  362. delete( m_pVideoComposer );
  363. m_pVideoComposer = NULL;
  364. };
  365. };
  366. };
  367. };
  368. };
  369. SLEEP(SLEEP_KERNEL_INTERVAL);
  370. };
  371. };
  372. void servaKernel::updateEncodeStatus( uint64_t dbid, uint8_t status, uint8_t perc ){
  373. m_pDBC->updateEncodeStatus( dbid, status, perc );
  374. };
  375. bool servaKernel::processVideoFile( uint64_t dbid ){
  376. if ( dbid == 0 ) return false;
  377. DBC_RPC_MEDIA_INFO *NFO = m_pDBC->getRemoteMediaInfo( dbid );
  378. if ( NFO ) {
  379. servaMediaEncoder *ptr = new servaMediaEncoder( dbid );
  380. if ( ptr ) {
  381. #ifdef DEBUG
  382. LOG("Encoder Constructed!");
  383. #endif
  384. char tA[MAX_PATH];
  385. char tB[MAX_PATH];
  386. //Todo -> Instead GetSessionBase -> calc Base based on TS of video
  387. CSTR BASE = m_pSM->getSessionBase( NFO->session_id );
  388. if( BASE ){
  389. sprintf( tA,"%s/%s.%s", BASE, NFO->uuid, NFO->fmt );
  390. LOG(tA);
  391. sprintf( tB,"%s/%s.mp4", BASE, NFO->uuid );
  392. LOG(tB);
  393. free(BASE);
  394. if ( ptr->create( tA, tB, NFO->fps ) ) {
  395. #ifdef DEBUG
  396. LOG("Encoder Created!");
  397. #endif
  398. m_vEncodersPool.push_back( ptr );
  399. return true;
  400. };
  401. };
  402. delete( ptr );
  403. };
  404. free( NFO );
  405. } else LOG("No Video Info!!");
  406. return false;
  407. };
  408. void servaKernel::handleEncodedVideo( servaMediaEncoder *ptr ){
  409. if ( ptr ) {
  410. m_pDBC->updateEncodeFormat( ptr->getDBID(), ptr->getDstFormat() );
  411. DBC_RPC_MEDIA_INFO_EX *tP = m_pDBC->getRemoteMediaInfoEx( ptr->getDBID() );
  412. if ( tP ) {
  413. if( m_pSM->addVideo( tP )){
  414. m_pDBC->removeRemoteMediaInfo( ptr->getDBID() );
  415. } else {
  416. LOG_ERROR("Cannot Add to session Media Info!");
  417. };
  418. LOG("Delete Extended Info");
  419. free( tP );
  420. } else {
  421. LOG_ERROR("Cannot get Extended Encoder Queue Media Info!");
  422. };
  423. LOG("Delete Encoder");
  424. delete( ptr );
  425. LOG("Done");
  426. };
  427. };
  428. bool servaKernel::dispatchEvent( EVENT_CONTAINER *Evt ){
  429. if ( Evt == NULL ) return false;
  430. /*
  431. switch ( Evt->m_eClass ) {
  432. case EVT_CLASS_VIDEO:{
  433. switch ( Evt->m_eType ) {
  434. case EVT_SRV_VIDEO_CHUNK:{
  435. SERVA_VIDEO_TRANSFER_INFO *tData = ( SERVA_VIDEO_TRANSFER_INFO * ) Evt->m_pEvtData;
  436. if ( tData ){
  437. if ( tData->status == MEDIA_STORED ) {
  438. updateEncodeStatus(tData->db_id, tData->status, tData->perc);
  439. };
  440. };
  441. return false; // Kernel is Final Destination for that Event
  442. };
  443. };
  444. break;
  445. };
  446. };*/
  447. return true;
  448. };
  449. //Todo -> Startup Cleanup Media Encoder Queue (with ability to finish unfinishing jobs -> Drop just unsucessful transfers
  450. // Make Treshold of Running Encoders to a server
  451. #undef LOG
  452. #undef LOG_ERROR