TopicPublisher.cpp 3.49 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 44 45 46 47

ROSBridge::ComPrivate::TopicPublisher::TopicPublisher(CasePacker *casePacker,
                                                      RosbridgeWsClient *rbc) :
      _running(false)
    , _casePacker(casePacker)
    , _rbc(rbc)
{

}

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

void ROSBridge::ComPrivate::TopicPublisher::start()
{
    if ( _running.load() )  // start called while thread running.
        return;
    _running.store(true);
    _rbc->addClient(ROSBridge::ComPrivate::_topicAdvertiserKey);
    _pThread.reset(new std::thread(&ROSBridge::ComPrivate::transmittLoop,
                                   std::cref(*_casePacker),
                                   std::ref(*_rbc),
                                   std::ref(_queue),
                                   std::ref(_queueMutex),
                                   std::ref(_advertisedTopicsHashList),
                                   std::cref(_running)));
}

void ROSBridge::ComPrivate::TopicPublisher::reset()
{
    if ( !_running.load() )  // stop called while thread not running.
        return;
    _running.store(false);
    if ( !_pThread )
        return;
    _pThread->join();
    _pThread.reset();    
    _rbc->removeClient(ROSBridge::ComPrivate::_topicAdvertiserKey);
    _queue.clear();
    _advertisedTopicsHashList.clear();
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
48 49 50 51
void ROSBridge::ComPrivate::transmittLoop(const ROSBridge::CasePacker &casePacker,
                                          RosbridgeWsClient &rbc,
                                          ROSBridge::ComPrivate::JsonQueue &queue,
                                          std::mutex &queueMutex,
Valentin Platzgummer's avatar
Valentin Platzgummer committed
52
                                          HashSet &advertisedTopicsHashList,
53
                                          const std::atomic<bool> &running)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
54 55 56
{
    rbc.addClient(ROSBridge::ComPrivate::_topicPublisherKey);

57
    while(running.load()){
Valentin Platzgummer's avatar
Valentin Platzgummer committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        // 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;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
79
        bool ret = casePacker.getTag(pJsonDoc, tag);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
80 81
        assert(ret); // Json message does not contain a tag;
        (void)ret;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95
        casePacker.removeTag(pJsonDoc);

        // Check if topic must be advertised.
        // Advertised topics are stored in advertisedTopicsHashList as
        // a hash.
        HashType hash = ROSBridge::ComPrivate::getHash(tag.topic());
        if ( advertisedTopicsHashList.count(hash) == 0) {
            advertisedTopicsHashList.insert(hash);
            rbc.advertise(ROSBridge::ComPrivate::_topicAdvertiserKey,
                          tag.topic(),
                          tag.messageType() );
        }
        // Debug output.
        //std::cout << "Hash Set size: " << advertisedTopicsHashList.size() << std::endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
96 97 98

        // Send Json message.
        rbc.publish(tag.topic(), *pJsonDoc.get());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
99
    } // while loop
Valentin Platzgummer's avatar
Valentin Platzgummer committed
100 101
}