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


4

5 6 7 8 9 10 11 12 13 14 15 16 17


struct ROSBridge::ComPrivate::ThreadData
{
    const ROSBridge::CasePacker      &casePacker;
    RosbridgeWsClient                &rbc;
    std::mutex                       &rbcMutex;
    ROSBridge::ComPrivate::JsonQueue &queue;
    std::mutex                       &queueMutex;
    const std::atomic<bool>          &running;
    std::condition_variable          &cv;
};

18
ROSBridge::ComPrivate::TopicPublisher::TopicPublisher(CasePacker *casePacker,
19 20
                                                      RosbridgeWsClient *rbc,
                                                      std::mutex *rbcMutex) :
21 22 23
      _running(false)
    , _casePacker(casePacker)
    , _rbc(rbc)
24
    , _rbcMutex(rbcMutex)
25 26 27 28 29 30 31 32 33 34 35 36 37 38
{

}

ROSBridge::ComPrivate::TopicPublisher::~TopicPublisher()
{
    this->reset();
}

void ROSBridge::ComPrivate::TopicPublisher::start()
{
    if ( _running.load() )  // start called while thread running.
        return;
    _running.store(true);
39 40 41 42 43 44 45 46 47 48
    ROSBridge::ComPrivate::ThreadData data{
                *_casePacker,
                *_rbc,
                *_rbcMutex,
                _queue,
                _queueMutex,
                _running,
                _cv
    };
    _pThread = std::make_unique<std::thread>(&ROSBridge::ComPrivate::transmittLoop, data);
49 50 51 52 53 54 55
}

void ROSBridge::ComPrivate::TopicPublisher::reset()
{
    if ( !_running.load() )  // stop called while thread not running.
        return;
    _running.store(false);
56
    _cv.notify_one(); // Wake publisher thread.
57 58 59 60 61 62
    if ( !_pThread )
        return;
    _pThread->join();
    _queue.clear();
}

63
void ROSBridge::ComPrivate::transmittLoop(ROSBridge::ComPrivate::ThreadData data)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
64
{
65
    // Init.
66 67 68 69 70 71
    ClientMap           clientMap;
//    {
//        std::lock_guard<std::mutex> lk(data.rbcMutex);
//        data.rbc.addClient(ROSBridge::ComPrivate::_topicAdvertiserKey);
//        data.rbc.addClient(ROSBridge::ComPrivate::_topicPublisherKey);
//    }
72
    // Main Loop.
73 74 75 76 77
    while(data.running.load()){
        std::unique_lock<std::mutex> lk(data.queueMutex);
        // Check if new data available, wait if not.
        if (data.queue.empty()){
            data.cv.wait(lk); // Wait for condition, spurious wakeups don't matter in this case.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
78 79
            continue;
        }
80 81 82 83
        // Pop message from queue.
        JsonDocUPtr pJsonDoc(std::move(data.queue.front()));
        data.queue.pop_front();
        lk.unlock();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
84 85 86

        // Get tag from Json message and remove it.
        Tag tag;
87
        bool ret = data.casePacker.getTag(pJsonDoc, tag);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
88 89
        assert(ret); // Json message does not contain a tag;
        (void)ret;
90
        data.casePacker.removeTag(pJsonDoc);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
91 92 93 94

        // Check if topic must be advertised.
        // Advertised topics are stored in advertisedTopicsHashList as
        // a hash.
95 96 97 98 99 100 101 102 103
        std::string clientName = ROSBridge::ComPrivate::_topicAdvertiserKey
                               + tag.topic();
        HashType hash = ROSBridge::ComPrivate::getHash(clientName);
        auto it = clientMap.find(hash);
        if ( it == clientMap.end()) { // Need to advertise topic?
            clientMap.insert(std::make_pair(hash, clientName));
            std::cout << clientName << ";"
                      << tag.topic() << ";"
                      << tag.messageType() << ";" << std::endl;
104 105
            {
                std::lock_guard<std::mutex> lk(data.rbcMutex);
106 107
                data.rbc.addClient(clientName);
                data.rbc.advertise(clientName,
108 109 110
                                   tag.topic(),
                                   tag.messageType() );
            }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
111
        }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
112

113
        // Publish Json message.
114 115 116 117
        {
            std::lock_guard<std::mutex> lk(data.rbcMutex);
            data.rbc.publish(tag.topic(), *pJsonDoc.get());
        }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
118
    } // while loop
119 120 121 122

    // Tidy up.
    {
        std::lock_guard<std::mutex> lk(data.rbcMutex);
123 124
        for (auto& it : clientMap)
            data.rbc.removeClient(it.second);
125
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
126 127
}