00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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
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
00098 time_ = e->time();
00099 if (e->is_valid()) {
00100 ASSERT(e->handler() != NULL);
00101
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 }
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 }