github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/cmd/dummy_neb/generator/generator_base.h (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  #pragma once
    21  #include "cmd/dummy_neb/dummy_common.h"
    22  #include "util/quitable_thread.h"
    23  #include <ff/network.h>
    24  
    25  define_nt(p_checkers, std::vector<std::string>);
    26  using checker_marshaler = ff::net::ntpackage<1, p_checkers>;
    27  
    28  class checker_task_base {
    29  public:
    30    checker_task_base();
    31  
    32    virtual ~checker_task_base();
    33    virtual void check() = 0;
    34    virtual std::string name() const;
    35    inline uint64_t &task_id() { return m_task_id; };
    36    inline uint64_t task_id() const { return m_task_id; }
    37    bool is_running() const { return m_b_is_running; }
    38  
    39    std::string status() const;
    40  
    41  protected:
    42    void apply_result(const std::string &result);
    43  
    44  protected:
    45    mutable std::mutex m_mutex;
    46    uint64_t m_task_id;
    47    std::chrono::steady_clock::time_point m_last_call_timepoint;
    48    std::unordered_set<std::string> m_exist_results;
    49    uint64_t m_call_times;
    50    uint16_t m_diff_result_num;
    51    bool m_b_is_running;
    52  
    53    static uint64_t s_task_id;
    54  };
    55  
    56  std::shared_ptr<checker_task_base>
    57  init_checker_from_string(const std::string &s);
    58  
    59  class task_executor : public neb::util::wakeable_thread,
    60                        public neb::util::singleton<task_executor> {};
    61  
    62  class checker_tasks : public neb::util::singleton<checker_tasks> {
    63  public:
    64    typedef std::vector<std::shared_ptr<checker_task_base>> task_container_t;
    65    typedef std::shared_ptr<task_container_t> task_container_ptr_t;
    66  
    67    // void init_from_db();
    68    // void write_to_db();
    69  
    70    void add_task(const std::shared_ptr<checker_task_base> &task);
    71    task_container_ptr_t get_tasks_with_name(const std::string &name);
    72    inline size_t size() const { return m_all_tasks.size(); }
    73  
    74    void randomly_schedule_no_running_tasks();
    75    void randomly_schedule_all_tasks(int num = 1);
    76  
    77    std::string status() const;
    78  
    79  protected:
    80    inline static std::string get_all_checker_info_key() {
    81      return std::string("all_checker_names");
    82    }
    83    inline static std::string get_checker_key_with_name(const std::string &name) {
    84      return std::string("checker_with_name") + name;
    85    }
    86  
    87  protected:
    88    typedef std::unordered_map<std::string, task_container_ptr_t>
    89        task_name_container_t;
    90    mutable std::mutex m_mutex;
    91    std::unordered_map<uint64_t, std::shared_ptr<checker_task_base>> m_all_tasks;
    92  };
    93  
    94  
    95  class generator_base {
    96  public:
    97    generator_base(all_accounts *accounts, generate_block *block,
    98                   int new_account_num, int tx_num);
    99    inline virtual ~generator_base(){};
   100  
   101    virtual void run();
   102    virtual std::shared_ptr<corepb::Account> gen_account() = 0;
   103    virtual std::shared_ptr<corepb::Transaction> gen_tx() = 0;
   104    virtual checker_tasks::task_container_ptr_t gen_tasks() = 0;
   105  
   106  protected:
   107    all_accounts *m_all_accounts;
   108    generate_block *m_block;
   109    int m_new_account_num;
   110    int m_new_tx_num;
   111  };
   112