Node.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 "SimEvent.h"
00018 #include "Node.h"
00019 #include "SimBundleActions.h"
00020 #include "bundling/BundleDaemon.h"
00021 #include "contacts/ContactManager.h"
00022 #include "contacts/Link.h"
00023 #include "routing/BundleRouter.h"
00024 #include "routing/RouteTable.h"
00025 #include "reg/Registration.h"
00026 
00027 using namespace dtn;
00028 
00029 namespace dtnsim {
00030 
00031 Node::Node(const char* name)
00032     : BundleDaemon(), name_(name)
00033 {
00034     logpathf("/node/%s", name);
00035     log_info("node %s initializing...", name);
00036 
00037     BundleDaemon::is_simulator_ = true;
00038 }
00039 
00043 void
00044 Node::do_init()
00045 {
00046     actions_ = new SimBundleActions();
00047     eventq_ = new std::queue<BundleEvent*>();
00048 
00049     BundleDaemon::instance_ = this;
00050     router_ = BundleRouter::create_router(BundleRouter::config_.type_.c_str());
00051 }
00052 
00057 void
00058 Node::post_event(BundleEvent* event, bool at_back)
00059 {
00060     (void)at_back;
00061     log_debug("posting event (%p) with type %s at %s ",event, event->type_str(),at_back ? "back" : "head");
00062         
00063     eventq_->push(event);
00064 }
00065 
00069 void
00070 Node::process_bundle_events()
00071 {
00072     log_debug("processing all bundle events");
00073     BundleEvent* event;
00074     while (!eventq_->empty()) {
00075         event = eventq_->front();
00076         eventq_->pop();
00077         handle_event(event);
00078         delete event;
00079         log_debug("event (%p) %s processed and deleted",event,event->type_str());
00080     }
00081     log_debug("done processing all bundle events");
00082 }
00083 
00084 
00085 void
00086 Node::process(SimEvent* simevent)
00087 {
00088     set_active();
00089     
00090     log_debug("handling event %s", simevent->type_str());
00091 
00092     switch(simevent->type()) {
00093     case SIM_ROUTER_EVENT:
00094         post_event(((SimRouterEvent*)simevent)->event_);
00095         break;
00096 
00097     case SIM_ADD_LINK: {
00098         SimAddLinkEvent* e = (SimAddLinkEvent*)simevent;
00099 
00100         // Add the link to contact manager, which posts a
00101         // LinkCreatedEvent to the daemon
00102         contactmgr_->add_link(e->link_);
00103         
00104         break;
00105     }
00106         
00107     case SIM_ADD_ROUTE: {
00108         SimAddRouteEvent* e = (SimAddRouteEvent*)simevent;
00109         
00110         Link* link = contactmgr()->find_link(e->nexthop_.c_str());
00111 
00112         // XXX/demmer handle search by endpoint
00113 
00114         if (link == NULL) {
00115             PANIC("no such link or node exists %s", e->nexthop_.c_str());
00116         }
00117 
00118         // XXX/demmer fix this ForwardingInfo::COPY_ACTION
00119         RouteEntry* entry = new RouteEntry(e->dest_, link);
00120         entry->action_ = ForwardingInfo::COPY_ACTION;
00121         post_event(new RouteAddEvent(entry));
00122         break;
00123     }
00124             
00125     default:
00126         PANIC("no Node handler for event %s", simevent->type_str());
00127     }
00128     
00129     process_bundle_events();
00130 
00131     delete simevent;
00132 }
00133 
00134 } // namespace dtnsim

Generated on Sat Sep 8 08:36:17 2007 for DTN Reference Implementation by  doxygen 1.5.3