Simulator.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 #include "Simulator.h"
00018 #include "Node.h"
00019 #include "Topology.h"
00020 #include "bundling/BundleTimestamp.h"
00021 #include "storage/BundleStore.h"
00022 #include "storage/LinkStore.h"
00023 #include "storage/GlobalStore.h"
00024 #include "storage/RegistrationStore.h"
00025 
00026 using namespace dtn;
00027 
00028 namespace dtnsim {
00029 
00030 Simulator* Simulator::instance_; 
00031 
00032 double Simulator::time_ = 0;
00033 double Simulator::runtill_ = -1;
00034 
00035 
00036 Simulator::Simulator(DTNStorageConfig* storage_config) 
00037     : DTNServer("/dtnsim/sim", storage_config), 
00038       eventq_(),
00039       store_(0)
00040 {
00041     Logger::classname_ = "Simulator";
00042 
00043     // override defaults from oasys storage config
00044     storage_config->type_ = "memorydb";
00045     storage_config->init_ = true;
00046     storage_config->tidy_ = true;
00047     storage_config->tidy_wait_ = 0;
00048     storage_config->leave_clean_file_ = false;
00049     storage_config->payload_dir_ = std::string("/tmp/dtnsim_payloads_") +
00050                                    getenv("USER");
00051 }
00052 
00053 void
00054 Simulator::post(SimEvent* e)
00055 {       
00056     instance_->eventq_.push(e);
00057 }
00058 
00059 void
00060 Simulator::exit() 
00061 {
00062     ::exit(0);
00063 }
00064 
00068 void
00069 Simulator::run()
00070 {
00071     oasys::Log* log = oasys::Log::instance();
00072     log->set_prefix("--");
00073     
00074     log_debug("Starting Simulator event loop...");
00075     is_running_ = true;
00076 
00077     log_debug("Handling events posted from the configuration...");
00078     Topology::NodeTable::iterator iter;
00079 
00080     for (iter =  Topology::node_table()->begin();
00081          iter != Topology::node_table()->end();
00082          ++iter)
00083     {
00084         Node* node = iter->second;
00085         node->set_active();
00086         node->process_bundle_events();
00087     }
00088         
00089     log->set_prefix("--");
00090     log_debug("Entering the event loop...");
00091     while(!eventq_.empty()) {
00092         log->set_prefix("--");
00093         if (is_running_) {
00094             SimEvent* e = eventq_.top();
00095             eventq_.pop();
00096             /* Move the clock */
00097             time_ = e->time();
00098             if (e->is_valid()) {
00099                 ASSERT(e->handler() != NULL);
00100                 /* Process the event */
00101                 log_debug("Event:%p type %s at time %f",
00102                           e, e->type_str(), time_);
00103                 e->handler()->process(e);
00104             }
00105             if ((Simulator::runtill_ != -1) &&
00106                 (time_ > Simulator::runtill_)) {
00107                 log_info("Exiting simulation. "
00108                          "Current time (%f) > Max time (%f)",
00109                          time_, Simulator::runtill_);
00110                 break;
00111             }
00112         } // if is_running_
00113     }
00114     log_info("eventq is empty, time is %f", time_);
00115 }
00116 
00117 extern "C" {
00118 int
00119 gettimeofday(struct timeval *tv, struct timezone *tz)
00120 {
00121     (void)tz;
00122     double now = Simulator::time();
00123     tv->tv_sec = (long int) now;
00124     tv->tv_usec = (int) ((now - tv->tv_sec) * 100000.0);
00125     return 0;
00126 }
00127 }
00128 
00129 void
00130 Simulator::process(SimEvent *e)
00131 {
00132     (void)e;
00133     NOTIMPLEMENTED;
00134 }
00135 
00136 } // namespace dtnsim

Generated on Thu Jun 7 12:54:29 2007 for DTN Reference Implementation by  doxygen 1.5.1