You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uoSocketClientServerSyncCommunicationTest.cpp 4.8KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*********************************************************************
  2. * Software License Agreement (AGPL-3 License)
  3. *
  4. * OpenViBE SDK Test Software
  5. * Based on OpenViBE V1.1.0, Copyright (C) Inria, 2006-2015
  6. * Copyright (C) Inria, 2015-2017,V1.0
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program.
  19. * If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <string>
  22. #include <thread>
  23. #include <atomic>
  24. #include <condition_variable>
  25. #include <vector>
  26. #include "socket/IConnection.h"
  27. #include "socket/IConnectionClient.h"
  28. #include "socket/IConnectionServer.h"
  29. #include "ovtAssert.h"
  30. namespace {
  31. std::condition_variable gServerStartedCondVar;
  32. std::mutex gServerStartedMutex;
  33. std::condition_variable gClientConnectedCondVar;
  34. std::mutex gClientConnectedMutex;
  35. std::vector<std::string> gReceivedData;
  36. bool gServerStarted = false;
  37. bool gClientConnected = false;
  38. // server callback run from a child thread
  39. void onServerListening(const int port, const size_t packetCount)
  40. {
  41. // only the server side modifies gReceivedData thus no need to handle race condition
  42. gReceivedData.clear();
  43. Socket::IConnection* clientConnection = nullptr;
  44. // create server
  45. Socket::IConnectionServer* server = Socket::createConnectionServer();
  46. server->listen(port);
  47. // keep the scope braces here, as it ensures mutex is released
  48. {
  49. std::lock_guard<std::mutex> lockOnServerStart(gServerStartedMutex);
  50. gServerStarted = true;
  51. }
  52. gServerStartedCondVar.notify_one();
  53. // connect clients
  54. while (!clientConnection) { if (server->isReadyToReceive()) { clientConnection = server->accept(); } }
  55. // keep the scope braces here, as it ensures mutex is released
  56. {
  57. std::lock_guard<std::mutex> lockOnClientConnected(gClientConnectedMutex);
  58. gClientConnected = true;
  59. }
  60. gClientConnectedCondVar.notify_one();
  61. while (gReceivedData.size() < packetCount)
  62. {
  63. if (clientConnection->isReadyToReceive())
  64. {
  65. size_t dataSize = 0;
  66. char dataBuffer[64];
  67. clientConnection->receiveBufferBlocking(&dataSize, sizeof(dataSize));
  68. clientConnection->receiveBufferBlocking(dataBuffer, dataSize);
  69. gReceivedData.push_back(std::string(dataBuffer, dataSize));
  70. }
  71. }
  72. server->release();
  73. }
  74. } // namespace
  75. int uoSocketClientServerSyncCommunicationTest(int argc, char* argv[])
  76. {
  77. OVT_ASSERT(argc == 4, "Failure to retrieve tests arguments. Expecting: server_name port_number packet_count");
  78. const std::string serverName = argv[1];
  79. char* end;
  80. const size_t port = strtol(argv[2], &end, 10);
  81. size_t packetCount = strtol(argv[3], &end, 10);
  82. // test synchronous data transmission from a single client to server:
  83. // - launch a server on a background thread
  84. // - connect single client
  85. // - make client sending data
  86. // - make server receiving and storing data
  87. // - join the thread and do assertions on received data in the main thread
  88. // create a client
  89. Socket::IConnectionClient* client = Socket::createConnectionClient();
  90. // launch server on background thread
  91. std::thread serverThread(onServerListening, port, packetCount);
  92. // wait until the server is started to connect client
  93. std::unique_lock<std::mutex> lockOnServerStart(gServerStartedMutex);
  94. gServerStartedCondVar.wait(lockOnServerStart, []() { return gServerStarted; });
  95. client->connect(serverName.c_str(), port);
  96. // wait until the connection is made to transmit data
  97. std::unique_lock<std::mutex> lockOnClientConnected(gClientConnectedMutex);
  98. gClientConnectedCondVar.wait(lockOnClientConnected, []() { return gClientConnected; });
  99. // transmit data
  100. // transmission follows the protocol: data size transmission + data transmission
  101. const std::string baseData = "Data packet index: ";
  102. for (size_t sendIndex = 0; sendIndex < packetCount; ++sendIndex)
  103. {
  104. std::string dataString = baseData + std::to_string(sendIndex);
  105. size_t dataSize = dataString.size();
  106. client->sendBufferBlocking(&dataSize, sizeof(dataSize));
  107. client->sendBufferBlocking(dataString.c_str(), dataSize);
  108. }
  109. serverThread.join(); // wait until the end of the thread
  110. // release resources
  111. client->close();
  112. client->release();
  113. // do the assertion on the main thread
  114. OVT_ASSERT(gReceivedData.size() == packetCount, "Failure to retrieve packet count");
  115. for (size_t receivedIndex = 0; receivedIndex < packetCount; ++receivedIndex)
  116. {
  117. OVT_ASSERT_STREQ(gReceivedData[receivedIndex], (baseData + std::to_string(receivedIndex)), "Failure to retrieve packet");
  118. }
  119. return EXIT_SUCCESS;
  120. }