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.

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