InterfaceTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ***************************************************************************/
  24. #include "InterfaceTest.hpp"
  25. #include "oatpp/network/virtual_/Interface.hpp"
  26. #include "oatpp/core/data/stream/BufferStream.hpp"
  27. #include <thread>
  28. #include <list>
  29. namespace oatpp { namespace test { namespace network { namespace virtual_ {
  30. namespace {
  31. typedef oatpp::network::virtual_::Interface Interface;
  32. typedef oatpp::network::virtual_::Socket Socket;
  33. typedef std::list<std::thread> ThreadList;
  34. class ClientTask : public oatpp::base::Countable {
  35. private:
  36. std::shared_ptr<Interface> m_interface;
  37. oatpp::String m_dataSample;
  38. public:
  39. ClientTask(const std::shared_ptr<Interface>& _interface,
  40. const oatpp::String& dataSample)
  41. : m_interface(_interface)
  42. , m_dataSample(dataSample)
  43. {}
  44. void run() {
  45. auto submission = m_interface->connect();
  46. auto socket = submission->getSocket();
  47. auto res = socket->writeExactSizeDataSimple(m_dataSample->data(), m_dataSample->size());
  48. OATPP_ASSERT(res == m_dataSample->size());
  49. v_char8 buffer[100];
  50. oatpp::data::stream::BufferOutputStream stream;
  51. res = oatpp::data::stream::transfer(socket.get(), &stream, 2, buffer, 100);
  52. OATPP_ASSERT(res == 2);
  53. OATPP_ASSERT(stream.getCurrentPosition() == res);
  54. OATPP_ASSERT(stream.toString() == "OK");
  55. //OATPP_LOGV("client", "finished - OK");
  56. }
  57. };
  58. class ServerTask : public oatpp::base::Countable {
  59. private:
  60. std::shared_ptr<Socket> m_socket;
  61. oatpp::String m_dataSample;
  62. public:
  63. ServerTask(const std::shared_ptr<Socket>& socket,
  64. const oatpp::String& dataSample)
  65. : m_socket(socket)
  66. , m_dataSample(dataSample)
  67. {}
  68. void run() {
  69. v_char8 buffer[100];
  70. oatpp::data::stream::BufferOutputStream stream;
  71. auto res = oatpp::data::stream::transfer(m_socket.get(), &stream, m_dataSample->size(), buffer, 100);
  72. OATPP_ASSERT(res == m_dataSample->size());
  73. OATPP_ASSERT(stream.getCurrentPosition() == res);
  74. OATPP_ASSERT(stream.toString() == m_dataSample);
  75. res = m_socket->writeExactSizeDataSimple("OK", 2);
  76. OATPP_ASSERT(res == 2);
  77. }
  78. };
  79. class Server : public oatpp::base::Countable {
  80. private:
  81. std::shared_ptr<Interface> m_interface;
  82. oatpp::String m_dataSample;
  83. v_int32 m_numTasks;
  84. public:
  85. Server(const std::shared_ptr<Interface>& _interface,
  86. const oatpp::String& dataSample,
  87. v_int32 numTasks)
  88. : m_interface(_interface)
  89. , m_dataSample(dataSample)
  90. , m_numTasks(numTasks)
  91. {}
  92. void run() {
  93. ThreadList threadList;
  94. for(v_int32 i = 0; i < m_numTasks; i++) {
  95. auto socket = m_interface->accept();
  96. threadList.push_back(std::thread(&ServerTask::run, ServerTask(socket, m_dataSample)));
  97. }
  98. for(auto& thread : threadList) {
  99. thread.join();
  100. }
  101. }
  102. };
  103. }
  104. void InterfaceTest::onRun() {
  105. oatpp::String dataSample = "1234567890-=][poiuytrewqasdfghjkl;'/.,mnbvcxzzxcvbnm,./';lkjhgfdsaqwertyuiop][=-0987654321";
  106. auto _interface = Interface::obtainShared("virtualhost");
  107. auto bindLock = _interface->bind();
  108. v_int32 numTasks = 100;
  109. ThreadList threadList;
  110. std::thread server(&Server::run, Server(_interface, dataSample, numTasks));
  111. for(v_int32 i = 0; i < numTasks; i++) {
  112. threadList.push_back(std::thread(&ClientTask::run, ClientTask(_interface, dataSample)));
  113. }
  114. for(auto& thread : threadList) {
  115. thread.join();
  116. }
  117. server.join();
  118. }
  119. }}}}