dtnsim.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include <errno.h>
00040 #include <string>
00041 #include <sys/time.h>
00042 
00043 #include <oasys/debug/Log.h>
00044 #include <oasys/tclcmd/TclCommand.h>
00045 #include <oasys/util/Getopt.h>
00046 #include <oasys/util/Random.h>
00047 
00048 #include "ConnCommand.h"
00049 #include "Simulator.h"
00050 #include "SimCommand.h"
00051 #include "SimConvergenceLayer.h"
00052 #include "contacts/ContactManager.h"
00053 #include "cmd/ParamCommand.h"
00054 #include "naming/SchemeTable.h"
00055 
00059 namespace dtnsim {}
00060 
00061 using namespace dtn;
00062 using namespace dtnsim;
00063 
00064 int
00065 main(int argc, char** argv)
00066 {
00067     // command line parameter vars
00068     int                random_seed;
00069     bool               random_seed_set = false;
00070     std::string        conf_file;
00071     bool               conf_file_set = false;
00072     std::string        logfile("-");
00073     std::string        loglevelstr;
00074     oasys::log_level_t loglevel;
00075     
00076     oasys::Getopt::addopt(
00077         new oasys::StringOpt('c', "conf", &conf_file, "<conf>",
00078                              "set the configuration file", &conf_file_set));
00079     
00080     oasys::Getopt::addopt(
00081         new oasys::IntOpt('s', "seed", &random_seed, "seed",
00082                           "random number generator seed", &random_seed_set));
00083 
00084     oasys::Getopt::addopt(
00085         new oasys::StringOpt('o', "output", &logfile, "<output>",
00086                              "file name for logging output "
00087                              "(default - indicates stdout)"));
00088 
00089     oasys::Getopt::addopt(
00090         new oasys::StringOpt('l', NULL, &loglevelstr, "<level>",
00091                              "default log level [debug|warn|info|crit]"));
00092 
00093     oasys::Getopt::getopt(argv[0], argc, argv);
00094 
00095     int remainder = oasys::Getopt::getopt(argv[0], argc, argv);
00096 
00097     if (!conf_file_set && remainder != argc) {
00098         conf_file.assign(argv[remainder]);
00099         conf_file_set = true;
00100         remainder++;
00101     }
00102 
00103     if (remainder != argc) {
00104         fprintf(stderr, "invalid argument '%s'\n", argv[remainder]);
00105         oasys::Getopt::usage("dtnsim");
00106         exit(1);
00107     }
00108 
00109     if (!conf_file_set) {
00110         fprintf(stderr, "must set the simulator conf file\n");
00111         oasys::Getopt::usage("dtnsim");
00112         exit(1);
00113     }
00114 
00115     // Parse the debugging level argument
00116     if (loglevelstr.length() == 0) {
00117         loglevel = LOG_DEFAULT_THRESHOLD;
00118     } else {
00119         loglevel = oasys::str2level(loglevelstr.c_str());
00120         if (loglevel == oasys::LOG_INVALID) {
00121             fprintf(stderr, "invalid level value '%s' for -l option, "
00122                     "expected debug | info | warning | error | crit\n",
00123                     loglevelstr.c_str());
00124             exit(1);
00125         }
00126     }
00127 
00128     // Initialize the simulator first and foremost since it's needed
00129     // for LogSim::gettimeofday
00130     Simulator* s = new Simulator();
00131     Simulator::init(s);
00132 
00133     // Initialize logging
00134     oasys::Log::init("-", loglevel, "--");
00135     log_info("/sim", "dtn simulator initializing...");
00136 
00137     // seed the random number generator
00138     if (!random_seed_set) {
00139       struct timeval tv;
00140       gettimeofday(&tv, NULL);
00141       random_seed = tv.tv_usec;
00142     }
00143     log_info("/sim", "random seed is %u\n", random_seed);
00144     oasys::Random::seed(random_seed);
00145     
00146     // Set up the command interpreter
00147     oasys::TclCommandInterp::init(argv[0]);
00148     oasys::TclCommandInterp* interp = oasys::TclCommandInterp::instance();
00149     interp->reg(new ConnCommand());
00150     interp->reg(new ParamCommand());
00151     interp->reg(new SimCommand());
00152 
00153     // Set up components
00154     SchemeTable::create();
00155     SimConvergenceLayer::init();
00156     ConvergenceLayer::add_clayer(SimConvergenceLayer::instance());
00157 
00158     if (interp->exec_file(conf_file.c_str()) != 0) {
00159         log_err("/sim", "error in configuration file, exiting...");
00160         exit(1);
00161     }
00162 
00163     // Run the event loop of simulator
00164     Simulator::instance()->run();
00165 }

Generated on Fri Dec 22 14:47:58 2006 for DTN Reference Implementation by  doxygen 1.5.1