SimpleNode.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 "SimpleNode.h"
00018 
00019 namespace dtnsim {
00020 
00021 SimpleNode::SimpleNode( int id, const char* logpath) 
00022     : Node(id,logpath),msgq_(0)
00023 {
00024 }
00025 
00026 SimpleNode::~SimpleNode() {}
00027 
00028 
00029 void 
00030 SimpleNode::chewing_complete(SimContact* c, double size, Message* msg) 
00031 {
00032     
00033     if (msg->size() != size){
00034 
00041         log_info("ChXXX for msg %d, frac %f",msg->id(),size*1.0/msg->size());
00042         if (size != 0) {
00043             msg = (Message* )msg->clone();
00044             msg->rm_bytes(size);
00045         }
00046         // Forward the new fragment
00047         forward(msg);
00048     }
00049     else {
00050         log_info("ChC  for msg %d",msg->id());
00051     }
00052 }
00053 
00060 bool 
00061 is_relevant(SimContact* c, Message* m) 
00062 {
00063     if (m->src() == c->dst()->id()) return false;
00064     return true;
00065 }
00066 
00070 void
00071 SimpleNode::forward(Message* msg) 
00072 {
00073     for(int i=0;i<Topology::contacts();i++){
00074         SimContact *c = Topology::contact(i);
00075         if (c->is_open() && (c->src()->id() == id() )) {
00076             
00077             if (is_relevant(c,msg)) {
00078                 c->chew_message(msg);
00079                 return;
00080             }
00081         }
00082     }
00083     /* No one ate the message, so store it */
00084     msgq_.push_back(msg);
00085 }
00086 
00090 void 
00091 SimpleNode::open_contact(SimContact* ct) 
00092 {
00093     for(u_int i=0;i<msgq_.size();i++) {
00094         Message* msg =  msgq_[i];
00095         if (is_relevant(ct,msg)) {
00096             
00097             log_debug("contact is open and valid for messaged %d",msg->id());
00098             std::vector<Message*>::iterator rmv = msgq_.begin() + i - 1;
00099             
00100             // now, actually remove this element from the array
00101             msgq_.erase(rmv);
00102             ct->chew_message(msg);
00103             return;
00104         }
00105     }
00106 
00107 }
00108 
00109 void 
00110 SimpleNode::close_contact(SimContact* c) 
00111 {
00112 // Do nothing
00113 }
00114 
00115 
00116 void
00117 SimpleNode::message_received(Message* msg) 
00118 {
00119     if (msg->dst() == id()) {
00120         // Local message, declare it to be received
00121         log_info("Rc msg at node %d, id %d, size-rcv %f",
00122                  id(),msg->id(),msg->size());
00123     }
00124     else {
00125         // Forward it to appropriate next hop
00126         log_info("Fw msg at node %d, id %d, size-rcv %f",
00127                  id(),msg->id(),msg->size());
00128         forward(msg);
00129     }
00130 }
00131 
00132 
00133 void
00134 SimpleNode::process(Event* e) 
00135 {
00136     
00137     if (e->type() == MESSAGE_RECEIVED) {
00138         
00139         Event_message_received* e1 = (Event_message_received*)e;
00140         Message* msg = e1->msg_;
00141         log_info("received msg (%d) size %3f",msg->id(),msg->size());
00142         
00143         // update the size of the message that is received
00144         msg->set_size(e1->sizesent_);
00145         message_received(msg);
00146         
00147     }
00148     else
00149         PANIC("unimplemented action code");
00150 }
00151 
00152 } // namespace dtnsim

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