TopicPublisher.cpp 2.58 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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43


void ROSBridge::ComPrivate::transmittLoop(const ROSBridge::CasePacker &casePacker,
                                          RosbridgeWsClient &rbc,
                                          ROSBridge::ComPrivate::JsonQueue &queue,
                                          std::mutex &queueMutex,
                                          const std::atomic<bool> &stopFlag)
{
    rbc.addClient(ROSBridge::ComPrivate::_topicPublisherKey);

    while(!stopFlag.load()){
        // Pop message from queue.
        queueMutex.lock();
        //std::cout << "Queue size: " << queue.size() << std::endl;
        if (queue.empty()){
            queueMutex.unlock();
            std::this_thread::sleep_for(std::chrono::milliseconds(20));
            continue;
        }
        JsonDocUPtr pJsonDoc(std::move(queue.front()));
        queue.pop_front();
        queueMutex.unlock();

        // Debug output.
//        std::cout << "Transmitter loop json document:" << std::endl;
//        rapidjson::OStreamWrapper out(std::cout);
//        rapidjson::Writer<rapidjson::OStreamWrapper> writer(out);
//        pJsonDoc->Accept(writer);
//        std::cout << std::endl << std::endl;

        // Get tag from Json message and remove it.
        Tag tag;
        bool ret = casePacker.getTag(*pJsonDoc.get(), tag);
        assert(ret); // Json message does not contain a tag;
        (void)ret;
        casePacker.removeTag(*pJsonDoc.get());

        // Send Json message.
        rbc.publish(tag.topic(), *pJsonDoc.get());
    }
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
44 45
ROSBridge::ComPrivate::TopicPublisher::TopicPublisher(CasePacker *casePacker,
                                                      RosbridgeWsClient *rbc) :
Valentin Platzgummer's avatar
Valentin Platzgummer committed
46 47 48 49 50 51 52
      _stopFlag(true)
    , _casePacker(casePacker)
    , _rbc(rbc)
{

}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
53 54 55 56 57 58
ROSBridge::ComPrivate::TopicPublisher::~TopicPublisher()
{
    this->stop();
}

void ROSBridge::ComPrivate::TopicPublisher::start()
Valentin Platzgummer's avatar
Valentin Platzgummer committed
59 60 61 62 63 64 65 66 67 68 69 70
{
    if ( !_stopFlag.load() )  // start called while thread running.
        return;
    _stopFlag.store(false);
    _pThread.reset(new std::thread(&ROSBridge::ComPrivate::transmittLoop,
                                   std::cref(*_casePacker),
                                   std::ref(*_rbc),
                                   std::ref(_queue),
                                   std::ref(_queueMutex),
                                   std::cref(_stopFlag)));
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
71
void ROSBridge::ComPrivate::TopicPublisher::stop()
Valentin Platzgummer's avatar
Valentin Platzgummer committed
72 73 74 75 76 77 78 79 80
{
    if ( _stopFlag.load() )  // start called while thread not running.
        return;
    _stopFlag.store(true);
    if ( !_pThread )
        return;
    _pThread->join();
    _pThread.reset();
}