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.

880 lines
25 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolKAFKA.h"
  2. #include "../../alg/generic/md5.h"
  3. #include "../json/json_tool.h"
  4. #include <json/reader.h>
  5. #include <json/value.h>
  6. #include <json/writer.h>
  7. #include <strings.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. {
  132. m_pLog = basicLogCollection::getInstance();
  133. n_pProducer = NULL;
  134. // m_pServerIP = NULL;
  135. m_pSettings = Settings;
  136. // memset( m_pKafkaBrokerSTR, 0 , sizeof( m_pKafkaBrokerSTR ));
  137. if (Settings != NULL)
  138. {
  139. // m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE);
  140. // if( m_pServerIP == NULL ) return;
  141. // memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE);
  142. // strncpy( m_pServerIP, Settings->brocker_addr, OS_CEP_ADDRESS_SIZE );
  143. // m_nServerPort = Settings->brocker_port;
  144. // sprintf( m_pKafkaBrokerSTR, "%s:%d", m_pServerIP, m_nServerPort );
  145. };
  146. }
  147. // protocolKAFKA::protocolKAFKA( CSTR ServerAddress, uint16_t Port ){
  148. // m_pLog = basicLogCollection::getInstance();
  149. // n_pProducer = NULL;
  150. // m_pServerIP = NULL;
  151. // if( ServerAddress == 0 || Port == 0 ) return;
  152. // m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE);
  153. // if( m_pServerIP == NULL ) return;
  154. // memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE);
  155. // memset( m_pKafkaBrokerSTR, 0 , sizeof( m_pKafkaBrokerSTR ));
  156. // strncpy( m_pServerIP, ServerAddress, OS_CEP_ADDRESS_SIZE );
  157. // m_nServerPort = Port;
  158. // sprintf( m_pKafkaBrokerSTR, "%s:%d", m_pServerIP, m_nServerPort );
  159. // };
  160. /**
  161. * @brief Message delivery report callback.
  162. *
  163. * This callback is called exactly once per message, indicating if
  164. * the message was succesfully delivered
  165. * (rkmessage->err == RD_KAFKA_RESP_ERR_NO_ERROR) or permanently
  166. * failed delivery (rkmessage->err != RD_KAFKA_RESP_ERR_NO_ERROR).
  167. *
  168. * The callback is triggered from rd_kafka_poll() and executes on
  169. * the application's thread.
  170. */
  171. /*
  172. static void dr_msg_cb( rd_kafka_t *rk, const rd_kafka_message_t *rkmessage, void *opaque ){
  173. if (rkmessage->err)
  174. fprintf(stderr, "%% Message delivery failed: %s\n",
  175. rd_kafka_err2str(rkmessage->err));
  176. else
  177. fprintf(stderr,
  178. "%% Message delivered (%zd bytes, "
  179. "partition %"PRId32")\n",
  180. rkmessage->len, rkmessage->partition);
  181. // The rkmessage is destroyed automatically by librdkafka
  182. };
  183. */
  184. protocolKAFKA::~protocolKAFKA()
  185. {
  186. // m_bEnabled = false;
  187. if (n_pProducer != NULL)
  188. {
  189. /* Wait for final messages to be delivered or fail.
  190. * rd_kafka_flush() is an abstraction over rd_kafka_poll() which
  191. * waits for all messages to be delivered. */
  192. LOG("Flushing final messages..");
  193. n_pProducer->flush(3000 /* wait for max 3 seconds */);
  194. /* If the output queue is still not empty there is an issue
  195. * with producing messages to the clusters. */
  196. if (n_pProducer->outq_len() > 0)
  197. {
  198. LOG_ERROR("Some messages are not delivered!");
  199. }
  200. delete n_pProducer;
  201. // if( n_pProducerTopic!= NULL ) delete n_pProducerTopic;
  202. };
  203. // if( m_pScannerName != NULL ) free(m_pScannerName);
  204. // if( m_pServerIP != NULL ) free(m_pServerIP);
  205. // if( m_pAccessURL != NULL ) free(m_pAccessURL);
  206. }
  207. // https://github.com/edenhill/librdkafka/blob/master/examples/producer.c
  208. // https://github.com/edenhill/librdkafka/blob/master/examples/producer.cpp
  209. void protocolKAFKA::dr_cb(RdKafka::Message & message)
  210. {
  211. /* If message.err() is non-zero the message delivery failed permanently
  212. * for the message. */
  213. char tBuffer[CTEXT];
  214. if (message.err())
  215. {
  216. sprintf(tBuffer, "Message delivery failed: %s", message.errstr().c_str());
  217. LOG_ERROR(tBuffer);
  218. }
  219. #ifdef DEBUG_KAFKA
  220. else
  221. {
  222. sprintf(tBuffer, "Message delivered to topic: %s [%d] at offset %ld", message.topic_name().c_str(), message.partition(), message.offset());
  223. LOG(tBuffer);
  224. }
  225. #endif
  226. ;
  227. }
  228. bool protocolKAFKA::init()
  229. {
  230. RdKafka::Conf * tGlobalConf;
  231. char zBuffer[CTEXT];
  232. std::string tBuffer;
  233. std::string tValue;
  234. if (m_pSettings == NULL)
  235. {
  236. return false;
  237. }
  238. tValue.reserve(256);
  239. #ifdef DEBUG_KAFKA
  240. LOG("INIT Begin");
  241. #endif
  242. tGlobalConf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
  243. if (tGlobalConf == NULL)
  244. {
  245. LOG_ERROR("Eror Creating Producer Conf!");
  246. return false;
  247. };
  248. // ToDo: Checking in case of SSL -> if library is build with SSL support!!
  249. #ifdef DEBUG_KAFKA
  250. LOG("RdKafka::Conf::CONF_GLOBAL Created");
  251. std::string features;
  252. tGlobalConf->get("builtin.features", features);
  253. sprintf(zBuffer, "librdkafka version %s (0x%08x, builtin.features \"%s\")", RdKafka::version_str().c_str(), RdKafka::version(), features.c_str());
  254. LOG(zBuffer);
  255. #endif
  256. if (strlen(m_pSettings->brocker_addr) == 0)
  257. {
  258. LOG_ERROR("No Kafka Brokers are defined!");
  259. delete(tGlobalConf);
  260. return false;
  261. };
  262. sprintf((char *) tValue.c_str(), "%s:%d", m_pSettings->brocker_addr, m_pSettings->brocker_port);
  263. if (tGlobalConf->set("bootstrap.servers", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  264. {
  265. LOG_ERROR(tBuffer.c_str());
  266. delete(tGlobalConf);
  267. return false;
  268. #ifdef DEBUG_KAFKA
  269. }
  270. else
  271. {
  272. LOG("bootstrap.servers is set");
  273. #endif
  274. };
  275. if (tGlobalConf->set("metadata.broker.list", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  276. {
  277. LOG_ERROR(tBuffer.c_str());
  278. delete(tGlobalConf);
  279. return false;
  280. #ifdef DEBUG_KAFKA
  281. }
  282. else
  283. {
  284. LOG(tValue.c_str());
  285. LOG("metadata.broker.list is SET");
  286. #endif
  287. };
  288. if (m_pSettings->use_ssl)
  289. {
  290. LOG("USE SSL!");
  291. if (m_pSettings->auth_by_cred)
  292. {
  293. sprintf((char *) tValue.c_str(), "SASL_SSL");
  294. if (tGlobalConf->set("security.protocol", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  295. {
  296. LOG_ERROR(tBuffer.c_str());
  297. delete(tGlobalConf);
  298. return false;
  299. #ifdef DEBUG_KAFKA
  300. }
  301. else
  302. {
  303. LOG("SEC Proto is SASL_SSL");
  304. #endif
  305. };
  306. sprintf((char *) tValue.c_str(), "%s", m_pSettings->cred_username);
  307. if (tGlobalConf->set("sasl.username", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  308. {
  309. LOG_ERROR(tBuffer.c_str());
  310. delete(tGlobalConf);
  311. return false;
  312. #ifdef DEBUG_KAFKA
  313. }
  314. else
  315. {
  316. LOG("SASL username is SET");
  317. #endif
  318. };
  319. sprintf((char *) tValue.c_str(), "%s", m_pSettings->cred_password);
  320. if (tGlobalConf->set("sasl.password", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  321. {
  322. LOG_ERROR(tBuffer.c_str());
  323. delete(tGlobalConf);
  324. return false;
  325. #ifdef DEBUG_KAFKA
  326. }
  327. else
  328. {
  329. LOG("SASL password is SET");
  330. #endif
  331. };
  332. }
  333. else
  334. {
  335. sprintf((char *) tValue.c_str(), "SSL");
  336. if (tGlobalConf->set("security.protocol", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  337. {
  338. LOG_ERROR(tBuffer.c_str());
  339. delete(tGlobalConf);
  340. return false;
  341. #ifdef DEBUG_KAFKA
  342. }
  343. else
  344. {
  345. LOG("SEC Proto is SSL");
  346. #endif
  347. };
  348. };
  349. if (m_pSettings->truststore)
  350. {
  351. sprintf((char *) tValue.c_str(), "./data/modules/VAULT/%s/%s.CA.pem", m_pSettings->vault_md5, m_pSettings->vault_md5);
  352. if (tGlobalConf->set("ssl.ca.location", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  353. {
  354. LOG_ERROR(tBuffer.c_str());
  355. delete(tGlobalConf);
  356. return false;
  357. #ifdef DEBUG_KAFKA
  358. }
  359. else
  360. {
  361. LOG("SSL CA Root is SET");
  362. #endif
  363. };
  364. };
  365. if (m_pSettings->certstore)
  366. {
  367. sprintf((char *) tValue.c_str(), "./data/modules/VAULT/%s/%s.CERT.pem", m_pSettings->vault_md5, m_pSettings->vault_md5);
  368. if (tGlobalConf->set("ssl.certificate.location", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  369. {
  370. LOG_ERROR(tBuffer.c_str());
  371. delete(tGlobalConf);
  372. return false;
  373. #ifdef DEBUG_KAFKA
  374. }
  375. else
  376. {
  377. LOG("SSL CERT is SET");
  378. #endif
  379. };
  380. };
  381. if (m_pSettings->keystore)
  382. {
  383. sprintf((char *) tValue.c_str(), "./data/modules/VAULT/%s/%s.key", m_pSettings->vault_md5, m_pSettings->vault_md5);
  384. if (tGlobalConf->set("ssl.key.location", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  385. {
  386. LOG_ERROR(tBuffer.c_str());
  387. delete(tGlobalConf);
  388. return false;
  389. #ifdef DEBUG_KAFKA
  390. }
  391. else
  392. {
  393. LOG("SSL KEY is SET");
  394. #endif
  395. };
  396. };
  397. if (strlen(m_pSettings->cred_password))
  398. {
  399. sprintf((char *) tValue.c_str(), m_pSettings->cred_password);
  400. if (tGlobalConf->set("ssl.key.password", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  401. {
  402. LOG_ERROR(tBuffer.c_str());
  403. delete(tGlobalConf);
  404. return false;
  405. #ifdef DEBUG_KAFKA
  406. }
  407. else
  408. {
  409. LOG("SSL KEY password is SET");
  410. #endif
  411. };
  412. };
  413. }
  414. else
  415. {
  416. if (m_pSettings->auth_by_cred)
  417. {
  418. sprintf((char *) tValue.c_str(), "SASL");
  419. if (tGlobalConf->set("security.protocol", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  420. {
  421. LOG_ERROR(tBuffer.c_str());
  422. delete(tGlobalConf);
  423. return false;
  424. #ifdef DEBUG_KAFKA
  425. }
  426. else
  427. {
  428. LOG("SEC Proto is SASL");
  429. #endif
  430. };
  431. sprintf((char *) tValue.c_str(), "PLAIN");
  432. if (tGlobalConf->set("sasl.mechanisms", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  433. {
  434. LOG_ERROR(tBuffer.c_str());
  435. delete(tGlobalConf);
  436. return false;
  437. #ifdef DEBUG_KAFKA
  438. }
  439. else
  440. {
  441. LOG("SASL mechanism is PLAIN");
  442. #endif
  443. };
  444. sprintf((char *) tValue.c_str(), "%s", m_pSettings->cred_username);
  445. if (tGlobalConf->set("sasl.username", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  446. {
  447. LOG_ERROR(tBuffer.c_str());
  448. delete(tGlobalConf);
  449. return false;
  450. #ifdef DEBUG_KAFKA
  451. }
  452. else
  453. {
  454. LOG("SASL username is SET");
  455. #endif
  456. };
  457. sprintf((char *) tValue.c_str(), "%s", m_pSettings->cred_password);
  458. if (tGlobalConf->set("sasl.password", tValue, tBuffer) != RdKafka::Conf::CONF_OK)
  459. {
  460. LOG_ERROR(tBuffer.c_str());
  461. delete(tGlobalConf);
  462. return false;
  463. #ifdef DEBUG_KAFKA
  464. }
  465. else
  466. {
  467. LOG("SASL password is SET");
  468. #endif
  469. };
  470. #ifdef DEBUG_KAFKA
  471. }
  472. else
  473. {
  474. LOG("No Auth Settings!");
  475. #endif
  476. };
  477. };
  478. /* Set the delivery report callback.
  479. * This callback will be called once per message to inform
  480. * the application if delivery succeeded or failed.
  481. * See dr_msg_cb() above.
  482. * The callback is only triggered from rd_kafka_poll() and
  483. * rd_kafka_flush(). */
  484. // tProducerConf->set("queue.buffering.max.ms", "100", tBuffer );
  485. // tProducerConf->set("socket.blocking.max.ms", "100", tBuffer );
  486. if (tGlobalConf->set("dr_cb", (RdKafka::DeliveryReportCb *) this, tBuffer) != RdKafka::Conf::CONF_OK)
  487. {
  488. LOG_ERROR(tBuffer.c_str());
  489. delete(tGlobalConf);
  490. return false;
  491. #ifdef DEBUG_KAFKA
  492. }
  493. else
  494. {
  495. LOG("dr_cb is SET");
  496. #endif
  497. };
  498. // if( tProducerConf->set("default_topic_conf", tTopicConf, tBuffer ) != RdKafka::Conf::CONF_OK ){
  499. // LOG_ERROR( tBuffer.c_str() );
  500. // delete( tProducerConf );
  501. // delete( tTopicConf );
  502. // tProducerConf = NULL;
  503. // tTopicConf = NULL;
  504. // return false;
  505. // };
  506. // LOG("default_topic_conf is SET");
  507. /*
  508. * Create producer instance.
  509. *
  510. * NOTE: rd_kafka_new() takes ownership of the conf object
  511. * and the application must not reference it again after
  512. * this call.
  513. */
  514. n_pProducer = RdKafka::Producer::create(tGlobalConf, tBuffer);
  515. if (n_pProducer == NULL)
  516. {
  517. LOG_ERROR(tBuffer.c_str());
  518. delete(tGlobalConf);
  519. return false;
  520. #ifdef DEBUG_KAFKA
  521. }
  522. else
  523. {
  524. LOG("RdKafka::Producer Created");
  525. #endif
  526. };
  527. if (tGlobalConf->set("enable.partition.eof", "true", tBuffer) != RdKafka::Conf::CONF_OK)
  528. {
  529. delete(n_pProducer);
  530. delete(tGlobalConf);
  531. LOG_ERROR(tBuffer.c_str());
  532. return false;
  533. #ifdef DEBUG_KAFKA
  534. }
  535. else
  536. {
  537. LOG("enable.partition.eof is SET");
  538. #endif
  539. };
  540. m_pConsumer = RdKafka::Consumer::create(tGlobalConf, tBuffer);
  541. if (m_pConsumer == NULL)
  542. {
  543. delete(n_pProducer);
  544. delete(tGlobalConf);
  545. LOG_ERROR("Failed to create consumer:");
  546. LOG_ERROR(tBuffer.c_str());
  547. return false;
  548. #ifdef DEBUG_KAFKA
  549. }
  550. else
  551. {
  552. LOG("RdKafka::Consumer Created");
  553. #endif
  554. };
  555. #ifdef DEBUG_KAFKA
  556. LOG("INIT Complete");
  557. #endif
  558. delete(tGlobalConf);
  559. return true;
  560. }
  561. bool protocolKAFKA::issuePost(CSTR tpc, CSTR Buffer, int BufferSize)
  562. {
  563. uint8_t rCnt = 3;
  564. char tBuf[128];
  565. RdKafka::ErrorCode err;
  566. RdKafka::Conf * tTopicConf = NULL;
  567. RdKafka::Topic * tKafkaTopic = NULL;
  568. std::string tBuffer;
  569. // std::string brokers = m_pKafkaBrokerSTR;
  570. std::string topic = tpc;
  571. if (n_pProducer == NULL)
  572. {
  573. return false;
  574. }
  575. tTopicConf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
  576. if (tTopicConf == NULL)
  577. {
  578. LOG_ERROR("Eror Creating Topic Conf!");
  579. return false;
  580. };
  581. #ifdef DEBUG_KAFKA
  582. LOG("RdKafka::Conf::CONF_TOPIC Created");
  583. #endif
  584. tKafkaTopic = RdKafka::Topic::create(n_pProducer, topic, tTopicConf, tBuffer);
  585. if (tKafkaTopic == NULL)
  586. {
  587. LOG_ERROR("No Topic Created!!");
  588. LOG_ERROR(tpc);
  589. LOG_ERROR(tBuffer.c_str());
  590. delete(tTopicConf);
  591. return false;
  592. };
  593. #ifdef DEBUG_KAFKA
  594. LOG("RdKafka::Topic Created");
  595. #endif
  596. /*
  597. if( tProducerConf->set( "default_topic_conf", tTopicConf, tBuffer ) != RdKafka::Conf::CONF_OK ){
  598. LOG_ERROR( tBuffer.c_str() );
  599. delete( tTopicConf );
  600. tProducerConf = NULL;
  601. tTopicConf = NULL;
  602. return false;
  603. };
  604. #ifdef DEBUG
  605. LOG("default_topic_conf is SET");
  606. #endif
  607. //*/
  608. retry:
  609. err = n_pProducer->produce(tKafkaTopic, RdKafka::Topic::PARTITION_UA, RdKafka::Producer::RK_MSG_COPY, (void *) Buffer, BufferSize, NULL, NULL);
  610. if (err != RdKafka::ERR_NO_ERROR)
  611. {
  612. /// std::cerr << "% Failed to produce to topic " << topic << ": " << RdKafka::err2str(err) << std::endl;
  613. sprintf(tBuf, "Failed to produce to topic[%s]: %s", topic.c_str(), RdKafka::err2str(err).c_str());
  614. LOG_ERROR(tBuf);
  615. if (err == RdKafka::ERR__QUEUE_FULL)
  616. {
  617. LOG_ERROR("Internal queue is full!");
  618. /* If the internal queue is full, wait for
  619. * messages to be delivered and then retry.
  620. * The internal queue represents both
  621. * messages to be sent and messages that have
  622. * been sent or failed, awaiting their
  623. * delivery report callback to be called.
  624. *
  625. * The internal queue is limited by the
  626. * configuration property
  627. * queue.buffering.max.messages */
  628. n_pProducer->poll(1000 /*block for max 1000ms*/);
  629. if (rCnt--)
  630. {
  631. goto retry;
  632. }
  633. };
  634. }
  635. #ifdef DEBUG_KAFKA
  636. else
  637. {
  638. // MOCK
  639. FILE * fd;
  640. fd = fopen("KAFKA_M.txt", "a+");
  641. if (fd)
  642. {
  643. fprintf(fd, "\n*****************\n");
  644. fprintf(fd, Buffer);
  645. fprintf(fd, "\n*****************\nEnqueued message (%d bytes) for topic %s\n", BufferSize, topic.c_str());
  646. fclose(fd);
  647. };
  648. sprintf(tBuf, "Enqueued message (%d bytes) for topic %s", BufferSize, topic.c_str());
  649. LOG(tBuf);
  650. }
  651. #endif
  652. ;
  653. /* A producer application should continually serve
  654. * the delivery report queue by calling poll()
  655. * at frequent intervals.
  656. * Either put the poll call in your main loop, or in a
  657. * dedicated thread, or call it after every produce() call.
  658. * Just make sure that poll() is still called
  659. * during periods where you are not producing any messages
  660. * to make sure previously produced messages have their
  661. * delivery report callback served (and any other callbacks
  662. * you register). */
  663. n_pProducer->poll(100);
  664. // uint8_t cnt = 100;
  665. // while ( cnt && n_pProducer->outq_len() > 0) {
  666. // LOG("Wait ...");
  667. // cnt--;
  668. // n_pProducer->poll(100);
  669. // };
  670. delete(tKafkaTopic);
  671. delete(tTopicConf);
  672. tKafkaTopic = NULL;
  673. tTopicConf = NULL;
  674. return true;
  675. }
  676. void protocolKAFKA::consume_cb(RdKafka::Message & A, void * B)
  677. {
  678. if((A.err() == RdKafka::ERR_NO_ERROR ) || ( B != NULL )) LOG("CB Called!");
  679. /*
  680. char tBuff[256];
  681. switch (message->err()) {
  682. case RdKafka::ERR__TIMED_OUT: break;
  683. case RdKafka::ERR_NO_ERROR:
  684. // Real message
  685. sprintf( tBuff, "Read msg at offset %dd ", message->offset() );
  686. LOG( tBuff );
  687. if( message->key() ){
  688. sprintf( tBuff, "Key: %s ", *message->key()->c_str() );
  689. LOG( tBuff );
  690. };
  691. RdKafka::Headers *headers = message->headers();
  692. if( headers ){
  693. std::vector<RdKafka::Headers::Header> hdrs = headers->get_all();
  694. for (size_t i = 0 ; i < hdrs.size() ; i++) {
  695. const RdKafka::Headers::Header hdr = hdrs[i];
  696. if (hdr.value() != NULL) {
  697. sprintf( tBuff, " Header: %s = \"%.*s\"\n", hdr.key().c_str(), (int)hdr.value_size(), (const char *)hdr.value());
  698. } else {
  699. sprintf( tBuff, " Header: %s = NULL\n", hdr.key().c_str());
  700. };
  701. LOG( tBuff );
  702. };
  703. };
  704. //printf("%.*s\n", static_cast<int>(message->len()), static_cast<const char *>(message->payload()));
  705. break;
  706. case RdKafka::ERR__PARTITION_EOF:
  707. // Last message
  708. //if (exit_eof) {
  709. //}
  710. break;
  711. case RdKafka::ERR__UNKNOWN_TOPIC:
  712. case RdKafka::ERR__UNKNOWN_PARTITION:
  713. default:
  714. LOG_ERROR( "Consume failed!" );
  715. LOG_ERROR( message->errstr().c_str() );
  716. }; */
  717. }
  718. bool protocolKAFKA::receivePost(CSTR Channel, int32_t Partition, int64_t & Offset, CSTR Buffer, int & BufferSize)
  719. {
  720. //uint8_t rCnt = 3;
  721. char tBuf[128];
  722. //RdKafka::ErrorCode err;
  723. RdKafka::Conf * tTopicConf = NULL;
  724. RdKafka::Topic * tKafkaTopic = NULL;
  725. std::string tBuffer;
  726. std::string topic = Channel;
  727. BufferSize = 0;
  728. if (m_pConsumer == NULL)
  729. {
  730. return false;
  731. }
  732. tTopicConf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
  733. if (tTopicConf == NULL)
  734. {
  735. LOG_ERROR("Eror Creating Consumer Topic Conf!");
  736. return false;
  737. };
  738. #ifdef DEBUG_KAFKA
  739. LOG("Consumer RdKafka::Conf::CONF_TOPIC Created");
  740. #endif
  741. tKafkaTopic = RdKafka::Topic::create(m_pConsumer, topic, tTopicConf, tBuffer);
  742. if (tKafkaTopic == NULL)
  743. {
  744. LOG_ERROR("Failed to create Consumer topic!");
  745. delete(tTopicConf);
  746. return false;
  747. };
  748. #ifdef DEBUG_KAFKA
  749. LOG("Consumer RdKafka::Topic Created");
  750. #endif
  751. /*
  752. * Start consumer for topic+partition at start offset
  753. */
  754. // Get To Last Message
  755. if (Offset == 0)
  756. {
  757. LOG("Correcting Offset");
  758. int64_t bOff = m_pConsumer->OffsetTail(Offset);
  759. Offset += bOff - 1;
  760. };
  761. RdKafka::ErrorCode resp = m_pConsumer->start(tKafkaTopic, Partition, Offset); // Partition, Offset );
  762. if (resp != RdKafka::ERR_NO_ERROR)
  763. {
  764. LOG_ERROR("Failed to start consumer!");
  765. LOG_ERROR(RdKafka::err2str(resp).c_str());
  766. delete(tKafkaTopic);
  767. delete(tTopicConf);
  768. return false;
  769. };
  770. RdKafka::Message * message = m_pConsumer->consume(tKafkaTopic, Partition, 1000);
  771. if (message == NULL)
  772. {
  773. LOG_ERROR("FAILURE");
  774. delete(tKafkaTopic);
  775. delete(tTopicConf);
  776. return false;
  777. }
  778. else
  779. {
  780. switch (message->err())
  781. {
  782. case RdKafka::ERR__TIMED_OUT: LOG("TIME OUT"); break;
  783. case RdKafka::ERR_NO_ERROR:
  784. /* Real message */
  785. Offset = message->offset();
  786. #ifdef DEBUG_KAFKA
  787. sprintf(tBuf, "Read msg at offset %ld ", Offset);
  788. LOG(tBuf);
  789. if (message->key())
  790. {
  791. sprintf(tBuf, "Key: %c ", *message->key()->c_str());
  792. LOG(tBuf);
  793. };
  794. #endif
  795. // printf("%.*s\n", static_cast<int>(message->len()), static_cast<const char *>(message->payload()));
  796. BufferSize = message->len();
  797. memcpy(Buffer, message->payload(), BufferSize);
  798. Buffer[BufferSize] = (char) 0;
  799. break;
  800. case RdKafka::ERR__PARTITION_EOF:
  801. #ifdef DEBUG_KAFKA
  802. sprintf(tBuf, "EOF Read msg at partition %d and offset %ld ", Partition, Offset);
  803. LOG(tBuf);
  804. #endif
  805. break;
  806. case RdKafka::ERR__UNKNOWN_TOPIC:
  807. case RdKafka::ERR__UNKNOWN_PARTITION:
  808. default:
  809. LOG_ERROR("Consume failed!");
  810. LOG_ERROR(message->errstr().c_str());
  811. delete(message);
  812. delete(tKafkaTopic);
  813. delete(tTopicConf);
  814. return false;
  815. };
  816. }
  817. delete(message);
  818. m_pConsumer->stop(tKafkaTopic, 0);
  819. m_pConsumer->poll(1000);
  820. delete(tKafkaTopic);
  821. delete(tTopicConf);
  822. return true;
  823. }
  824. void protocolKAFKA::event_cb(RdKafka::Event & event)
  825. {
  826. if( event.type() == RdKafka::Event::EVENT_ERROR ) LOG("Evt ERROR!");
  827. /* switch (event.type()){
  828. case RdKafka::Event::EVENT_ERROR:
  829. if (event.fatal()) {
  830. std::cerr << "FATAL ";
  831. run = 0;
  832. }
  833. std::cerr << "ERROR (" << RdKafka::err2str(event.err()) << "): " << event.str() << std::endl;
  834. break;
  835. case RdKafka::Event::EVENT_STATS:
  836. std::cerr << "\"STATS\": " << event.str() << std::endl;
  837. break;
  838. case RdKafka::Event::EVENT_LOG:
  839. fprintf(stderr, "LOG-%i-%s: %s\n", event.severity(), event.fac().c_str(), event.str().c_str());
  840. break;
  841. default:
  842. std::cerr << "EVENT " << event.type() << " (" << RdKafka::err2str(event.err()) << "): " << event.str() << std::endl;
  843. break;
  844. }; */
  845. }
  846. #undef LOG
  847. #undef LOG_ERROR