Browse Source

KAFKA Simulator Done

AGP
Evgeny Gagua 6 years ago
parent
commit
3ae7cb8e67
  1. 91
      lib/components/base/basicFunctionality.cpp
  2. 11
      lib/components/base/basicFunctionality.h
  3. 18
      lib/data/protocol/protocolDefender.cpp
  4. 6
      lib/data/protocol/protocolDefender.h
  5. 2
      lib/data/protocol/protocolKAFKA.cpp
  6. 77
      lib/data/protocol/protocolSafeShore.cpp
  7. 6
      lib/rgoose/rgooseDBConnector.cpp
  8. 138
      lib/rgoose/rgooseKafkaModule.cpp
  9. 2
      lib/rgoose/rgooseKafkaModule.h

91
lib/components/base/basicFunctionality.cpp

@ -36,11 +36,51 @@ CSTR getBuildID( void ){
return NULL;
};
/* Global Time Diff Value to correct Timestamps */
static int g_iTimeDiff = 0;
/**
* number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
*/
// Defined in Header File
//#define EPOCH_DIFF 11644473600LL
//#define UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT 116444736000000000
bool correctSysTime( int diff ){
#ifdef __linux__
unsigned long long getSHDTimeValue( void ){
struct timeval tData;
struct timezone tTZ;
tTZ.tz_minuteswest = 0;
tTZ.tz_dsttime = 0;
if( gettimeofday( &tData, &tTZ ) == -1 ){
return false;
} else {
tData.tv_sec += diff / 1000;
tData.tv_usec += ( diff * 1000 ) % 1000000;
};
if( settimeofday( &tData, &tTZ ) == -1 ){
return false;
};
#else
unsigned __int64 tMilliseconds;
SYSTEMTIME Ztime;
GetSystemTime(&Ztime);
SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
tMilliseconds += diff * 10000;
FileTimeToSystemTime( (FILETIME*) &tMilliseconds, &Ztime );
if( 0 == SetSystemTime(&Ztime) ) return false;
#endif
return true;
};
bool correctSoftTime( int diff ){
g_iTimeDiff = diff;
return true;
};
#ifdef __linux__
unsigned long long getTimeValue( void ){
unsigned long long tMilliseconds;
struct timeval tData;
struct timezone tTZ;
@ -60,7 +100,7 @@ void TimetToFileTime( time_t t, LPFILETIME pft ){
pft->dwLowDateTime = (DWORD) ll;
pft->dwHighDateTime = ll >>32;
}*/
unsigned __int64 getSHDTimeValue( void ){
unsigned __int64 getTimeValue( void ){
unsigned __int64 tMilliseconds;
SYSTEMTIME Ztime;
GetSystemTime(&Ztime);
@ -72,7 +112,7 @@ unsigned __int64 getSHDTimeValue( void ){
return tMilliseconds;
};
CSTR getSHDTimeStamp( void ){
CSTR getTimeStamp( void ){
CSTR RetVal = NULL;
time_t rawtime;
uint32_t msec;
@ -80,7 +120,7 @@ CSTR getSHDTimeStamp( void ){
time ( &rawtime );
ptm = gmtime ( &rawtime );
msec = getSHDTimeValue() % 1000;
msec = getTimeValue() % 1000;
//2019-03-24T12:32:59.670Z
if( ptm != NULL ) {
RetVal = (CSTR) malloc( 128 );
@ -100,13 +140,43 @@ CSTR getSHDTimeStamp( void ){
};
CSTR getUniqID_MD5( void ){
CSTR RetVal;
RetVal = (CSTR) malloc(64);
if(RetVal) sprintf( RetVal, "%s", MD5String( getSHDTimeStamp() ) );
return RetVal;
CSTR tVal = NULL;
char buf[34];
tVal = getTimeStamp();
if( tVal ){
sprintf( buf, "%s", MD5String( tVal ) );
free( tVal );
} else {
buf[0] = (char)0;
};
#ifdef __linux__
return strdup( buf );
#else
return _strdup( buf );
#endif
};
CSTR getUniqID_SHA256(void){ return NULL;};
CSTR getUniqID_UUID(void){ return NULL;};
CSTR getUniqID_UUID( uint32_t SEED ){
char buf[46];
if( SEED ) {
srand( SEED );
} else {
srand( time(NULL) );
};
snprintf( buf, 42, "%8.8x-%4.4x-%4.4x-%4.4x-%6.6x%6.6x",
rand() & 0xFFFFFFFF,
rand() & 0xFFFF,
(rand() & 0x0FFF) | 0x4000, /* M 4 MSB version => version 4 */
(rand() & 0x1FFF) | 0x8000, /* N: 3 MSB variant => variant 1 */
rand() & 0xFFFFFF, rand() & 0xFFFFFF
);
#ifdef __linux__
return strdup( buf );
#else
return _strdup( buf );
#endif
};
CSTR trim( CSTR val ){
int i = 0;
@ -148,8 +218,7 @@ CSTR strReplace( CSTR orig, CSTR rep, CSTR with ){
tmp = result = (CSTR) malloc(strlen(orig) + (len_with - len_rep) * count + 1);
if (!result)
return NULL;
if( !result ) return NULL;
// first time through the loop, all the variable are set correctly
// from here on,

11
lib/components/base/basicFunctionality.h

@ -36,19 +36,22 @@ CSTR implode( CArray *Data, CSTR S_Str, U8 Start, U8 End );
CSTR getTStr( CSTR Fmt = NULL );
CSTR getUniqID_SHA256(void);
CSTR getUniqID_MD5(void);
CSTR getUniqID_UUID(void);
CSTR getUniqID_UUID( uint32_t SEED = 0);
CSTR strReplace( CSTR orig, CSTR rep, CSTR with );
//#define EPOCH_DIFF 11644473600LL
#define UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT 116444736000000000
bool correctSysTime( int diff );
bool correctSoftTime( int diff );
#ifdef __linux__
unsigned long long getSHTimeValue( void );
unsigned long long getTimeValue( void );
#else
unsigned __int64 getSHTimeValue( void );
unsigned __int64 getTimeValue( void );
#endif
CSTR getSHDTimeStamp(void);
CSTR getTimeStamp(void);
#ifdef __linux__
inline PF_TIMESTAMP * getPFTS( PF_TIMESTAMP * ptr = NULL ){

18
lib/data/protocol/protocolDefender.cpp

@ -23,12 +23,6 @@ CSTR protocolDefender::buildURL( CSTR templ, int dim, CSTR devID ){
return RetVal;
};
CSTR protocolDefender::buildUniqID( void ){
CSTR RetVal;
RetVal = (CSTR) malloc(64);
if(RetVal) sprintf( RetVal, "%s", MD5String( getSHDTimeStamp() ) );
return RetVal;
};
/* Default Constructor */
protocolDefender::protocolDefender( config_setting_t *Settings ): protocolHTTP( basicSettingsProvider::getInstance()->getSubNode(Settings, "DEFENDER_CONNECTION") ){
@ -188,7 +182,7 @@ CSTR protocolDefender::assembleStatusData( bool StatusOK ){
CSTR tID;
RetVal = (CSTR) malloc(CLOG);
if ( RetVal != NULL ) {
TIME_STAMP = getSHDTimeStamp();
TIME_STAMP = getTimeStamp();
if( TIME_STAMP != NULL ) {
tTARGET = (CSTR) malloc(128);
if( tTARGET!= NULL) {
@ -243,7 +237,7 @@ CSTR protocolDefender::assembleFaultData( uint8_t Code, CSTR Desc ){
CSTR tID;
RetVal = (CSTR) malloc(CLOG);
if ( RetVal != NULL ) {
TIME_STAMP = getSHDTimeStamp();
TIME_STAMP = getTimeStamp();
if( TIME_STAMP != NULL ) {
tTARGET = (CSTR) malloc(128);
if( tTARGET!= NULL) {
@ -298,7 +292,7 @@ CSTR protocolDefender::assembleTargetData( int targetID, int Range, int Width, i
CSTR tID;
RetVal = (CSTR) malloc(CLOG);
if ( RetVal != NULL ) {
TIME_STAMP = getSHDTimeStamp();
TIME_STAMP = getTimeStamp();
if( TIME_STAMP != NULL ) {
tTARGET = (CSTR) malloc(128);
if( tTARGET!= NULL) {
@ -355,7 +349,7 @@ size_t protocolDefender::assembleDefenderJSON( CSTR Buffer, defenderCategoryType
size_t RetVal;
CSTR tBuf;
RetVal = 0;
tBuf = getSHDTimeStamp();
tBuf = getTimeStamp();
DEFENDER_MESSAGE_ID++;
//memset( tBuffer, 0, CLOG );
if( tBuf != NULL ){
@ -504,7 +498,7 @@ bool protocolDefender::Alarm2D( float Angle, float Range, float Width, int ID ){
CSTR URL;
CSTR UID;
if(!m_bEnabled) return false;
UID = buildUniqID();
UID = getUniqID_MD5();
if( UID!= NULL ){
URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName );
#ifdef DEBUG
@ -527,7 +521,7 @@ bool protocolDefender::Alarm3D( float Angle1, float Angle2, float Range, int ID
CSTR URL;
CSTR UID;
if(!m_bEnabled) return false;
UID = buildUniqID();
UID = getUniqID_MD5();
if( UID!= NULL ){
URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName );
#ifdef DEBUG

6
lib/data/protocol/protocolDefender.h

@ -43,7 +43,6 @@ private:
int m_nServerPort;
bool m_bCurrentStatus;
CSTR buildURL( CSTR templ, int dim, CSTR devID );
CSTR buildUniqID( void );
bool buildClockSyncPackage( unsigned char *Buffer, int &BufferSize );
bool buildCallBackPackage( unsigned char *Buffer, int &BufferSize, CSTR CallBackPath );
bool buildStatusPackage( unsigned char *Buffer, int &BufferSize, bool Status );
@ -84,10 +83,5 @@ public:
bool getMitigationStatus(void) { return m_bMitigationStatus; };
};
#ifdef __linux__
unsigned long long getSHTimeValue( void );
#else
unsigned __int64 getSHTimeValue( void );
#endif
#endif

2
lib/data/protocol/protocolKAFKA.cpp

@ -233,7 +233,7 @@ bool protocolKAFKA::init(){
std::string brokers = m_pKafkaBrokerSTR;
//std::string topic = m_pKafkaTopicSTR;
LOG("Begin Init");
LOG("INIT Begin");
if( strlen( m_pKafkaBrokerSTR ) == 0 ) {
LOG_ERROR("No Kafka Brokers are defined!");
return false;

77
lib/data/protocol/protocolSafeShore.cpp

@ -14,93 +14,24 @@ CSTR systemFaultCode[] = {
"Info"
};
/* Global Time Diff Value to correct Timestamps */
static int g_iTimeDiff = 0;
/**
* number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
*/
//#define EPOCH_DIFF 11644473600LL
#define UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT 116444736000000000
#ifdef __linux__
unsigned long long getSHTimeValue( void ){
unsigned long long tMilliseconds;
struct timeval tData;
struct timezone tTZ;
tMilliseconds = 0;
tTZ.tz_minuteswest = 0;
tTZ.tz_dsttime = 0;
if( gettimeofday( &tData, &tTZ ) == -1 ){
} else {
tMilliseconds = tData.tv_sec;
tMilliseconds = tMilliseconds *1000;
tMilliseconds += tData.tv_usec / 1000;
};
#else
/*
void TimetToFileTime( time_t t, LPFILETIME pft ){
LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
pft->dwLowDateTime = (DWORD) ll;
pft->dwHighDateTime = ll >>32;
}*/
unsigned __int64 getSHTimeValue( void ){
unsigned __int64 tMilliseconds;
SYSTEMTIME Ztime;
GetSystemTime(&Ztime);
SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
tMilliseconds -= UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT;
tMilliseconds = tMilliseconds / 10000;
#endif
tMilliseconds += g_iTimeDiff;
return tMilliseconds;
};
CSTR getSHTimeStamp( void ){
CSTR RetVal;
#ifdef __linux__
RetVal = (CSTR) malloc( 64 );
if( RetVal!=NULL ){
sprintf( RetVal, "%llu", getSHTimeValue() );
sprintf( RetVal, "%llu", getTimeValue() );
};
#else
RetVal = (CSTR) malloc( 64 );
if( RetVal!=NULL ){
sprintf( RetVal, "%I64d", getSHTimeValue() );
sprintf( RetVal, "%I64d", getTimeValue() );
};
#endif
return RetVal;
};
bool correctSHTime( int diff ){
#ifdef __linux__
struct timeval tData;
struct timezone tTZ;
tTZ.tz_minuteswest = 0;
tTZ.tz_dsttime = 0;
if( gettimeofday( &tData, &tTZ ) == -1 ){
return false;
} else {
tData.tv_sec += diff / 1000;
tData.tv_usec += ( diff * 1000 ) % 1000000;
};
if( settimeofday( &tData, &tTZ ) == -1 ){
return false;
};
#else
unsigned __int64 tMilliseconds;
SYSTEMTIME Ztime;
GetSystemTime(&Ztime);
SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
tMilliseconds += diff * 10000;
FileTimeToSystemTime( (FILETIME*) &tMilliseconds, &Ztime );
if( 0 == SetSystemTime(&Ztime) ) return false;
#endif
return true;
};
CSTR protocolSafeShore::buildURL( CSTR templ, int dim, CSTR devID ){
CSTR RetVal;
char tpl[256];
@ -470,7 +401,7 @@ bool protocolSafeShore::packetHandler( protocolPackage *Package ){
XMLNode node3 = node1.getChildNode("t0", 0);
XMLNode node4 = node1.getChildNode("t1", 0);
XMLNode node5 = node1.getChildNode("t2", 0);
t3 = getSHTimeValue();
t3 = getTimeValue();
#ifdef __linux__
t0 = atoll(node3.getText(0));
t1 = atoll(node4.getText(0));
@ -488,7 +419,7 @@ bool protocolSafeShore::packetHandler( protocolPackage *Package ){
// g_iTimeDiff = 0;
//} else {
// LOG_ERROR("Time Sync Unucessfull - Will use DIFF!");
g_iTimeDiff = jagajaga;
correctSoftTime( jagajaga );
//};
};
};

6
lib/rgoose/rgooseDBConnector.cpp

@ -1434,13 +1434,15 @@ KAFKA_TEMPLATE *argusDBC::getKafkaTemplateForEvent( uint32_t ModuleID, uint32_t
cArr = result->at(0);
it = cArr->find("topic");
if( it != cArr->end() ){
memset( tVal->topic, 0, 128 );
strncpy( tVal->topic , it->second.c_str(), 127 );
};
it = cArr->find("template");
if( it != cArr->end() ){
tVal->tpl = (CSTR)malloc( it->second.length() );
tVal->tpl = (CSTR)malloc( it->second.length() + 1 );
if( tVal->tpl!= NULL) {
strncpy( tVal->tpl, it->second.c_str(), 255 );
memset( tVal->tpl, 0, it->second.length() + 1 );
strncpy( tVal->tpl, it->second.c_str(), it->second.length() );
};
};
};

138
lib/rgoose/rgooseKafkaModule.cpp

@ -23,11 +23,14 @@ argusKafkaModule::argusKafkaModule( uint32_t ModuleID ): argusKernelModule( Modu
argusKafkaModule::~argusKafkaModule(){
m_bEnabled = false;
waitThreadEnd( true );
if( m_pProtocol!= NULL ) {
delete( m_pProtocol );
};
if( m_pEvtTemplates!= NULL ) {
for(int i=0; i < EVT_CLASS_COUNT; i++ ) {
if( m_pEvtTemplates[i] != NULL ){
@ -45,17 +48,55 @@ argusKafkaModule::~argusKafkaModule(){
};
/* Periodical Job Handler */
void argusKafkaModule::m_fThreadProc( void ){
#ifdef SIMULATOR
float sim_Az = 0.f;
float sim_El = 20.f;
float sim_Ran = 80.35f;
float tr_Az_step = 0.3f;
uint32_t src = 128;
#endif
#ifdef DEBUG
LOG("Pereodical Jobs Started");
#endif
m_bThreadStatus = TH_WORKING;
if( !m_bEnabled ) m_bThreadStatus = TH_ENDED;
if( !m_bEnabled ) {
m_bThreadStatus = TH_ENDED;
#ifdef DEBUG
LOG("Module is Disabled!");
#endif
};
while( m_bThreadStatus < TH_ENDED ) {
m_llTicks ++;//= getMilliseconds();
if( m_llTicks % 20 == 0 ) { // each 2 seconds
//m_pProtocol->StatusUpdate();
//m_lCycleCounter == 0;
//char tBuff[256];
//sprintf(tBuff,"MY MESSAGE %dd", m_llTicks );
//LOG( tBuff );
//m_pProtocol->issuePost( "My_TEST_TOPIC", tBuff, strlen( tBuff ) );
sim_Az += tr_Az_step;
#ifdef SIMULATOR
for(int i=0; i < EVT_CLASS_COUNT; i++ ) {
if( m_pEvtTemplates[i] != NULL ){
for( int j=0; j< EVT_COUNT; j++ ) {
if( m_pEvtTemplates[i][j] != NULL ){
CSTR tpl;
CSTR ts;
CSTR uuid;
char AZ[10];
char EL[10];
char RN[10];
sprintf( AZ, "%.3f", sim_Az);
sprintf( EL, "%.3f", sim_El);
sprintf( RN, "%.3f", sim_Ran);
uuid = getUniqID_UUID();
ts = getTimeStamp();
tpl = buildTemplate( m_pEvtTemplates[i][j]->tpl, src, ts, uuid, AZ, RN, EL );
if( tpl ) {
m_pProtocol->issuePost( m_pEvtTemplates[i][j]->topic, tpl, strlen( tpl ) );
free( tpl );
};
if( uuid ) free( uuid );
if( ts ) free( ts );
};
};
};
};
#endif
};
/* No need for time Sync
if( m_llTicks % 350 == 0 ) { // each 35 seconds
@ -68,14 +109,18 @@ void argusKafkaModule::m_fThreadProc( void ){
};
bool argusKafkaModule::startService(){
if(! m_bEnabled) return false;
if(!m_bEnabled) return false;
if( reloadSettings() ) {
if( m_pSettings!= NULL ){
m_pProtocol = new protocolKAFKA( m_pSettings );
if( m_pProtocol->init() ){
// No need pereodical jobs
//return init();
// No need pereodical jobs unless SIMULATOR
#ifdef SIMULATOR
LOG("Start PJ");
return init();
#else
return true;
#endif
};
};
};
@ -135,46 +180,47 @@ CSTR argusKafkaModule::buildTemplate(
CSTR el
){
CSTR ct1, ct2;
char tVar[64];
if( tpl == NULL ) return NULL;
sprintf( tVar, "%d", source_id );
ct1 = strReplace( tpl, KAFKA_TPL_SUBST_ID, tVar );
if( ct1 ) {
ct2 = strReplace( ct1, KAFKA_TPL_SUBST_TS, ts );
if( ct2 ) {
free( ct1 );
ct1 = ct2;
};
if( uuid ){
ct2 = strReplace( ct1, KAFKA_TPL_SUBST_UUID, uuid );
if( ct2 ) {
free( ct1 );
ct1 = ct2;
};
};
if( az ){
ct2 = strReplace( ct1, KAFKA_TPL_SUBST_AZ, az );
char sid[64];
if( tpl ) {
sprintf( sid, "%d", source_id );
ct1 = strReplace( tpl, KAFKA_TPL_SUBST_ID, sid );
if( ct1 ) {
ct2 = strReplace( ct1, KAFKA_TPL_SUBST_TS, ts );
if( ct2 ) {
free( ct1 );
ct1 = ct2;
};
};
if( range ){
ct2 = strReplace( ct1, KAFKA_TPL_SUBST_RANGE, range );
if( ct2 ) {
free( ct1 );
ct1 = ct2;
};
};
if( el ){
ct2 = strReplace( ct1, KAFKA_TPL_SUBST_EL, el );
if( ct2 ) {
free( ct1 );
ct1 = ct2;
};
};
if( uuid ){
ct2 = strReplace( ct1, KAFKA_TPL_SUBST_UUID, uuid );
if( ct2 ) {
free( ct1 );
ct1 = ct2;
};
};
if( az ){
ct2 = strReplace( ct1, KAFKA_TPL_SUBST_AZ, az );
if( ct2 ) {
free( ct1 );
ct1 = ct2;
};
};
if( range ){
ct2 = strReplace( ct1, KAFKA_TPL_SUBST_RANGE, range );
if( ct2 ) {
free( ct1 );
ct1 = ct2;
};
};
if( el ){
ct2 = strReplace( ct1, KAFKA_TPL_SUBST_EL, el );
if( ct2 ) {
free( ct1 );
ct1 = ct2;
};
};
return ct1;
};
return ct1;
};
}
return NULL;
};

2
lib/rgoose/rgooseKafkaModule.h

@ -6,6 +6,8 @@
#include "../components/base/basicThread.h"
#include "../data/protocol/protocolKAFKA.h"
#define SIMULATOR
#define KAFKA_TPL_SUBST_ID "%%ID%%"
#define KAFKA_TPL_SUBST_UUID "%%UUID%%"
#define KAFKA_TPL_SUBST_TS "%%TIMESTAMP%%"

Loading…
Cancel
Save