|
|
|
@ -137,31 +137,32 @@ protocolKAFKA::protocolKAFKA( config_setting_t *Settings ){ |
|
|
|
protocolKAFKA::protocolKAFKA( KAFKA_SETTINGS *Settings ){ |
|
|
|
m_pLog = basicLogCollection::getInstance(); |
|
|
|
n_pProducer = NULL; |
|
|
|
m_pServerIP = NULL; |
|
|
|
memset( m_pKafkaBrokerSTR, 0 , sizeof( m_pKafkaBrokerSTR )); |
|
|
|
//m_pServerIP = NULL;
|
|
|
|
m_pSettings = Settings; |
|
|
|
//memset( m_pKafkaBrokerSTR, 0 , sizeof( m_pKafkaBrokerSTR ));
|
|
|
|
if( Settings!= NULL ) { |
|
|
|
m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE); |
|
|
|
if( m_pServerIP == NULL ) return; |
|
|
|
memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE); |
|
|
|
strncpy( m_pServerIP, Settings->brocker_addr, OS_CEP_ADDRESS_SIZE ); |
|
|
|
m_nServerPort = Settings->brocker_port; |
|
|
|
sprintf( m_pKafkaBrokerSTR, "%s:%d", m_pServerIP, m_nServerPort ); |
|
|
|
//m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE);
|
|
|
|
//if( m_pServerIP == NULL ) return;
|
|
|
|
//memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE);
|
|
|
|
//strncpy( m_pServerIP, Settings->brocker_addr, OS_CEP_ADDRESS_SIZE );
|
|
|
|
//m_nServerPort = Settings->brocker_port;
|
|
|
|
//sprintf( m_pKafkaBrokerSTR, "%s:%d", m_pServerIP, m_nServerPort );
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
protocolKAFKA::protocolKAFKA( CSTR ServerAddress, uint16_t Port ){ |
|
|
|
m_pLog = basicLogCollection::getInstance(); |
|
|
|
n_pProducer = NULL; |
|
|
|
m_pServerIP = NULL; |
|
|
|
if( ServerAddress == 0 || Port == 0 ) return; |
|
|
|
m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE); |
|
|
|
if( m_pServerIP == NULL ) return; |
|
|
|
memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE); |
|
|
|
memset( m_pKafkaBrokerSTR, 0 , sizeof( m_pKafkaBrokerSTR )); |
|
|
|
strncpy( m_pServerIP, ServerAddress, OS_CEP_ADDRESS_SIZE ); |
|
|
|
m_nServerPort = Port; |
|
|
|
sprintf( m_pKafkaBrokerSTR, "%s:%d", m_pServerIP, m_nServerPort ); |
|
|
|
}; |
|
|
|
//protocolKAFKA::protocolKAFKA( CSTR ServerAddress, uint16_t Port ){
|
|
|
|
// m_pLog = basicLogCollection::getInstance();
|
|
|
|
// n_pProducer = NULL;
|
|
|
|
// m_pServerIP = NULL;
|
|
|
|
// if( ServerAddress == 0 || Port == 0 ) return;
|
|
|
|
// m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE);
|
|
|
|
// if( m_pServerIP == NULL ) return;
|
|
|
|
// memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE);
|
|
|
|
// memset( m_pKafkaBrokerSTR, 0 , sizeof( m_pKafkaBrokerSTR ));
|
|
|
|
// strncpy( m_pServerIP, ServerAddress, OS_CEP_ADDRESS_SIZE );
|
|
|
|
// m_nServerPort = Port;
|
|
|
|
// sprintf( m_pKafkaBrokerSTR, "%s:%d", m_pServerIP, m_nServerPort );
|
|
|
|
//};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Message delivery report callback. |
|
|
|
@ -204,7 +205,7 @@ protocolKAFKA::~protocolKAFKA(){ |
|
|
|
//if( n_pProducerTopic!= NULL ) delete n_pProducerTopic;
|
|
|
|
}; |
|
|
|
//if( m_pScannerName != NULL ) free(m_pScannerName);
|
|
|
|
if( m_pServerIP != NULL ) free(m_pServerIP); |
|
|
|
//if( m_pServerIP != NULL ) free(m_pServerIP);
|
|
|
|
//if( m_pAccessURL != NULL ) free(m_pAccessURL);
|
|
|
|
}; |
|
|
|
//https://github.com/edenhill/librdkafka/blob/master/examples/producer.c
|
|
|
|
@ -229,15 +230,15 @@ void protocolKAFKA::dr_cb( RdKafka::Message &message ){ |
|
|
|
|
|
|
|
bool protocolKAFKA::init(){ |
|
|
|
RdKafka::Conf * tGlobalConf; |
|
|
|
char zBuffer[CTEXT]; |
|
|
|
std::string tBuffer; |
|
|
|
std::string brokers = m_pKafkaBrokerSTR; |
|
|
|
std::string tValue; |
|
|
|
if( m_pSettings == NULL ) return false; |
|
|
|
tValue.reserve(256); |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
LOG("INIT Begin"); |
|
|
|
#endif
|
|
|
|
if( strlen( m_pKafkaBrokerSTR ) == 0 ) { |
|
|
|
LOG_ERROR("No Kafka Brokers are defined!"); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
|
|
|
|
tGlobalConf = RdKafka::Conf::create( RdKafka::Conf::CONF_GLOBAL ); |
|
|
|
if( tGlobalConf == NULL ) { |
|
|
|
LOG_ERROR("Eror Creating Producer Conf!"); |
|
|
|
@ -245,36 +246,180 @@ bool protocolKAFKA::init(){ |
|
|
|
}; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
LOG("RdKafka::Conf::CONF_GLOBAL Created"); |
|
|
|
std::string features; |
|
|
|
tGlobalConf->get( "builtin.features", features ); |
|
|
|
sprintf( zBuffer, "librdkafka version %s (0x%08x, builtin.features \"%s\")", RdKafka::version_str().c_str(), RdKafka::version(), features.c_str() ); |
|
|
|
LOG( zBuffer ); |
|
|
|
#endif
|
|
|
|
if( tGlobalConf->set( "bootstrap.servers", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
if( strlen( m_pSettings->brocker_addr ) == 0 ) { |
|
|
|
LOG_ERROR("No Kafka Brokers are defined!"); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
//std::string Kuser = "";
|
|
|
|
//std::string Kpass = "";
|
|
|
|
//if( tGlobalConf->set( "sasl.username", Kuser, tBuffer ) != RdKafka::Conf::CONF_OK ){
|
|
|
|
// LOG_ERROR( tBuffer.c_str() );
|
|
|
|
// delete( tGlobalConf );
|
|
|
|
// return false;
|
|
|
|
//};
|
|
|
|
//if( tGlobalConf->set( "sasl.password", Kuser, tBuffer ) != RdKafka::Conf::CONF_OK ){
|
|
|
|
// LOG_ERROR( tBuffer.c_str() );
|
|
|
|
// delete( tGlobalConf );
|
|
|
|
// return false;
|
|
|
|
//};
|
|
|
|
|
|
|
|
sprintf( (char *)tValue.c_str(), "%s:%d", m_pSettings->brocker_addr, m_pSettings->brocker_port ); |
|
|
|
if( tGlobalConf->set( "bootstrap.servers", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
LOG( m_pKafkaBrokerSTR ); |
|
|
|
LOG("bootstrap.servers is SET"); |
|
|
|
} else { |
|
|
|
LOG("bootstrap.servers is set"); |
|
|
|
#endif
|
|
|
|
if( tGlobalConf->set("metadata.broker.list", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
}; |
|
|
|
|
|
|
|
if( tGlobalConf->set("metadata.broker.list", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG( tValue.c_str() ); |
|
|
|
LOG("metadata.broker.list is SET"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
|
|
|
|
if( m_pSettings->use_ssl ) { |
|
|
|
LOG( "USE SSL!"); |
|
|
|
if (m_pSettings->auth_by_cred) { |
|
|
|
sprintf((char *)tValue.c_str(), "SASL_SSL"); |
|
|
|
if( tGlobalConf->set( "security.protocol", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG("SEC Proto is SASL_SSL"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
sprintf((char *)tValue.c_str(), "%s", m_pSettings->cred_username); |
|
|
|
if( tGlobalConf->set( "sasl.username", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG("SASL username is SET"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
sprintf((char *)tValue.c_str(), "%s", m_pSettings->cred_password); |
|
|
|
if( tGlobalConf->set( "sasl.password", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG("SASL password is SET"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
} else { |
|
|
|
sprintf((char *)tValue.c_str(), "SSL"); |
|
|
|
if( tGlobalConf->set( "security.protocol", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG("SEC Proto is SSL"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
}; |
|
|
|
if ( m_pSettings->truststore ) { |
|
|
|
sprintf((char *)tValue.c_str(), "./data/modules/VAULT/%s/%s.CA.pem", m_pSettings->vault_md5, m_pSettings->vault_md5 ); |
|
|
|
if( tGlobalConf->set( "ssl.ca.location", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
LOG("metadata.broker.list is SET"); |
|
|
|
} else { |
|
|
|
LOG("SSL CA Root is SET"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
}; |
|
|
|
if ( m_pSettings->certstore ) { |
|
|
|
sprintf((char *)tValue.c_str(), "./data/modules/VAULT/%s/%s.CERT.pem", m_pSettings->vault_md5, m_pSettings->vault_md5 ); |
|
|
|
if( tGlobalConf->set( "ssl.certificate.location", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG("SSL CERT is SET"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
}; |
|
|
|
if ( m_pSettings->keystore ) { |
|
|
|
sprintf((char *)tValue.c_str(), "./data/modules/VAULT/%s/%s.key", m_pSettings->vault_md5, m_pSettings->vault_md5 ); |
|
|
|
if( tGlobalConf->set( "ssl.key.location", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG("SSL KEY is SET"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
}; |
|
|
|
if (strlen(m_pSettings->cred_password)) { |
|
|
|
sprintf((char *)tValue.c_str(), m_pSettings->cred_password ); |
|
|
|
if( tGlobalConf->set( "ssl.key.password", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG("SSL KEY password is SET"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
}; |
|
|
|
} else { |
|
|
|
if (m_pSettings->auth_by_cred) { |
|
|
|
sprintf((char *)tValue.c_str(), "SASL"); |
|
|
|
if( tGlobalConf->set( "security.protocol", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG("SEC Proto is SASL"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
sprintf((char *)tValue.c_str(), "PLAIN"); |
|
|
|
if( tGlobalConf->set( "sasl.mechanisms", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG("SASL mechanism is PLAIN"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
sprintf((char *)tValue.c_str(), "%c", m_pSettings->cred_username); |
|
|
|
if( tGlobalConf->set( "sasl.username", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG("SASL username is SET"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
sprintf((char *)tValue.c_str(), "%c", m_pSettings->cred_password); |
|
|
|
if( tGlobalConf->set( "sasl.password", tValue, tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG("SASL password is SET"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
} else { |
|
|
|
LOG("No Auth Settings!"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
}; |
|
|
|
/* Set the delivery report callback.
|
|
|
|
* This callback will be called once per message to inform |
|
|
|
* the application if delivery succeeded or failed. |
|
|
|
@ -287,10 +432,11 @@ bool protocolKAFKA::init(){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
LOG("dr_cb is SET"); |
|
|
|
} else { |
|
|
|
LOG("dr_cb is SET"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
//if( tProducerConf->set("default_topic_conf", tTopicConf, tBuffer ) != RdKafka::Conf::CONF_OK ){
|
|
|
|
// LOG_ERROR( tBuffer.c_str() );
|
|
|
|
// delete( tProducerConf );
|
|
|
|
@ -312,19 +458,21 @@ bool protocolKAFKA::init(){ |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
LOG("RdKafka::Producer Created"); |
|
|
|
#endif
|
|
|
|
} else { |
|
|
|
LOG("RdKafka::Producer Created"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
if( tGlobalConf->set( "enable.partition.eof", "true", tBuffer ) != RdKafka::Conf::CONF_OK ){ |
|
|
|
delete( n_pProducer ); |
|
|
|
delete( tGlobalConf ); |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
return false; |
|
|
|
}; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
LOG("enable.partition.eof is SET"); |
|
|
|
} else { |
|
|
|
LOG("enable.partition.eof is SET"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
m_pConsumer = RdKafka::Consumer::create( tGlobalConf, tBuffer ); |
|
|
|
if( m_pConsumer == NULL ){ |
|
|
|
delete( n_pProducer ); |
|
|
|
@ -332,9 +480,12 @@ bool protocolKAFKA::init(){ |
|
|
|
LOG_ERROR( "Failed to create consumer:"); |
|
|
|
LOG_ERROR( tBuffer.c_str() ); |
|
|
|
return false; |
|
|
|
} |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
LOG("RdKafka::Consumer Created"); |
|
|
|
} else { |
|
|
|
LOG("RdKafka::Consumer Created"); |
|
|
|
#endif
|
|
|
|
}; |
|
|
|
#ifdef DEBUG_KAFKA
|
|
|
|
LOG("INIT Complete"); |
|
|
|
#endif
|
|
|
|
delete( tGlobalConf ); |
|
|
|
|