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.

573 lines
22 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
  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. LOG("INIT Begin");
  222. if( strlen( m_pKafkaBrokerSTR ) == 0 ) {
  223. LOG_ERROR("No Kafka Brokers are defined!");
  224. return false;
  225. };
  226. tGlobalConf = RdKafka::Conf::create( RdKafka::Conf::CONF_GLOBAL );
  227. if( tGlobalConf == NULL ) {
  228. LOG_ERROR("Eror Creating Producer Conf!");
  229. return false;
  230. };
  231. LOG("RdKafka::Conf::CONF_GLOBAL Created");
  232. if( tGlobalConf->set( "bootstrap.servers", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){
  233. LOG_ERROR( tBuffer.c_str() );
  234. delete( tGlobalConf );
  235. return false;
  236. };
  237. LOG( m_pKafkaBrokerSTR );
  238. LOG("bootstrap.servers is SET");
  239. if( tGlobalConf->set("metadata.broker.list", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){
  240. LOG_ERROR( tBuffer.c_str() );
  241. delete( tGlobalConf );
  242. return false;
  243. };
  244. LOG("metadata.broker.list is SET");
  245. /* Set the delivery report callback.
  246. * This callback will be called once per message to inform
  247. * the application if delivery succeeded or failed.
  248. * See dr_msg_cb() above.
  249. * The callback is only triggered from rd_kafka_poll() and
  250. * rd_kafka_flush(). */
  251. //tProducerConf->set("queue.buffering.max.ms", "100", tBuffer );
  252. //tProducerConf->set("socket.blocking.max.ms", "100", tBuffer );
  253. if( tGlobalConf->set( "dr_cb", (RdKafka::DeliveryReportCb *) this, tBuffer ) != RdKafka::Conf::CONF_OK ){
  254. LOG_ERROR( tBuffer.c_str() );
  255. delete( tGlobalConf );
  256. return false;
  257. };
  258. LOG("dr_cb is SET");
  259. //if( tProducerConf->set("default_topic_conf", tTopicConf, tBuffer ) != RdKafka::Conf::CONF_OK ){
  260. // LOG_ERROR( tBuffer.c_str() );
  261. // delete( tProducerConf );
  262. // delete( tTopicConf );
  263. // tProducerConf = NULL;
  264. // tTopicConf = NULL;
  265. // return false;
  266. //};
  267. //LOG("default_topic_conf is SET");
  268. /*
  269. * Create producer instance.
  270. *
  271. * NOTE: rd_kafka_new() takes ownership of the conf object
  272. * and the application must not reference it again after
  273. * this call.
  274. */
  275. n_pProducer = RdKafka::Producer::create( tGlobalConf, tBuffer );
  276. if( n_pProducer == NULL ){
  277. LOG_ERROR( tBuffer.c_str() );
  278. delete( tGlobalConf );
  279. return false;
  280. };
  281. LOG("RdKafka::Producer Created");
  282. if( tGlobalConf->set( "enable.partition.eof", "true", tBuffer ) != RdKafka::Conf::CONF_OK ){
  283. delete( n_pProducer );
  284. delete( tGlobalConf );
  285. LOG_ERROR( tBuffer.c_str() );
  286. return false;
  287. };
  288. LOG("enable.partition.eof is SET");
  289. m_pConsumer = RdKafka::Consumer::create( tGlobalConf, tBuffer );
  290. if( m_pConsumer == NULL ){
  291. delete( n_pProducer );
  292. delete( tGlobalConf );
  293. LOG_ERROR( "Failed to create consumer:");
  294. LOG_ERROR( tBuffer.c_str() );
  295. return false;
  296. }
  297. LOG("RdKafka::Consumer Created");
  298. LOG("INIT Complete");
  299. delete( tGlobalConf );
  300. return true;
  301. };
  302. bool protocolKAFKA::issuePost( CSTR tpc, CSTR Buffer, int BufferSize ){
  303. uint8_t rCnt = 3;
  304. char tBuf[128];
  305. RdKafka::ErrorCode err;
  306. RdKafka::Conf * tTopicConf = NULL;
  307. RdKafka::Topic * tKafkaTopic = NULL;
  308. std::string tBuffer;
  309. //std::string brokers = m_pKafkaBrokerSTR;
  310. std::string topic = tpc;
  311. if( n_pProducer == NULL) return false;
  312. tTopicConf = RdKafka::Conf::create( RdKafka::Conf::CONF_TOPIC );
  313. if( tTopicConf==NULL ){
  314. LOG_ERROR("Eror Creating Topic Conf!");
  315. return false;
  316. };
  317. #ifdef DEBUG
  318. LOG("RdKafka::Conf::CONF_TOPIC Created");
  319. #endif
  320. tKafkaTopic = RdKafka::Topic::create( n_pProducer, topic, tTopicConf, tBuffer );
  321. if( tKafkaTopic == NULL ) {
  322. LOG_ERROR( "No Topic Created!!" );
  323. LOG_ERROR( tpc );
  324. LOG_ERROR( tBuffer.c_str() );
  325. delete( tTopicConf );
  326. return false;
  327. };
  328. #ifdef DEBUG
  329. LOG("RdKafka::Topic Created");
  330. #endif
  331. /*
  332. if( tProducerConf->set( "default_topic_conf", tTopicConf, tBuffer ) != RdKafka::Conf::CONF_OK ){
  333. LOG_ERROR( tBuffer.c_str() );
  334. delete( tTopicConf );
  335. tProducerConf = NULL;
  336. tTopicConf = NULL;
  337. return false;
  338. };
  339. #ifdef DEBUG
  340. LOG("default_topic_conf is SET");
  341. #endif
  342. //*/
  343. retry:
  344. err = n_pProducer->produce(
  345. tKafkaTopic,
  346. RdKafka::Topic::PARTITION_UA,
  347. RdKafka::Producer::RK_MSG_COPY,
  348. (void *)Buffer,
  349. BufferSize,
  350. NULL,
  351. NULL
  352. );
  353. if( err != RdKafka::ERR_NO_ERROR ) {
  354. ///std::cerr << "% Failed to produce to topic " << topic << ": " << RdKafka::err2str(err) << std::endl;
  355. sprintf( tBuf, "Failed to produce to topic[%s]: %s", topic.c_str(), RdKafka::err2str(err).c_str() );
  356. LOG_ERROR( tBuf );
  357. if( err == RdKafka::ERR__QUEUE_FULL ){
  358. LOG_ERROR("Internal queue is full!");
  359. /* If the internal queue is full, wait for
  360. * messages to be delivered and then retry.
  361. * The internal queue represents both
  362. * messages to be sent and messages that have
  363. * been sent or failed, awaiting their
  364. * delivery report callback to be called.
  365. *
  366. * The internal queue is limited by the
  367. * configuration property
  368. * queue.buffering.max.messages */
  369. n_pProducer->poll(1000/*block for max 1000ms*/);
  370. if( rCnt-- ) goto retry;
  371. };
  372. }
  373. #ifdef DEBUG
  374. else {
  375. sprintf( tBuf, "Enqueued message (%d bytes) for topic %s", BufferSize, topic.c_str() );
  376. LOG( tBuf );
  377. }
  378. #endif
  379. ;
  380. /* A producer application should continually serve
  381. * the delivery report queue by calling poll()
  382. * at frequent intervals.
  383. * Either put the poll call in your main loop, or in a
  384. * dedicated thread, or call it after every produce() call.
  385. * Just make sure that poll() is still called
  386. * during periods where you are not producing any messages
  387. * to make sure previously produced messages have their
  388. * delivery report callback served (and any other callbacks
  389. * you register). */
  390. n_pProducer->poll(100);
  391. //uint8_t cnt = 100;
  392. //while ( cnt && n_pProducer->outq_len() > 0) {
  393. // LOG("Wait ...");
  394. // cnt--;
  395. // n_pProducer->poll(100);
  396. //};
  397. delete( tKafkaTopic );
  398. delete( tTopicConf );
  399. tKafkaTopic = NULL;
  400. tTopicConf = NULL;
  401. return true;
  402. };
  403. void protocolKAFKA::consume_cb( RdKafka::Message& A, void* B ){
  404. char tBuff[256];
  405. /*
  406. switch (message->err()) {
  407. case RdKafka::ERR__TIMED_OUT: break;
  408. case RdKafka::ERR_NO_ERROR:
  409. // Real message
  410. sprintf( tBuff, "Read msg at offset %dd ", message->offset() );
  411. LOG( tBuff );
  412. if( message->key() ){
  413. sprintf( tBuff, "Key: %s ", *message->key()->c_str() );
  414. LOG( tBuff );
  415. };
  416. //*
  417. RdKafka::Headers *headers = message->headers();
  418. if( headers ){
  419. std::vector<RdKafka::Headers::Header> hdrs = headers->get_all();
  420. for (size_t i = 0 ; i < hdrs.size() ; i++) {
  421. const RdKafka::Headers::Header hdr = hdrs[i];
  422. if (hdr.value() != NULL) {
  423. sprintf( tBuff, " Header: %s = \"%.*s\"\n", hdr.key().c_str(), (int)hdr.value_size(), (const char *)hdr.value());
  424. } else {
  425. sprintf( tBuff, " Header: %s = NULL\n", hdr.key().c_str());
  426. };
  427. LOG( tBuff );
  428. };
  429. };
  430. //* /
  431. //printf("%.*s\n", static_cast<int>(message->len()), static_cast<const char *>(message->payload()));
  432. break;
  433. case RdKafka::ERR__PARTITION_EOF:
  434. // Last message
  435. //if (exit_eof) {
  436. //}
  437. break;
  438. case RdKafka::ERR__UNKNOWN_TOPIC:
  439. case RdKafka::ERR__UNKNOWN_PARTITION:
  440. default:
  441. LOG_ERROR( "Consume failed!" );
  442. LOG_ERROR( message->errstr().c_str() );
  443. }; */
  444. };
  445. bool protocolKAFKA::receivePost( CSTR Channel, int32_t Partition, int64_t Offset, CSTR Buffer, int &BufferSize ){
  446. uint8_t rCnt = 3;
  447. char tBuf[128];
  448. RdKafka::ErrorCode err;
  449. RdKafka::Conf * tTopicConf = NULL;
  450. RdKafka::Topic * tKafkaTopic = NULL;
  451. std::string tBuffer;
  452. std::string topic = Channel;
  453. BufferSize = 0;
  454. if( m_pConsumer == NULL) return false;
  455. tTopicConf = RdKafka::Conf::create( RdKafka::Conf::CONF_TOPIC );
  456. if( tTopicConf==NULL ){
  457. LOG_ERROR("Eror Creating Consumer Topic Conf!");
  458. return false;
  459. };
  460. #ifdef DEBUG
  461. LOG("Consumer RdKafka::Conf::CONF_TOPIC Created");
  462. #endif
  463. tKafkaTopic = RdKafka::Topic::create( m_pConsumer, topic, tTopicConf, tBuffer );
  464. if( tKafkaTopic == NULL ) {
  465. LOG_ERROR( "Failed to create Consumer topic!");
  466. delete( tTopicConf );
  467. return false;
  468. };
  469. /*
  470. * Start consumer for topic+partition at start offset
  471. */
  472. RdKafka::ErrorCode resp = m_pConsumer->start( tKafkaTopic, Partition, Offset );
  473. if (resp != RdKafka::ERR_NO_ERROR) {
  474. LOG_ERROR( "Failed to start consumer!" );
  475. LOG_ERROR( RdKafka::err2str(resp).c_str() );
  476. delete( tKafkaTopic );
  477. delete( tTopicConf );
  478. return false;
  479. };
  480. RdKafka::Message *message = m_pConsumer->consume( tKafkaTopic, Partition, 1000 );
  481. if( message == NULL ) {
  482. delete( tKafkaTopic );
  483. delete( tTopicConf );
  484. return false;
  485. } else {
  486. switch (message->err()) {
  487. case RdKafka::ERR__TIMED_OUT: break;
  488. case RdKafka::ERR_NO_ERROR:
  489. /* Real message */
  490. sprintf( tBuf, "Read msg at offset %dd ", message->offset() );
  491. LOG( tBuf );
  492. if( message->key() ){
  493. sprintf( tBuf, "Key: %s ", *message->key()->c_str() );
  494. LOG( tBuf );
  495. };
  496. //printf("%.*s\n", static_cast<int>(message->len()), static_cast<const char *>(message->payload()));
  497. BufferSize = message->len();
  498. memcpy( Buffer, message->payload(), BufferSize );
  499. break;
  500. case RdKafka::ERR__PARTITION_EOF:
  501. break;
  502. case RdKafka::ERR__UNKNOWN_TOPIC:
  503. case RdKafka::ERR__UNKNOWN_PARTITION:
  504. default:
  505. LOG_ERROR( "Consume failed!" );
  506. LOG_ERROR( message->errstr().c_str() );
  507. delete( message );
  508. delete( tKafkaTopic );
  509. delete( tTopicConf );
  510. return false;
  511. };
  512. }
  513. delete( message );
  514. m_pConsumer->stop( tKafkaTopic, Partition );
  515. m_pConsumer->poll( 1000 );
  516. delete( tKafkaTopic );
  517. delete( tTopicConf );
  518. return true;
  519. };
  520. void protocolKAFKA::event_cb( RdKafka::Event &event ){
  521. /* switch (event.type()){
  522. case RdKafka::Event::EVENT_ERROR:
  523. if (event.fatal()) {
  524. std::cerr << "FATAL ";
  525. run = 0;
  526. }
  527. std::cerr << "ERROR (" << RdKafka::err2str(event.err()) << "): " << event.str() << std::endl;
  528. break;
  529. case RdKafka::Event::EVENT_STATS:
  530. std::cerr << "\"STATS\": " << event.str() << std::endl;
  531. break;
  532. case RdKafka::Event::EVENT_LOG:
  533. fprintf(stderr, "LOG-%i-%s: %s\n", event.severity(), event.fac().c_str(), event.str().c_str());
  534. break;
  535. default:
  536. std::cerr << "EVENT " << event.type() << " (" << RdKafka::err2str(event.err()) << "): " << event.str() << std::endl;
  537. break;
  538. }; */
  539. };
  540. #undef LOG
  541. #undef LOG_ERROR