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.

ovCMessagingClient.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "ovCMessagingClient.h"
  2. #include "ovCMessagingImpl.hpp"
  3. namespace Communication {
  4. MessagingClient::MessagingClient() : CMessaging(), m_Client(Socket::createConnectionClient()) {}
  5. MessagingClient::~MessagingClient()
  6. {
  7. this->close();
  8. m_Client->release();
  9. }
  10. bool MessagingClient::connect(const std::string& uri, const size_t port)
  11. {
  12. this->reset();
  13. if (!m_Client->connect(uri.c_str(), port))
  14. {
  15. this->setLastError(Socket_FailedToConnect);
  16. return false;
  17. }
  18. this->setConnection(m_Client);
  19. // Once the connection is done, the client push an authentication message to the server
  20. if (!this->pushAuthentication(impl->m_ConnectionID))
  21. {
  22. // Error set in the function
  23. const ELibraryError error = this->getLastError();
  24. this->close();
  25. this->setLastError(error);
  26. return false;
  27. }
  28. if (!this->startSyncing())
  29. {
  30. this->close();
  31. return false;
  32. }
  33. // To work, the client need to receive box information message. So we wait to receive the box information message.
  34. // A timeout of 10 seconds is set.
  35. const std::chrono::time_point<std::chrono::system_clock> startClock = std::chrono::system_clock::now();
  36. uint64_t packetId;
  37. while (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - startClock).count() < 10)
  38. {
  39. if (this->popBoxDescriptions(packetId, impl->m_BoxDesc))
  40. {
  41. m_BoxDescriptionReceived = true;
  42. break;
  43. }
  44. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  45. }
  46. if (!m_BoxDescriptionReceived)
  47. {
  48. this->close();
  49. this->setLastError(BoxDescriptionNotReceived);
  50. return false;
  51. }
  52. return true;
  53. }
  54. bool MessagingClient::close() const
  55. {
  56. this->pushMessage(EndMessage());
  57. this->stopSyncing();
  58. if (m_Client != nullptr)
  59. {
  60. if (!m_Client->close())
  61. {
  62. this->setLastError(Socket_FailedToCloseConnection);
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. size_t MessagingClient::getParameterCount() const
  69. {
  70. if (!m_BoxDescriptionReceived) { return 0; }
  71. return impl->m_BoxDesc.getParameters()->size();
  72. }
  73. size_t MessagingClient::getInputCount() const
  74. {
  75. if (!m_BoxDescriptionReceived) { return 0; }
  76. return impl->m_BoxDesc.getInputs()->size();
  77. }
  78. size_t MessagingClient::getOutputCount() const
  79. {
  80. if (!m_BoxDescriptionReceived) { return 0; }
  81. return impl->m_BoxDesc.getOutputs()->size();
  82. }
  83. bool MessagingClient::getParameter(const size_t i, uint64_t& id, uint64_t& type, std::string& name, std::string& value) const
  84. {
  85. if (!m_BoxDescriptionReceived) { return false; }
  86. const std::vector<Parameter>* parameters = impl->m_BoxDesc.getParameters();
  87. if (parameters->size() <= i) { return false; }
  88. id = parameters->at(i).getId();
  89. type = parameters->at(i).getType();
  90. name = parameters->at(i).getName();
  91. value = parameters->at(i).getValue();
  92. return true;
  93. }
  94. bool MessagingClient::getInput(const size_t i, uint64_t& id, uint64_t& type, std::string& name) const
  95. {
  96. if (!m_BoxDescriptionReceived) { return false; }
  97. const std::vector<InputOutput>* inputs = impl->m_BoxDesc.getInputs();
  98. if (inputs->size() <= i) { return false; }
  99. id = inputs->at(i).getId();
  100. type = inputs->at(i).getType();
  101. name = inputs->at(i).getName();
  102. return true;
  103. }
  104. bool MessagingClient::getOutput(const size_t i, uint64_t& id, uint64_t& type, std::string& name) const
  105. {
  106. if (!m_BoxDescriptionReceived)
  107. {
  108. const_cast<MessagingClient*>(this)->setLastError(BoxDescriptionNotReceived);
  109. return false;
  110. }
  111. const std::vector<InputOutput>* outputs = impl->m_BoxDesc.getOutputs();
  112. if (outputs->size() <= i) { return false; }
  113. id = outputs->at(i).getId();
  114. type = outputs->at(i).getType();
  115. name = outputs->at(i).getName();
  116. return true;
  117. }
  118. bool MessagingClient::popError(uint64_t& packetId, EError& type, uint64_t& guiltyId) { return CMessaging::popError(packetId, type, guiltyId); }
  119. bool MessagingClient::popEBML(uint64_t& packetId, size_t& index, uint64_t& startTime, uint64_t& endTime, std::shared_ptr<const std::vector<uint8_t>>& ebml)
  120. {
  121. return CMessaging::popEBML(packetId, index, startTime, endTime, ebml);
  122. }
  123. bool MessagingClient::pushAuthentication(const std::string& connectionID) const { return this->pushMessage(AuthenticationMessage(connectionID)); }
  124. bool MessagingClient::pushLog(const ELogLevel logLevel, const std::string& log) const { return this->pushMessage(LogMessage(logLevel, log)); }
  125. bool MessagingClient::pushEBML(const size_t index, const uint64_t startTime, const uint64_t endTime,
  126. const std::shared_ptr<const std::vector<uint8_t>>& ebml) const
  127. {
  128. return this->pushMessage(EBMLMessage(index, startTime, endTime, ebml));
  129. }
  130. bool MessagingClient::pushSync() const { return this->pushMessage(SyncMessage()); }
  131. bool MessagingClient::waitForSyncMessage() { return CMessaging::waitForSyncMessage(); }
  132. } // namespace Communication