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.

380 lines
14 KiB

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