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 "SimEvent.h" 00039 #include "Node.h" 00040 #include "SimBundleActions.h" 00041 #include "bundling/BundleDaemon.h" 00042 #include "contacts/ContactManager.h" 00043 #include "contacts/Link.h" 00044 #include "routing/BundleRouter.h" 00045 #include "routing/RouteTable.h" 00046 #include "reg/Registration.h" 00047 00048 using namespace dtn; 00049 00050 namespace dtnsim { 00051 00052 Node::Node(const char* name) 00053 : BundleDaemon(), name_(name), 00054 next_bundleid_(0), next_regid_(Registration::MAX_RESERVED_REGID) 00055 { 00056 logpathf("/node/%s", name); 00057 log_info("node %s initializing...", name); 00058 } 00059 00063 void 00064 Node::do_init() 00065 { 00066 actions_ = new SimBundleActions(); 00067 eventq_ = new std::queue<BundleEvent*>(); 00068 00069 BundleDaemon::instance_ = this; 00070 router_ = BundleRouter::create_router(BundleRouter::Config.type_.c_str()); 00071 } 00072 00077 void 00078 Node::post_event(BundleEvent* event) 00079 { 00080 eventq_->push(event); 00081 } 00082 00086 void 00087 Node::process_bundle_events() 00088 { 00089 log_debug("processing all bundle events"); 00090 BundleEvent* event; 00091 while (!eventq_->empty()) { 00092 event = eventq_->front(); 00093 eventq_->pop(); 00094 handle_event(event); 00095 delete event; 00096 } 00097 } 00098 00099 00100 void 00101 Node::process(SimEvent* simevent) 00102 { 00103 set_active(); 00104 00105 log_debug("handling event %s", simevent->type_str()); 00106 00107 switch(simevent->type()) { 00108 case SIM_ROUTER_EVENT: 00109 post_event(((SimRouterEvent*)simevent)->event_); 00110 break; 00111 00112 case SIM_ADD_LINK: { 00113 SimAddLinkEvent* e = (SimAddLinkEvent*)simevent; 00114 00115 // Add the link to contact manager, which posts a 00116 // LinkCreatedEvent to the daemon 00117 contactmgr_->add_link(e->link_); 00118 00119 break; 00120 } 00121 00122 case SIM_ADD_ROUTE: { 00123 SimAddRouteEvent* e = (SimAddRouteEvent*)simevent; 00124 00125 Link* link = contactmgr()->find_link(e->nexthop_.c_str()); 00126 00127 // XXX/demmer handle search by endpoint 00128 00129 if (link == NULL) { 00130 PANIC("no such link or node exists %s", e->nexthop_.c_str()); 00131 } 00132 00133 // XXX/demmer fix this ForwardingInfo::COPY_ACTION 00134 RouteEntry* entry = new RouteEntry(e->dest_, link); 00135 entry->action_ = ForwardingInfo::COPY_ACTION; 00136 post_event(new RouteAddEvent(entry)); 00137 break; 00138 } 00139 00140 default: 00141 PANIC("no Node handler for event %s", simevent->type_str()); 00142 } 00143 00144 process_bundle_events(); 00145 00146 delete simevent; 00147 } 00148 00149 } // namespace dtnsim