github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/common/ipc/itest_shm_service.cpp (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  #include "common/ipc/shm_service.h"
    21  #include "core/command.h"
    22  #include "core/exception_handler.h"
    23  #include "fs/util.h"
    24  #include "test/common/ipc/ipc_test.h"
    25  #include <thread>
    26  
    27  typedef neb::ipc::shm_service_server<128 * 1024> shm_server_t;
    28  typedef neb::ipc::shm_service_util<128 * 1024> shm_util_t;
    29  typedef neb::ipc::shm_service_client<128 * 1024> shm_client_t;
    30  
    31  static std::string base_name = neb::fs::get_user_name() + "test_session_simple";
    32  
    33  IPC_PRELUDE(test_service_simple) {
    34    boost::interprocess::named_mutex::remove(base_name.c_str());
    35    shm_util_t s(base_name, 128, 128);
    36    LOG(INFO) << "to reset";
    37    s.reset();
    38    LOG(INFO) << "reset done";
    39  }
    40  
    41  IPC_SERVER(test_service_simple) {
    42    neb::core::exception_handler eh;
    43    eh.run();
    44    std::thread thrd([]() {
    45      std::this_thread::sleep_for(std::chrono::seconds(20));
    46      neb::core::command_queue::instance().send_command(
    47          std::make_shared<neb::core::exit_command>());
    48    });
    49    shm_util_t us(base_name, 128, 128);
    50    us.reset();
    51    LOG(INFO) << "reset done";
    52  
    53    shm_server_t s(base_name, 128, 128);
    54    s.init_local_env();
    55    s.run();
    56    thrd.join();
    57    eh.kill();
    58    LOG(INFO) << "got client start!";
    59  
    60  }
    61  IPC_CLIENT(test_service_simple) {
    62    neb::core::exception_handler eh;
    63    eh.run();
    64    std::thread thrd([]() {
    65      std::this_thread::sleep_for(std::chrono::seconds(10));
    66      neb::core::command_queue::instance().send_command(
    67          std::make_shared<neb::core::exit_command>());
    68    });
    69  
    70    shm_client_t c(base_name, 128, 128);
    71    c.init_local_env();
    72    c.run();
    73  
    74    LOG(INFO) << " c done!";
    75    thrd.join();
    76  
    77    eh.kill();
    78  }
    79  
    80  struct SamplePkg {
    81    static constexpr neb::ipc::shm_type_id_t pkg_identifier = 12;
    82  
    83    SamplePkg(uint64_t v) : m_value(v) {}
    84    uint64_t m_value;
    85  };
    86  const neb::ipc::shm_type_id_t SamplePkg::pkg_identifier;
    87  
    88  IPC_SERVER(test_service_message) {
    89    neb::core::exception_handler eh;
    90    eh.run();
    91    shm_util_t us(base_name, 128, 128);
    92    us.reset();
    93    shm_server_t s(base_name, 128, 128);
    94    s.init_local_env();
    95    s.add_handler<SamplePkg>([](SamplePkg *p) {
    96      LOG(INFO) << "got data from client " << p->m_value;
    97      IPC_EXPECT(p->m_value == 2);
    98      // neb::core::command_queue::instance().send_command(
    99      // std::make_shared<neb::core::exit_command>());
   100    });
   101  
   102    s.run();
   103    LOG(INFO) << "got client start!";
   104    eh.kill();
   105  
   106    // s.wait_till_finish();
   107  }
   108  
   109  IPC_CLIENT(test_service_message) {
   110    neb::core::exception_handler eh;
   111    eh.run();
   112  
   113    shm_client_t c(base_name, 128, 128);
   114    c.init_local_env();
   115    std::thread thrd([&c]() {
   116      std::this_thread::sleep_for(std::chrono::seconds(1));
   117      SamplePkg *pkg = c.construct<SamplePkg>(2);
   118      c.push_back(pkg);
   119  
   120      std::this_thread::sleep_for(std::chrono::seconds(10));
   121      neb::core::command_queue::instance().send_command(
   122          std::make_shared<neb::core::exit_command>());
   123    });
   124    c.run();
   125    thrd.join();
   126  
   127    // c.wait_till_finish();
   128    eh.kill();
   129  }
   130  
   131  IPC_SERVER(test_service_message_pingpong) {
   132    neb::core::exception_handler eh;
   133    eh.run();
   134    shm_util_t us(base_name, 128, 128);
   135    us.reset();
   136    shm_server_t s(base_name, 128, 128);
   137    s.init_local_env();
   138  
   139    uint64_t v = 0;
   140    s.add_handler<SamplePkg>([&s, &v](SamplePkg *p) {
   141      // std::cout << "got data from client " << p->m_value;
   142      IPC_EXPECT(p->m_value == v);
   143  
   144      v++;
   145      SamplePkg *pkg = s.construct<SamplePkg>(v);
   146      s.push_back(pkg);
   147      // LOG(INFO) << "push back data " << v;
   148      // neb::core::command_queue::instance().send_command(
   149      // std::make_shared<neb::core::exit_command>());
   150    });
   151  
   152    s.run();
   153    LOG(INFO) << "got client start!";
   154    eh.kill();
   155  }
   156  
   157  IPC_CLIENT(test_service_message_pingpong) {
   158    neb::core::exception_handler eh;
   159    eh.run();
   160  
   161    shm_client_t c(base_name, 128, 128);
   162    c.init_local_env();
   163    uint64_t v = 0;
   164    std::thread thrd([&c, v]() {
   165      std::this_thread::sleep_for(std::chrono::seconds(3));
   166      SamplePkg *pkg = c.construct<SamplePkg>(v);
   167      c.push_back(pkg);
   168    });
   169  
   170    c.add_handler<SamplePkg>([&c, &v](SamplePkg *p) {
   171  
   172      SamplePkg *pkg = c.construct<SamplePkg>(p->m_value);
   173      c.push_back(pkg);
   174  
   175      if (p->m_value > 10000) {
   176        neb::core::command_queue::instance().send_command(
   177            std::make_shared<neb::core::exit_command>());
   178      }
   179    });
   180    c.run();
   181    thrd.join();
   182  
   183    // c.wait_till_finish();
   184    eh.kill();
   185  }
   186  
   187  struct SampleStringPkg {
   188    static constexpr neb::ipc::shm_type_id_t pkg_identifier = 13;
   189  
   190    SampleStringPkg(uint64_t v, const char *str,
   191                    const neb::ipc::default_allocator_t &alloc)
   192        : m_value(v), m_string_val(str, alloc) {}
   193    uint64_t m_value;
   194    neb::ipc::char_string_t m_string_val;
   195  };
   196  
   197  const neb::ipc::shm_type_id_t SampleStringPkg::pkg_identifier;
   198  
   199  IPC_SERVER(test_service_string_message) {
   200    neb::core::exception_handler eh;
   201    eh.run();
   202    shm_util_t us(base_name, 128, 128);
   203    us.reset();
   204    shm_server_t s(base_name, 128, 128);
   205    s.init_local_env();
   206    s.add_handler<SampleStringPkg>([](SampleStringPkg *p) {
   207      LOG(INFO) << "got data from client " << p->m_value << ", "
   208                << p->m_string_val;
   209      IPC_EXPECT(p->m_string_val == "test xxx");
   210    });
   211  
   212    s.run();
   213    LOG(INFO) << "got client start!";
   214    eh.kill();
   215  }
   216  
   217  IPC_CLIENT(test_service_string_message) {
   218    neb::core::exception_handler eh;
   219    eh.run();
   220  
   221    shm_client_t c(base_name, 128, 128);
   222    c.init_local_env();
   223    std::thread thrd([&c]() {
   224      std::this_thread::sleep_for(std::chrono::seconds(1));
   225      SampleStringPkg *pkg =
   226          c.construct<SampleStringPkg>(2, "test xxx", c.default_allocator());
   227      c.push_back(pkg);
   228  
   229      std::this_thread::sleep_for(std::chrono::seconds(10));
   230      neb::core::command_queue::instance().send_command(
   231          std::make_shared<neb::core::exit_command>());
   232    });
   233    c.run();
   234    thrd.join();
   235  
   236    // c.wait_till_finish();
   237    eh.kill();
   238  }