RosBridgeClient.h 24.6 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#pragma once

/*
*  Created on: Apr 16, 2018
*      Author: Poom Pianpak
*/

#include "Simple-WebSocket-Server/client_ws.hpp"

#include "rapidjson/include/rapidjson/document.h"
#include "rapidjson/include/rapidjson/writer.h"
#include "rapidjson/include/rapidjson/stringbuffer.h"

#include <chrono>
#include <functional>
#include <thread>
17
#include <future>
18
#include <mutex>
19 20
#include <tuple>
#include <deque>
Valentin Platzgummer's avatar
Valentin Platzgummer committed
21 22 23 24

using WsClient = SimpleWeb::SocketClient<SimpleWeb::WS>;
using InMessage =  std::function<void(std::shared_ptr<WsClient::Connection>, std::shared_ptr<WsClient::InMessage>)>;

25 26 27 28 29 30
template <typename T>
constexpr typename std::underlying_type<T>::type integral(T value)
{
    return static_cast<typename std::underlying_type<T>::type>(value);
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
31 32
class RosbridgeWsClient
{
33 34 35 36 37 38 39 40 41 42 43
  using TopicData = std::tuple<std::string /*topic*/,
                               std::string /*client name*/,
                               std::shared_ptr<WsClient> /*client*/,
                               std::shared_ptr<std::atomic_bool> /*topic ready*/>;
  enum class TopicEnum{
      TopicName = 0,
      ClientName = 1,
      Client = 2,
      Ready = 3
  };

44
  std::string server_port_path;
45 46 47 48
  std::unordered_map<std::string /*client name*/,
                     std::shared_ptr<WsClient> /*client*/> client_map;
  std::deque<TopicData> advertised_topics_list;
  std::mutex mutex;
49

50 51 52
  // Set to true on construction and to false on destruction. Used to notify independet threads.
  std::shared_ptr<std::atomic_bool> alive;

53 54 55 56 57 58
  void start(const std::string& client_name, std::shared_ptr<WsClient> client, const std::string& message)
  {
    if (!client->on_open)
    {
#ifdef DEBUG
      client->on_open = [client_name, message](std::shared_ptr<WsClient::Connection> connection) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
59
#else
60
      client->on_open = [message](std::shared_ptr<WsClient::Connection> connection) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
61 62
#endif

63 64 65
#ifdef DEBUG
        std::cout << client_name << ": Opened connection" << std::endl;
        std::cout << client_name << ": Sending message: " << message << std::endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
66
#endif
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
        connection->send(message);
      };
    }

#ifdef DEBUG
    if (!client->on_message)
    {
      client->on_message = [client_name](std::shared_ptr<WsClient::Connection> /*connection*/, std::shared_ptr<WsClient::InMessage> in_message) {
        std::cout << client_name << ": Message received: " << in_message->string() << std::endl;
      };
    }

    if (!client->on_close)
    {
      client->on_close = [client_name](std::shared_ptr<WsClient::Connection> /*connection*/, int status, const std::string & /*reason*/) {
        std::cout << client_name << ": Closed connection with status code " << status << std::endl;
      };
    }

    if (!client->on_error)
    {
      // See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings
      client->on_error = [client_name](std::shared_ptr<WsClient::Connection> /*connection*/, const SimpleWeb::error_code &ec) {
        std::cout << client_name << ": Error: " << ec << ", error message: " << ec.message() << std::endl;
      };
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
93

94
#endif
95 96
#ifdef DEBUG
    std::thread client_thread([client_name, client]() {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
97
#else
98
    std::thread client_thread([client]() {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
99
#endif
100
      client->start();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
101

102 103
#ifdef DEBUG
      std::cout << client_name << ": Terminated" << std::endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
104
#endif
105 106 107 108 109
      client->on_open = NULL;
      client->on_message = NULL;
      client->on_close = NULL;
      client->on_error = NULL;
    });
Valentin Platzgummer's avatar
Valentin Platzgummer committed
110

111 112
    client_thread.detach();
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
113 114

public:
115 116
  RosbridgeWsClient(const std::string& server_port_path) :
      alive(std::make_shared<std::atomic_bool>(true))
117 118 119 120 121 122
  {
    this->server_port_path = server_port_path;
  }

  ~RosbridgeWsClient()
  {
123 124 125 126
    for (auto& topicData : advertised_topics_list){
        unadvertise(std::get<integral(TopicEnum::ClientName)>(topicData),
                    std::get<integral(TopicEnum::TopicName)>(topicData));
    }
127 128
    for (auto& client : client_map)
    {
129
      removeClient(client.first);
130
    }
131
    alive->store(false);
132
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
 // The execution can take up to 100 ms!
 bool connected(){

    auto p = std::make_shared<std::promise<void>>();
    auto future = p->get_future();

    auto callback = [](std::shared_ptr<WsClient::Connection> connection, std::shared_ptr<std::promise<void>> p) {
        p->set_value();
        connection->send_close(1000);
    };
    std::shared_ptr<WsClient> status_client = std::make_shared<WsClient>(server_port_path);
    status_client->on_open = std::bind(callback, std::placeholders::_1, p);

    std::async([status_client]{

        status_client->start();
        status_client->on_open = NULL;
        status_client->on_message = NULL;
        status_client->on_close = NULL;
        status_client->on_error = NULL;

    });

    auto status = future.wait_for(std::chrono::milliseconds(100));
    return status == std::future_status::ready;
 }

161 162 163
  // Adds a client to the client_map.
  void addClient(const std::string& client_name)
  {
164
    std::lock_guard<std::mutex> lk(mutex);
165 166 167 168 169 170 171 172 173 174
    std::unordered_map<std::string, std::shared_ptr<WsClient>>::iterator it = client_map.find(client_name);
    if (it == client_map.end())
    {
      client_map[client_name] = std::make_shared<WsClient>(server_port_path);
    }
#ifdef DEBUG
    else
    {
      std::cerr << client_name << " has already been created" << std::endl;
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
175
#endif
176 177 178 179
  }

  std::shared_ptr<WsClient> getClient(const std::string& client_name)
  {
180
    std::lock_guard<std::mutex> lk(mutex);
181 182 183 184 185 186 187 188 189 190
    std::unordered_map<std::string, std::shared_ptr<WsClient>>::iterator it = client_map.find(client_name);
    if (it != client_map.end())
    {
      return it->second;
    }
    return NULL;
  }

  void stopClient(const std::string& client_name)
  {
191
    std::lock_guard<std::mutex> lk(mutex);
192 193 194 195 196 197 198 199 200 201
    std::unordered_map<std::string, std::shared_ptr<WsClient>>::iterator it = client_map.find(client_name);
    if (it != client_map.end())
    {
      it->second->stop();
      it->second->on_open = NULL;
      it->second->on_message = NULL;
      it->second->on_close = NULL;
      it->second->on_error = NULL;
#ifdef DEBUG
      std::cout << client_name << " has been stopped" << std::endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
202
#endif
203 204 205 206 207 208
    }
#ifdef DEBUG
    else
    {
      std::cerr << client_name << " has not been created" << std::endl;
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
209
#endif
210 211 212 213
  }

  void removeClient(const std::string& client_name)
  {
214
    std::lock_guard<std::mutex> lk(mutex);
215 216 217 218 219
    std::unordered_map<std::string, std::shared_ptr<WsClient>>::iterator it = client_map.find(client_name);
    if (it != client_map.end())
    {
      // Stop the client asynchronously in 100 ms.
      // This is to ensure, that all threads involving the client have been launched.
220 221 222 223 224 225 226 227
      std::shared_ptr<WsClient> client = it->second;
#ifdef DEBUG
      std::thread t([client, client_name](){
#else
      std::thread t([client](){
#endif
          std::this_thread::sleep_for(std::chrono::milliseconds(100));
          client->stop();
228 229
#ifdef DEBUG
      std::cout << client_name << " has been removed" << std::endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
230
#endif
231 232 233
      });
      client_map.erase(it);
      t.detach();
234 235 236 237 238 239
    }
#ifdef DEBUG
    else
    {
      std::cerr << client_name << " has not been created" << std::endl;
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
240
#endif
241 242
  }

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
  //!
  //! \brief Returns a string containing all advertised topics.
  //! \return Returns a string containing all advertised topics.
  //!
  //! \note This function will wait until the /rosapi/topics service is available.
  //! \note Call connected() in advance to ensure that a connection was established.
  //!
  std::string get_advertised_topics(){
      this->waitForService("/rosapi/topics");
      std::shared_ptr<std::promise<std::string>> promise;
      auto future = promise->get_future();
      // Call /rosapi/topics to see if topic is already available.
      this->callService("/rosapi/topics", [promise](
                        std::shared_ptr<WsClient::Connection> connection,
                        std::shared_ptr<WsClient::InMessage> in_message){
          promise->set_value(in_message->string());
          connection->send_close(1000);
      });
      future.wait();
      return future.get();
  }

  //!
  //! \brief Returns a string containing all advertised services.
  //! \return Returns a string containing all advertised services.
  //!
  //! \note This function will wait until the /rosapi/services service is available.
  //! \note Call connected() in advance to ensure that a connection was established.
  //!
  std::string get_advertised_services(){
      this->waitForService("/rosapi/services");
      std::shared_ptr<std::promise<std::string>> promise;
      auto future = promise->get_future();
      // Call /rosapi/services to see if topic is already available.
      this->callService("/rosapi/services", [promise](
                        std::shared_ptr<WsClient::Connection> connection,
                        std::shared_ptr<WsClient::InMessage> in_message){
          promise->set_value(in_message->string());
          connection->send_close(1000);
      });
      future.wait();
      return future.get();
  }

287 288 289 290
  // Gets the client from client_map and starts it. Advertising is essentially sending a message.
  // One client per topic!
  void advertise(const std::string& client_name, const std::string& topic, const std::string& type, const std::string& id = "")
  {
291 292 293
    std::lock_guard<std::mutex> lk(mutex);
    std::unordered_map<std::string, std::shared_ptr<WsClient>>::iterator it_client = client_map.find(client_name);
    if (it_client != client_map.end())
294
    {
295 296 297 298 299 300 301 302 303 304 305 306 307 308
      auto it_topic = std::find_if(advertised_topics_list.begin(),
                                   advertised_topics_list.end(),
                                   [topic](const TopicData &td){
          return topic == std::get<integral(TopicEnum::TopicName)>(td);
      });
      if ( it_topic != advertised_topics_list.end()){
#ifdef DEBUG
            std::cerr << "topic: " << topic << " already advertised" << std::endl;
#endif
            return;
      }
      auto client = it_client->second;
      auto ready = std::make_shared<std::atomic_bool>(false);
      advertised_topics_list.push_back(std::make_tuple(topic, client_name, client, ready));
309

310
      std::string message = "\"op\":\"advertise\", \"topic\":\"" + topic + "\", \"type\":\"" + type + "\"";
311 312 313 314 315 316
      if (id.compare("") != 0)
      {
        message += ", \"id\":\"" + id + "\"";
      }
      message = "{" + message + "}";

317
#ifdef DEBUG
318
      client->on_open = [this, topic, message, ready, client_name](std::shared_ptr<WsClient::Connection> connection) {
319
#else
320
      client->on_open = [this, topic, message, ready](std::shared_ptr<WsClient::Connection> connection) {
321 322 323 324 325 326 327
#endif

#ifdef DEBUG
        std::cout << client_name << ": Opened connection" << std::endl;
        std::cout << client_name << ": Sending message: " << message << std::endl;
#endif
        connection->send(message);
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
        // Now wait until topic is successfully advertised.
        while (true){
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
            std::string advertised_topics = this->get_advertised_topics();
            auto pos = advertised_topics.find(topic);
            if (pos != std::string::npos){ // topic advertised!
#ifdef DEBUG
                std::cout << client_name << ": topic " << topic  << "successfully advertised" << std::endl;
#endif
                break;
            }
#ifdef DEBUG
                std::cout << client_name << ": waiting for topic " << topic  << "to be advertised" << std::endl;
#endif
        }
        ready->store(true); // Mark topic as successfully advertised.
344 345 346
      };

      start(client_name, client, message);
347 348 349 350 351 352
    }
#ifdef DEBUG
    else
    {
      std::cerr << client_name << "has not been created" << std::endl;
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
353
#endif
354
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
355

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
  void unadvertise(const std::string& client_name,
                   const std::string& topic,
                   const std::string& id = ""){
      std::lock_guard<std::mutex> lk(mutex);
      std::unordered_map<std::string, std::shared_ptr<WsClient>>::iterator it_client = client_map.find(client_name);
      if (it_client != client_map.end())
      {
          // Topic advertised?
          auto it_topic = std::find_if(advertised_topics_list.begin(),
                                       advertised_topics_list.end(),
                                       [topic](const TopicData &td){
              return topic == std::get<integral(TopicEnum::TopicName)>(td);
          });
        if ( it_topic == advertised_topics_list.end()){
  #ifdef DEBUG
              std::cerr << "topic: " << topic << " not advertised" << std::endl;
  #endif
              return;
        }

        std::string message = "\"op\":\"unadvertise\"";
        if (id.compare("") != 0)
        {
          message += ", \"id\":\"" + id + "\"";
        }
        message += ", \"topic\":\"" + topic + "\"";
        message = "{" + message + "}";

        auto client = it_client->second;
        auto ready = std::get<integral(TopicEnum::Ready)>(*it_topic);
  #ifdef DEBUG
        client->on_open = [client_name, message, ready](std::shared_ptr<WsClient::Connection> connection) {
  #else
        client->on_open = [message, ready](std::shared_ptr<WsClient::Connection> connection) {
  #endif

  #ifdef DEBUG
          std::cout << client_name << ": Opened connection" << std::endl;
          std::cout << client_name << ": Sending message: " << message << std::endl;
  #endif
          while ( !ready->load() ){ // Wait for topic to be advertised.
              std::this_thread::sleep_for(std::chrono::milliseconds(500));
          }
          connection->send(message);
        };

        start(client_name, client, message);
        advertised_topics_list.erase(it_topic);
      }
  #ifdef DEBUG
      else
      {
        std::cerr << client_name << "has not been created" << std::endl;
      }
  #endif

  }

414 415
  void publish(const std::string& topic, const rapidjson::Document& msg, const std::string& id = "")
  {
416 417 418 419 420 421 422 423 424 425 426 427
    std::lock_guard<std::mutex> lk(mutex);
    auto it_topic = std::find_if(advertised_topics_list.begin(),
                                 advertised_topics_list.end(),
                                 [&topic](const TopicData &td){
        return topic == std::get<integral(TopicEnum::TopicName)>(td);
    });
    if ( it_topic == advertised_topics_list.end() ){
#ifdef DEBUG
        std::cerr << "topic: " << topic << " not yet advertised" << std::endl;
#endif
        return;
    }
428 429 430
    rapidjson::StringBuffer strbuf;
    rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
    msg.Accept(writer);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
431

432
    std::string client_name = "publish_client" + topic;
433
    std::string message = "\"op\":\"publish\", \"topic\":\"" + topic + "\", \"msg\":" + strbuf.GetString();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
434

435 436 437 438 439
    if (id.compare("") != 0)
    {
      message += ", \"id\":\"" + id + "\"";
    }
    message = "{" + message + "}";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
440

441
    std::shared_ptr<WsClient> publish_client = std::make_shared<WsClient>(server_port_path);
442 443
    auto ready = std::get<integral(TopicEnum::Ready)>(*it_topic);
    publish_client->on_open = [message, client_name, ready](std::shared_ptr<WsClient::Connection> connection) {
444
#ifdef DEBUG
445 446 447
      std::cout << client_name << ": Opened connection" << std::endl;
      std::cout << client_name << ": Sending message." << std::endl;
      //std::cout << client_name << ": Sending message: " << message << std::endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
448
#endif
449 450
      while ( !ready->load() ){ // Wait for the topic to be successfully advertised.
          std::this_thread::sleep_for(std::chrono::milliseconds(100));
451
      }
452
      connection->send(message);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
453

454 455 456
      // TODO: This could be improved by creating a thread to keep publishing the message instead of closing it right away
      connection->send_close(1000);
    };
Valentin Platzgummer's avatar
Valentin Platzgummer committed
457

458
    start(client_name, publish_client, message);
459 460 461 462
  }

  void subscribe(const std::string& client_name, const std::string& topic, const InMessage& callback, const std::string& id = "", const std::string& type = "", int throttle_rate = -1, int queue_length = -1, int fragment_size = -1, const std::string& compression = "")
  {
463
    std::lock_guard<std::mutex> lk(mutex);
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    std::unordered_map<std::string, std::shared_ptr<WsClient>>::iterator it = client_map.find(client_name);
    if (it != client_map.end())
    {
      std::string message = "\"op\":\"subscribe\", \"topic\":\"" + topic + "\"";

      if (id.compare("") != 0)
      {
        message += ", \"id\":\"" + id + "\"";
      }
      if (type.compare("") != 0)
      {
        message += ", \"type\":\"" + type + "\"";
      }
      if (throttle_rate > -1)
      {
        message += ", \"throttle_rate\":" + std::to_string(throttle_rate);
      }
      if (queue_length > -1)
      {
        message += ", \"queue_length\":" + std::to_string(queue_length);
      }
      if (fragment_size > -1)
      {
        message += ", \"fragment_size\":" + std::to_string(fragment_size);
      }
      if (compression.compare("none") == 0 || compression.compare("png") == 0)
      {
        message += ", \"compression\":\"" + compression + "\"";
      }
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
      message = "{" + message + "}";    

      auto client = it->second;
      client->on_message = callback;
      const auto cAlive = this->alive;
      // Start thread to wait for topic to be advertised.
      std::thread t([this, client_name, client, message, cAlive, topic](){
          while (cAlive->load()){ // RosBridgeClient alive?
              auto advertised_topics = this->get_advertised_topics();
              auto pos = advertised_topics.find(topic);
              if (pos != std::string::npos){ // desired topic available!
#ifdef DEBUG
                std::cout << client_name << ": topic " << topic  << "available" << std::endl;
#endif
                break;
            }
#ifdef DEBUG
                std::cout << client_name << ": waiting for topic " << topic << std::endl;
#endif
                std::this_thread::sleep_for(std::chrono::seconds(1));
          }
514

515 516 517 518 519 520
         if ( cAlive->load() ){
            this->start(client_name, client, message); // subscribe to topic
         }
      });

      t.detach();
521 522 523 524 525 526
    }
#ifdef DEBUG
    else
    {
      std::cerr << client_name << "has not been created" << std::endl;
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
527
#endif
528 529 530 531
  }

  void advertiseService(const std::string& client_name, const std::string& service, const std::string& type, const InMessage& callback)
  {
532
    std::lock_guard<std::mutex> lk(mutex);
533 534 535 536 537 538 539 540 541 542 543 544 545
    std::unordered_map<std::string, std::shared_ptr<WsClient>>::iterator it = client_map.find(client_name);
    if (it != client_map.end())
    {
      std::string message = "{\"op\":\"advertise_service\", \"service\":\"" + service + "\", \"type\":\"" + type + "\"}";

      it->second->on_message = callback;
      start(client_name, it->second, message);
    }
#ifdef DEBUG
    else
    {
      std::cerr << client_name << "has not been created" << std::endl;
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
546
#endif
547
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
548

549
  void serviceResponse(const std::string& service, const std::string& id, bool result, const rapidjson::Document& values)
550 551
  {
    std::string message = "\"op\":\"service_response\", \"service\":\"" + service + "\", \"result\":" + ((result)? "true" : "false");
Valentin Platzgummer's avatar
Valentin Platzgummer committed
552

553 554 555
    // Rosbridge somehow does not allow service_response to be sent without id and values
    // , so we cannot omit them even though the documentation says they are optional.
    message += ", \"id\":\"" + id + "\"";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
556

557 558 559 560
    // Convert JSON document to string
    rapidjson::StringBuffer strbuf;
    rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
    values.Accept(writer);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
561

562 563
    message += ", \"values\":" + std::string(strbuf.GetString());
    message = "{" + message + "}";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
564

565
    std::shared_ptr<WsClient> service_response_client = std::make_shared<WsClient>(server_port_path);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
566

567 568 569 570
    service_response_client->on_open = [message](std::shared_ptr<WsClient::Connection> connection) {
#ifdef DEBUG
      std::cout << "service_response_client: Opened connection" << std::endl;
      std::cout << "service_response_client: Sending message: " << message << std::endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
571
#endif
572
      connection->send(message);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
573

574 575
      connection->send_close(1000);
    };
Valentin Platzgummer's avatar
Valentin Platzgummer committed
576

577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    start("service_response_client", service_response_client, message);
  }

  void callService(const std::string& service, const InMessage& callback, const rapidjson::Document& args = {}, const std::string& id = "", int fragment_size = -1, const std::string& compression = "")
  {
    std::string message = "\"op\":\"call_service\", \"service\":\"" + service + "\"";

    if (!args.IsNull())
    {
      rapidjson::StringBuffer strbuf;
      rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
      args.Accept(writer);

      message += ", \"args\":" + std::string(strbuf.GetString());
    }
    if (id.compare("") != 0)
    {
      message += ", \"id\":\"" + id + "\"";
    }
    if (fragment_size > -1)
    {
      message += ", \"fragment_size\":" + std::to_string(fragment_size);
    }
    if (compression.compare("none") == 0 || compression.compare("png") == 0)
    {
      message += ", \"compression\":\"" + compression + "\"";
    }
    message = "{" + message + "}";

    std::shared_ptr<WsClient> call_service_client = std::make_shared<WsClient>(server_port_path);

    if (callback)
    {
      call_service_client->on_message = callback;
    }
    else
    {
      call_service_client->on_message = [](std::shared_ptr<WsClient::Connection> connection, std::shared_ptr<WsClient::InMessage> in_message) {
#ifdef DEBUG
        std::cout << "call_service_client: Message received: " << in_message->string() << std::endl;
        std::cout << "call_service_client: Sending close connection" << std::endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
618
#endif
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
        connection->send_close(1000);
      };
    }

    start("call_service_client", call_service_client, message);
  }

  //!
  //! \brief Checks if the service \p service is available.
  //! \param service Service name.
  //! \return Returns true if the service is available, false either.
  //! \note Don't call this function to frequently. Use \fn waitForService() instead.
  //!
  bool serviceAvailable(const std::string& service)
  {
    const rapidjson::Document args = {};
    const std::string& id = "";
    const int fragment_size = -1;
    const std::string& compression = "";
    std::string message = "\"op\":\"call_service\", \"service\":\"" + service + "\"";
    std::string client_name("wait_for_service_client");

    if (!args.IsNull())
    {
      rapidjson::StringBuffer strbuf;
      rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
      args.Accept(writer);

      message += ", \"args\":" + std::string(strbuf.GetString());
    }
    if (id.compare("") != 0)
    {
      message += ", \"id\":\"" + id + "\"";
    }
    if (fragment_size > -1)
    {
      message += ", \"fragment_size\":" + std::to_string(fragment_size);
    }
    if (compression.compare("none") == 0 || compression.compare("png") == 0)
    {
      message += ", \"compression\":\"" + compression + "\"";
    }
    message = "{" + message + "}";

    std::shared_ptr<WsClient> wait_for_service_client = std::make_shared<WsClient>(server_port_path);
    std::shared_ptr<std::promise<bool>> p(std::make_shared<std::promise<bool>>());
    auto future = p->get_future();
#ifdef DEBUG
    wait_for_service_client->on_message = [p, service, client_name](
#else
    wait_for_service_client->on_message = [p](
#endif
            std::shared_ptr<WsClient::Connection> connection,
            std::shared_ptr<WsClient::InMessage> in_message){
#ifdef DEBUG
#endif
        rapidjson::Document doc;
        doc.Parse(in_message->string().c_str());
        if ( !doc.HasParseError()
             && doc.HasMember("result")
             && doc["result"].IsBool()
             && doc["result"] == true )
        {
#ifdef DEBUG
            std::cout << client_name << ": "
                      << "service " << service
                      << " available." << std::endl;
            std::cout << client_name << ": "
                      << "message: " << in_message->string()
                      << std::endl;
#endif
            p->set_value(true);
        } else {
            p->set_value(false);
        }
        connection->send_close(1000);
    };
    start(client_name, wait_for_service_client, message);
    return future.get();
  }

  //!
  //! \brief Waits until the service with the name \p service is available.
  //! \param service Service name.
  //! @note This function will block as long as the service is not available.
  //!
  void waitForService(const std::string& service)
  {


#ifdef DEBUG
    auto s = std::chrono::high_resolution_clock::now();
    long counter = 0;
#endif
    while(true)
    {
#ifdef DEBUG
        ++counter;
#endif
        if (serviceAvailable(service)){
            break;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(50)); // Avoid excessive polling.
    };
#ifdef DEBUG
    auto e = std::chrono::high_resolution_clock::now();
    std::cout << "waitForService(): time: "
              << std::chrono::duration_cast<std::chrono::milliseconds>(e-s).count()
              << " ms." << std::endl;
    std::cout << "waitForService(): clients launched: "
              << counter << std::endl;
#endif
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
732
};