TopicSubscriber.h 1.47 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10
#pragma once

#include "ros_bridge/include/ComPrivateInclude.h"
#include "ros_bridge/include/RosBridgeClient.h"
#include "ros_bridge/include/TypeFactory.h"
#include "ros_bridge/include/CasePacker.h"

namespace ROSBridge {
namespace ComPrivate {

11 12 13 14 15 16 17 18

typedef std::function<void(JsonDocUPtr)> CallbackType;
struct CallbackMapWrapper{
    typedef std::map<HashType, CallbackType> Map;
    Map map;
    std::mutex mutex;
};

Valentin Platzgummer's avatar
Valentin Platzgummer committed
19 20
class TopicSubscriber
{
21
    typedef std::vector<std::string> ClientList;
22
public:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
23 24

    TopicSubscriber() = delete;
25
    TopicSubscriber(CasePacker *casePacker, RosbridgeWsClient *rbc, std::mutex *rbcMutex);
26
    ~TopicSubscriber();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
27

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

    //! @brief Resets the subscriber.
    void reset();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
33 34 35 36 37 38 39

    //! @return Returns false if a subscription to this topic allready exists.
    //!
    //! @note Only one callback can be registered.
    bool subscribe(const char* topic, const CallbackType &callback);

private:
40 41 42



Valentin Platzgummer's avatar
Valentin Platzgummer committed
43 44
    CasePacker         *_casePacker;
    RosbridgeWsClient  *_rbc;
45 46
    std::mutex         *_rbcMutex;
    CallbackMapWrapper         _callbackMap;
47 48
    ClientList          _clientList;
    bool                _running;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
49 50 51
};

void subscriberCallback(const HashType &hash,
52
                        CallbackMapWrapper &mapWrapper,
Valentin Platzgummer's avatar
Valentin Platzgummer committed
53 54
                        std::shared_ptr<WsClient::Connection> /*connection*/,
                        std::shared_ptr<WsClient::InMessage> in_message);
55 56
} // namespace ComPrivate
} // namespace ROSBridge