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.

772 lines
29 KiB

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