kafka_client.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef __KAFKA_CLIENT_H__
  21. #define __KAFKA_CLIENT_H__
  22. #include <librdkafka/rdkafka.h>
  23. #include <stdarg.h>
  24. #include <functional>
  25. #include <string>
  26. namespace cnstream {
  27. class KafkaClient {
  28. public:
  29. enum class TYPE {
  30. PRODUCER = 0,
  31. CONSUMER,
  32. };
  33. explicit KafkaClient(TYPE type, const std::string &brokers, const std::string &topic, int32_t partition);
  34. ~KafkaClient();
  35. bool Start();
  36. bool Stop(bool instant = false);
  37. bool Produce(const uint8_t *p_payload, size_t length);
  38. bool Consume(uint8_t **p_payload, size_t *p_length, int timeout_ms = 0);
  39. private:
  40. enum class STATE {
  41. IDLE = 0,
  42. PRE_PRODUCE,
  43. PRE_CONSUME,
  44. PRODUCE,
  45. CONSUME,
  46. };
  47. static void logger(const rd_kafka_t *rk, int level, const char *fac, const char *buf);
  48. static void msg_delivered(rd_kafka_t *rk, const rd_kafka_message_t *msg, void *opaque);
  49. bool msg_consume(rd_kafka_message_t *msg, uint8_t **p_payload, size_t *p_len);
  50. TYPE type_;
  51. const std::string brokers_;
  52. const std::string topic_;
  53. int32_t partition_ = 0;
  54. STATE state_ = STATE::IDLE;
  55. rd_kafka_t *rk_ = nullptr;
  56. rd_kafka_topic_t *rkt_ = nullptr;
  57. rd_kafka_conf_t *conf_ = nullptr;
  58. rd_kafka_topic_conf_t *topic_conf_ = nullptr;
  59. rd_kafka_message_t *message_ = nullptr;
  60. };
  61. } // namespace cnstream
  62. #endif // __KAFKA_CLIENT_H__