|
|
//----------------------------------------------------------------------------
// Dr.Frucht Systems Ltd.
// Copyright � 2000-2022. All Rights Reserved.
// Version 1.0 11/19/2022
// Last modification: 11/19/2022
// PROJECT:
// SUBSYSTEM:
// FILE: mojoKernel.cpp
// AUTHOR: gagua
// OVERVIEW
// ~~~~~~~~
// LRF_Kernel.cpp
//----------------------------------------------------------------------------
//
#include "servaKernel.h"
#include "../data/json/json_tool.h"
#include <strings.h>
#include <sys/time.h>
#include <time.h>
#include <json/value.h>
#include <json/writer.h>
#include <json/reader.h>
#include "../common/kernelEventFilterAPF.h"
#include "../common/kernelEventFilterTrain.h"
#include "../common/kernelModuleEmbedVideo.h"
#include "../common/serverMySQL_dataBinder.h"
#define LOG( X ) kernelPrototype::m_pLog->Log( "KRNL", this, X )
#ifdef DEBUG
#define LOG_ERROR( X ) {\
char _tBfrErrMSG234[256];\ sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\ }#else
#define LOG_ERROR( X ) kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
#endif
servaKernel *servaKernel::_instance = NULL;
servaKernel *servaKernel::getInstance( config_setting_t *ptr ){ if ( _instance == NULL ) { _instance = new servaKernel( ptr ); }; return _instance;};
servaKernel::servaKernel( config_setting_t *ptr ):kernelPrototype( ptr ){ //m_pEventHeap = basicEventHeap::getInstance();
// memset( &m_sSysInfo, 0, sizeof( DBC_SYS_LOAD_INFO ));
//m_pShadow = NULL;
//m_pSystemAnchor = NULL;
// m_bSessionStarted = false;
// m_bWorkWithRemoteServer = false;
// m_bKeepLocalReport = true;
// m_bMediaMode = 0;
m_pLifeMonitor = servaLifeMonitoring::getInstance(); m_pRPC = NULL;//??
setWM( WM_NONE );};
servaKernel::~servaKernel(){ WaitFinish( true );
//stopSession( false );
//while(1){
// char tB[200];
// sprintf( tB, "[%d] Seconds Till System Restart", timeout );
// LOG( "BopBop!" );
// SLEEP(1000);
//};
#ifdef LINUX_KERNEL_WATCHDOG
LOG("Disable WD!"); if( m_iWatchdogFd > -1 ) { char tB[64]; sprintf( tB, "-1\n" ); //ToDo: Make Error Handling here!
write( m_iWatchdogFd, &tB, strlen( tB ) ); }; LOG("WatchDog turned Off!");#endif
_instance = NULL;};
bool servaKernel::create(){ if( m_pSettings != NULL ) { config_setting_t * pSetting; int tVal; const char * tBuf; char tBuffer[128]; LOG("Getting Settings ..."); /*if( !config_setting_lookup_string( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){
char * tVar; char * tTS; tTS = getTStr(NULL); tVar = MD5String(tTS); free(tTS); sprintf( m_sIdentity, tVar ); pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING ); config_setting_set_string( pSetting, tVar ); free( tVar ); basicSettingsProvider::getInstance()->compromise(); LOG("Server Identity not defined - generating new!"); } else { sprintf( m_sIdentity, tBuf ); sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf ); LOG( tBuffer ); }; if( !config_setting_lookup_int( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, &tVal ) ){ tVal = m_nInstanceID = 1; pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT ); config_setting_set_int( pSetting, tVal ); basicSettingsProvider::getInstance()->compromise(); LOG("Site ID not defined - using [1] as number!"); } else { m_nInstanceID = tVal; sprintf( tBuffer, "App Instance ID Read From Settings: %d", m_nInstanceID ); LOG( tBuffer ); };*/ basicSettingsProvider::getInstance()->validate(); } else { LOG_ERROR("Failed Read Config Settings!"); return false; };
LOG("Register Server ... "); registerServA(); char tBuffer[128]; //sprintf( tBuffer,"Server Registered as [%d] nodeID[%d]",m_nServerID, m_nServerNodeID );
//LOG( tBuffer );
//DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData( 1 );
// if( tSiteData ) {
// C3D_WGS84_LOC t;
// t.lat = tSiteData->lat;
// t.lon = tSiteData->lon;
// t.alt = tSiteData->alt;
//m_pSystemAnchor = systemAnchor::getInstance( tSiteData->SH, &t, tSiteData->local_binding );
//memcpy( &m_sSiteData, tSiteData, sizeof( DBC_SITE_DATA ));
//free( tSiteData );
// } else {
// LOG_ERROR("Error Loading Site DATA!");
// C3D_WGS84_LOC t;
// t.lat = 0;
// t.lon = 0;
// t.alt = 0;
//memset( &m_sSiteData, 0, sizeof( DBC_SITE_DATA ));
//m_pSystemAnchor = systemAnchor::getInstance( false, &t, true );
//};
//Check date/Time if correct!
// On some systems the System Date may be in past cause last session was interrupted by power loose
/*
uint32_t tTS = m_pDBC->getLastEventTS(); if ( tTS ) { // Have last TS
struct timespec tS = { 0 }; if( clock_gettime( CLOCK_REALTIME, &tS ) == 0 ){ //time_t cTS = time(NULL);
if ( tTS > tS.tv_sec ) { // System Time is in the Past
LOG("Correcting System Time!"); tS.tv_sec = tTS; tS.tv_nsec = tTS * 1000000; if (clock_settime(CLOCK_REALTIME, &tS) != 0) LOG_ERROR("Time Correction Failed!"); }; }; } #ifdef DEBUG
else LOG("No Last TS fetched from DB!")#endif
; */
//m_bDebugMode = m_pDBC->getAppDebugMode();
//m_bSessionStarted = m_pDBC->getEventSessionStatus( m_nServerNodeID );
//stopSession( false );
m_pRPC = servaRemoteClient::getInstance( this );
if (m_pRPC) { if( m_pRPC->start() ) return init(); };
return false;};
void servaKernel::m_fThreadProc( void ){ time_t tTimeTSeconds = 0; time_t cTimeTSeconds = 0; m_bThreadStatus = TH_WORKING; LOG("ServA Started!"); while ( m_bThreadStatus < TH_ENDED ) { cTimeTSeconds = time( NULL ); //Each Second
if( cTimeTSeconds != tTimeTSeconds ){ tTimeTSeconds = cTimeTSeconds;#ifdef LINUX_KERNEL_WATCHDOG
if( m_iWatchdogFd > -1 ) { char tB[64]; sprintf( tB, "2000\n" ); //ToDo: Make Error Handling here!
write( m_iWatchdogFd, &tB, strlen( tB ) ); };#endif
updateStatus();
if ( m_vEncodersPool.size() ) { for ( V_MEDIA_ENCODERS::iterator idx = m_vEncodersPool.begin(); idx < m_vEncodersPool.end(); idx++ ) { if ( (*idx)->getStatus() == ENCODER_FINISHED ) { handleEncodedVideo( (*idx)->getDBID() ); //delete( *idx );
m_vEncodersPool.erase( idx ); }; }; }; if ( uint64_t new_id = m_pDBC->getRecentMediaId() ) { LOG("Start Encoder!"); if ( processVideoFile( new_id ) ) updateEncodeStatus( new_id, MEDIA_ENCODE_PROGRESS, 0 ); }; //m_pHardwareManager->updateSysInfo( &m_sSysInfo );
DBC_PUSHED_CMD * tCMD = NULL;//m_pDBC->getNodePushedCMD( 0 ); //Kernel
if( tCMD != NULL ) { //m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_STARTED );
switch( tCMD->CMD ){ case CMD_KRNL_RELOAD:{ LOG("Reload Serva Node!"); m_bThreadStatus = TH_ENDED; setExitCause( EXIT_CODE_RELOAD_SERVICES ); break; }; case CMD_KRNL_RESTART:{ LOG("Restart Serva Node!"); m_bThreadStatus = TH_ENDED; setExitCause( EXIT_CODE_KERNEL_CLEAR ); break; }; case CMD_KRNL_FACTORY_RESET:{ LOG("Restart Serva -> Factory Reset!"); m_bThreadStatus = TH_ENDED; setExitCause( EXIT_CODE_FACTORY_RESET ); break; }; case CMD_NODES_RELOAD:{ //if( m_pNodeFactory != NULL ) {
// m_pNodeFactory->reloadNodes();
//};
break; }; case CMD_NODE_RELOAD:{ uint32_t tNodeID = atoi( tCMD->PARAM ); //if( m_pNodeFactory != NULL ) {
// m_pNodeFactory->reloadNode( tNodeID );
//};
break; }; case CMD_KRNL_PNP:{ //if( m_pHardwareManager!= NULL ) {
// LOG_ERROR( "Restart and Detect HardWare!" );
// // Must be shutdowned all service that uses hardware prior to detection!
// m_bThreadStatus = TH_ENDED;
// setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
//};
break; }; case CMD_KRNL_UPDATE_IDENT:{ //LOG("Restart Mojo -> Update Ident!");
//sprintf( m_sIdentity, tCMD->PARAM );
//config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
//config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
//config_setting_set_string( pSetting, tCMD->PARAM );
//basicSettingsProvider::getInstance()->compromise();
//basicSettingsProvider::getInstance()->validate();
//m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
m_bThreadStatus = TH_ENDED; setExitCause( EXIT_CODE_KERNEL_CLEAR ); break; }; case CMD_KRNL_REPLACE_DATABASE:{ LOG("Restart Mojo -> Replace Database!"); //CSTR tNAME = m_pDBC->getDataParam( 0 );
//CSTR tUSER = m_pDBC->getDataParam( 1 );
//CSTR tGROUP = m_pDBC->getDataParam( 2 );
//if ( tNAME ) {
// char tB[256];
// sprintf( tB,"./data/%s.db", tCMD->PARAM );
// if ( fileExist( tB )) {
// LOG("Restart Mojo -> Close Database!");
//Closing Database
// delete( m_pDBC );
// m_pDBC = NULL;
// LOG("Restart Mojo -> Delete Database!");
// //unlink( tNAME );
// LOG("Restart Mojo -> Copy Database!");
// fileCopy( tB, tNAME );
// LOG("Restart Mojo -> Chown Database!");
// ch_chown( tNAME, tUSER, tGROUP );
// LOG("Restart Mojo -> Delete Old Database!");
// unlink( tB );
// m_bThreadStatus = TH_ENDED;
// LOG("Restart Mojo -> Exit!");
// setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
// m_bThreadStatus = TH_ENDED;
// } else {
// LOG_ERROR("New Database Does not Exists!");
// };
//free( tNAME );
//free( tUSER );
//free( tGROUP );
//};
break; }; case CMD_KRNL_SYNC_TIME:{ uint32_t uts = atoi( tCMD->PARAM ); if( uts ){#if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
time_t ts = (time_t)uts; if( stime( &ts ) == 0 ){ // Restert Server;
m_bThreadStatus = TH_ENDED; setExitCause( EXIT_CODE_KERNEL_CLEAR ); };#else
timespec ts; ts.tv_sec = (time_t)uts; ts.tv_nsec = 0; if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){ // Restert Server;
m_bThreadStatus = TH_ENDED; setExitCause( EXIT_CODE_KERNEL_CLEAR ); };#endif
}; break; }; case CMD_KRNL_SYSTEM_TASK:{ break; }; case NODE_FORCE_FINISH_ALARM:{ //stopSession();
break; }; }; free( tCMD ); }; //if ( m_pDBC ) m_pDBC->cleanNodePushedCMD();
}; SLEEP(SLEEP_KERNEL_INTERVAL); };};
void servaKernel::updateEncodeStatus( uint64_t dbid, uint8_t status, uint8_t perc ){ m_pDBC->updateEncodeStatus( dbid, status, perc );};
bool servaKernel::processVideoFile( uint64_t dbid ){ if ( dbid == 0 ) return false; DBC_RPC_MEDIA_INFO *NFO = m_pDBC->getRemoteMediaInfo( dbid ); if ( NFO ) { servaMediaEncoder *ptr = new servaMediaEncoder( dbid ); if ( ptr ) { LOG("Encoder Constructed!"); char tA[MAX_PATH]; char tB[MAX_PATH]; sprintf( tA,"data/%s.%s", NFO->uuid, NFO->fmt ); sprintf( tB,"data/%s.mp4", NFO->uuid ); if ( ptr->create( tA, tB, /*NFO->fps*/ 25 ) ) { LOG("Encoder Created!"); m_vEncodersPool.push_back( ptr ); return true; }; delete( ptr ); }; free( NFO ); }; return false;};
void servaKernel::handleEncodedVideo( uint64_t dbid ){
};
uint64_t servaKernel::registerRemoteVideo( CSTR uuid, uint64_t src, uint32_t ts, uint16_t tsms, uint16_t width, uint16_t height, CSTR fmt ){ if ( m_pDBC ) { LOG("Video Register 1"); CSTR tMD5 = getUniqID_MD5(); if ( tMD5 ) { LOG( fmt ); LOG( tMD5 ); uint64_t rv = m_pDBC->addMediaQueueRecord( uuid, tMD5, src, ts, tsms, width, height, fmt ); free( tMD5 ); return rv; }; }; return 0;};
#undef LOG
#undef LOG_ERROR
|