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

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1