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.

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