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