|
|
|
@ -30,14 +30,14 @@ protocolKAFKA::protocolKAFKA( config_setting_t *Settings ){ |
|
|
|
|
|
|
|
m_pLog = basicLogCollection::getInstance(); |
|
|
|
m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "INFOSTRESS_CONNECTION"); |
|
|
|
m_pKafka = NULL; |
|
|
|
m_pKafkaBrokerSTR = (CSTR)malloc(CLOG); |
|
|
|
if( m_pKafkaBrokerSTR == NULL ) return; |
|
|
|
memset( m_pKafkaBrokerSTR, 0 , CLOG); |
|
|
|
m_pKafkaTopicSTR= (CSTR)malloc(CLOG); |
|
|
|
if( m_pKafkaTopicSTR == NULL ) return; |
|
|
|
memset( m_pKafkaTopicSTR, 0 , CLOG); |
|
|
|
m_pKafkaTopic = NULL; |
|
|
|
n_pProducer = NULL; |
|
|
|
n_pProducerBrokerSTR = (CSTR)malloc(CLOG); |
|
|
|
if( n_pProducerBrokerSTR == NULL ) return; |
|
|
|
memset( n_pProducerBrokerSTR, 0 , CLOG); |
|
|
|
n_pProducerTopicSTR= (CSTR)malloc(CLOG); |
|
|
|
if( n_pProducerTopicSTR == NULL ) return; |
|
|
|
memset( n_pProducerTopicSTR, 0 , CLOG); |
|
|
|
n_pProducerTopic = NULL; |
|
|
|
|
|
|
|
m_pScannerName = (CSTR)malloc(OS_SCANNER_NAME_SIZE); |
|
|
|
if( m_pScannerName == NULL ) return; |
|
|
|
@ -119,15 +119,15 @@ protocolKAFKA::protocolKAFKA( config_setting_t *Settings ){ |
|
|
|
config_setting_set_int(pSetting, m_nServerPort); |
|
|
|
basicSettingsProvider::getInstance()->compromise(); |
|
|
|
}; |
|
|
|
sprintf( m_pKafkaBrokerSTR, "%s:%d", m_pServerIP, m_nServerPort ); |
|
|
|
LOG( m_pKafkaBrokerSTR ); |
|
|
|
if (!config_setting_lookup_string(m_pSettings, "KAFKA_TOPIC", (const char**)&m_pKafkaTopicSTR)) { |
|
|
|
strncpy( m_pKafkaTopicSTR, DFSL_INFOSTRESS_DEFAULT_KAFKA_TOPIC, CTEXT ); |
|
|
|
sprintf( n_pProducerBrokerSTR, "%s:%d", m_pServerIP, m_nServerPort ); |
|
|
|
LOG( n_pProducerBrokerSTR ); |
|
|
|
if (!config_setting_lookup_string(m_pSettings, "KAFKA_TOPIC", (const char**)&n_pProducerTopicSTR)) { |
|
|
|
strncpy( n_pProducerTopicSTR, DFSL_INFOSTRESS_DEFAULT_KAFKA_TOPIC, CTEXT ); |
|
|
|
pSetting = config_setting_add(m_pSettings, "KAFKA_TOPIC", CONFIG_TYPE_STRING); |
|
|
|
config_setting_set_string(pSetting, m_pKafkaTopicSTR); |
|
|
|
config_setting_set_string(pSetting, n_pProducerTopicSTR); |
|
|
|
basicSettingsProvider::getInstance()->compromise(); |
|
|
|
}; |
|
|
|
LOG( m_pKafkaTopicSTR ); |
|
|
|
LOG( n_pProducerTopicSTR ); |
|
|
|
basicSettingsProvider::getInstance()->validate(); |
|
|
|
|
|
|
|
}; |
|
|
|
@ -136,7 +136,7 @@ protocolKAFKA::protocolKAFKA( config_setting_t *Settings ){ |
|
|
|
|
|
|
|
protocolKAFKA::protocolKAFKA( KAFKA_SETTINGS *Settings ){ |
|
|
|
m_pLog = basicLogCollection::getInstance(); |
|
|
|
m_pKafka = NULL; |
|
|
|
n_pProducer = NULL; |
|
|
|
m_pServerIP = NULL; |
|
|
|
memset( m_pKafkaBrokerSTR, 0 , sizeof( m_pKafkaBrokerSTR )); |
|
|
|
if( Settings!= NULL ) { |
|
|
|
@ -151,7 +151,7 @@ protocolKAFKA::protocolKAFKA( KAFKA_SETTINGS *Settings ){ |
|
|
|
|
|
|
|
protocolKAFKA::protocolKAFKA( CSTR ServerAddress, uint16_t Port ){ |
|
|
|
m_pLog = basicLogCollection::getInstance(); |
|
|
|
m_pKafka = NULL; |
|
|
|
n_pProducer = NULL; |
|
|
|
m_pServerIP = NULL; |
|
|
|
if( ServerAddress == 0 || Port == 0 ) return; |
|
|
|
m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE); |
|
|
|
@ -191,17 +191,17 @@ static void dr_msg_cb( rd_kafka_t *rk, const rd_kafka_message_t *rkmessage, void |
|
|
|
|
|
|
|
protocolKAFKA::~protocolKAFKA(){ |
|
|
|
//m_bEnabled = false;
|
|
|
|
if( m_pKafka != NULL ){ |
|
|
|
if( n_pProducer != NULL ){ |
|
|
|
/* Wait for final messages to be delivered or fail.
|
|
|
|
* rd_kafka_flush() is an abstraction over rd_kafka_poll() which |
|
|
|
* waits for all messages to be delivered. */ |
|
|
|
LOG("Flushing final messages.."); |
|
|
|
m_pKafka->flush( 3000 /* wait for max 3 seconds */); |
|
|
|
n_pProducer->flush( 3000 /* wait for max 3 seconds */); |
|
|
|
/* If the output queue is still not empty there is an issue
|
|
|
|
* with producing messages to the clusters. */ |
|
|
|
if (m_pKafka->outq_len() > 0) LOG_ERROR("Some messages are not delivered!"); |
|
|
|
delete m_pKafka; |
|
|
|
//if( m_pKafkaTopic!= NULL ) delete m_pKafkaTopic;
|
|
|
|
if (n_pProducer->outq_len() > 0) LOG_ERROR("Some messages are not delivered!"); |
|
|
|
delete n_pProducer; |
|
|
|
//if( n_pProducerTopic!= NULL ) delete n_pProducerTopic;
|
|
|
|
}; |
|
|
|
//if( m_pScannerName != NULL ) free(m_pScannerName);
|
|
|
|
if( m_pServerIP != NULL ) free(m_pServerIP); |
|
|
|
@ -228,48 +228,33 @@ void protocolKAFKA::dr_cb( RdKafka::Message &message ){ |
|
|
|
}; |
|
|
|
|
|
|
|
bool protocolKAFKA::init(){ |
|
|
|
RdKafka::Conf * tProducerConf; |
|
|
|
RdKafka::Conf * tGlobalConf; |
|
|
|
std::string tBuffer; |
|
|
|
std::string brokers = m_pKafkaBrokerSTR; |
|
|
|
//std::string topic = m_pKafkaTopicSTR;
|
|
|
|
|
|
|
|
LOG("INIT Begin"); |
|
|
|
if( strlen( m_pKafkaBrokerSTR ) == 0 ) { |
|
|
|
LOG_ERROR("No Kafka Brokers are defined!"); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
tProducerConf = RdKafka::Conf::create( RdKafka::Conf::CONF_GLOBAL ); |
|
|
|
if( tProducerConf == NULL ) { |
|
|
|
tGlobalConf = RdKafka::Conf::create( RdKafka::Conf::CONF_GLOBAL ); |
|
|
|
if( tGlobalConf == NULL ) { |
|
|
|
LOG_ERROR("Eror Creating Producer Conf!"); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
LOG("RdKafka::Conf::CONF_GLOBAL Created"); |
|
|
|
//tTopicConf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
|
|
|
|
//if( tTopicConf==NULL ){
|
|
|
|
// LOG_ERROR("Eror Creating Topic Conf!");
|
|
|
|
// delete( tProducerConf );
|
|
|
|
// tProducerConf = NULL;
|
|
|
|
// return false;
|
|
|
|
//};
|
|
|
|
//LOG("RdKafka::Conf::CONF_TOPIC Created");
|
|
|
|
|
|
|
|
if( tProducerConf->set( "bootstrap.servers", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
if( tGlobalConf->set( "bootstrap.servers", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tProducerConf ); |
|
|
|
//delete( tTopicConf );
|
|
|
|
//tProducerConf = NULL;
|
|
|
|
//tTopicConf = NULL;
|
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
LOG( m_pKafkaBrokerSTR ); |
|
|
|
LOG("bootstrap.servers is SET"); |
|
|
|
|
|
|
|
if( tProducerConf->set("metadata.broker.list", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
if( tGlobalConf->set("metadata.broker.list", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tProducerConf ); |
|
|
|
//delete( tTopicConf );
|
|
|
|
//tProducerConf = NULL;
|
|
|
|
//tTopicConf = NULL;
|
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
LOG("metadata.broker.list is SET"); |
|
|
|
@ -281,12 +266,9 @@ bool protocolKAFKA::init(){ |
|
|
|
* rd_kafka_flush(). */ |
|
|
|
//tProducerConf->set("queue.buffering.max.ms", "100", tBuffer );
|
|
|
|
//tProducerConf->set("socket.blocking.max.ms", "100", tBuffer );
|
|
|
|
if( tProducerConf->set( "dr_cb", this, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
if( tGlobalConf->set( "dr_cb", (RdKafka::DeliveryReportCb *) this, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tProducerConf ); |
|
|
|
//delete( tTopicConf );
|
|
|
|
//tProducerConf = NULL;
|
|
|
|
//tTopicConf = NULL;
|
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
LOG("dr_cb is SET"); |
|
|
|
@ -306,22 +288,33 @@ bool protocolKAFKA::init(){ |
|
|
|
* and the application must not reference it again after |
|
|
|
* this call. |
|
|
|
*/ |
|
|
|
m_pKafka = RdKafka::Producer::create( tProducerConf, tBuffer ); |
|
|
|
if( m_pKafka == NULL ){ |
|
|
|
n_pProducer = RdKafka::Producer::create( tGlobalConf, tBuffer ); |
|
|
|
if( n_pProducer == NULL ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tProducerConf ); |
|
|
|
//delete( tTopicConf );
|
|
|
|
//tProducerConf = NULL;
|
|
|
|
//tTopicConf = NULL;
|
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
LOG("RdKafka::Producer Created"); |
|
|
|
|
|
|
|
delete( tProducerConf ); |
|
|
|
|
|
|
|
// tProducerConf = NULL;
|
|
|
|
if( tGlobalConf->set( "enable.partition.eof", "true", tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
delete( n_pProducer ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
LOG("enable.partition.eof is SET"); |
|
|
|
|
|
|
|
m_pConsumer = RdKafka::Consumer::create( tGlobalConf, tBuffer ); |
|
|
|
if( m_pConsumer == NULL ){ |
|
|
|
delete( n_pProducer ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
LOG_ERROR( "Failed to create consumer:"); |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
return false; |
|
|
|
} |
|
|
|
LOG("RdKafka::Consumer Created"); |
|
|
|
LOG("INIT Complete"); |
|
|
|
// m_bEnabled = true;
|
|
|
|
delete( tGlobalConf ); |
|
|
|
return true; |
|
|
|
}; |
|
|
|
|
|
|
|
@ -332,10 +325,10 @@ bool protocolKAFKA::issuePost( CSTR tpc, CSTR Buffer, int BufferSize ){ |
|
|
|
RdKafka::Conf * tTopicConf = NULL; |
|
|
|
RdKafka::Topic * tKafkaTopic = NULL; |
|
|
|
std::string tBuffer; |
|
|
|
std::string brokers = m_pKafkaBrokerSTR; |
|
|
|
//std::string brokers = m_pKafkaBrokerSTR;
|
|
|
|
std::string topic = tpc; |
|
|
|
|
|
|
|
if( m_pKafka == NULL) return false; |
|
|
|
if( n_pProducer == NULL) return false; |
|
|
|
|
|
|
|
tTopicConf = RdKafka::Conf::create( RdKafka::Conf::CONF_TOPIC ); |
|
|
|
if( tTopicConf==NULL ){ |
|
|
|
@ -345,8 +338,8 @@ bool protocolKAFKA::issuePost( CSTR tpc, CSTR Buffer, int BufferSize ){ |
|
|
|
#ifdef DEBUG
|
|
|
|
LOG("RdKafka::Conf::CONF_TOPIC Created"); |
|
|
|
#endif
|
|
|
|
tKafkaTopic = RdKafka::Topic::create( m_pKafka, topic, tTopicConf, tBuffer ); |
|
|
|
if( m_pKafkaTopic == NULL ) { |
|
|
|
tKafkaTopic = RdKafka::Topic::create( n_pProducer, topic, tTopicConf, tBuffer ); |
|
|
|
if( tKafkaTopic == NULL ) { |
|
|
|
LOG_ERROR( "No Topic Created!!" ); |
|
|
|
LOG_ERROR( tpc ); |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
@ -369,7 +362,7 @@ bool protocolKAFKA::issuePost( CSTR tpc, CSTR Buffer, int BufferSize ){ |
|
|
|
#endif
|
|
|
|
//*/
|
|
|
|
retry: |
|
|
|
err = m_pKafka->produce( |
|
|
|
err = n_pProducer->produce( |
|
|
|
tKafkaTopic, |
|
|
|
RdKafka::Topic::PARTITION_UA, |
|
|
|
RdKafka::Producer::RK_MSG_COPY, |
|
|
|
@ -394,7 +387,7 @@ retry: |
|
|
|
* The internal queue is limited by the |
|
|
|
* configuration property |
|
|
|
* queue.buffering.max.messages */ |
|
|
|
m_pKafka->poll(1000/*block for max 1000ms*/); |
|
|
|
n_pProducer->poll(1000/*block for max 1000ms*/); |
|
|
|
if( rCnt-- ) goto retry; |
|
|
|
}; |
|
|
|
} |
|
|
|
@ -415,12 +408,12 @@ retry: |
|
|
|
* to make sure previously produced messages have their |
|
|
|
* delivery report callback served (and any other callbacks |
|
|
|
* you register). */ |
|
|
|
m_pKafka->poll(100); |
|
|
|
n_pProducer->poll(100); |
|
|
|
//uint8_t cnt = 100;
|
|
|
|
//while ( cnt && m_pKafka->outq_len() > 0) {
|
|
|
|
//while ( cnt && n_pProducer->outq_len() > 0) {
|
|
|
|
// LOG("Wait ...");
|
|
|
|
// cnt--;
|
|
|
|
// m_pKafka->poll(100);
|
|
|
|
// n_pProducer->poll(100);
|
|
|
|
//};
|
|
|
|
delete( tKafkaTopic ); |
|
|
|
delete( tTopicConf ); |
|
|
|
@ -429,385 +422,152 @@ retry: |
|
|
|
return true; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
void protocolKAFKA::setScannerName( CSTR Name ){ |
|
|
|
sprintf( m_pScannerName, "%s", Name ); |
|
|
|
}; |
|
|
|
void protocolKAFKA::setProtocolType( bool DeviceIs3D ){ |
|
|
|
if( DeviceIs3D ) { |
|
|
|
m_nScannerDimentions = 3; |
|
|
|
} else { |
|
|
|
m_nScannerDimentions = 2; |
|
|
|
}; |
|
|
|
}; |
|
|
|
CSTR protocolKAFKA::assembleStatusData( bool StatusOK ){ |
|
|
|
CSTR RetVal = NULL; |
|
|
|
CSTR TIME_STAMP; |
|
|
|
CSTR tSRC; |
|
|
|
CSTR tTARGET; |
|
|
|
CSTR tID; |
|
|
|
RetVal = (CSTR) malloc(CLOG); |
|
|
|
if ( RetVal != NULL ) { |
|
|
|
TIME_STAMP = getSHDTimeStamp(); |
|
|
|
if( TIME_STAMP != NULL ) { |
|
|
|
tTARGET = (CSTR) malloc(128); |
|
|
|
if( tTARGET!= NULL) { |
|
|
|
tSRC = (CSTR) malloc(128); |
|
|
|
if( tSRC ) { |
|
|
|
tID = (CSTR) malloc(64); |
|
|
|
if( tID != NULL ) { |
|
|
|
sprintf( tID, "%s", MD5String( TIME_STAMP ) ); |
|
|
|
sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName ); |
|
|
|
sprintf( tTARGET, DFSL_STATUS_DTO, TIME_STAMP , defenderStatusMode[((StatusOK)?1:0)] ); |
|
|
|
sprintf( RetVal, |
|
|
|
PACKAGE_SE_STATUS_DATA_ELEMENT, |
|
|
|
defenderTypeDescription[DC_STATUS], |
|
|
|
TIME_STAMP, |
|
|
|
tSRC, |
|
|
|
"DFSL_LIDAR_SENSOR_STATUS", |
|
|
|
DFSL_DATA_TYPE_STATUS, |
|
|
|
tID, |
|
|
|
tTARGET |
|
|
|
); |
|
|
|
free( tID ); |
|
|
|
free( tSRC ); |
|
|
|
free( tTARGET ); |
|
|
|
free( TIME_STAMP ); |
|
|
|
} else { |
|
|
|
free( tSRC ); |
|
|
|
free( tTARGET ); |
|
|
|
free( TIME_STAMP ); |
|
|
|
free( RetVal ); |
|
|
|
} |
|
|
|
} else { |
|
|
|
free( tTARGET ); |
|
|
|
free( TIME_STAMP ); |
|
|
|
free( RetVal ); |
|
|
|
}; |
|
|
|
} else { |
|
|
|
free( TIME_STAMP ); |
|
|
|
free( RetVal ); |
|
|
|
}; |
|
|
|
} else { |
|
|
|
free( RetVal ); |
|
|
|
RetVal = NULL; |
|
|
|
}; |
|
|
|
}; |
|
|
|
return RetVal; |
|
|
|
}; |
|
|
|
CSTR protocolKAFKA::assembleFaultData( uint8_t Code, CSTR Desc ){ |
|
|
|
CSTR RetVal; |
|
|
|
CSTR TIME_STAMP; |
|
|
|
CSTR tSRC; |
|
|
|
CSTR tTARGET; |
|
|
|
CSTR tID; |
|
|
|
RetVal = (CSTR) malloc(CLOG); |
|
|
|
if ( RetVal != NULL ) { |
|
|
|
TIME_STAMP = getSHDTimeStamp(); |
|
|
|
if( TIME_STAMP != NULL ) { |
|
|
|
tTARGET = (CSTR) malloc(128); |
|
|
|
if( tTARGET!= NULL) { |
|
|
|
tSRC = (CSTR) malloc(128); |
|
|
|
if( tSRC ) { |
|
|
|
tID = (CSTR) malloc(64); |
|
|
|
if( tID != NULL ) { |
|
|
|
sprintf( tID, "%s", MD5String( TIME_STAMP ) ); |
|
|
|
sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName ); |
|
|
|
sprintf( tTARGET, DFSL_ERROR_DTO, TIME_STAMP, Code, Desc ); |
|
|
|
sprintf( RetVal, |
|
|
|
PACKAGE_SE_STATUS_DATA_ELEMENT, |
|
|
|
Desc, |
|
|
|
TIME_STAMP, |
|
|
|
tSRC, |
|
|
|
"DFSL_LIDARE_SENSOR_FAULT_DATA", |
|
|
|
DFSL_DATA_TYPE_ERROR, |
|
|
|
tID, |
|
|
|
tTARGET |
|
|
|
); |
|
|
|
free( tID ); |
|
|
|
free( tSRC ); |
|
|
|
free( tTARGET ); |
|
|
|
free( TIME_STAMP ); |
|
|
|
} else { |
|
|
|
free( tSRC ); |
|
|
|
free( tTARGET ); |
|
|
|
free( TIME_STAMP ); |
|
|
|
free( RetVal ); |
|
|
|
} |
|
|
|
} else { |
|
|
|
free( tTARGET ); |
|
|
|
free( TIME_STAMP ); |
|
|
|
free( RetVal ); |
|
|
|
}; |
|
|
|
} else { |
|
|
|
free( TIME_STAMP ); |
|
|
|
free( RetVal ); |
|
|
|
}; |
|
|
|
} else { |
|
|
|
free( RetVal ); |
|
|
|
RetVal = NULL; |
|
|
|
}; |
|
|
|
}; |
|
|
|
return RetVal; |
|
|
|
}; |
|
|
|
CSTR protocolKAFKA::assembleTargetData( bool is3D, int targetID, int Range, int Width, int Azimuth, int Elevation ){ |
|
|
|
CSTR RetVal = NULL; |
|
|
|
RetVal = (CSTR) malloc(CLOG); |
|
|
|
if ( RetVal != NULL ) { |
|
|
|
CSTR TIME_STAMP = NULL; |
|
|
|
TIME_STAMP = getTStr(); |
|
|
|
if( TIME_STAMP!= NULL ) { |
|
|
|
char tID[64]; |
|
|
|
float Az; |
|
|
|
float Ra; |
|
|
|
sprintf( tID, "%s", MD5String( TIME_STAMP ) ); |
|
|
|
free( TIME_STAMP ); |
|
|
|
Ra = Range; |
|
|
|
Az = Azimuth; |
|
|
|
if( is3D ) { |
|
|
|
float El; |
|
|
|
El = Elevation; |
|
|
|
sprintf( RetVal, PACKAGE_INFOSTRESS_DATA_3D, tID, m_pScannerName, Az, El, Ra ); |
|
|
|
} else { |
|
|
|
sprintf( RetVal, PACKAGE_INFOSTRESS_DATA_2D, tID, m_pScannerName, Az, Ra, Width ); |
|
|
|
void protocolKAFKA::consume_cb( RdKafka::Message& A, void* B ){ |
|
|
|
char tBuff[256]; |
|
|
|
/*
|
|
|
|
switch (message->err()) { |
|
|
|
case RdKafka::ERR__TIMED_OUT: break; |
|
|
|
case RdKafka::ERR_NO_ERROR: |
|
|
|
// Real message
|
|
|
|
sprintf( tBuff, "Read msg at offset %dd ", message->offset() ); |
|
|
|
LOG( tBuff ); |
|
|
|
if( message->key() ){ |
|
|
|
sprintf( tBuff, "Key: %s ", *message->key()->c_str() ); |
|
|
|
LOG( tBuff ); |
|
|
|
}; |
|
|
|
} else { |
|
|
|
free( RetVal ); |
|
|
|
RetVal = NULL; |
|
|
|
}; |
|
|
|
}; |
|
|
|
return RetVal; |
|
|
|
//*
|
|
|
|
RdKafka::Headers *headers = message->headers(); |
|
|
|
if( headers ){ |
|
|
|
std::vector<RdKafka::Headers::Header> hdrs = headers->get_all(); |
|
|
|
for (size_t i = 0 ; i < hdrs.size() ; i++) { |
|
|
|
const RdKafka::Headers::Header hdr = hdrs[i]; |
|
|
|
if (hdr.value() != NULL) { |
|
|
|
sprintf( tBuff, " Header: %s = \"%.*s\"\n", hdr.key().c_str(), (int)hdr.value_size(), (const char *)hdr.value()); |
|
|
|
} else { |
|
|
|
sprintf( tBuff, " Header: %s = NULL\n", hdr.key().c_str()); |
|
|
|
}; |
|
|
|
LOG( tBuff ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
//* /
|
|
|
|
//printf("%.*s\n", static_cast<int>(message->len()), static_cast<const char *>(message->payload()));
|
|
|
|
break; |
|
|
|
case RdKafka::ERR__PARTITION_EOF: |
|
|
|
// Last message
|
|
|
|
//if (exit_eof) {
|
|
|
|
|
|
|
|
//}
|
|
|
|
break; |
|
|
|
case RdKafka::ERR__UNKNOWN_TOPIC: |
|
|
|
case RdKafka::ERR__UNKNOWN_PARTITION: |
|
|
|
default: |
|
|
|
LOG_ERROR( "Consume failed!" ); |
|
|
|
LOG_ERROR( message->errstr().c_str() ); |
|
|
|
}; */ |
|
|
|
}; |
|
|
|
|
|
|
|
static uint32_t INFOSTRESS_MESSAGE_ID = 0; |
|
|
|
size_t protocolKAFKA::assembleDefenderJSON( CSTR Buffer, defenderCategoryTypes Type, uint8_t Severity, CSTR Location, CSTR Data ){ |
|
|
|
char tBuffer[CLOG]; |
|
|
|
size_t RetVal; |
|
|
|
CSTR tBuf; |
|
|
|
RetVal = 0; |
|
|
|
tBuf = getSHDTimeStamp(); |
|
|
|
INFOSTRESS_MESSAGE_ID++; |
|
|
|
//memset( tBuffer, 0, CLOG );
|
|
|
|
if( tBuf != NULL ){ |
|
|
|
//char ID[64];
|
|
|
|
switch ( Type ) { |
|
|
|
case DC_STATUS: |
|
|
|
Severity = 0; |
|
|
|
break; |
|
|
|
case DC_ERROR: |
|
|
|
Severity = 2; |
|
|
|
break; |
|
|
|
case DC_ALARM: |
|
|
|
Severity = 4; |
|
|
|
break; |
|
|
|
default: |
|
|
|
Severity = 2; |
|
|
|
}; |
|
|
|
//sprintf( ID, "%s", MD5String( tBuf ) );
|
|
|
|
if( (m_nScannerDimentions > 2) && ( Type == DC_ALARM ) ) Type = DC_ALARM_3D; |
|
|
|
sprintf( tBuffer, |
|
|
|
DFSL_SE_ENVELOPE, |
|
|
|
//DEFENDER_MESSAGE_ID,
|
|
|
|
defenderCategories[Type], |
|
|
|
defenderTypeSeverity[Severity], |
|
|
|
tBuf, tBuf, |
|
|
|
Location, |
|
|
|
Data |
|
|
|
); |
|
|
|
RetVal = strlen(tBuffer); |
|
|
|
memcpy( Buffer, tBuffer, RetVal ); |
|
|
|
Buffer[RetVal] = (char) 0; |
|
|
|
free( tBuf ); |
|
|
|
}; |
|
|
|
return RetVal; |
|
|
|
}; |
|
|
|
|
|
|
|
bool protocolKAFKA::buildStatusPackage( unsigned char *Buffer, int &BufferSize, bool Status ){ |
|
|
|
if( Buffer!= NULL ){ |
|
|
|
CSTR tBuf; |
|
|
|
tBuf = assembleStatusData( Status ); |
|
|
|
#ifdef DEBUG
|
|
|
|
//LOG(tBuf);
|
|
|
|
#endif
|
|
|
|
if( tBuf != NULL ) { |
|
|
|
char DFSL_LOCATION[256]; |
|
|
|
sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat ); |
|
|
|
BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_STATUS, ((Status)?0:2), DFSL_LOCATION, tBuf ); |
|
|
|
#ifdef DEBUG
|
|
|
|
LOG((CSTR)Buffer); |
|
|
|
#endif
|
|
|
|
free( tBuf ); |
|
|
|
return true; |
|
|
|
} |
|
|
|
}; |
|
|
|
return false; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
bool protocolKAFKA::buildFaultPackage( unsigned char *Buffer, int &BufferSize, unsigned char Type, CSTR Description ){ |
|
|
|
if( Buffer!= NULL ){ |
|
|
|
CSTR tBuf; |
|
|
|
tBuf = assembleFaultData( Type, Description ); |
|
|
|
if( tBuf != NULL ){ |
|
|
|
char DFSL_LOCATION[256]; |
|
|
|
sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat ); |
|
|
|
BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ERROR, 3, DFSL_LOCATION, tBuf ); |
|
|
|
free( tBuf ); |
|
|
|
return true; |
|
|
|
}; |
|
|
|
}; |
|
|
|
return false; |
|
|
|
}; |
|
|
|
bool protocolKAFKA::receivePost( CSTR Channel, int32_t Partition, int64_t Offset, CSTR Buffer, int &BufferSize ){ |
|
|
|
uint8_t rCnt = 3; |
|
|
|
char tBuf[128]; |
|
|
|
RdKafka::ErrorCode err; |
|
|
|
RdKafka::Conf * tTopicConf = NULL; |
|
|
|
RdKafka::Topic * tKafkaTopic = NULL; |
|
|
|
std::string tBuffer; |
|
|
|
std::string topic = Channel; |
|
|
|
|
|
|
|
bool protocolKAFKA::buildEvent2DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle, float Range, float Width){ |
|
|
|
if( Buffer!= NULL ){ |
|
|
|
CSTR tBuf; |
|
|
|
tBuf = assembleTargetData( false, targetID, Range, Width, Angle, 0 ); |
|
|
|
if( tBuf != NULL ){ |
|
|
|
BufferSize = strlen( tBuf ); |
|
|
|
strncpy( (char *)Buffer, tBuf, BufferSize ); |
|
|
|
Buffer[BufferSize] = (char) 0; |
|
|
|
#ifdef DEBUG
|
|
|
|
LOG((CSTR)Buffer); |
|
|
|
#endif
|
|
|
|
free( tBuf ); |
|
|
|
return true; |
|
|
|
}; |
|
|
|
}; |
|
|
|
return false; |
|
|
|
}; |
|
|
|
BufferSize = 0; |
|
|
|
|
|
|
|
bool protocolKAFKA::buildEvent3DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle1, float Angle2, float Range ){ |
|
|
|
if( Buffer!= NULL ){ |
|
|
|
CSTR tBuf; |
|
|
|
tBuf = assembleTargetData( true, targetID, Range, 0, Angle1, Angle2 ); |
|
|
|
if( tBuf != NULL ){ |
|
|
|
BufferSize = strlen( tBuf ); |
|
|
|
strncpy( (char *)Buffer, tBuf, BufferSize ); |
|
|
|
Buffer[BufferSize] = (char) 0; |
|
|
|
if( m_pConsumer == NULL) return false; |
|
|
|
tTopicConf = RdKafka::Conf::create( RdKafka::Conf::CONF_TOPIC ); |
|
|
|
if( tTopicConf==NULL ){ |
|
|
|
LOG_ERROR("Eror Creating Consumer Topic Conf!"); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
#ifdef DEBUG
|
|
|
|
LOG((CSTR)Buffer); |
|
|
|
LOG("Consumer RdKafka::Conf::CONF_TOPIC Created"); |
|
|
|
#endif
|
|
|
|
free( tBuf ); |
|
|
|
return true; |
|
|
|
}; |
|
|
|
}; |
|
|
|
return false; |
|
|
|
}; |
|
|
|
|
|
|
|
bool protocolKAFKA::buildClockSyncPackage( unsigned char *Buffer, int &BufferSize ){ |
|
|
|
if( Buffer!= NULL ){ |
|
|
|
CSTR tBuf; |
|
|
|
tBuf = getSHTimeStamp(); |
|
|
|
if( tBuf != NULL ){ |
|
|
|
sprintf( |
|
|
|
((char *)(Buffer+BufferSize)), |
|
|
|
PACKAGE_ENVELOPE_TEMPLATE_CLOCKSYNC, |
|
|
|
tBuf |
|
|
|
); |
|
|
|
BufferSize = strlen( (char *)Buffer ); |
|
|
|
free( tBuf ); |
|
|
|
return true; |
|
|
|
}; |
|
|
|
}; |
|
|
|
return false; |
|
|
|
}; |
|
|
|
|
|
|
|
bool protocolKAFKA::buildCallBackPackage( unsigned char *Buffer, int &BufferSize, CSTR CallBackPath ){ |
|
|
|
if( Buffer!= NULL ){ |
|
|
|
sprintf( |
|
|
|
((char *)(Buffer+BufferSize)), |
|
|
|
PACKAGE_ENVELOPE_TEMPLATE_REG_CALL, |
|
|
|
CallBackPath |
|
|
|
); |
|
|
|
BufferSize = strlen( (char *)Buffer ); |
|
|
|
return true; |
|
|
|
}; |
|
|
|
return false; |
|
|
|
}; |
|
|
|
bool protocolKAFKA::Alarm2D( float Angle, float Range, float Width, int ID ){ |
|
|
|
bool RetVal= false; |
|
|
|
char Buffer[1024]; |
|
|
|
int BufferSize = 0; |
|
|
|
CSTR UID; |
|
|
|
if(!m_bEnabled) return false; |
|
|
|
UID = buildUniqID(); |
|
|
|
if( UID!= NULL ){ |
|
|
|
RetVal = buildEvent2DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle, Range, Width ); |
|
|
|
RetVal = issuePost( Buffer, BufferSize ); |
|
|
|
free( UID ); |
|
|
|
}; |
|
|
|
return RetVal; |
|
|
|
}; |
|
|
|
|
|
|
|
bool protocolKAFKA::Alarm3D( float Angle1, float Angle2, float Range, int ID ){ |
|
|
|
bool RetVal= false; |
|
|
|
char Buffer[1024]; |
|
|
|
int BufferSize = 0; |
|
|
|
CSTR UID; |
|
|
|
if(!m_bEnabled) return false; |
|
|
|
UID = buildUniqID(); |
|
|
|
if( UID!= NULL ){ |
|
|
|
RetVal = buildEvent3DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle1, Angle2, Range ); |
|
|
|
RetVal = issuePost( Buffer, BufferSize ); |
|
|
|
free( UID ); |
|
|
|
}; |
|
|
|
return RetVal; |
|
|
|
}; |
|
|
|
|
|
|
|
bool protocolKAFKA::VisibilityChanged( bool Worsened ){ |
|
|
|
if( !m_bEnableStatus ) return true; |
|
|
|
bool RetVal= false; |
|
|
|
char Buffer[1024]; |
|
|
|
int BufferSize = 0; |
|
|
|
if(!m_bEnabled) return false; |
|
|
|
RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, !Worsened ); |
|
|
|
RetVal = issuePost( Buffer, BufferSize ); |
|
|
|
return RetVal; |
|
|
|
}; |
|
|
|
|
|
|
|
bool protocolKAFKA::StatusUpdate( void ){ |
|
|
|
if( !m_bEnableStatus ) return true; |
|
|
|
bool RetVal= false; |
|
|
|
char Buffer[1024]; |
|
|
|
int BufferSize = 0; |
|
|
|
if(!m_bEnabled) return false; |
|
|
|
RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, m_bCurrentStatus ); |
|
|
|
RetVal = issuePost( Buffer, BufferSize ); |
|
|
|
return RetVal; |
|
|
|
}; |
|
|
|
tKafkaTopic = RdKafka::Topic::create( m_pConsumer, topic, tTopicConf, tBuffer ); |
|
|
|
if( tKafkaTopic == NULL ) { |
|
|
|
LOG_ERROR( "Failed to create Consumer topic!"); |
|
|
|
delete( tTopicConf ); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
|
|
|
|
bool protocolKAFKA::ErrorReporting( int lvl, CSTR Descr ){ |
|
|
|
if( !m_bEnableStatus ) return true; |
|
|
|
bool RetVal= false; |
|
|
|
char Buffer[1024]; |
|
|
|
int BufferSize = 0; |
|
|
|
if(!m_bEnabled) return false; |
|
|
|
RetVal = buildFaultPackage( ( unsigned char *) Buffer, BufferSize, lvl, Descr ); |
|
|
|
RetVal = issuePost( Buffer, BufferSize ); |
|
|
|
return RetVal; |
|
|
|
/*
|
|
|
|
* Start consumer for topic+partition at start offset |
|
|
|
*/ |
|
|
|
RdKafka::ErrorCode resp = m_pConsumer->start( tKafkaTopic, Partition, Offset ); |
|
|
|
if (resp != RdKafka::ERR_NO_ERROR) { |
|
|
|
LOG_ERROR( "Failed to start consumer!" ); |
|
|
|
LOG_ERROR( RdKafka::err2str(resp).c_str() ); |
|
|
|
delete( tKafkaTopic ); |
|
|
|
delete( tTopicConf ); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
RdKafka::Message *message = m_pConsumer->consume( tKafkaTopic, Partition, 1000 ); |
|
|
|
if( message == NULL ) { |
|
|
|
delete( tKafkaTopic ); |
|
|
|
delete( tTopicConf ); |
|
|
|
return false; |
|
|
|
} else { |
|
|
|
switch (message->err()) { |
|
|
|
case RdKafka::ERR__TIMED_OUT: break; |
|
|
|
case RdKafka::ERR_NO_ERROR: |
|
|
|
/* Real message */ |
|
|
|
sprintf( tBuf, "Read msg at offset %dd ", message->offset() ); |
|
|
|
LOG( tBuf ); |
|
|
|
if( message->key() ){ |
|
|
|
sprintf( tBuf, "Key: %s ", *message->key()->c_str() ); |
|
|
|
LOG( tBuf ); |
|
|
|
}; |
|
|
|
//printf("%.*s\n", static_cast<int>(message->len()), static_cast<const char *>(message->payload()));
|
|
|
|
BufferSize = message->len(); |
|
|
|
memcpy( Buffer, message->payload(), BufferSize ); |
|
|
|
break; |
|
|
|
case RdKafka::ERR__PARTITION_EOF: |
|
|
|
|
|
|
|
break; |
|
|
|
case RdKafka::ERR__UNKNOWN_TOPIC: |
|
|
|
case RdKafka::ERR__UNKNOWN_PARTITION: |
|
|
|
default: |
|
|
|
LOG_ERROR( "Consume failed!" ); |
|
|
|
LOG_ERROR( message->errstr().c_str() ); |
|
|
|
delete( message ); |
|
|
|
delete( tKafkaTopic ); |
|
|
|
delete( tTopicConf ); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
} |
|
|
|
delete( message ); |
|
|
|
m_pConsumer->stop( tKafkaTopic, Partition ); |
|
|
|
m_pConsumer->poll( 1000 ); |
|
|
|
delete( tKafkaTopic ); |
|
|
|
delete( tTopicConf ); |
|
|
|
return true; |
|
|
|
}; |
|
|
|
|
|
|
|
bool protocolKAFKA::StartTimeSynch( void ){ |
|
|
|
if( !m_bEnableStatus ) return true; |
|
|
|
bool RetVal= false; |
|
|
|
//LOG("TimeSYnc");
|
|
|
|
char Buffer[1024]; |
|
|
|
int BufferSize = 0; |
|
|
|
CSTR URL = NULL; |
|
|
|
URL = buildURL( SERVICE_URL_TEMPLATE_CLOCKSYNC, m_nScannerDimentions, m_pScannerName ); |
|
|
|
RetVal = buildClockSyncPackage(( unsigned char *) Buffer, BufferSize ); |
|
|
|
if( URL != NULL ){ |
|
|
|
RetVal = issuePost( URL, Buffer, BufferSize ); |
|
|
|
free( URL ); |
|
|
|
}; |
|
|
|
return RetVal; |
|
|
|
void protocolKAFKA::event_cb( RdKafka::Event &event ){ |
|
|
|
/* switch (event.type()){
|
|
|
|
case RdKafka::Event::EVENT_ERROR: |
|
|
|
if (event.fatal()) { |
|
|
|
std::cerr << "FATAL "; |
|
|
|
run = 0; |
|
|
|
} |
|
|
|
std::cerr << "ERROR (" << RdKafka::err2str(event.err()) << "): " << event.str() << std::endl; |
|
|
|
break; |
|
|
|
|
|
|
|
case RdKafka::Event::EVENT_STATS: |
|
|
|
std::cerr << "\"STATS\": " << event.str() << std::endl; |
|
|
|
break; |
|
|
|
case RdKafka::Event::EVENT_LOG: |
|
|
|
fprintf(stderr, "LOG-%i-%s: %s\n", event.severity(), event.fac().c_str(), event.str().c_str()); |
|
|
|
break; |
|
|
|
default: |
|
|
|
std::cerr << "EVENT " << event.type() << " (" << RdKafka::err2str(event.err()) << "): " << event.str() << std::endl; |
|
|
|
break; |
|
|
|
}; */ |
|
|
|
}; |
|
|
|
*/ |
|
|
|
|
|
|
|
#undef LOG
|
|
|
|
#undef LOG_ERROR
|