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.

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