github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/core/neb_ipc/simple/server.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/common.h"
    21  #include "common/configuration.h"
    22  #include "core/neb_ipc/ipc_interface.h"
    23  #include "core/neb_ipc/server/ipc_server_endpoint.h"
    24  #include "fs/util.h"
    25  
    26  std::mutex local_mutex;
    27  std::condition_variable local_cond_var;
    28  bool to_quit = false;
    29  
    30  void nbre_version_callback(ipc_status_code isc, void *handler, uint32_t major,
    31                             uint32_t minor, uint32_t patch) {
    32    if (isc != ipc_status_succ) {
    33      LOG(INFO) << "call back fail";
    34      return;
    35    }
    36    std::cout << "got version: " << major << ", " << minor << ", " << patch
    37              << std::endl;
    38    std::unique_lock<std::mutex> _l(local_mutex);
    39    to_quit = true;
    40    local_cond_var.notify_all();
    41    _l.unlock();
    42    // local_cond_var.notify_one();
    43  }
    44  
    45  int main(int argc, char *argv[]) {
    46    FLAGS_logtostderr = true;
    47  
    48    ::google::InitGoogleLogging(argv[0]);
    49  
    50    const char *root_dir = neb::configuration::instance().nbre_root_dir().c_str();
    51    std::string nbre_path =
    52        neb::fs::join_path(root_dir, "bin/test_neb_ipc_client");
    53  
    54    set_recv_nbre_version_callback(nbre_version_callback);
    55  
    56    nbre_params_t params{root_dir,
    57                         nbre_path.c_str(),
    58                         neb::configuration::instance().neb_db_dir().c_str(),
    59                         neb::configuration::instance().nbre_db_dir().c_str(),
    60                         neb::configuration::instance().nbre_log_dir().c_str(),
    61                         "auth address here!"};
    62    auto ret = start_nbre_ipc(params);
    63    if (ret != ipc_status_succ) {
    64      to_quit = false;
    65      return 1;
    66    }
    67  
    68    uint64_t height = 100;
    69  
    70    ipc_nbre_version(&local_mutex, height);
    71  
    72    std::unique_lock<std::mutex> _l(local_mutex);
    73    if (to_quit)
    74      return 0;
    75    local_cond_var.wait(_l);
    76    nbre_ipc_shutdown();
    77    LOG(INFO) << "to exit";
    78    //::google::ShutdownGoogleLogging();
    79  
    80    return 0;
    81  }