github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/core/ir_warden.cpp (about)

     1  // Copyright (C) 2017 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  
    21  #include "core/ir_warden.h"
    22  #include "core/command.h"
    23  #include <boost/bind.hpp>
    24  #include <boost/date_time/posix_time/posix_time.hpp>
    25  
    26  namespace neb {
    27  namespace core {
    28  
    29  const command_type_t exit_command::command_type;
    30  ir_warden::~ir_warden() {}
    31  
    32  std::unique_ptr<nbre::NBREIR>
    33  ir_warden::get_ir_by_name_version(const std::string &name, uint64_t version) {
    34    return m_ir_manager->read_ir(name, version);
    35  }
    36  
    37  std::unique_ptr<std::vector<nbre::NBREIR>>
    38  ir_warden::get_ir_by_name_height(const std::string &name, uint64_t height,
    39                                   bool depends) {
    40    return m_ir_manager->read_irs(name, height, depends);
    41  }
    42  
    43  bool ir_warden::is_sync_already() const {
    44    std::unique_lock<std::mutex> _l(m_sync_mutex);
    45    return m_is_sync_already;
    46  }
    47  
    48  void ir_warden::wait_until_sync() {
    49    LOG(INFO) << "wait until sync ...";
    50    std::unique_lock<std::mutex> _l(m_sync_mutex);
    51    if (m_is_sync_already) {
    52      return;
    53    }
    54    m_sync_cond_var.wait(_l);
    55    LOG(INFO) << "wait until sync done";
    56  }
    57  
    58  void ir_warden::on_timer() {
    59    m_ir_manager->parse_irs(m_queue);
    60    std::unique_lock<std::mutex> _l(m_sync_mutex);
    61    if (!m_is_sync_already) {
    62      m_is_sync_already = true;
    63      _l.unlock();
    64      m_sync_cond_var.notify_one();
    65    }
    66  }
    67  
    68  void ir_warden::on_receive_ir_transactions(
    69      const std::shared_ptr<nbre_ir_transactions_req> &txs_ptr) {
    70    m_queue.push_back(txs_ptr);
    71  }
    72  
    73  ir_warden::ir_warden() : m_is_sync_already(false) {
    74    m_ir_manager = std::make_unique<fs::ir_manager>();
    75  }
    76  }
    77  } // namespace neb