ROSBridge.h 1.85 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6
#pragma once

#include "ros_bridge/rapidjson/include/rapidjson/document.h"
#include "ros_bridge/include/CasePacker.h"
#include "ros_bridge/include/TypeFactory.h"
#include "ros_bridge/include/JsonFactory.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
7
#include "ros_bridge/include/TopicPublisher.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
8
#include "ros_bridge/include/TopicSubscriber.h"
9
#include "ros_bridge/include/Server.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
10 11

#include <memory>
12
#include <functional>
Valentin Platzgummer's avatar
Valentin Platzgummer committed
13 14 15 16 17 18 19 20 21 22

namespace ROSBridge {
class ROSBridge
{
public:    
    typedef MessageTag Tag;
    typedef rapidjson::Document JsonDoc;
    typedef std::unique_ptr<JsonDoc> JsonDocUPtr;

    explicit ROSBridge();
23
    explicit ROSBridge(const char* connectionString);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
24 25

    template<class T>
Valentin Platzgummer's avatar
Valentin Platzgummer committed
26 27
    void publish(T &msg, const std::string &topic){
        _topicPublisher.publish(msg, topic);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
28
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
29
    void publish(JsonDocUPtr doc);
30 31 32 33 34
    void subscribe(const char *topic,
                   const std::function<void(JsonDocUPtr)> &callBack);
    void advertiseService(const std::string &service,
                          const std::string &type,
                          const std::function<JsonDocUPtr(JsonDocUPtr)> &callback);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
35 36

    const CasePacker *casePacker() const;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
37

38
    //! @brief Start ROS bridge.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
39
    void start();
40
    //! @brief Stops ROS bridge.
41
    void reset();
42 43
    //! @return Returns true if connected to the rosbridge_server, false either.
    //! @note This function can block up to 100ms. However normal execution time is smaller.
44
    bool connected();
45 46

    bool isRunning();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
47 48

private:
49
    std::shared_ptr<std::atomic_bool> _stopped;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
50 51 52 53
    TypeFactory             _typeFactory;
    JsonFactory             _jsonFactory;
    CasePacker              _casePacker;
    RosbridgeWsClient       _rbc;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
54 55
    ComPrivate::TopicPublisher     _topicPublisher;
    ComPrivate::TopicSubscriber    _topicSubscriber;
56
    ComPrivate::Server             _server;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
57 58

};
59 60

bool isValidConnectionString(const char* connectionString);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
61
}