TopicSubscriber.cpp 3.22 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8
#include "TopicSubscriber.h"


ROSBridge::ComPrivate::TopicSubscriber::TopicSubscriber(
        ROSBridge::CasePacker *casePacker,
        RosbridgeWsClient *rbc) :
    _casePacker(casePacker)
  , _rbc(rbc)
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 27 28 29 30 31 32
    if ( _running ){
        for (std::string clientName : _clientList)
            _rbc->removeClient(clientName);
        _running = false;
        _callbackMap.clear();
        _clientList.clear();
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
33 34 35 36 37 38
}

bool ROSBridge::ComPrivate::TopicSubscriber::subscribe(
        const char *topic,
        const std::function<void(JsonDocUPtr)> &callback)
{
39 40 41 42 43 44 45 46
    if ( !_running )
        return false;

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

    HashType hash = getHash(clientName);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
47 48 49 50
    auto ret = _callbackMap.insert(std::make_pair(hash, callback)); //
    if ( !ret.second )
        return false; // Topic subscription already present.

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    {
    using namespace std::placeholders;
    auto f = std::bind(&ROSBridge::ComPrivate::subscriberCallback,
                      hash,
                      std::cref(_callbackMap),
                      _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;
    _rbc->addClient(clientName);
    _rbc->subscribe(clientName,
                    topic,
                    f );
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    return true;
}


using WsClient = SimpleWeb::SocketClient<SimpleWeb::WS>;
void ROSBridge::ComPrivate::subscriberCallback(
        const HashType &hash,
        const ROSBridge::ComPrivate::TopicSubscriber::CallbackMap &map,
        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.
    auto it = map.find(hash);
    if (it == map.end()) {
        assert(false); // callback not found
        return;
    }

    // Extract message and call callback.
    JsonDocUPtr pDoc(new JsonDoc());
    pDoc->CopyFrom(docFull["msg"].Move(), docFull.GetAllocator());
    it->second(std::move(pDoc)); // Call callback.
    return;

}
113 114 115 116 117 118 119 120 121 122

void ROSBridge::ComPrivate::test(
        std::shared_ptr<WsClient::Connection>,
        std::shared_ptr<WsClient::InMessage> in_message)
{

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