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.

424 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
2 years 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. #define LOG( X ) kernelPrototype::m_pLog->Log( "KRNL", this, X )
  28. #ifdef DEBUG
  29. #define LOG_ERROR( X ) {\
  30. char _tBfrErrMSG234[256];\
  31. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  32. kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\
  33. }
  34. #else
  35. #define LOG_ERROR( X ) kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
  36. #endif
  37. servaKernel *servaKernel::_instance = NULL;
  38. servaKernel *servaKernel::getInstance( config_setting_t *ptr ){
  39. if ( _instance == NULL ) {
  40. _instance = new servaKernel( ptr );
  41. };
  42. return _instance;
  43. };
  44. servaKernel::servaKernel( config_setting_t *ptr ):kernelPrototype( ptr ){
  45. //m_pEventHeap = basicEventHeap::getInstance();
  46. // memset( &m_sSysInfo, 0, sizeof( DBC_SYS_LOAD_INFO ));
  47. //m_pShadow = NULL;
  48. //m_pSystemAnchor = NULL;
  49. // m_bSessionStarted = false;
  50. // m_bWorkWithRemoteServer = false;
  51. // m_bKeepLocalReport = true;
  52. // m_bMediaMode = 0;
  53. m_pLifeMonitor = servaLifeMonitoring::getInstance();
  54. m_pSM = NULL;
  55. m_pRPC = NULL;//??
  56. setWM( WM_NONE );
  57. };
  58. servaKernel::~servaKernel(){
  59. WaitFinish( true );
  60. //stopSession( false );
  61. //while(1){
  62. // char tB[200];
  63. // sprintf( tB, "[%d] Seconds Till System Restart", timeout );
  64. // LOG( "BopBop!" );
  65. // SLEEP(1000);
  66. //};
  67. #ifdef LINUX_KERNEL_WATCHDOG
  68. LOG("Disable WD!");
  69. if( m_iWatchdogFd > -1 ) {
  70. char tB[64];
  71. sprintf( tB, "-1\n" );
  72. //ToDo: Make Error Handling here!
  73. write( m_iWatchdogFd, &tB, strlen( tB ) );
  74. };
  75. LOG("WatchDog turned Off!");
  76. #endif
  77. _instance = NULL;
  78. };
  79. bool servaKernel::create(){
  80. if( m_pSettings != NULL ) {
  81. config_setting_t * pSetting;
  82. int tVal;
  83. const char * tBuf;
  84. char tBuffer[128];
  85. LOG("Getting Settings ...");
  86. /*if( !config_setting_lookup_string( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){
  87. char * tVar;
  88. char * tTS;
  89. tTS = getTStr(NULL);
  90. tVar = MD5String(tTS);
  91. free(tTS);
  92. sprintf( m_sIdentity, tVar );
  93. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  94. config_setting_set_string( pSetting, tVar );
  95. free( tVar );
  96. basicSettingsProvider::getInstance()->compromise();
  97. LOG("Server Identity not defined - generating new!");
  98. } else {
  99. sprintf( m_sIdentity, tBuf );
  100. sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf );
  101. LOG( tBuffer );
  102. };
  103. if( !config_setting_lookup_int( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, &tVal ) ){
  104. tVal = m_nInstanceID = 1;
  105. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT );
  106. config_setting_set_int( pSetting, tVal );
  107. basicSettingsProvider::getInstance()->compromise();
  108. LOG("Site ID not defined - using [1] as number!");
  109. } else {
  110. m_nInstanceID = tVal;
  111. sprintf( tBuffer, "App Instance ID Read From Settings: %d", m_nInstanceID );
  112. LOG( tBuffer );
  113. };*/
  114. basicSettingsProvider::getInstance()->validate();
  115. } else {
  116. LOG_ERROR("Failed Read Config Settings!");
  117. return false;
  118. };
  119. LOG("Register Server ... ");
  120. registerServA();
  121. char tBuffer[128];
  122. //sprintf( tBuffer,"Server Registered as [%d] nodeID[%d]",m_nServerID, m_nServerNodeID );
  123. //LOG( tBuffer );
  124. //DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData( 1 );
  125. // if( tSiteData ) {
  126. // C3D_WGS84_LOC t;
  127. // t.lat = tSiteData->lat;
  128. // t.lon = tSiteData->lon;
  129. // t.alt = tSiteData->alt;
  130. //m_pSystemAnchor = systemAnchor::getInstance( tSiteData->SH, &t, tSiteData->local_binding );
  131. //memcpy( &m_sSiteData, tSiteData, sizeof( DBC_SITE_DATA ));
  132. //free( tSiteData );
  133. // } else {
  134. // LOG_ERROR("Error Loading Site DATA!");
  135. // C3D_WGS84_LOC t;
  136. // t.lat = 0;
  137. // t.lon = 0;
  138. // t.alt = 0;
  139. //memset( &m_sSiteData, 0, sizeof( DBC_SITE_DATA ));
  140. //m_pSystemAnchor = systemAnchor::getInstance( false, &t, true );
  141. //};
  142. //Check date/Time if correct!
  143. // On some systems the System Date may be in past cause last session was interrupted by power loose
  144. /*
  145. uint32_t tTS = m_pDBC->getLastEventTS();
  146. if ( tTS ) {
  147. // Have last TS
  148. struct timespec tS = { 0 };
  149. if( clock_gettime( CLOCK_REALTIME, &tS ) == 0 ){
  150. //time_t cTS = time(NULL);
  151. if ( tTS > tS.tv_sec ) {
  152. // System Time is in the Past
  153. LOG("Correcting System Time!");
  154. tS.tv_sec = tTS;
  155. tS.tv_nsec = tTS * 1000000;
  156. if (clock_settime(CLOCK_REALTIME, &tS) != 0) LOG_ERROR("Time Correction Failed!");
  157. };
  158. };
  159. }
  160. #ifdef DEBUG
  161. else LOG("No Last TS fetched from DB!")
  162. #endif
  163. ;
  164. */
  165. //m_bDebugMode = m_pDBC->getAppDebugMode();
  166. //m_bSessionStarted = m_pDBC->getEventSessionStatus( m_nServerNodeID );
  167. //stopSession( false );
  168. m_pSM = servaSessionManager::getInstance( m_pSettings );
  169. if ( m_pSM ){
  170. if ( m_pSM->create() ) {
  171. m_pRPC = servaRemoteClient::getInstance(this);
  172. if ( m_pRPC ) {
  173. if( m_pRPC->start() ) return init();
  174. };
  175. };
  176. };
  177. return false;
  178. };
  179. void servaKernel::m_fThreadProc( void ){
  180. time_t tTimeTSeconds = 0;
  181. time_t cTimeTSeconds = 0;
  182. m_bThreadStatus = TH_WORKING;
  183. LOG("ServA Started!");
  184. while ( m_bThreadStatus < TH_ENDED ) {
  185. cTimeTSeconds = time( NULL );
  186. //Each Second
  187. if( cTimeTSeconds != tTimeTSeconds ){
  188. tTimeTSeconds = cTimeTSeconds;
  189. #ifdef LINUX_KERNEL_WATCHDOG
  190. if( m_iWatchdogFd > -1 ) {
  191. char tB[64];
  192. sprintf( tB, "2000\n" );
  193. //ToDo: Make Error Handling here!
  194. write( m_iWatchdogFd, &tB, strlen( tB ) );
  195. };
  196. #endif
  197. updateStatus();
  198. if ( m_vEncodersPool.size() ) {
  199. for ( V_MEDIA_ENCODERS::iterator idx = m_vEncodersPool.begin(); idx < m_vEncodersPool.end(); idx++ ) {
  200. if ( (*idx)->getStatus() == ENCODER_FINISHED ) {
  201. handleEncodedVideo( *idx );
  202. m_vEncodersPool.erase( idx );
  203. };
  204. };
  205. };
  206. if ( uint64_t new_id = m_pDBC->getRecentMediaId() ) {
  207. if ( processVideoFile( new_id ) ) updateEncodeStatus( new_id, MEDIA_ENCODE_PROGRESS, 0 );
  208. };
  209. if ( m_pSM ) m_pSM->validate();
  210. //m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  211. DBC_RPC_WEB_CMD * tCMD = m_pDBC->fetchWebCmd( 0 ); //Kernel
  212. if( tCMD != NULL ) {
  213. m_pDBC->updateWebCmdStatus( tCMD->id, DBC_CMD_STARTED );
  214. switch( tCMD->cmd ){
  215. case CMD_KRNL_RELOAD:{
  216. LOG("Reload Serva Node!");
  217. m_bThreadStatus = TH_ENDED;
  218. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  219. break;
  220. };
  221. case CMD_KRNL_RESTART:{
  222. LOG("Restart Serva Node!");
  223. m_bThreadStatus = TH_ENDED;
  224. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  225. break;
  226. };
  227. case CMD_KRNL_FACTORY_RESET:{
  228. LOG("Restart Serva -> Factory Reset!");
  229. m_bThreadStatus = TH_ENDED;
  230. setExitCause( EXIT_CODE_FACTORY_RESET );
  231. break;
  232. };
  233. case CMD_NODES_RELOAD:{
  234. //if( m_pNodeFactory != NULL ) {
  235. // m_pNodeFactory->reloadNodes();
  236. //};
  237. break;
  238. };
  239. case CMD_NODE_RELOAD:{
  240. uint32_t tNodeID = atoi( tCMD->param );
  241. //if( m_pNodeFactory != NULL ) {
  242. // m_pNodeFactory->reloadNode( tNodeID );
  243. //};
  244. break;
  245. };
  246. case CMD_KRNL_PNP:{
  247. //if( m_pHardwareManager!= NULL ) {
  248. // LOG_ERROR( "Restart and Detect HardWare!" );
  249. // // Must be shutdowned all service that uses hardware prior to detection!
  250. // m_bThreadStatus = TH_ENDED;
  251. // setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  252. //};
  253. break;
  254. };
  255. case CMD_KRNL_UPDATE_IDENT:{
  256. //LOG("Restart Mojo -> Update Ident!");
  257. //sprintf( m_sIdentity, tCMD->PARAM );
  258. //config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
  259. //config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  260. //config_setting_set_string( pSetting, tCMD->PARAM );
  261. //basicSettingsProvider::getInstance()->compromise();
  262. //basicSettingsProvider::getInstance()->validate();
  263. //m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
  264. m_bThreadStatus = TH_ENDED;
  265. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  266. break;
  267. };
  268. case CMD_KRNL_REPLACE_DATABASE:{
  269. LOG("Restart Mojo -> Replace Database!");
  270. //CSTR tNAME = m_pDBC->getDataParam( 0 );
  271. //CSTR tUSER = m_pDBC->getDataParam( 1 );
  272. //CSTR tGROUP = m_pDBC->getDataParam( 2 );
  273. //if ( tNAME ) {
  274. // char tB[256];
  275. // sprintf( tB,"./data/%s.db", tCMD->PARAM );
  276. // if ( fileExist( tB )) {
  277. // LOG("Restart Mojo -> Close Database!");
  278. //Closing Database
  279. // delete( m_pDBC );
  280. // m_pDBC = NULL;
  281. // LOG("Restart Mojo -> Delete Database!");
  282. // //unlink( tNAME );
  283. // LOG("Restart Mojo -> Copy Database!");
  284. // fileCopy( tB, tNAME );
  285. // LOG("Restart Mojo -> Chown Database!");
  286. // ch_chown( tNAME, tUSER, tGROUP );
  287. // LOG("Restart Mojo -> Delete Old Database!");
  288. // unlink( tB );
  289. // m_bThreadStatus = TH_ENDED;
  290. // LOG("Restart Mojo -> Exit!");
  291. // setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
  292. // m_bThreadStatus = TH_ENDED;
  293. // } else {
  294. // LOG_ERROR("New Database Does not Exists!");
  295. // };
  296. //free( tNAME );
  297. //free( tUSER );
  298. //free( tGROUP );
  299. //};
  300. break;
  301. };
  302. case CMD_KRNL_SYNC_TIME:{
  303. uint32_t uts = atoi( tCMD->param );
  304. if( uts ){
  305. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  306. time_t ts = (time_t)uts;
  307. if( stime( &ts ) == 0 ){
  308. // Restert Server;
  309. m_bThreadStatus = TH_ENDED;
  310. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  311. };
  312. #else
  313. timespec ts;
  314. ts.tv_sec = (time_t)uts;
  315. ts.tv_nsec = 0;
  316. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  317. // Restert Server;
  318. m_bThreadStatus = TH_ENDED;
  319. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  320. };
  321. #endif
  322. };
  323. break;
  324. };
  325. case CMD_KRNL_SYSTEM_TASK:{
  326. break;
  327. };
  328. case NODE_FORCE_FINISH_ALARM:{
  329. //stopSession();
  330. break;
  331. };
  332. };
  333. m_pDBC->updateWebCmdStatus( tCMD->id, DBC_CMD_READY_TO_CLEAN );
  334. free( tCMD );
  335. };
  336. m_pDBC->cleanWebCmd();
  337. };
  338. SLEEP(SLEEP_KERNEL_INTERVAL);
  339. };
  340. };
  341. void servaKernel::updateEncodeStatus( uint64_t dbid, uint8_t status, uint8_t perc ){
  342. m_pDBC->updateEncodeStatus( dbid, status, perc );
  343. };
  344. bool servaKernel::processVideoFile( uint64_t dbid ){
  345. if ( dbid == 0 ) return false;
  346. DBC_RPC_MEDIA_INFO *NFO = m_pDBC->getRemoteMediaInfo( dbid );
  347. if ( NFO ) {
  348. servaMediaEncoder *ptr = new servaMediaEncoder( dbid );
  349. if ( ptr ) {
  350. #ifdef DEBUG_EXTRA
  351. LOG("Encoder Constructed!");
  352. #endif
  353. char tA[MAX_PATH];
  354. char tB[MAX_PATH];
  355. //Todo -> Instead GetSessionBase -> calc Base based on TS of video
  356. sprintf( tA,"%s/%s.%s", m_pSM->getSessionBase( NFO->session_id ), NFO->uuid, NFO->fmt );
  357. sprintf( tB,"%s/%s.mp4", m_pSM->getSessionBase( NFO->session_id ), NFO->uuid );
  358. if ( ptr->create( tA, tB, NFO->fps ) ) {
  359. #ifdef DEBUG
  360. LOG("Encoder Created!");
  361. #endif
  362. m_vEncodersPool.push_back( ptr );
  363. return true;
  364. };
  365. delete( ptr );
  366. };
  367. free( NFO );
  368. };
  369. return false;
  370. };
  371. void servaKernel::handleEncodedVideo( servaMediaEncoder *ptr ){
  372. if ( ptr ) {
  373. m_pDBC->updateEncodeFormat( ptr->getDBID(), ptr->getDstFormat() );
  374. DBC_RPC_MEDIA_INFO_EX *tP = m_pDBC->getRemoteMediaInfoEx( ptr->getDBID() );
  375. if ( tP ) {
  376. if( m_pSM->addVideo( tP )){
  377. m_pDBC->removeRemoteMediaInfo( ptr->getDBID() );
  378. } else {
  379. LOG_ERROR("Cannot Add to session Media Info!");
  380. };
  381. LOG("Delete Extended Info");
  382. free( tP );
  383. } else {
  384. LOG_ERROR("Cannot get Extended Encoder Queue Media Info!");
  385. };
  386. LOG("Delete Encoder");
  387. delete( ptr );
  388. LOG("Done");
  389. };
  390. };
  391. //Todo -> Startup Cleanup Media Encoder Queue (with ability to finish unfinishing jobs -> Drop just unsucessful transfers
  392. // Make Treshold of Running Encoders to a server
  393. #undef LOG
  394. #undef LOG_ERROR