|
|
//----------------------------------------------------------------------------
// 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 "mojoKernel.h"
#include "../data/json/json_tool.h"
#include <strings.h>
#include <json/value.h>
#include <json/writer.h>
#include <json/reader.h>
#include "../common/kernelEventFilterAPF.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
mojoKernel *mojoKernel::_instance = NULL;
mojoKernel *mojoKernel::getInstance( config_setting_t *ptr ){ if ( _instance == NULL ) { _instance = new mojoKernel( ptr ); }; return _instance;};
mojoKernel::mojoKernel( 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; setWM( WM_NONE );};
mojoKernel::~mojoKernel(){ WaitFinish( true );#ifdef DEBUG
LOG(" Deleting Node Factory...");#endif
//if( m_pNodeFactory != NULL ) delete( m_pNodeFactory );
#ifdef DEBUG
LOG(" Deleting Module Factory...");#endif
if( m_pModuleFactory != NULL ) delete( m_pModuleFactory );#ifdef DEBUG
LOG(" Deleting Target Filters...");#endif
unloadFilters();
if( EXIT_CODE_KERNEL_CLEAR == getExitCause() ) {#ifdef DEBUG
LOG("Unregister Server");#endif
m_pDBC->unregisterServer();#ifdef DEBUG
//LOG("Close DataBase Connection...");
#endif
//if( m_pDBC != NULL ) delete( m_pDBC );//Deleting in Backend
//m_pDBC = NULL;
} else { if( EXIT_CODE_DETECT_HARDWARE == getExitCause() ) {#ifdef DEBUG
LOG("HW PNP Detection Requested...");#endif
//m_pDBC->updateNodePushedCMD( tCMD->id, 30 );
m_pHardwareManager->enumerateHardware(); //m_pDBC->updateNodePushedCMD( tCMD->id, 60 );
};#ifdef DEBUG
//LOG("Close DataBase Connection...");
#endif
//if( m_pDBC != NULL ) delete( m_pDBC );
//m_pDBC = NULL;
}; delete( m_pHardwareManager ); _instance = NULL;};
bool mojoKernel::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 ); }; } else { LOG_ERROR("Failed Read Config Settings!"); return false; }; basicSettingsProvider::getInstance()->validate();
LOG("Register Server ... "); m_pDBC->registerServer( m_nInstanceID, m_sIdentity, getpid(), &m_nServerID, &m_nServerNodeID ); char tBuffer[128]; sprintf( tBuffer,"Server Registered as [%d] nodeID[%d]",m_nServerID, m_nServerNodeID ); LOG( tBuffer );
DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData( m_nInstanceID );
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 ); free( tSiteData ); } else { LOG_ERROR("Error Loading Site DATA!"); C3D_WGS84_LOC t; t.lat = 0; t.lon = 0; t.alt = 0; m_pSystemAnchor = systemAnchor::getInstance( false, &t, true ); };
m_pHardwareManager = hardwareManager::getInstance();
loadFilters();
m_pModuleFactory = kernelModuleFactory::getInstance( m_nInstanceID ); if( !m_pModuleFactory ) { LOG_ERROR("Module Factory Failure!"); return false; }; //Autoloading Modules
//if( !m_pModuleFactory->loadModules() ){
// LOG_ERROR("Modules Loading Failure!");
// return false;
//};
m_pNodeFactory = deviceNodeFactory::getInstance(); if( !m_pNodeFactory ){ LOG_ERROR("Node Factory Failure!"); return false; }; return init();};
void mojoKernel::m_fThreadProc( void ){ time_t tTimeTSeconds = 0; m_bThreadStatus = TH_WORKING; while ( m_bThreadStatus < TH_ENDED ) { m_tTimeTSeconds = time( NULL ); //Each Second
if( m_tTimeTSeconds != tTimeTSeconds ){ tTimeTSeconds = m_tTimeTSeconds;
m_pHardwareManager->updateSysInfo( &m_sSysInfo );
DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD( m_nServerNodeID ); //Kernel
if( tCMD != NULL ) { m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_STARTED ); switch( tCMD->CMD ){ case CMD_KRNL_RELOAD:{ LOG("Reload Mojo Node!"); m_bThreadStatus = TH_ENDED; setExitCause( EXIT_CODE_RELOAD_SERVICES ); break; }; case CMD_KRNL_RESTART:{ LOG("Restart Mojo Node!"); m_bThreadStatus = TH_ENDED; setExitCause( EXIT_CODE_KERNEL_CLEAR ); break; }; case CMD_KRNL_FACTORY_RESET:{ LOG("Restart Mojo -> Factory Reset!"); m_bThreadStatus = TH_ENDED; setExitCause( EXIT_CODE_FACTORY_RESET ); break; }; case CMD_NODES_RELOAD:{ //if( m_pNodeFactory != NULL ) {
// m_pNodeFactory->reloadNodes( m_nSiteID );
//};
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_pHardwareManager->enumerateHardware();
m_bThreadStatus = TH_ENDED; setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
}; break; }; case CMD_KRNL_SYNC_TIME:{ uint32_t uts = atoi( tCMD->PARAM ); if( uts ){ time_t ts = (time_t)uts; if( stime( &ts ) == 0 ){ // Restert Server;
m_bThreadStatus = TH_ENDED; setExitCause( EXIT_CODE_KERNEL_CLEAR ); }; }; break; }; case CMD_KRNL_SYSTEM_TASK:{ /*
try{ Json::Value root; JSONCPP_STRING err; Json::CharReaderBuilder builder;
int len = strlen( tCMD->PARAM );
if ( len ) { const std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); if (!reader->parse( tCMD->PARAM, tCMD->PARAM + len, &root, &err)) { LOG_ERROR( "Error parsing the string" ); LOG_ERROR( err.c_str() ); } else { if (root.size()) { Json::Value tVal;
tVal = root["action"]; if (!tVal.isNull()) { LOG( tVal.asString().c_str() ); //SYS_COMMAND_ENUMERATOR tENUM = SYS_CMD_NONE;
CSTR pTemp = NULL; SYS_COMMAND_ENUMERATOR eType = SYS_CMD_NONE; for( int i = 0; i < 4; i++ ){ pTemp = (CSTR) strstr( tVal.asString().c_str(), SYS_COMANDS_SCRIPTS[i] ); if( pTemp ){ eType = (SYS_COMMAND_ENUMERATOR)i; break; }; }; if( eType != SYS_CMD_NONE ){ switch( eType ){ case SYS_CMD_REBOOT: { int rv = system("./scripts/linux/reboot_system.sh"); if( rv == -1 ) { LOG_ERROR("Error issuing System Command!"); } else { m_bThreadStatus = TH_ENDED; m_bExitCause = 0; }; break; }; case SYS_CMD_SET_STATIC:{ tVal = root["nif_id"]; if (!tVal.isNull()) { LOG( tVal.asString().c_str() ); }; break; }; }; }; }; }; }; }; } catch (exception e) { m_pDBC->updateNodePushedCMD( tCMD->id, DBC_CMD_FINISHED_ERROR ); LOG_ERROR("EXCEPTION"); }; */ break; }; }; free( tCMD ); }; m_pDBC->cleanNodePushedCMD();
}; SLEEP(SLEEP_KERNEL_INTERVAL); };};
void mojoKernel::loadFilters(){ if( m_pDBC == NULL ) return; DBC_INT_LIST *tList = m_pDBC->listFilters( m_nServerID ); if( tList != NULL ) { if( tList->size() > 0 ) { for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) { DBC_TARGET_FLITER_DATA *tData = NULL; tData = m_pDBC->getTargetFilterData( (*idx) ); if( tData != NULL ) { switch( tData->type ) { case TF_AUTHORISED_PEOPLE_FILTER: { kernelEventFilterAPF *tFilter = new kernelEventFilterAPF( tData->aux_id ); if( tFilter != NULL ) { if( tFilter->initFilter() ){ m_vTargetFilters.push_back( (kernelEventFilter*) tFilter ); } else { delete( tFilter ); }; }; break; }; }; free( tData ); }; }; tList->clear(); }; delete( tList );#ifdef DEBUG
} else { LOG("No Target Filters Assigned!");#endif
};};
void mojoKernel::unloadFilters(){ if( m_vTargetFilters.size() > 0 ) {#ifdef DEBUG
LOG("Have Filters to Unload");#endif
for( vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++ ){ switch( (*idx)->getType() ) { case TF_AUTHORISED_PEOPLE_FILTER:{ delete( (kernelEventFilterAPF*)(*idx) ); break; }; default:{ char tBuffer[64]; sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() ); LOG( tBuffer ); }; }; }; m_vTargetFilters.clear(); LOG("Unload Filters Done!");#ifdef DEBUG
} else { LOG("No Filters to Unload!");#endif
};};
#undef LOG
#undef LOG_ERROR
|