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

Generated on Thu Jun 7 12:54:27 2007 for DTN Reference Implementation by  doxygen 1.5.1