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.

621 lines
23 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolKAFKA.h"
  2. #include "../../alg/generic/md5.h"
  3. #include "../json/json_tool.h"
  4. #include <strings.h>
  5. #include <json/value.h>
  6. #include <json/writer.h>
  7. #include <json/reader.h>
  8. //https://il.iherb.com/pr/KAL-Blood-Sugar-Defense-60-Tablets/70138
  9. #define LOG( X ) m_pLog->Log( "KAFKA", this, X )
  10. #define LOGS( X ) basicLogCollection::getInstance()->Log( "KAFKA_S", NULL, X )
  11. #define LOG_ERROR( X ) m_pLog->Log( "KAFKA", this, _LOG_ERROR, X )
  12. /*
  13. CSTR protocolKAFKA::buildURL( CSTR templ, int dim, CSTR devID ){
  14. CSTR RetVal;
  15. RetVal = (CSTR) malloc( 256 );
  16. if (RetVal != NULL) {
  17. if (m_bUseSSL) {
  18. sprintf(RetVal, "https://%s:%d%s", m_pServerIP, m_nServerPort, m_pAccessURL);
  19. } else {
  20. sprintf(RetVal, "http://%s:%d%s", m_pServerIP, m_nServerPort, m_pAccessURL);
  21. };
  22. };
  23. return RetVal;
  24. };
  25. */
  26. /* Default Constructor */
  27. /*
  28. protocolKAFKA::protocolKAFKA( config_setting_t *Settings ){
  29. m_pLog = basicLogCollection::getInstance();
  30. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "INFOSTRESS_CONNECTION");
  31. n_pProducer = NULL;
  32. n_pProducerBrokerSTR = (CSTR)malloc(CLOG);
  33. if( n_pProducerBrokerSTR == NULL ) return;
  34. memset( n_pProducerBrokerSTR, 0 , CLOG);
  35. n_pProducerTopicSTR= (CSTR)malloc(CLOG);
  36. if( n_pProducerTopicSTR == NULL ) return;
  37. memset( n_pProducerTopicSTR, 0 , CLOG);
  38. n_pProducerTopic = NULL;
  39. m_pScannerName = (CSTR)malloc(OS_SCANNER_NAME_SIZE);
  40. if( m_pScannerName == NULL ) return;
  41. memset( m_pScannerName, 0 , OS_SCANNER_NAME_SIZE);
  42. m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE);
  43. if( m_pServerIP == NULL ) return;
  44. memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE);
  45. m_pAccessURL = (CSTR)malloc(CTEXT);
  46. if (m_pAccessURL == NULL) return;
  47. memset(m_pAccessURL, 0, CTEXT);
  48. m_bCurrentStatus = false;
  49. m_bMitigationStatus = false;
  50. m_bEnabled = false;
  51. m_bEnableStatus = false;
  52. if( m_pSettings != NULL ){
  53. int intVar;
  54. double floatVar;
  55. config_setting_t *pSetting;
  56. if (!config_setting_lookup_float(m_pSettings, "LOCATION_LON", &floatVar)) {
  57. m_fLon = floatVar = 35.0938962;
  58. pSetting = config_setting_add(m_pSettings, "LOCATION_LON", CONFIG_TYPE_FLOAT);
  59. config_setting_set_float(pSetting, floatVar);
  60. basicSettingsProvider::getInstance()->compromise();
  61. } else {
  62. m_fLon = floatVar;
  63. };
  64. if (!config_setting_lookup_float(m_pSettings, "LOCATION_LAT", &floatVar)) {
  65. m_fLat = floatVar = 32.904208;
  66. pSetting = config_setting_add(m_pSettings, "LOCATION_LAT", CONFIG_TYPE_FLOAT);
  67. config_setting_set_float(pSetting, floatVar);
  68. basicSettingsProvider::getInstance()->compromise();
  69. } else {
  70. m_fLat = floatVar;
  71. };
  72. if (!config_setting_lookup_bool(m_pSettings, "USE_SSL", &intVar)) {
  73. intVar = m_bUseSSL = false;
  74. LOG("SSL DISABLED[default]");
  75. pSetting = config_setting_add(m_pSettings, "USE_SSL", CONFIG_TYPE_BOOL);
  76. config_setting_set_bool(pSetting, intVar);
  77. basicSettingsProvider::getInstance()->compromise();
  78. } else {
  79. m_bUseSSL = (bool)intVar;
  80. };
  81. if (!config_setting_lookup_bool(m_pSettings, "ENABLE_STATUS_REPORT", &intVar)) {
  82. intVar = m_bEnableStatus = false;
  83. LOG("Status Report DISABLED[default]");
  84. pSetting = config_setting_add(m_pSettings, "ENABLE_STATUS_REPORT", CONFIG_TYPE_BOOL);
  85. config_setting_set_bool(pSetting, intVar);
  86. basicSettingsProvider::getInstance()->compromise();
  87. } else {
  88. m_bEnableStatus = (bool)intVar;
  89. };
  90. if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)) {
  91. strncpy(m_pScannerName, "LRF_2D_001", 64);
  92. LOG(m_pScannerName);
  93. pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING);
  94. config_setting_set_string(pSetting, m_pScannerName);
  95. basicSettingsProvider::getInstance()->compromise();
  96. };
  97. if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions)) {
  98. m_nScannerDimentions = 2;
  99. pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT);
  100. config_setting_set_int(pSetting, m_nScannerDimentions);
  101. basicSettingsProvider::getInstance()->compromise();
  102. };
  103. if (!config_setting_lookup_string(m_pSettings, "KAFKA_BROKER_IP", (const char**)&m_pServerIP)) {
  104. strncpy(m_pServerIP, "192.168.200.204", 16);
  105. LOG(m_pServerIP);
  106. pSetting = config_setting_add(m_pSettings, "KAFKA_BROKER_IP", CONFIG_TYPE_STRING);
  107. config_setting_set_string(pSetting, m_pServerIP);
  108. basicSettingsProvider::getInstance()->compromise();
  109. };
  110. LOG(m_pServerIP);
  111. if (!config_setting_lookup_int(m_pSettings, "KAFKA_BROKER_PORT", &m_nServerPort)) {
  112. m_nServerPort = 80;
  113. pSetting = config_setting_add(m_pSettings, "KAFKA_BROKER_PORT", CONFIG_TYPE_INT);
  114. config_setting_set_int(pSetting, m_nServerPort);
  115. basicSettingsProvider::getInstance()->compromise();
  116. };
  117. sprintf( n_pProducerBrokerSTR, "%s:%d", m_pServerIP, m_nServerPort );
  118. LOG( n_pProducerBrokerSTR );
  119. if (!config_setting_lookup_string(m_pSettings, "KAFKA_TOPIC", (const char**)&n_pProducerTopicSTR)) {
  120. strncpy( n_pProducerTopicSTR, DFSL_INFOSTRESS_DEFAULT_KAFKA_TOPIC, CTEXT );
  121. pSetting = config_setting_add(m_pSettings, "KAFKA_TOPIC", CONFIG_TYPE_STRING);
  122. config_setting_set_string(pSetting, n_pProducerTopicSTR);
  123. basicSettingsProvider::getInstance()->compromise();
  124. };
  125. LOG( n_pProducerTopicSTR );
  126. basicSettingsProvider::getInstance()->validate();
  127. };
  128. }
  129. ;*/
  130. protocolKAFKA::protocolKAFKA( KAFKA_SETTINGS *Settings ){
  131. m_pLog = basicLogCollection::getInstance();
  132. n_pProducer = NULL;
  133. m_pServerIP = NULL;
  134. memset( m_pKafkaBrokerSTR, 0 , sizeof( m_pKafkaBrokerSTR ));
  135. if( Settings!= NULL ) {
  136. m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE);
  137. if( m_pServerIP == NULL ) return;
  138. memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE);
  139. strncpy( m_pServerIP, Settings->brocker_addr, OS_CEP_ADDRESS_SIZE );
  140. m_nServerPort = Settings->brocker_port;
  141. sprintf( m_pKafkaBrokerSTR, "%s:%d", m_pServerIP, m_nServerPort );
  142. };
  143. };
  144. protocolKAFKA::protocolKAFKA( CSTR ServerAddress, uint16_t Port ){
  145. m_pLog = basicLogCollection::getInstance();
  146. n_pProducer = NULL;
  147. m_pServerIP = NULL;
  148. if( ServerAddress == 0 || Port == 0 ) return;
  149. m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE);
  150. if( m_pServerIP == NULL ) return;
  151. memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE);
  152. memset( m_pKafkaBrokerSTR, 0 , sizeof( m_pKafkaBrokerSTR ));
  153. strncpy( m_pServerIP, ServerAddress, OS_CEP_ADDRESS_SIZE );
  154. m_nServerPort = Port;
  155. sprintf( m_pKafkaBrokerSTR, "%s:%d", m_pServerIP, m_nServerPort );
  156. };
  157. /**
  158. * @brief Message delivery report callback.
  159. *
  160. * This callback is called exactly once per message, indicating if
  161. * the message was succesfully delivered
  162. * (rkmessage->err == RD_KAFKA_RESP_ERR_NO_ERROR) or permanently
  163. * failed delivery (rkmessage->err != RD_KAFKA_RESP_ERR_NO_ERROR).
  164. *
  165. * The callback is triggered from rd_kafka_poll() and executes on
  166. * the application's thread.
  167. */
  168. /*
  169. static void dr_msg_cb( rd_kafka_t *rk, const rd_kafka_message_t *rkmessage, void *opaque ){
  170. if (rkmessage->err)
  171. fprintf(stderr, "%% Message delivery failed: %s\n",
  172. rd_kafka_err2str(rkmessage->err));
  173. else
  174. fprintf(stderr,
  175. "%% Message delivered (%zd bytes, "
  176. "partition %"PRId32")\n",
  177. rkmessage->len, rkmessage->partition);
  178. // The rkmessage is destroyed automatically by librdkafka
  179. };
  180. */
  181. protocolKAFKA::~protocolKAFKA(){
  182. //m_bEnabled = false;
  183. if( n_pProducer != NULL ){
  184. /* Wait for final messages to be delivered or fail.
  185. * rd_kafka_flush() is an abstraction over rd_kafka_poll() which
  186. * waits for all messages to be delivered. */
  187. LOG("Flushing final messages..");
  188. n_pProducer->flush( 3000 /* wait for max 3 seconds */);
  189. /* If the output queue is still not empty there is an issue
  190. * with producing messages to the clusters. */
  191. if (n_pProducer->outq_len() > 0) LOG_ERROR("Some messages are not delivered!");
  192. delete n_pProducer;
  193. //if( n_pProducerTopic!= NULL ) delete n_pProducerTopic;
  194. };
  195. //if( m_pScannerName != NULL ) free(m_pScannerName);
  196. if( m_pServerIP != NULL ) free(m_pServerIP);
  197. //if( m_pAccessURL != NULL ) free(m_pAccessURL);
  198. };
  199. //https://github.com/edenhill/librdkafka/blob/master/examples/producer.c
  200. //https://github.com/edenhill/librdkafka/blob/master/examples/producer.cpp
  201. void protocolKAFKA::dr_cb( RdKafka::Message &message ){
  202. /* If message.err() is non-zero the message delivery failed permanently
  203. * for the message. */
  204. char tBuffer[CTEXT];
  205. if( message.err() ){
  206. sprintf(tBuffer,"Message delivery failed: %s", message.errstr().c_str() );
  207. LOG_ERROR( tBuffer );
  208. }
  209. #ifdef DEBUG_KAFKA
  210. else {
  211. sprintf(tBuffer,"Message delivered to topic: %s [%d] at offset %d", message.topic_name().c_str(), message.partition(), message.offset() );
  212. LOG( tBuffer );
  213. }
  214. #endif
  215. ;
  216. };
  217. bool protocolKAFKA::init(){
  218. RdKafka::Conf * tGlobalConf;
  219. std::string tBuffer;
  220. std::string brokers = m_pKafkaBrokerSTR;
  221. #ifdef DEBUG_KAFKA
  222. LOG("INIT Begin");
  223. #endif
  224. if( strlen( m_pKafkaBrokerSTR ) == 0 ) {
  225. LOG_ERROR("No Kafka Brokers are defined!");
  226. return false;
  227. };
  228. tGlobalConf = RdKafka::Conf::create( RdKafka::Conf::CONF_GLOBAL );
  229. if( tGlobalConf == NULL ) {
  230. LOG_ERROR("Eror Creating Producer Conf!");
  231. return false;
  232. };
  233. #ifdef DEBUG_KAFKA
  234. LOG("RdKafka::Conf::CONF_GLOBAL Created");
  235. #endif
  236. if( tGlobalConf->set( "bootstrap.servers", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){
  237. LOG_ERROR( tBuffer.c_str() );
  238. delete( tGlobalConf );
  239. return false;
  240. };
  241. //std::string Kuser = "";
  242. //std::string Kpass = "";
  243. //if( tGlobalConf->set( "sasl.username", Kuser, tBuffer ) != RdKafka::Conf::CONF_OK ){
  244. // LOG_ERROR( tBuffer.c_str() );
  245. // delete( tGlobalConf );
  246. // return false;
  247. //};
  248. //if( tGlobalConf->set( "sasl.password", Kuser, tBuffer ) != RdKafka::Conf::CONF_OK ){
  249. // LOG_ERROR( tBuffer.c_str() );
  250. // delete( tGlobalConf );
  251. // return false;
  252. //};
  253. #ifdef DEBUG_KAFKA
  254. LOG( m_pKafkaBrokerSTR );
  255. LOG("bootstrap.servers is SET");
  256. #endif
  257. if( tGlobalConf->set("metadata.broker.list", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){
  258. LOG_ERROR( tBuffer.c_str() );
  259. delete( tGlobalConf );
  260. return false;
  261. };
  262. #ifdef DEBUG_KAFKA
  263. LOG("metadata.broker.list is SET");
  264. #endif
  265. /* Set the delivery report callback.
  266. * This callback will be called once per message to inform
  267. * the application if delivery succeeded or failed.
  268. * See dr_msg_cb() above.
  269. * The callback is only triggered from rd_kafka_poll() and
  270. * rd_kafka_flush(). */
  271. //tProducerConf->set("queue.buffering.max.ms", "100", tBuffer );
  272. //tProducerConf->set("socket.blocking.max.ms", "100", tBuffer );
  273. if( tGlobalConf->set( "dr_cb", (RdKafka::DeliveryReportCb *) this, tBuffer ) != RdKafka::Conf::CONF_OK ){
  274. LOG_ERROR( tBuffer.c_str() );
  275. delete( tGlobalConf );
  276. return false;
  277. };
  278. #ifdef DEBUG_KAFKA
  279. LOG("dr_cb is SET");
  280. #endif
  281. //if( tProducerConf->set("default_topic_conf", tTopicConf, tBuffer ) != RdKafka::Conf::CONF_OK ){
  282. // LOG_ERROR( tBuffer.c_str() );
  283. // delete( tProducerConf );
  284. // delete( tTopicConf );
  285. // tProducerConf = NULL;
  286. // tTopicConf = NULL;
  287. // return false;
  288. //};
  289. //LOG("default_topic_conf is SET");
  290. /*
  291. * Create producer instance.
  292. *
  293. * NOTE: rd_kafka_new() takes ownership of the conf object
  294. * and the application must not reference it again after
  295. * this call.
  296. */
  297. n_pProducer = RdKafka::Producer::create( tGlobalConf, tBuffer );
  298. if( n_pProducer == NULL ){
  299. LOG_ERROR( tBuffer.c_str() );
  300. delete( tGlobalConf );
  301. return false;
  302. };
  303. #ifdef DEBUG_KAFKA
  304. LOG("RdKafka::Producer Created");
  305. #endif
  306. if( tGlobalConf->set( "enable.partition.eof", "true", tBuffer ) != RdKafka::Conf::CONF_OK ){
  307. delete( n_pProducer );
  308. delete( tGlobalConf );
  309. LOG_ERROR( tBuffer.c_str() );
  310. return false;
  311. };
  312. #ifdef DEBUG_KAFKA
  313. LOG("enable.partition.eof is SET");
  314. #endif
  315. m_pConsumer = RdKafka::Consumer::create( tGlobalConf, tBuffer );
  316. if( m_pConsumer == NULL ){
  317. delete( n_pProducer );
  318. delete( tGlobalConf );
  319. LOG_ERROR( "Failed to create consumer:");
  320. LOG_ERROR( tBuffer.c_str() );
  321. return false;
  322. }
  323. #ifdef DEBUG_KAFKA
  324. LOG("RdKafka::Consumer Created");
  325. LOG("INIT Complete");
  326. #endif
  327. delete( tGlobalConf );
  328. return true;
  329. };
  330. bool protocolKAFKA::issuePost( CSTR tpc, CSTR Buffer, int BufferSize ){
  331. uint8_t rCnt = 3;
  332. char tBuf[128];
  333. RdKafka::ErrorCode err;
  334. RdKafka::Conf * tTopicConf = NULL;
  335. RdKafka::Topic * tKafkaTopic = NULL;
  336. std::string tBuffer;
  337. //std::string brokers = m_pKafkaBrokerSTR;
  338. std::string topic = tpc;
  339. if( n_pProducer == NULL) return false;
  340. tTopicConf = RdKafka::Conf::create( RdKafka::Conf::CONF_TOPIC );
  341. if( tTopicConf==NULL ){
  342. LOG_ERROR("Eror Creating Topic Conf!");
  343. return false;
  344. };
  345. #ifdef DEBUG_KAFKA
  346. LOG("RdKafka::Conf::CONF_TOPIC Created");
  347. #endif
  348. tKafkaTopic = RdKafka::Topic::create( n_pProducer, topic, tTopicConf, tBuffer );
  349. if( tKafkaTopic == NULL ) {
  350. LOG_ERROR( "No Topic Created!!" );
  351. LOG_ERROR( tpc );
  352. LOG_ERROR( tBuffer.c_str() );
  353. delete( tTopicConf );
  354. return false;
  355. };
  356. #ifdef DEBUG_KAFKA
  357. LOG("RdKafka::Topic Created");
  358. #endif
  359. /*
  360. if( tProducerConf->set( "default_topic_conf", tTopicConf, tBuffer ) != RdKafka::Conf::CONF_OK ){
  361. LOG_ERROR( tBuffer.c_str() );
  362. delete( tTopicConf );
  363. tProducerConf = NULL;
  364. tTopicConf = NULL;
  365. return false;
  366. };
  367. #ifdef DEBUG
  368. LOG("default_topic_conf is SET");
  369. #endif
  370. //*/
  371. retry:
  372. err = n_pProducer->produce(
  373. tKafkaTopic,
  374. RdKafka::Topic::PARTITION_UA,
  375. RdKafka::Producer::RK_MSG_COPY,
  376. (void *)Buffer,
  377. BufferSize,
  378. NULL,
  379. NULL
  380. );
  381. if( err != RdKafka::ERR_NO_ERROR ) {
  382. ///std::cerr << "% Failed to produce to topic " << topic << ": " << RdKafka::err2str(err) << std::endl;
  383. sprintf( tBuf, "Failed to produce to topic[%s]: %s", topic.c_str(), RdKafka::err2str(err).c_str() );
  384. LOG_ERROR( tBuf );
  385. if( err == RdKafka::ERR__QUEUE_FULL ){
  386. LOG_ERROR("Internal queue is full!");
  387. /* If the internal queue is full, wait for
  388. * messages to be delivered and then retry.
  389. * The internal queue represents both
  390. * messages to be sent and messages that have
  391. * been sent or failed, awaiting their
  392. * delivery report callback to be called.
  393. *
  394. * The internal queue is limited by the
  395. * configuration property
  396. * queue.buffering.max.messages */
  397. n_pProducer->poll(1000/*block for max 1000ms*/);
  398. if( rCnt-- ) goto retry;
  399. };
  400. }
  401. #ifdef DEBUG_KAFKA
  402. else {
  403. //MOCK
  404. FILE *fd;
  405. fd = fopen("KAFKA_M.txt","a+");
  406. if ( fd ) {
  407. fprintf( fd, "\n*****************\n");
  408. fprintf( fd, Buffer );
  409. fprintf( fd, "\n*****************\nEnqueued message (%d bytes) for topic %s\n", BufferSize, topic.c_str());
  410. fclose( fd );
  411. };
  412. sprintf(tBuf, "Enqueued message (%d bytes) for topic %s", BufferSize, topic.c_str());
  413. LOG( tBuf );
  414. }
  415. #endif
  416. ;
  417. /* A producer application should continually serve
  418. * the delivery report queue by calling poll()
  419. * at frequent intervals.
  420. * Either put the poll call in your main loop, or in a
  421. * dedicated thread, or call it after every produce() call.
  422. * Just make sure that poll() is still called
  423. * during periods where you are not producing any messages
  424. * to make sure previously produced messages have their
  425. * delivery report callback served (and any other callbacks
  426. * you register). */
  427. n_pProducer->poll(100);
  428. //uint8_t cnt = 100;
  429. //while ( cnt && n_pProducer->outq_len() > 0) {
  430. // LOG("Wait ...");
  431. // cnt--;
  432. // n_pProducer->poll(100);
  433. //};
  434. delete( tKafkaTopic );
  435. delete( tTopicConf );
  436. tKafkaTopic = NULL;
  437. tTopicConf = NULL;
  438. return true;
  439. };
  440. void protocolKAFKA::consume_cb( RdKafka::Message& A, void* B ){
  441. char tBuff[256];
  442. /*
  443. switch (message->err()) {
  444. case RdKafka::ERR__TIMED_OUT: break;
  445. case RdKafka::ERR_NO_ERROR:
  446. // Real message
  447. sprintf( tBuff, "Read msg at offset %dd ", message->offset() );
  448. LOG( tBuff );
  449. if( message->key() ){
  450. sprintf( tBuff, "Key: %s ", *message->key()->c_str() );
  451. LOG( tBuff );
  452. };
  453. //*
  454. RdKafka::Headers *headers = message->headers();
  455. if( headers ){
  456. std::vector<RdKafka::Headers::Header> hdrs = headers->get_all();
  457. for (size_t i = 0 ; i < hdrs.size() ; i++) {
  458. const RdKafka::Headers::Header hdr = hdrs[i];
  459. if (hdr.value() != NULL) {
  460. sprintf( tBuff, " Header: %s = \"%.*s\"\n", hdr.key().c_str(), (int)hdr.value_size(), (const char *)hdr.value());
  461. } else {
  462. sprintf( tBuff, " Header: %s = NULL\n", hdr.key().c_str());
  463. };
  464. LOG( tBuff );
  465. };
  466. };
  467. //* /
  468. //printf("%.*s\n", static_cast<int>(message->len()), static_cast<const char *>(message->payload()));
  469. break;
  470. case RdKafka::ERR__PARTITION_EOF:
  471. // Last message
  472. //if (exit_eof) {
  473. //}
  474. break;
  475. case RdKafka::ERR__UNKNOWN_TOPIC:
  476. case RdKafka::ERR__UNKNOWN_PARTITION:
  477. default:
  478. LOG_ERROR( "Consume failed!" );
  479. LOG_ERROR( message->errstr().c_str() );
  480. }; */
  481. };
  482. bool protocolKAFKA::receivePost( CSTR Channel, int32_t Partition, int64_t &Offset, CSTR Buffer, int &BufferSize ){
  483. uint8_t rCnt = 3;
  484. char tBuf[128];
  485. RdKafka::ErrorCode err;
  486. RdKafka::Conf * tTopicConf = NULL;
  487. RdKafka::Topic * tKafkaTopic = NULL;
  488. std::string tBuffer;
  489. std::string topic = Channel;
  490. BufferSize = 0;
  491. if( m_pConsumer == NULL) return false;
  492. tTopicConf = RdKafka::Conf::create( RdKafka::Conf::CONF_TOPIC );
  493. if( tTopicConf==NULL ){
  494. LOG_ERROR("Eror Creating Consumer Topic Conf!");
  495. return false;
  496. };
  497. #ifdef DEBUG_KAFKA
  498. LOG("Consumer RdKafka::Conf::CONF_TOPIC Created");
  499. #endif
  500. tKafkaTopic = RdKafka::Topic::create( m_pConsumer, topic, tTopicConf, tBuffer );
  501. if( tKafkaTopic == NULL ) {
  502. LOG_ERROR( "Failed to create Consumer topic!");
  503. delete( tTopicConf );
  504. return false;
  505. };
  506. #ifdef DEBUG_KAFKA
  507. LOG("Consumer RdKafka::Topic Created");
  508. #endif
  509. /*
  510. * Start consumer for topic+partition at start offset
  511. */
  512. //Get To Last Message
  513. if( Offset == 0 ) {
  514. LOG("Correcting Offset");
  515. int64_t bOff = m_pConsumer->OffsetTail( Offset );
  516. Offset += bOff - 1;
  517. };
  518. RdKafka::ErrorCode resp = m_pConsumer->start( tKafkaTopic, Partition, Offset );//Partition, Offset );
  519. if (resp != RdKafka::ERR_NO_ERROR) {
  520. LOG_ERROR( "Failed to start consumer!" );
  521. LOG_ERROR( RdKafka::err2str(resp).c_str() );
  522. delete( tKafkaTopic );
  523. delete( tTopicConf );
  524. return false;
  525. };
  526. RdKafka::Message *message = m_pConsumer->consume( tKafkaTopic, Partition, 1000 );
  527. if( message == NULL ) {
  528. LOG_ERROR( "FAILURE" );
  529. delete( tKafkaTopic );
  530. delete( tTopicConf );
  531. return false;
  532. } else {
  533. switch ( message->err() ) {
  534. case RdKafka::ERR__TIMED_OUT: LOG("TIME OUT"); break;
  535. case RdKafka::ERR_NO_ERROR:
  536. /* Real message */
  537. Offset = message->offset();
  538. #ifdef DEBUG_KAFKA
  539. sprintf( tBuf, "Read msg at offset %d ", Offset );
  540. LOG( tBuf );
  541. if( message->key() ){
  542. sprintf( tBuf, "Key: %s ", *message->key()->c_str() );
  543. LOG( tBuf );
  544. };
  545. #endif
  546. //printf("%.*s\n", static_cast<int>(message->len()), static_cast<const char *>(message->payload()));
  547. BufferSize = message->len();
  548. memcpy( Buffer, message->payload(), BufferSize );
  549. Buffer[BufferSize] = (char)0;
  550. break;
  551. case RdKafka::ERR__PARTITION_EOF:
  552. #ifdef DEBUG_KAFKA
  553. sprintf( tBuf, "EOF Read msg at partition %d and offset %d ", Partition, Offset );
  554. LOG( tBuf );
  555. #endif
  556. break;
  557. case RdKafka::ERR__UNKNOWN_TOPIC:
  558. case RdKafka::ERR__UNKNOWN_PARTITION:
  559. default:
  560. LOG_ERROR( "Consume failed!" );
  561. LOG_ERROR( message->errstr().c_str() );
  562. delete( message );
  563. delete( tKafkaTopic );
  564. delete( tTopicConf );
  565. return false;
  566. };
  567. }
  568. delete( message );
  569. m_pConsumer->stop( tKafkaTopic, 0 );
  570. m_pConsumer->poll( 1000 );
  571. delete( tKafkaTopic );
  572. delete( tTopicConf );
  573. return true;
  574. };
  575. void protocolKAFKA::event_cb( RdKafka::Event &event ){
  576. /* switch (event.type()){
  577. case RdKafka::Event::EVENT_ERROR:
  578. if (event.fatal()) {
  579. std::cerr << "FATAL ";
  580. run = 0;
  581. }
  582. std::cerr << "ERROR (" << RdKafka::err2str(event.err()) << "): " << event.str() << std::endl;
  583. break;
  584. case RdKafka::Event::EVENT_STATS:
  585. std::cerr << "\"STATS\": " << event.str() << std::endl;
  586. break;
  587. case RdKafka::Event::EVENT_LOG:
  588. fprintf(stderr, "LOG-%i-%s: %s\n", event.severity(), event.fac().c_str(), event.str().c_str());
  589. break;
  590. default:
  591. std::cerr << "EVENT " << event.type() << " (" << RdKafka::err2str(event.err()) << "): " << event.str() << std::endl;
  592. break;
  593. }; */
  594. };
  595. #undef LOG
  596. #undef LOG_ERROR