TopicSubscriber.cpp 3.49 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3
#include "TopicSubscriber.h"


4 5
ROSBridge::ComPrivate::TopicSubscriber::TopicSubscriber(ROSBridge::CasePacker *casePacker,
        RosbridgeWsClient *rbc, std::mutex *rbcMutex) :
Valentin Platzgummer's avatar
Valentin Platzgummer committed
6 7
    _casePacker(casePacker)
  , _rbc(rbc)
8
  , _rbcMutex(rbcMutex)
9
  , _running(false)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
10 11 12 13
{

}

14 15 16 17 18
ROSBridge::ComPrivate::TopicSubscriber::~TopicSubscriber()
{
    this->reset();
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
19 20
void ROSBridge::ComPrivate::TopicSubscriber::start()
{
21
    _running = true;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
22 23
}

24
void ROSBridge::ComPrivate::TopicSubscriber::reset()
Valentin Platzgummer's avatar
Valentin Platzgummer committed
25
{
26
    if ( _running ){
27 28 29 30 31 32
        {
            std::lock_guard<std::mutex> lk(*_rbcMutex);
            for (std::string clientName : _clientList){
                _rbc->removeClient(clientName);
            }
        }
33
        _running = false;
34 35 36 37
        {
            std::lock_guard<std::mutex> lk(_callbackMap.mutex);
            _callbackMap.map.clear();
        }
38 39
        _clientList.clear();
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
40 41 42 43 44 45
}

bool ROSBridge::ComPrivate::TopicSubscriber::subscribe(
        const char *topic,
        const std::function<void(JsonDocUPtr)> &callback)
{
46 47 48 49 50 51 52 53 54
    if ( !_running )
        return false;

    std::string clientName = ROSBridge::ComPrivate::_topicSubscriberKey
                           + std::string(topic);
    _clientList.push_back(clientName);

    HashType hash = getHash(clientName);
    {
55 56 57 58 59 60 61
        std::lock_guard<std::mutex> lk(_callbackMap.mutex);
        auto ret = _callbackMap.map.insert(std::make_pair(hash, callback)); //
        if ( !ret.second )
            return false; // Topic subscription already present.

    }

62 63 64
    using namespace std::placeholders;
    auto f = std::bind(&ROSBridge::ComPrivate::subscriberCallback,
                      hash,
65
                      std::ref(_callbackMap),
66 67 68 69 70 71
                      _1, _2);

//    std::cout << std::endl;
//    std::cout << "topic subscription" << std::endl;
//    std::cout << "client name: " << clientName << std::endl;
//    std::cout << "topic: " << topic << std::endl;
72 73 74 75 76 77
    {
        std::lock_guard<std::mutex> lk(*_rbcMutex);
        _rbc->addClient(clientName);
        _rbc->subscribe(clientName,
                        topic,
                        f );
78
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
79 80 81 82 83 84 85 86

    return true;
}


using WsClient = SimpleWeb::SocketClient<SimpleWeb::WS>;
void ROSBridge::ComPrivate::subscriberCallback(
        const HashType &hash,
87
        ROSBridge::ComPrivate::CallbackMapWrapper &mapWrapper,
Valentin Platzgummer's avatar
Valentin Platzgummer committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        std::shared_ptr<WsClient::Connection>,
        std::shared_ptr<WsClient::InMessage> in_message)
{
    // Parse document.
    JsonDoc docFull;
    docFull.Parse(in_message->string().c_str());
    if ( docFull.HasParseError() ) {
        std::cout << "Json document has parse error: "
                  << in_message->string()
                  << std::endl;
        return;
    } else if (!docFull.HasMember("msg")) {
        std::cout << "Json document does not contain a message (\"msg\"): "
                  << in_message->string()
                  << std::endl;
        return;
    }


//    std::cout << "Json document: "
//              << in_message->string()
//              << std::endl;

    // Search callback.
112 113 114 115 116 117 118 119 120
    CallbackType callback;
    {
        std::lock_guard<std::mutex> lk(mapWrapper.mutex);
        auto it = mapWrapper.map.find(hash);
        if (it == mapWrapper.map.end()) {
            //assert(false); // callback not found
            return;
        }
        callback = it->second;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
121 122 123 124 125
    }

    // Extract message and call callback.
    JsonDocUPtr pDoc(new JsonDoc());
    pDoc->CopyFrom(docFull["msg"].Move(), docFull.GetAllocator());
126
    callback(std::move(pDoc));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
127 128 129
    return;

}