github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/3rd_party/fflib/src/net/framework/application.cpp (about)

     1  /***********************************************
     2    The MIT License (MIT)
     3  
     4    Copyright (c) 2012 Athrun Arthur <athrunarthur@gmail.com>
     5  
     6    Permission is hereby granted, free of charge, to any person obtaining a copy
     7    of this software and associated documentation files (the "Software"), to deal
     8    in the Software without restriction, including without limitation the rights
     9    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10    copies of the Software, and to permit persons to whom the Software is
    11    furnished to do so, subject to the following conditions:
    12  
    13    The above copyright notice and this permission notice shall be included in
    14    all copies or substantial portions of the Software.
    15  
    16    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    22    THE SOFTWARE.
    23   *************************************************/
    24  #include "ff/net/framework/application.h"
    25  #include "ff/net/common/defines.h"
    26  #include "ff/net/framework/routine.h"
    27  
    28  namespace ff {
    29  namespace net {
    30  application::application(const std::string &app_name)
    31      : m_app_name(app_name), m_app_desc("Allowed options") {
    32    m_app_desc.add_options()("help", "show help messages")(
    33        "net_mode", boost::program_options::value<int>(),
    34        "set net mode, real_net:1, simu_net:2, single_net:3")(
    35        "routine", boost::program_options::value<std::string>(),
    36        "to run routine")("list", "list routines");
    37  }
    38  
    39  void application::initialize(int argc, char *argv[]) {
    40    boost::program_options::store(
    41        boost::program_options::parse_command_line(argc, argv, m_app_desc),
    42        m_app_vm);
    43  }
    44  
    45  void application::register_routine(routine *rp) {
    46    assert(rp != NULL);
    47    m_routines.push_back(rp);
    48  }
    49  
    50  void application::run() {
    51    if (m_app_vm.count("help")) {
    52      std::cout << m_app_desc << std::endl;
    53      return;
    54    }
    55    if (m_app_vm.count("list")) {
    56      list_routines();
    57      return;
    58    }
    59    if (m_app_vm["routine"].as<std::string>() != std::string("")) {
    60      m_routine_name.push_back(m_app_vm["routine"].as<std::string>());
    61    }
    62    m_nm = net_mode::real_net;
    63    if (m_app_vm.count("net_mode")) {
    64      m_nm = static_cast<net_mode>(m_app_vm["net_mode"].as<int>());
    65    }
    66    run_routine();
    67  }
    68  
    69  void application::list_routines() {
    70    std::cout << "\tall:  represent all routines" << std::endl;
    71    for (size_t i = 0; i < m_routines.size(); ++i) {
    72      std::cout << "\t" << m_routines[i]->get_name() << std::endl;
    73    }
    74  }
    75  
    76  void application::run_routine() {
    77    std::map<std::string, routine *> rnames;
    78    for (size_t i = 0; i < m_routines.size(); ++i) {
    79      std::string n = m_routines[i]->get_name();
    80      if (rnames.find(n) != rnames.end()) {
    81        std::cout << "More than one routine with same name, " << n << std::endl;
    82        return;
    83      }
    84      rnames.insert(std::make_pair(m_routines[i]->get_name(), m_routines[i]));
    85    }
    86  
    87    for (size_t i = 0; i < m_routine_name.size(); ++i) {
    88      std::string s = m_routine_name[i];
    89      if (rnames.find(s) == rnames.end()) {
    90        std::cout << "Cannot find routine " << s << std::endl;
    91        return;
    92      }
    93    }
    94  
    95    if (m_routine_name.size() == 0) {
    96      std::cout << "No available routines to run!" << std::endl;
    97    }
    98    for (size_t i = 0; i < m_routine_name.size(); ++i) {
    99      std::string s = m_routine_name[i];
   100      routine *pr = rnames[s];
   101  
   102      start_routine(pr);
   103    }
   104  }
   105  
   106  void application::start_routine(routine *r) {
   107    r->initialize(m_nm, m_routine_args);
   108    r->run();
   109    /*
   110    if(m_nm == real_net){
   111      r->initialize(m_nm, m_routine_args);
   112      r->run();
   113    }else if(m_nm == simu_net){
   114  
   115      std::cout<<"TODO start_routine:do something here!"<<std::endl;
   116  
   117    }*/
   118  }
   119  
   120  } // namespace net
   121  } // namespace ff
   122