00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <dtn-config.h>
00019 #endif
00020
00021 #include <oasys/thread/Timer.h>
00022 #include "SimEvent.h"
00023 #include "SimLog.h"
00024 #include "Node.h"
00025 #include "bundling/BundleDaemon.h"
00026 #include "contacts/ContactManager.h"
00027 #include "contacts/Link.h"
00028 #include "routing/BundleRouter.h"
00029 #include "routing/RouteTable.h"
00030 #include "reg/Registration.h"
00031
00032 using namespace dtn;
00033
00034 namespace dtnsim {
00035
00036
00037 Node::Node(const char* name)
00038 : BundleDaemon(), name_(name),
00039 storage_config_("storage",
00040 "memorydb",
00041 "DTN", "")
00042
00043 {
00044 logpathf("/node/%s", name);
00045 log_info("node %s initializing...", name);
00046
00047 storage_config_.init_ = true;
00048 storage_config_.tidy_ = true;
00049 storage_config_.tidy_wait_ = 0;
00050 storage_config_.leave_clean_file_ = false;
00051
00052
00053 storage_config_.payload_dir_ = "NO_PAYLOAD_FILES";
00054 }
00055
00056
00057 void
00058 Node::do_init()
00059 {
00060 BundleDaemon::instance_ = this;
00061
00062 actions_ = new BundleActions();
00063 eventq_ = new std::queue<BundleEvent*>();
00064
00065
00066 oasys::Singleton<oasys::TimerSystem>::force_set_instance(NULL);
00067 oasys::TimerSystem::create();
00068 timersys_ = oasys::TimerSystem::instance();
00069
00070 store_ = new oasys::DurableStore("/dtnsim/storage");
00071 store_->create_store(storage_config_);
00072
00073
00074
00075
00076 if (! GlobalStore::initialized()) {
00077 if (GlobalStore::init(storage_config_, store_) != 0) {
00078 PANIC("Error initializing GlobalStore");
00079 }
00080 }
00081
00082
00083 BundleStore::force_set_instance(NULL);
00084 ProphetStore::force_set_instance(NULL);
00085 LinkStore::force_set_instance(NULL);
00086 RegistrationStore::force_set_instance(NULL);
00087
00088 log_info("creating storage tables");
00089 if ((BundleStore::init(storage_config_, store_) != 0) ||
00090 (LinkStore::init(storage_config_, store_) != 0) ||
00091 (ProphetStore::init(storage_config_, store_) != 0) ||
00092 (RegistrationStore::init(storage_config_, store_) != 0))
00093 {
00094 PANIC("Error initializing storage tables");
00095 }
00096
00097 bundle_store_ = BundleStore::instance();
00098 prophet_store_ = ProphetStore::instance();
00099 link_store_ = LinkStore::instance();
00100 reg_store_ = RegistrationStore::instance();
00101 }
00102
00103
00104 void
00105 Node::set_active()
00106 {
00107 if (instance_ == this) return;
00108
00109 instance_ = this;
00110 oasys::Singleton<oasys::TimerSystem>::force_set_instance(timersys_);
00111 oasys::Log::instance()->set_prefix(name_.c_str());
00112
00113 BundleStore::force_set_instance(bundle_store_);
00114 ProphetStore::force_set_instance(prophet_store_);
00115 LinkStore::force_set_instance(link_store_);
00116 RegistrationStore::force_set_instance(reg_store_);
00117 }
00118
00119
00120 void
00121 Node::configure()
00122 {
00123 set_active();
00124
00125 router_ = BundleRouter::create_router(BundleRouter::config_.type_.c_str());
00126 router_->initialize();
00127 }
00128
00129
00130 void
00131 Node::post_event(BundleEvent* event, bool at_back)
00132 {
00133 (void)at_back;
00134 log_debug("posting event (%p) with type %s at %s ",
00135 event, event->type_str(),at_back ? "back" : "head");
00136
00137 eventq_->push(event);
00138 }
00139
00140
00141 bool
00142 Node::process_one_bundle_event()
00143 {
00144 BundleEvent* event;
00145 if (!eventq_->empty()) {
00146 event = eventq_->front();
00147 eventq_->pop();
00148 handle_event(event);
00149 delete event;
00150 log_debug("event (%p) %s processed and deleted",event,event->type_str());
00151 return true;
00152 }
00153 return false;
00154 }
00155
00156
00157 void
00158 Node::run_one_event_now(BundleEvent* event)
00159 {
00160 Node* cur_active = active_node();
00161 set_active();
00162 handle_event(event);
00163 log_debug("event (%p) %s processed",event,event->type_str());
00164 cur_active->set_active();
00165 }
00166
00167
00168 void
00169 Node::process(SimEvent* e)
00170 {
00171 switch (e->type()) {
00172 case SIM_BUNDLE_EVENT:
00173 post_event(((SimBundleEvent*)e)->event_);
00174 break;
00175
00176 default:
00177 NOTREACHED;
00178 }
00179 }
00180
00181
00182 void
00183 Node::handle_bundle_delivered(BundleDeliveredEvent* event)
00184 {
00185 SimLog::instance()->log_arrive(this, event->bundleref_.object());
00186 BundleDaemon::handle_bundle_delivered(event);
00187 }
00188
00189
00190 void
00191 Node::handle_bundle_received(BundleReceivedEvent* event)
00192 {
00193 Bundle* bundle = event->bundleref_.object();
00194 SimLog::instance()->log_recv(this, bundle);
00195
00196
00197
00198
00199 Bundle* duplicate = find_duplicate(bundle);
00200 if (duplicate != NULL) {
00201 SimLog::instance()->log_dup(this, bundle);
00202 }
00203 BundleDaemon::handle_bundle_received(event);
00204 }
00205
00206
00207 void
00208 Node::handle_bundle_transmitted(BundleTransmittedEvent* event)
00209 {
00210 SimLog::instance()->log_xmit(this, event->bundleref_.object());
00211 BundleDaemon::handle_bundle_transmitted(event);
00212 }
00213
00214
00215
00216 void
00217 Node::handle_bundle_expired(BundleExpiredEvent* event)
00218 {
00219 SimLog::instance()->log_expire(this, event->bundleref_.object());
00220 BundleDaemon::handle_bundle_expired(event);
00221 }
00222
00223 }