You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

890 lines
25 KiB

#include "protocolKAFKA.h"
#include "../../alg/generic/md5.h"
#include "../json/json_tool.h"
#include <json/reader.h>
#include <json/value.h>
#include <json/writer.h>
#include <strings.h>
// https://il.iherb.com/pr/KAL-Blood-Sugar-Defense-60-Tablets/70138
#define LOG(X) m_pLog->Log("KAFKA", this, X)
#define LOGS(X) basicLogCollection::getInstance()->Log("KAFKA_S", NULL, X)
#define LOG_ERROR(X) m_pLog->Log("KAFKA", this, _LOG_ERROR, X)
/*
CSTR protocolKAFKA::buildURL( CSTR templ, int dim, CSTR devID ){
CSTR RetVal;
RetVal = (CSTR) malloc( 256 );
if (RetVal != NULL) {
if (m_bUseSSL) {
sprintf(RetVal, "https://%s:%d%s", m_pServerIP, m_nServerPort, m_pAccessURL);
} else {
sprintf(RetVal, "http://%s:%d%s", m_pServerIP, m_nServerPort, m_pAccessURL);
};
};
return RetVal;
};
*/
/* Default Constructor */
/*
protocolKAFKA::protocolKAFKA( config_setting_t *Settings ){
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "INFOSTRESS_CONNECTION");
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;
memset( m_pScannerName, 0 , OS_SCANNER_NAME_SIZE);
m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE);
if( m_pServerIP == NULL ) return;
memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE);
m_pAccessURL = (CSTR)malloc(CTEXT);
if (m_pAccessURL == NULL) return;
memset(m_pAccessURL, 0, CTEXT);
m_bCurrentStatus = false;
m_bMitigationStatus = false;
m_bEnabled = false;
m_bEnableStatus = false;
if( m_pSettings != NULL ){
int intVar;
double floatVar;
config_setting_t *pSetting;
if (!config_setting_lookup_float(m_pSettings, "LOCATION_LON", &floatVar)) {
m_fLon = floatVar = 35.0938962;
pSetting = config_setting_add(m_pSettings, "LOCATION_LON", CONFIG_TYPE_FLOAT);
config_setting_set_float(pSetting, floatVar);
basicSettingsProvider::getInstance()->compromise();
} else {
m_fLon = floatVar;
};
if (!config_setting_lookup_float(m_pSettings, "LOCATION_LAT", &floatVar)) {
m_fLat = floatVar = 32.904208;
pSetting = config_setting_add(m_pSettings, "LOCATION_LAT", CONFIG_TYPE_FLOAT);
config_setting_set_float(pSetting, floatVar);
basicSettingsProvider::getInstance()->compromise();
} else {
m_fLat = floatVar;
};
if (!config_setting_lookup_bool(m_pSettings, "USE_SSL", &intVar)) {
intVar = m_bUseSSL = false;
LOG("SSL DISABLED[default]");
pSetting = config_setting_add(m_pSettings, "USE_SSL", CONFIG_TYPE_BOOL);
config_setting_set_bool(pSetting, intVar);
basicSettingsProvider::getInstance()->compromise();
} else {
m_bUseSSL = (bool)intVar;
};
if (!config_setting_lookup_bool(m_pSettings, "ENABLE_STATUS_REPORT", &intVar)) {
intVar = m_bEnableStatus = false;
LOG("Status Report DISABLED[default]");
pSetting = config_setting_add(m_pSettings, "ENABLE_STATUS_REPORT", CONFIG_TYPE_BOOL);
config_setting_set_bool(pSetting, intVar);
basicSettingsProvider::getInstance()->compromise();
} else {
m_bEnableStatus = (bool)intVar;
};
if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)) {
strncpy(m_pScannerName, "LRF_2D_001", 64);
LOG(m_pScannerName);
pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING);
config_setting_set_string(pSetting, m_pScannerName);
basicSettingsProvider::getInstance()->compromise();
};
if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions)) {
m_nScannerDimentions = 2;
pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT);
config_setting_set_int(pSetting, m_nScannerDimentions);
basicSettingsProvider::getInstance()->compromise();
};
if (!config_setting_lookup_string(m_pSettings, "KAFKA_BROKER_IP", (const char**)&m_pServerIP)) {
strncpy(m_pServerIP, "192.168.200.204", 16);
LOG(m_pServerIP);
pSetting = config_setting_add(m_pSettings, "KAFKA_BROKER_IP", CONFIG_TYPE_STRING);
config_setting_set_string(pSetting, m_pServerIP);
basicSettingsProvider::getInstance()->compromise();
};
LOG(m_pServerIP);
if (!config_setting_lookup_int(m_pSettings, "KAFKA_BROKER_PORT", &m_nServerPort)) {
m_nServerPort = 80;
pSetting = config_setting_add(m_pSettings, "KAFKA_BROKER_PORT", CONFIG_TYPE_INT);
config_setting_set_int(pSetting, m_nServerPort);
basicSettingsProvider::getInstance()->compromise();
};
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, n_pProducerTopicSTR);
basicSettingsProvider::getInstance()->compromise();
};
LOG( n_pProducerTopicSTR );
basicSettingsProvider::getInstance()->validate();
};
}
;*/
protocolKAFKA::protocolKAFKA(KAFKA_SETTINGS * Settings)
{
m_pLog = basicLogCollection::getInstance();
n_pProducer = NULL;
m_pConsumer = NULL;
// 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 );
};
}
// 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.
*
* This callback is called exactly once per message, indicating if
* the message was succesfully delivered
* (rkmessage->err == RD_KAFKA_RESP_ERR_NO_ERROR) or permanently
* failed delivery (rkmessage->err != RD_KAFKA_RESP_ERR_NO_ERROR).
*
* The callback is triggered from rd_kafka_poll() and executes on
* the application's thread.
*/
/*
static void dr_msg_cb( rd_kafka_t *rk, const rd_kafka_message_t *rkmessage, void *opaque ){
if (rkmessage->err)
fprintf(stderr, "%% Message delivery failed: %s\n",
rd_kafka_err2str(rkmessage->err));
else
fprintf(stderr,
"%% Message delivered (%zd bytes, "
"partition %"PRId32")\n",
rkmessage->len, rkmessage->partition);
// The rkmessage is destroyed automatically by librdkafka
};
*/
protocolKAFKA::~protocolKAFKA()
{
// m_bEnabled = false;
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..");
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 (n_pProducer->outq_len() > 0)
{
LOG_ERROR("Some messages are not delivered!");
}
delete n_pProducer;
n_pProducer = NULL;
// if( n_pProducerTopic!= NULL ) delete n_pProducerTopic;
};
// The consumer handle (with its librdkafka threads and buffers) was never released
if (m_pConsumer != NULL)
{
delete m_pConsumer;
m_pConsumer = NULL;
};
// if( m_pScannerName != NULL ) free(m_pScannerName);
// if( m_pServerIP != NULL ) free(m_pServerIP);
// if( m_pAccessURL != NULL ) free(m_pAccessURL);
}
// https://github.com/edenhill/librdkafka/blob/master/examples/producer.c
// https://github.com/edenhill/librdkafka/blob/master/examples/producer.cpp
void protocolKAFKA::dr_cb(RdKafka::Message & message)
{
/* If message.err() is non-zero the message delivery failed permanently
* for the message. */
char tBuffer[CTEXT];
if (message.err())
{
sprintf(tBuffer, "Message delivery failed: %s", message.errstr().c_str());
LOG_ERROR(tBuffer);
}
#ifdef DEBUG_KAFKA
else
{
sprintf(tBuffer, "Message delivered to topic: %s [%d] at offset %ld", message.topic_name().c_str(), message.partition(), message.offset());
LOG(tBuffer);
}
#endif
;
}
bool protocolKAFKA::init()
{
RdKafka::Conf * tGlobalConf;
char zBuffer[CTEXT];
std::string tBuffer;
std::string tValue;
if (m_pSettings == NULL)
{
return false;
}
tValue.reserve(256);
#ifdef DEBUG_KAFKA
LOG("INIT Begin");
#endif
tGlobalConf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
if (tGlobalConf == NULL)
{
LOG_ERROR("Eror Creating Producer Conf!");
return false;
};
// ToDo: Checking in case of SSL -> if library is build with SSL support!!
#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 (strlen(m_pSettings->brocker_addr) == 0)
{
LOG_ERROR("No Kafka Brokers are defined!");
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
}
else
{
LOG("bootstrap.servers is set");
#endif
};
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
}
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(), "%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
};
#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.
* See dr_msg_cb() above.
* The callback is only triggered from rd_kafka_poll() and
* rd_kafka_flush(). */
// tProducerConf->set("queue.buffering.max.ms", "100", tBuffer );
// tProducerConf->set("socket.blocking.max.ms", "100", tBuffer );
if (tGlobalConf->set("dr_cb", (RdKafka::DeliveryReportCb *) this, tBuffer) != RdKafka::Conf::CONF_OK)
{
LOG_ERROR(tBuffer.c_str());
delete(tGlobalConf);
return false;
#ifdef DEBUG_KAFKA
}
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 );
// delete( tTopicConf );
// tProducerConf = NULL;
// tTopicConf = NULL;
// return false;
// };
// LOG("default_topic_conf is SET");
/*
* Create producer instance.
*
* NOTE: rd_kafka_new() takes ownership of the conf object
* and the application must not reference it again after
* this call.
*/
n_pProducer = RdKafka::Producer::create(tGlobalConf, tBuffer);
if (n_pProducer == NULL)
{
LOG_ERROR(tBuffer.c_str());
delete(tGlobalConf);
return false;
#ifdef DEBUG_KAFKA
}
else
{
LOG("RdKafka::Producer Created");
#endif
};
if (tGlobalConf->set("enable.partition.eof", "true", tBuffer) != RdKafka::Conf::CONF_OK)
{
delete(n_pProducer);
n_pProducer = NULL; // destructor would flush/delete it again
delete(tGlobalConf);
LOG_ERROR(tBuffer.c_str());
return false;
#ifdef DEBUG_KAFKA
}
else
{
LOG("enable.partition.eof is SET");
#endif
};
m_pConsumer = RdKafka::Consumer::create(tGlobalConf, tBuffer);
if (m_pConsumer == NULL)
{
delete(n_pProducer);
n_pProducer = NULL; // destructor would flush/delete it again
delete(tGlobalConf);
LOG_ERROR("Failed to create consumer:");
LOG_ERROR(tBuffer.c_str());
return false;
#ifdef DEBUG_KAFKA
}
else
{
LOG("RdKafka::Consumer Created");
#endif
};
#ifdef DEBUG_KAFKA
LOG("INIT Complete");
#endif
delete(tGlobalConf);
return true;
}
bool protocolKAFKA::issuePost(CSTR tpc, 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 brokers = m_pKafkaBrokerSTR;
std::string topic = tpc;
if (n_pProducer == NULL)
{
return false;
}
tTopicConf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
if (tTopicConf == NULL)
{
LOG_ERROR("Eror Creating Topic Conf!");
return false;
};
#ifdef DEBUG_KAFKA
LOG("RdKafka::Conf::CONF_TOPIC Created");
#endif
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());
delete(tTopicConf);
return false;
};
#ifdef DEBUG_KAFKA
LOG("RdKafka::Topic Created");
#endif
/*
if( tProducerConf->set( "default_topic_conf", tTopicConf, tBuffer ) != RdKafka::Conf::CONF_OK ){
LOG_ERROR( tBuffer.c_str() );
delete( tTopicConf );
tProducerConf = NULL;
tTopicConf = NULL;
return false;
};
#ifdef DEBUG
LOG("default_topic_conf is SET");
#endif
//*/
retry:
err = n_pProducer->produce(tKafkaTopic, RdKafka::Topic::PARTITION_UA, RdKafka::Producer::RK_MSG_COPY, (void *) Buffer, BufferSize, NULL, NULL);
if (err != RdKafka::ERR_NO_ERROR)
{
/// std::cerr << "% Failed to produce to topic " << topic << ": " << RdKafka::err2str(err) << std::endl;
sprintf(tBuf, "Failed to produce to topic[%s]: %s", topic.c_str(), RdKafka::err2str(err).c_str());
LOG_ERROR(tBuf);
if (err == RdKafka::ERR__QUEUE_FULL)
{
LOG_ERROR("Internal queue is full!");
/* If the internal queue is full, wait for
* messages to be delivered and then retry.
* The internal queue represents both
* messages to be sent and messages that have
* been sent or failed, awaiting their
* delivery report callback to be called.
*
* The internal queue is limited by the
* configuration property
* queue.buffering.max.messages */
n_pProducer->poll(1000 /*block for max 1000ms*/);
if (rCnt--)
{
goto retry;
}
};
}
#ifdef DEBUG_KAFKA
else
{
// MOCK
FILE * fd;
fd = fopen("KAFKA_M.txt", "a+");
if (fd)
{
fprintf(fd, "\n*****************\n");
fprintf(fd, Buffer);
fprintf(fd, "\n*****************\nEnqueued message (%d bytes) for topic %s\n", BufferSize, topic.c_str());
fclose(fd);
};
sprintf(tBuf, "Enqueued message (%d bytes) for topic %s", BufferSize, topic.c_str());
LOG(tBuf);
}
#endif
;
/* A producer application should continually serve
* the delivery report queue by calling poll()
* at frequent intervals.
* Either put the poll call in your main loop, or in a
* dedicated thread, or call it after every produce() call.
* Just make sure that poll() is still called
* during periods where you are not producing any messages
* to make sure previously produced messages have their
* delivery report callback served (and any other callbacks
* you register). */
n_pProducer->poll(100);
// uint8_t cnt = 100;
// while ( cnt && n_pProducer->outq_len() > 0) {
// LOG("Wait ...");
// cnt--;
// n_pProducer->poll(100);
// };
delete(tKafkaTopic);
delete(tTopicConf);
tKafkaTopic = NULL;
tTopicConf = NULL;
return true;
}
void protocolKAFKA::consume_cb(RdKafka::Message & A, void * B)
{
if((A.err() == RdKafka::ERR_NO_ERROR ) || ( B != NULL )) LOG("CB Called!");
/*
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 );
};
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() );
}; */
}
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;
BufferSize = 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_KAFKA
LOG("Consumer RdKafka::Conf::CONF_TOPIC Created");
#endif
tKafkaTopic = RdKafka::Topic::create(m_pConsumer, topic, tTopicConf, tBuffer);
if (tKafkaTopic == NULL)
{
LOG_ERROR("Failed to create Consumer topic!");
delete(tTopicConf);
return false;
};
#ifdef DEBUG_KAFKA
LOG("Consumer RdKafka::Topic Created");
#endif
/*
* Start consumer for topic+partition at start offset
*/
// Get To Last Message
if (Offset == 0)
{
LOG("Correcting Offset");
int64_t bOff = m_pConsumer->OffsetTail(Offset);
Offset += bOff - 1;
};
RdKafka::ErrorCode resp = m_pConsumer->start(tKafkaTopic, Partition, Offset); // 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)
{
LOG_ERROR("FAILURE");
delete(tKafkaTopic);
delete(tTopicConf);
return false;
}
else
{
switch (message->err())
{
case RdKafka::ERR__TIMED_OUT: LOG("TIME OUT"); break;
case RdKafka::ERR_NO_ERROR:
/* Real message */
Offset = message->offset();
#ifdef DEBUG_KAFKA
sprintf(tBuf, "Read msg at offset %ld ", Offset);
LOG(tBuf);
if (message->key())
{
sprintf(tBuf, "Key: %c ", *message->key()->c_str());
LOG(tBuf);
};
#endif
// printf("%.*s\n", static_cast<int>(message->len()), static_cast<const char *>(message->payload()));
BufferSize = message->len();
memcpy(Buffer, message->payload(), BufferSize);
Buffer[BufferSize] = (char) 0;
break;
case RdKafka::ERR__PARTITION_EOF:
#ifdef DEBUG_KAFKA
sprintf(tBuf, "EOF Read msg at partition %d and offset %ld ", Partition, Offset);
LOG(tBuf);
#endif
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, 0);
m_pConsumer->poll(1000);
delete(tKafkaTopic);
delete(tTopicConf);
return true;
}
void protocolKAFKA::event_cb(RdKafka::Event & event)
{
if( event.type() == RdKafka::Event::EVENT_ERROR ) LOG("Evt ERROR!");
/* 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