dtnsim.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include <errno.h>
00019 #include <string>
00020 #include <sys/time.h>
00021 
00022 #include <oasys/debug/Log.h>
00023 #include <oasys/tclcmd/TclCommand.h>
00024 #include <oasys/util/Getopt.h>
00025 #include <oasys/util/Random.h>
00026 
00027 #include "ConnCommand.h"
00028 #include "Simulator.h"
00029 #include "SimCommand.h"
00030 #include "SimConvergenceLayer.h"
00031 #include "contacts/ContactManager.h"
00032 #include "cmd/ParamCommand.h"
00033 #include "naming/SchemeTable.h"
00034 
00035 #include "servlib/bundling/BundleTimestamp.h"
00036 #include "servlib/bundling/BundleProtocol.h"
00037 #include "oasys/storage/MemoryStore.h"
00038 #include "oasys/storage/StorageConfig.h"
00039 
00040 
00044 namespace dtnsim {}
00045 
00046 using namespace dtn;
00047 using namespace dtnsim;
00048 
00049 int
00050 main(int argc, char** argv)
00051 {
00052     // reset timeval conversion, so timestamps are ok 
00053     dtn::BundleTimestamp::TIMEVAL_CONVERSION = 0;
00054         
00055     // command line parameter vars
00056     int                random_seed;
00057     bool               random_seed_set = false;
00058     std::string        conf_file;
00059     bool               conf_file_set = false;
00060     std::string        logfile("-");
00061     std::string        loglevelstr;
00062     oasys::log_level_t loglevel;
00063 
00064     oasys::Getopt opts;
00065     opts.addopt(
00066         new oasys::StringOpt('c', "conf", &conf_file, "<conf>",
00067                              "set the configuration file", &conf_file_set));
00068     
00069     opts.addopt(
00070         new oasys::IntOpt('s', "seed", &random_seed, "seed",
00071                           "random number generator seed", &random_seed_set));
00072 
00073     opts.addopt(
00074         new oasys::StringOpt('o', "output", &logfile, "<output>",
00075                              "file name for logging output "
00076                              "(default - indicates stdout)"));
00077 
00078     opts.addopt(
00079         new oasys::StringOpt('l', NULL, &loglevelstr, "<level>",
00080                              "default log level [debug|warn|info|crit]"));
00081 
00082     opts.getopt(argv[0], argc, argv);
00083 
00084     int remainder = opts.getopt(argv[0], argc, argv);
00085 
00086     if (!conf_file_set && remainder != argc) {
00087         conf_file.assign(argv[remainder]);
00088         conf_file_set = true;
00089         remainder++;
00090     }
00091 
00092     if (remainder != argc) {
00093         fprintf(stderr, "invalid argument '%s'\n", argv[remainder]);
00094         opts.usage("dtnsim");
00095         exit(1);
00096     }
00097 
00098     if (!conf_file_set) {
00099         fprintf(stderr, "must set the simulator conf file\n");
00100         opts.usage("dtnsim");
00101         exit(1);
00102     }
00103 
00104     // Parse the debugging level argument
00105     if (loglevelstr.length() == 0) {
00106         loglevel = LOG_DEFAULT_THRESHOLD;
00107     } else {
00108         loglevel = oasys::str2level(loglevelstr.c_str());
00109         if (loglevel == oasys::LOG_INVALID) {
00110             fprintf(stderr, "invalid level value '%s' for -l option, "
00111                     "expected debug | info | warning | error | crit\n",
00112                     loglevelstr.c_str());
00113             exit(1);
00114         }
00115     }
00116 
00117     // Initialize the simulator first and foremost since it's needed
00118     // for LogSim::gettimeofday
00119     Simulator* s = new Simulator(new DTNStorageConfig(
00120                                      "storage","memorydb","",""));
00121     Simulator::init(s);
00122     
00123     // Initialize logging
00124     oasys::Log::init("-", loglevel, "--");
00125     log_info_p("/dtsim", "dtn simulator initializing...");
00126 
00127     // seed the random number generator
00128     if (!random_seed_set) {
00129         struct timeval tv;
00130         gettimeofday(&tv, NULL);
00131         random_seed = tv.tv_usec;
00132     }
00133     log_info_p("/dtsim", "random seed is %u\n", random_seed);
00134     oasys::Random::seed(random_seed);
00135     
00136     // Set up the command interpreter
00137     if (oasys::TclCommandInterp::init(argv[0]) != 0)
00138     {
00139         log_crit_p("/dtsim", "Can't init TCL");
00140         exit(1);
00141     }
00142         
00143     oasys::TclCommandInterp* interp = oasys::TclCommandInterp::instance();
00144     interp->reg(new ConnCommand());
00145     interp->reg(new ParamCommand());
00146     interp->reg(new SimCommand());
00147     log_info_p("/dtsim","registered dtnsim commands");
00148 
00149     SchemeTable::create();
00150     SimConvergenceLayer::init();
00151     ConvergenceLayer::add_clayer(SimConvergenceLayer::instance());
00152     BundleProtocol::init_default_processors();
00153     log_info_p("/dtsim","intialized dtnsim components");
00154         
00155     // initializing data store to memorydb
00156     if (!Simulator::instance()->init_datastore()) {
00157         log_err_p("/dtnsim", "error initializing data store, exiting...");
00158         exit(1);
00159     }
00160         
00161     log_info_p("/dtsim","parsing configuration file %s...", conf_file.c_str());
00162     if (interp->exec_file(conf_file.c_str()) != 0) {
00163         log_err_p("/dtsim", "error in configuration file, exiting...");
00164         exit(1);
00165     }
00166     
00167     // Run the event loop of simulator
00168     Simulator::instance()->run();       
00169         
00170     Simulator::instance()->close_datastore();
00171 }

Generated on Sat Sep 8 08:43:27 2007 for DTN Reference Implementation by  doxygen 1.5.3