github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/util/controller.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 "util/controller.h"
    21  #include <boost/process/environment.hpp>
    22  
    23  namespace neb {
    24  namespace util {
    25  void elfin::run() {
    26    try {
    27      ff::net::net_nervure nn;
    28      ff::net::typed_pkg_hub hub;
    29      hub.to_recv_pkg<ctl_kill_req_t>(
    30          [this](std::shared_ptr<ctl_kill_req_t>) { handle_kill_req(); });
    31  
    32      nn.add_pkg_hub(hub);
    33      nn.add_tcp_server("127.0.0.1", 0x1969);
    34  
    35      nn.run();
    36    } catch (...) {
    37      LOG(ERROR) << "got exception";
    38    }
    39  }
    40  
    41  void elfin::handle_kill_req() {
    42    LOG(INFO) << boost::this_process::get_id();
    43    LOG(ERROR) << "got killed";
    44    throw std::invalid_argument("got kill command");
    45  }
    46  
    47  void magic_wand::kill_nbre() {
    48    m_package = std::make_shared<ctl_kill_req_t>();
    49    start_and_join();
    50  }
    51  
    52  void magic_wand::start_and_join() {
    53    ff::net::net_nervure nn;
    54    ff::net::typed_pkg_hub hub;
    55    ff::net::tcp_connection_base_ptr conn;
    56    nn.get_event_handler()->listen<::ff::net::event::tcp_get_connection>(
    57        [&, this](::ff::net::tcp_connection_base *conn) {
    58          conn->send(m_package);
    59        });
    60    nn.get_event_handler()->listen<::ff::net::event::tcp_lost_connection>(
    61        [&](::ff::net::tcp_connection_base *) { nn.ioservice().stop(); });
    62  
    63    nn.add_pkg_hub(hub);
    64    conn = nn.add_tcp_client("127.0.0.1", 0x1969);
    65  
    66    nn.run();
    67  }
    68  } // namespace util
    69  } // namespace neb