kafka.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 MODULES_KAFKA_HPP_
  21. #define MODULES_KAFKA_HPP_
  22. #include <memory>
  23. #include <mutex>
  24. #include <string>
  25. #include <unordered_map>
  26. #include "cnstream_module.hpp"
  27. #include "cnstream_frame_va.hpp"
  28. namespace cnstream {
  29. struct KafkaContext;
  30. using CNFrameInfoPtr = std::shared_ptr<cnstream::CNFrameInfo>;
  31. class Kafka : public cnstream::Module, public cnstream::ModuleCreator<Kafka> {
  32. public:
  33. explicit Kafka(const std::string &name);
  34. ~Kafka();
  35. bool Open(cnstream::ModuleParamSet paramSet) override;
  36. void Close() override;
  37. int Process(CNFrameInfoPtr data) override;
  38. private:
  39. KafkaContext *GetContext(CNFrameInfoPtr data);
  40. std::mutex mutex_;
  41. std::unordered_map<int, KafkaContext *> contexts_;
  42. std::string brokers_;
  43. std::string handler_name_;
  44. // the topic_ is prefix of a real topic. eg: if you set topic in json is "cndata",
  45. // the stream_id 0`s real topic is "cndata_0"
  46. std::string topic_;
  47. }; // class Kafka
  48. } // namespace cnstream
  49. #endif // MODULES_KAFKA_HPP_