|
|
#include "protocolKAFKA.h"
#include "../../alg/generic/md5.h"
#include "../json/json_tool.h"
#include <strings.h>
#include <json/value.h>
#include <json/writer.h>
#include <json/reader.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_pServerIP = NULL; 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; //if( n_pProducerTopic!= NULL ) delete n_pProducerTopic;
}; //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
else { sprintf(tBuffer,"Message delivered to topic: %s [%d] at offset %d", message.topic_name().c_str(), message.partition(), message.offset() ); LOG( tBuffer ); }#endif
;};
bool protocolKAFKA::init(){ RdKafka::Conf * tGlobalConf; std::string tBuffer; std::string brokers = m_pKafkaBrokerSTR;
LOG("INIT Begin"); 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!"); return false; }; LOG("RdKafka::Conf::CONF_GLOBAL Created");
if( tGlobalConf->set( "bootstrap.servers", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){ LOG_ERROR( tBuffer.c_str() ); delete( tGlobalConf ); return false; }; LOG( m_pKafkaBrokerSTR ); LOG("bootstrap.servers is SET");
if( tGlobalConf->set("metadata.broker.list", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){ LOG_ERROR( tBuffer.c_str() ); delete( tGlobalConf ); return false; }; LOG("metadata.broker.list is SET"); /* 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; }; LOG("dr_cb is SET"); //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; }; LOG("RdKafka::Producer Created"); 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"); 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
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
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
else { 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 ){ 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
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; };
/*
* 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;};
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
|