GlueNode.cc

Go to the documentation of this file.
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 "GlueNode.h"
00039 
00040 #include "bundling/Bundle.h"
00041 #include "bundling/BundleActions.h"
00042 #include "bundling/BundleEvent.h"
00043 #include "contacts/Contact.h"
00044 
00045 #include "SimConvergenceLayer.h"
00046 
00047 namespace dtnsim {
00048 
00049 GlueNode::GlueNode(int id,const char* logpath): Node(id,logpath) 
00050 {
00051     router_ = BundleRouter::create_router(BundleRouter::type_.c_str());
00052     log_info("N[%d]: creating router of type:%s",id,BundleRouter::type_.c_str());
00053     
00054     consumer_ = NULL;
00055 }
00056 
00057 
00058 void 
00059 GlueNode::message_received(Message* msg) 
00060 {
00061 
00062     if (msg->dst() == id()) {
00063         log_info("RCV[%d]: src:%d id:%d, size-rcv %f",
00064                  id(),msg->src(),msg->id(),msg->size());
00065     }
00066     else {
00067         log_info("FWD[%d]: src:%d id:%d, size-rcv %f",
00068                  id(),msg->src(),msg->id(),msg->size());
00069     }
00070     forward(msg);
00071 }
00072 
00073 void forward_event(BundleEvent* event) ;
00074 
00075 
00076 void 
00077 GlueNode::chewing_complete(SimContact* c, double size, Message* msg) 
00078 {
00079 
00080     bool acked = true;
00081     Bundle* bundle = SimConvergenceLayer::msg2bundle(msg);
00082     Contact* consumer = SimConvergenceLayer::simlink2ct(c);
00083     int tsize = (int)size;
00084     BundleTransmittedEvent* e = 
00085         new BundleTransmittedEvent(bundle,consumer,tsize,acked ? tsize : 0);
00086     forward_event(e);
00087     
00088 }
00089 
00090 void 
00091 GlueNode::open_contact(SimContact* c) 
00092 {
00093     Link* link = SimConvergenceLayer::simlink2dtnlink(c);
00094     LinkCreatedEvent* e = new LinkCreatedEvent(link);
00095     log_debug("N[%d]: C:%d [%d->%d]:UP",id(),c->id(),id(),c->dst()->id());
00096     forward_event(e);
00097 }
00098 
00099 
00100 void 
00101 GlueNode::close_contact(SimContact* c)
00102 {
00103     Contact* ct = SimConvergenceLayer::simlink2ct(c);
00104     
00105     // ct may be null when the contact was never created
00106     if (ct != NULL) {
00107         ContactDownEvent* e = new ContactDownEvent(ct);
00108         log_debug("N[%d]: C:%d [%d->%d]:DOWN",id(),c->id(),id(),c->dst()->id());
00109         forward_event(e);
00110     }
00111 }
00112 
00113 
00114 void
00115 GlueNode::process(Event* e) {
00116     
00117     switch (e->type()) {
00118     case MESSAGE_RECEIVED:    {
00119         Event_message_received* e1 = (Event_message_received*)e;
00120         Message* msg = e1->msg_;
00121         log_info("GOT[%d]: id:%d size:%3f",id(),msg->id(),msg->size());
00122         
00123         // update the size of the message that is received
00124         msg->set_size(e1->sizesent_);
00125         message_received(msg);
00126         break;
00127     }
00128         
00129     case FOR_BUNDLE_ROUTER: {
00130         BundleEvent* be = ((Event_for_br* )e)->bundle_event_;
00131         forward_event(be);
00132         break;
00133     }
00134     default:
00135         PANIC("unimplemented action code");
00136     }
00137 }
00138 
00139 
00140 void
00141 GlueNode::forward(Message* msg) 
00142 {
00143     // first see if there exists a  bundle in the global hashtable
00144     Bundle *b = SimConvergenceLayer::msg2bundle(msg); 
00145     forward_event(new BundleReceivedEvent(b, EVENTSRC_PEER));
00146 }
00147 
00148 
00149 
00154 void
00155 GlueNode::execute_router_action(BundleAction* action)
00156 {
00157     Bundle* bundle;
00158     bundle = action->bundleref_.bundle();
00159     
00160     switch (action->action_) {
00161     case ENQUEUE_BUNDLE: {
00162         BundleEnqueueAction* enqaction = (BundleEnqueueAction*)action;
00163 
00164         BundleConsumer* bc = enqaction->nexthop_;
00165 
00166         if (bc->is_local()) {
00167             log_info("N[%d] reached destination id:%d",
00168                      id(), bundle->bundleid_);
00169         } else {
00170             log_info("N[%d] enqueue id:%d as told by routercode",
00171                      id(), bundle->bundleid_);
00172         }
00173         
00174         bc->enqueue_bundle(bundle);
00175         break;
00176     }
00177     case STORE_ADD: { 
00178         log_debug("N[%d] storing ignored %d", id(), bundle->bundleid_);
00179         break;
00180     }
00181     
00182     case STORE_DEL: {
00183         log_debug("N[%d] deletion ignored %d", id(), bundle->bundleid_);
00184         break;
00185     }
00186     default:
00187         PANIC("unimplemented action code %s",
00188               bundle_action_toa(action->action_));
00189     }
00190     delete action;
00191 }
00192 
00193 
00194 void 
00195 GlueNode::forward_event(BundleEvent* event) 
00196 {
00197 
00198     BundleActions actions;
00199     BundleActions::iterator iter;
00200     
00201     ASSERT(event);
00202     
00203     // always clear the action list
00204     actions.clear();
00205     
00206     // dispatch to the router to fill in the actions list
00207     router_->handle_event(event, &actions);
00208     
00209     // process the actions
00210     for (iter = actions.begin(); iter != actions.end(); ++iter) {
00211         execute_router_action(*iter);
00212     }
00213     
00214 }
00215 
00216 
00217 
00218 void    
00219 GlueNode::create_consumer()
00220 {
00221     consumer_ = new FloodConsumer(id_,"dtn://2");
00222     consumer_->set_router(router_);
00223 
00224     RegistrationAddedEvent *reg_add =
00225         new RegistrationAddedEvent(consumer_, EVENTSRC_ADMIN);
00226     forward_event(reg_add);
00227 }
00228 
00229 } // namespace dtnsim

Generated on Fri Dec 22 14:47:59 2006 for DTN Reference Implementation by  doxygen 1.5.1