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.

461 lines
17 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. m_pRPC = servaRemoteClient::getInstance(this);
  141. if ( m_pRPC ) {
  142. if( m_pRPC->start() ) return init();
  143. };
  144. };
  145. };
  146. return false;
  147. };
  148. void servaKernel::m_fThreadProc( void ){
  149. time_t tTimeTSeconds = 0;
  150. time_t cTimeTSeconds = 0;
  151. m_bThreadStatus = TH_WORKING;
  152. LOG("ServA Started!");
  153. while ( m_bThreadStatus < TH_ENDED ) {
  154. if( DBC_RPC_WEB_CMD * tCMD = m_pDBC->fetchWebCmd( 0 ) ) {
  155. m_pDBC->updateWebCmdStatus( tCMD->id, DBC_CMD_STARTED );
  156. switch( tCMD->cmd ){
  157. case CMD_KRNL_RELOAD:{
  158. LOG("Reload ServA Node!");
  159. m_bThreadStatus = TH_ENDED;
  160. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  161. break;
  162. };
  163. case CMD_KRNL_RESTART:{
  164. LOG("Restart ServA Node!");
  165. m_bThreadStatus = TH_ENDED;
  166. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  167. break;
  168. };
  169. case CMD_KRNL_FACTORY_RESET:{
  170. LOG("Restart ServA -> Factory Reset!");
  171. m_bThreadStatus = TH_ENDED;
  172. setExitCause( EXIT_CODE_FACTORY_RESET );
  173. break;
  174. };
  175. case CMD_NODES_RELOAD:{
  176. //if( m_pNodeFactory != NULL ) {
  177. // m_pNodeFactory->reloadNodes();
  178. //};
  179. break;
  180. };
  181. case CMD_NODE_RELOAD:{
  182. uint32_t tNodeID = atoi( tCMD->param );
  183. //if( m_pNodeFactory != NULL ) {
  184. // m_pNodeFactory->reloadNode( tNodeID );
  185. //};
  186. break;
  187. };
  188. case CMD_KRNL_PNP:{
  189. //if( m_pHardwareManager!= NULL ) {
  190. // LOG_ERROR( "Restart and Detect HardWare!" );
  191. // // Must be shutdowned all service that uses hardware prior to detection!
  192. // m_bThreadStatus = TH_ENDED;
  193. // setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  194. //};
  195. break;
  196. };
  197. case CMD_KRNL_UPDATE_IDENT:{
  198. //LOG("Restart Mojo -> Update Ident!");
  199. //sprintf( m_sIdentity, tCMD->PARAM );
  200. //config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
  201. //config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  202. //config_setting_set_string( pSetting, tCMD->PARAM );
  203. //basicSettingsProvider::getInstance()->compromise();
  204. //basicSettingsProvider::getInstance()->validate();
  205. //m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
  206. m_bThreadStatus = TH_ENDED;
  207. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  208. break;
  209. };
  210. case CMD_KRNL_REPLACE_DATABASE:{
  211. LOG("Restart Mojo -> Replace Database!");
  212. //CSTR tNAME = m_pDBC->getDataParam( 0 );
  213. //CSTR tUSER = m_pDBC->getDataParam( 1 );
  214. //CSTR tGROUP = m_pDBC->getDataParam( 2 );
  215. //if ( tNAME ) {
  216. // char tB[256];
  217. // sprintf( tB,"./data/%s.db", tCMD->PARAM );
  218. // if ( fileExist( tB )) {
  219. // LOG("Restart Mojo -> Close Database!");
  220. //Closing Database
  221. // delete( m_pDBC );
  222. // m_pDBC = NULL;
  223. // LOG("Restart Mojo -> Delete Database!");
  224. // //unlink( tNAME );
  225. // LOG("Restart Mojo -> Copy Database!");
  226. // fileCopy( tB, tNAME );
  227. // LOG("Restart Mojo -> Chown Database!");
  228. // ch_chown( tNAME, tUSER, tGROUP );
  229. // LOG("Restart Mojo -> Delete Old Database!");
  230. // unlink( tB );
  231. // m_bThreadStatus = TH_ENDED;
  232. // LOG("Restart Mojo -> Exit!");
  233. // setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
  234. // m_bThreadStatus = TH_ENDED;
  235. // } else {
  236. // LOG_ERROR("New Database Does not Exists!");
  237. // };
  238. //free( tNAME );
  239. //free( tUSER );
  240. //free( tGROUP );
  241. //};
  242. break;
  243. };
  244. case CMD_KRNL_SYNC_TIME:{
  245. uint32_t uts = atoi( tCMD->param );
  246. if( uts ){
  247. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  248. time_t ts = (time_t)uts;
  249. if( stime( &ts ) == 0 ){
  250. // Restert Server;
  251. m_bThreadStatus = TH_ENDED;
  252. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  253. };
  254. #else
  255. timespec ts;
  256. ts.tv_sec = (time_t)uts;
  257. ts.tv_nsec = 0;
  258. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  259. // Restert Server;
  260. m_bThreadStatus = TH_ENDED;
  261. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  262. };
  263. #endif
  264. };
  265. break;
  266. };
  267. case CMD_KRNL_SYSTEM_TASK:{
  268. break;
  269. };
  270. case CMD_KRNL_PURGE_SESSION:{
  271. if ( tCMD->aux_id ) {
  272. if (m_pSM) m_pSM->eraseSession( tCMD->aux_id );
  273. };
  274. break;
  275. };
  276. case CMD_KRNL_SET_SESSION_HZL:{
  277. uint8_t tLVL = atoi( tCMD->param );
  278. if ( tCMD->aux_id ) {
  279. LOG("Life Session Hazard Level Updated");
  280. /* Actual DataBase chenge done in WebApp Here we just making internal Event */
  281. SESSION_RATE_DATA *tData = (SESSION_RATE_DATA *) malloc( sizeof( SESSION_RATE_DATA ) );
  282. if ( tData ) {
  283. tData->level = tLVL;
  284. tData->track_id = tCMD->aux_id;
  285. EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_SESSION_RATED, NULL, NULL, tData );
  286. if ( tEvt ) {
  287. basicEventHeap::getInstance()->broadcast( tEvt );
  288. } else {
  289. free( tData );
  290. };
  291. };
  292. };
  293. break;
  294. };
  295. case CMD_KRNL_TEST_MODBUS:{
  296. MODBUS_TEST_DATA *tData = (MODBUS_TEST_DATA *) malloc( sizeof( MODBUS_TEST_DATA ) );
  297. if ( tData ) {
  298. tData->platform_id = tCMD->aux_id;
  299. tData->c_OutBits = atoi( tCMD->param );
  300. EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_TEST_MODBUS, NULL, NULL, tData );
  301. if ( tEvt ) {
  302. basicEventHeap::getInstance()->broadcast( tEvt );
  303. } else {
  304. free( tData );
  305. };
  306. };
  307. break;
  308. };
  309. };
  310. m_pDBC->updateWebCmdStatus( tCMD->id, DBC_CMD_READY_TO_CLEAN );
  311. free( tCMD );
  312. };
  313. cTimeTSeconds = time( NULL );
  314. if( cTimeTSeconds != tTimeTSeconds ){ //Each Second
  315. tTimeTSeconds = cTimeTSeconds;
  316. #ifdef LINUX_KERNEL_WATCHDOG
  317. if( m_iWatchdogFd > -1 ) {
  318. char tB[64];
  319. sprintf( tB, "2000\n" );
  320. //ToDo: Make Error Handling here!
  321. write( m_iWatchdogFd, &tB, strlen( tB ) );
  322. };
  323. #endif
  324. m_pDBC->cleanWebCmd();
  325. updateStatus();
  326. if ( m_vEncodersPool.size() ) {
  327. for ( V_MEDIA_ENCODERS::iterator idx = m_vEncodersPool.begin(); idx < m_vEncodersPool.end(); ) {
  328. if ( (*idx)->getStatus() == ENCODER_FINISHED ) {
  329. handleEncodedVideo( *idx );
  330. m_vEncodersPool.erase( idx );
  331. } else idx++;
  332. };
  333. };
  334. if ( m_sServaConfig.encoderLimit ) {
  335. if (m_vEncodersPool.size() < m_sServaConfig.encoderLimit) {
  336. if (uint64_t new_id = m_pDBC->getRecentMediaId()) {
  337. LOG("New Encoder!");
  338. if ( processVideoFile( new_id ) ) {
  339. updateEncodeStatus( new_id, MEDIA_ENCODE_PROGRESS, 0 );
  340. } else {
  341. LOG_ERROR("Cannot Encode Queued Video -> Removing Task!");
  342. m_pDBC->removeRemoteMediaInfo( new_id );
  343. };
  344. };
  345. };
  346. } else LOG_ERROR("Video Encoders Disabled??");
  347. if ( m_pSM ) m_pSM->validate();
  348. if ( m_pLifeMonitor ) m_pLifeMonitor->validate();
  349. if ( m_pModBus ) ((servaModBusManager *)m_pModBus)->validate();
  350. //m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  351. };
  352. SLEEP(SLEEP_KERNEL_INTERVAL);
  353. };
  354. };
  355. void servaKernel::updateEncodeStatus( uint64_t dbid, uint8_t status, uint8_t perc ){
  356. m_pDBC->updateEncodeStatus( dbid, status, perc );
  357. };
  358. bool servaKernel::processVideoFile( uint64_t dbid ){
  359. if ( dbid == 0 ) return false;
  360. DBC_RPC_MEDIA_INFO *NFO = m_pDBC->getRemoteMediaInfo( dbid );
  361. if ( NFO ) {
  362. servaMediaEncoder *ptr = new servaMediaEncoder( dbid );
  363. if ( ptr ) {
  364. #ifdef DEBUG
  365. LOG("Encoder Constructed!");
  366. #endif
  367. char tA[MAX_PATH];
  368. char tB[MAX_PATH];
  369. //Todo -> Instead GetSessionBase -> calc Base based on TS of video
  370. CSTR BASE = m_pSM->getSessionBase( NFO->session_id );
  371. if( BASE ){
  372. sprintf( tA,"%s/%s.%s", BASE, NFO->uuid, NFO->fmt );
  373. LOG(tA);
  374. sprintf( tB,"%s/%s.mp4", BASE, NFO->uuid );
  375. LOG(tB);
  376. free(BASE);
  377. if ( ptr->create( tA, tB, NFO->fps ) ) {
  378. #ifdef DEBUG
  379. LOG("Encoder Created!");
  380. #endif
  381. m_vEncodersPool.push_back( ptr );
  382. return true;
  383. };
  384. };
  385. delete( ptr );
  386. };
  387. free( NFO );
  388. } else LOG("No Video Info!!");
  389. return false;
  390. };
  391. void servaKernel::handleEncodedVideo( servaMediaEncoder *ptr ){
  392. if ( ptr ) {
  393. m_pDBC->updateEncodeFormat( ptr->getDBID(), ptr->getDstFormat() );
  394. DBC_RPC_MEDIA_INFO_EX *tP = m_pDBC->getRemoteMediaInfoEx( ptr->getDBID() );
  395. if ( tP ) {
  396. if( m_pSM->addVideo( tP )){
  397. m_pDBC->removeRemoteMediaInfo( ptr->getDBID() );
  398. } else {
  399. LOG_ERROR("Cannot Add to session Media Info!");
  400. };
  401. LOG("Delete Extended Info");
  402. free( tP );
  403. } else {
  404. LOG_ERROR("Cannot get Extended Encoder Queue Media Info!");
  405. };
  406. LOG("Delete Encoder");
  407. delete( ptr );
  408. LOG("Done");
  409. };
  410. };
  411. bool servaKernel::dispatchEvent( EVENT_CONTAINER *Evt ){
  412. if ( Evt == NULL ) return false;
  413. /*
  414. switch ( Evt->m_eClass ) {
  415. case EVT_CLASS_VIDEO:{
  416. switch ( Evt->m_eType ) {
  417. case EVT_SRV_VIDEO_CHUNK:{
  418. SERVA_VIDEO_TRANSFER_INFO *tData = ( SERVA_VIDEO_TRANSFER_INFO * ) Evt->m_pEvtData;
  419. if ( tData ){
  420. if ( tData->status == MEDIA_STORED ) {
  421. updateEncodeStatus(tData->db_id, tData->status, tData->perc);
  422. };
  423. };
  424. return false; // Kernel is Final Destination for that Event
  425. };
  426. };
  427. break;
  428. };
  429. };*/
  430. return true;
  431. };
  432. //Todo -> Startup Cleanup Media Encoder Queue (with ability to finish unfinishing jobs -> Drop just unsucessful transfers
  433. // Make Treshold of Running Encoders to a server
  434. #undef LOG
  435. #undef LOG_ERROR