@@ -47,67 +47,4 @@ void Messenger::PubStruct(const std::string &topic, const void *data, const size
4747}
4848std::string Messenger::GetPubEndpoint () const { return endpoint_; }
4949
50-
51- std::unordered_set<std::string> TopicMonitor::GetTopics () const {
52- std::lock_guard lock (topics_mutex_);
53- return topics_;
54- }
55- void TopicMonitor::Start () {
56- if (monitor_thread_.joinable ()) {
57- return ;
58- }
59- should_run_.store (true );
60- monitor_thread_ = std::thread (&TopicMonitor::MonitorLoop, this );
61- }
62-
63- void TopicMonitor::Stop () {
64- if (!should_run_.load ()) {
65- return ;
66- }
67- should_run_.store (false );
68- if (monitor_thread_.joinable ()) {
69- monitor_thread_.join ();
70- }
71- }
72- TopicMonitor::TopicMonitor () : context_(1 ), subscriber_(context_, ZMQ_SUB), should_run_(false ) {
73- try {
74- subscriber_.connect (Messenger::GetInstance ().GetPubEndpoint ());
75- subscriber_.set (zmq::sockopt::subscribe, " " );
76- } catch (const zmq::error_t &e) {
77- LOG (ERROR) << " [TopicMonitor] Initialization failed: " << e.what ();
78- throw ;
79- }
80- }
81- TopicMonitor::~TopicMonitor () {
82- Stop ();
83- try {
84- subscriber_.close ();
85- context_.close ();
86- } catch (const zmq::error_t &e) {
87- LOG (ERROR) << " [TopicMonitor] Cleanup error: " << e.what ();
88- }
89- }
90- void TopicMonitor::MonitorLoop () {
91- zmq::message_t msg;
92-
93- while (should_run_.load ()) {
94- try {
95- if (subscriber_.recv (msg, zmq::recv_flags::dontwait)) {
96- {
97- std::string topic (static_cast <char *>(msg.data ()), msg.size ());
98- std::lock_guard lock (topics_mutex_);
99- topic_frequencies_[topic]++;
100- }
101- if (subscriber_.get (zmq::sockopt::rcvmore)) {
102- zmq::message_t dummy;
103- subscriber_.recv (dummy);
104- }
105- }
106- } catch (const zmq::error_t &e) {
107- if (e.num () != ETERM) {
108- }
109- }
110- }
111- }
112-
11350} // namespace infinite_sense
0 commit comments