TopicSubscriber.h 1.5 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12
#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 {

class TopicSubscriber
{
13
    typedef std::vector<std::string> ClientList;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
14 15 16 17 18 19
public:    
    typedef std::function<void(JsonDocUPtr)> CallbackType;
    typedef std::map<HashType, CallbackType> CallbackMap;

    TopicSubscriber() = delete;
    TopicSubscriber(CasePacker *casePacker, RosbridgeWsClient *rbc);
20
    ~TopicSubscriber();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
21

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

    //! @brief Resets the subscriber.
    void reset();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
27 28 29 30 31 32 33 34 35 36

    //! @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:
    CasePacker         *_casePacker;
    RosbridgeWsClient  *_rbc;
    CallbackMap         _callbackMap;
37 38
    ClientList          _clientList;
    bool                _running;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
39 40 41 42 43 44 45
};

void subscriberCallback(const HashType &hash,
                        const TopicSubscriber::CallbackMap &map,
                        std::shared_ptr<WsClient::Connection> /*connection*/,
                        std::shared_ptr<WsClient::InMessage> in_message);

46 47 48 49
void test(std::shared_ptr<WsClient::Connection> /*connection*/,
                        std::shared_ptr<WsClient::InMessage> in_message);
} // namespace ComPrivate
} // namespace ROSBridge