topic_subscriber.h 942 Bytes
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2
#pragma once

3
#include "ros_bridge/include/com_private.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
4 5
#include "ros_bridge/include/RosBridgeClient.h"

6 7
#include <unordered_map>

8 9
namespace ros_bridge {
namespace com_private {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
10

11 12 13

typedef std::function<void(JsonDocUPtr)> CallbackType;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
14 15
class TopicSubscriber
{
16
    typedef std::unordered_map<std::string, std::string> TopicMap;
17
public:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
18 19

    TopicSubscriber() = delete;
20
    TopicSubscriber(RosbridgeWsClient &rbc);
21
    ~TopicSubscriber();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
22

23
    //! @brief Starts the subscriber.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
24
    void start();
25 26 27

    //! @brief Resets the subscriber.
    void reset();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
28 29 30 31

    //! @return Returns false if a subscription to this topic allready exists.
    //!
    //! @note Only one callback can be registered.
32
    void subscribe(const char* topic, const CallbackType &callback);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
33 34

private:
35
    RosbridgeWsClient   &_rbc;
36 37
    TopicMap            _topicMap;
    std::shared_ptr<std::atomic_bool>   _stopped;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
38 39
};

40 41
} // namespace ComPrivate
} // namespace ROSBridge