Simulator.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 #include "Simulator.h"
00039 #include "Node.h"
00040 #include "Topology.h"
00041 
00042 namespace dtnsim {
00043 
00044 Simulator* Simulator::instance_; 
00045 
00046 double Simulator::time_ = 0;
00047 double Simulator::runtill_ = -1;
00048 
00049 Simulator::Simulator() 
00050     : Logger("Simulator", "/sim/main"),
00051       eventq_()
00052 {
00053 }
00054 
00055 
00056 void
00057 Simulator::post(SimEvent* e)
00058 {
00059     instance_->eventq_.push(e);
00060 }
00061 
00062 void
00063 Simulator::exit() 
00064 {
00065     ::exit(0);
00066 }
00067 
00071 void
00072 Simulator::run()
00073 {
00074     oasys::Log* log = oasys::Log::instance();
00075     log->set_prefix("--");
00076     
00077     log_debug("starting event loop...");
00078     is_running_ = true;
00079 
00080     // first handle all events posted from the configuration
00081     Topology::NodeTable::iterator iter;
00082 
00083     for (iter =  Topology::node_table()->begin();
00084          iter != Topology::node_table()->end();
00085          ++iter)
00086     {
00087         Node* node = iter->second;
00088         node->set_active();
00089         node->process_bundle_events();
00090     }
00091 
00092      while(!eventq_.empty()) {
00093          log->set_prefix("--");
00094          if (is_running_) {
00095              SimEvent* e = eventq_.top();
00096              eventq_.pop();
00097              /* Move the clock */
00098              time_ = e->time();
00099              if (e->is_valid()) {
00100                  ASSERT(e->handler() != NULL);
00101                  /* Process the event */
00102                  log_debug("Event:%p type %s at time %f",
00103                            e, e->type_str(), time_);
00104                  e->handler()->process(e);
00105              }
00106              if ((Simulator::runtill_ != -1) &&
00107                  (time_ > Simulator::runtill_)) {
00108                  log_info("Exiting simulation. "
00109                           "Current time (%f) > Max time (%f)",
00110                           time_, Simulator::runtill_);
00111                  exit();
00112              }
00113          } // if is_running_
00114      }
00115      log_info("eventq is empty, time is %f", time_);
00116 }
00117 
00118 extern "C" {
00119 int
00120 gettimeofday(struct timeval *tv, struct timezone *tz)
00121 {
00122     (void)tz;
00123     double now = Simulator::time();
00124     tv->tv_sec = (long int) now;
00125     tv->tv_usec = (int) ((now - tv->tv_sec) * 100000.0);
00126     return 0;
00127 }
00128 }
00129 
00130 void
00131 Simulator::process(SimEvent *e)
00132 {
00133     (void)e;
00134     NOTIMPLEMENTED;
00135 }
00136 
00137 } // namespace dtnsim

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1