TopicPublisher.cpp 3.92 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 18


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

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

}

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

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

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

66
void ROSBridge::ComPrivate::transmittLoop(ROSBridge::ComPrivate::ThreadData data)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
67
{
68 69 70 71 72 73 74
    // Init.
    {
        std::lock_guard<std::mutex> lk(data.rbcMutex);
        data.rbc.addClient(ROSBridge::ComPrivate::_topicAdvertiserKey);
        data.rbc.addClient(ROSBridge::ComPrivate::_topicPublisherKey);
    }
    // Main Loop.
75 76 77 78 79
    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
80 81
            continue;
        }
82 83 84 85
        // Pop message from queue.
        JsonDocUPtr pJsonDoc(std::move(data.queue.front()));
        data.queue.pop_front();
        lk.unlock();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
86 87 88

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

        // Check if topic must be advertised.
        // Advertised topics are stored in advertisedTopicsHashList as
        // a hash.
        HashType hash = ROSBridge::ComPrivate::getHash(tag.topic());
98 99 100
        if ( data.advertisedTopicsHashList.count(hash) == 0) {
            data.advertisedTopicsHashList.insert(hash);
            {
101 102 103
                std::cout << ROSBridge::ComPrivate::_topicAdvertiserKey << ";"
                          << tag.topic() << ";"
                          << tag.messageType() << ";" << std::endl;
104 105 106 107 108
                std::lock_guard<std::mutex> lk(data.rbcMutex);
                data.rbc.advertise(ROSBridge::ComPrivate::_topicAdvertiserKey,
                                   tag.topic(),
                                   tag.messageType() );
            }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
109
        }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
110

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

    // Tidy up.
    {
        std::lock_guard<std::mutex> lk(data.rbcMutex);
        data.rbc.removeClient(ROSBridge::ComPrivate::_topicAdvertiserKey);
        data.rbc.removeClient(ROSBridge::ComPrivate::_topicPublisherKey);
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
124 125
}