ProphetNode.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Baylor University
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 "ProphetNode.h"
00018 #include <netinet/in.h> // for ntoh*,hton*
00019 #include <math.h> // for pow()
00020 
00021 namespace dtn {
00022 
00023 ProphetNode::ProphetNode(
00024         ProphetNodeParams* params,
00025         const char* logbase)
00026     : oasys::Logger("ProphetNode",logbase),
00027       params_(params),
00028       p_value_(0.0), relay_(false), custody_(false),
00029       internet_gateway_(false), remote_eid_(EndpointID::NULL_EID())
00030 {
00031     age_.get_time(); // fill with current time
00032 }
00033 
00034 ProphetNode::ProphetNode(const ProphetNode& n)
00035     : oasys::Logger("ProphetNode",
00036                     ((oasys::Logger)n).logpath()),
00037       params_(n.params_),
00038       p_value_(n.p_value_), relay_(n.relay_), custody_(n.custody_),
00039       internet_gateway_(n.internet_gateway_), remote_eid_(n.remote_eid_)
00040 {
00041 }
00042 
00043 void
00044 RIBNode::dump(oasys::StringBuffer* buf)
00045 {
00046     buf->appendf("\tSID %d\n",sid_);
00047     ProphetNode::dump(buf);
00048 }
00049 
00050 void
00051 ProphetNode::dump(oasys::StringBuffer* buf)
00052 {
00053     buf->appendf("\tEndpointID %s\n"
00054                  "\tP Value %0.2f\n"
00055                  "\tRelay %s\n"
00056                  "\tCustody %s\n"
00057                  "\tInternet GW %s\n",
00058                  remote_eid_.c_str(),
00059                  p_value_,
00060                  relay_ ? "true" : "false",
00061                  custody_ ? "true" : "false",
00062                  internet_gateway_ ? "true" : "false");
00063 }
00064 
00065 void
00066 BundleOffer::dump(oasys::StringBuffer* buf)
00067 {
00068     buf->appendf("\t%s\n"
00069                  "\tCreation TS %d\n"
00070                  "\tSID %d\n"
00071                  "\tCustody %s\n"
00072                  "\tAccept %s\n"
00073                  "\tAck %s\n",
00074                  type_to_str(type_),
00075                  cts_, sid_,
00076                  custody_ ? "true" : "false",
00077                  accept_ ? "true" : "false",
00078                  ack_ ? "true" : "false");
00079 }
00080 
00081 bool
00082 ProphetNode::route_to_me(const EndpointID& eid) const
00083 {
00084     return Prophet::eid_to_route(remote_eid_).match(eid);
00085 }
00086 
00091 void
00092 ProphetNode::update_pvalue()
00093 {
00094     // new p_value is P_(A,B), previous p_value_ is P_(A,B)_old
00095     // params_->encounter_ is P_encounter
00096     // A is the local node
00097     // B is the node just encountered, represented by this ProphetNode instance
00098     ASSERT(p_value_ >= 0.0 && p_value_ <= 1.0);
00099     double prev = p_value_;
00100     (void)prev;
00101     p_value_ = p_value_ + (1.0 - p_value_) * params_->encounter_;
00102     log_debug("update_pvalue: before %.2f after %.2f",prev,p_value_);
00103 }
00104 
00109 void
00110 ProphetNode::update_transitive(double ab, double bc)
00111 {
00112     // new p_value_ is P_(A,C), previous p_value is P_(A,C)_old
00113     // params_->beta_ is beta
00114     // A is the local node
00115     // B is the node just encountered (originator of RIB)
00116     // C is the node represented by this ProphetNode instance
00117     ASSERT(p_value_ >= 0.0 && p_value_ <= 1.0);
00118     ASSERT(ab >= 0.0 && ab <= 1.0);
00119     ASSERT(bc >= 0.0 && bc <= 1.0);
00120     double prev = p_value_;
00121     (void)prev;
00122     p_value_ = p_value_ + (1.0 - p_value_) * ab * bc * params_->beta_;
00123     log_debug("update_transitive: ab %.2f bc %.2f before %.2f after %.2f",
00124               ab,bc,prev,p_value_);
00125 }
00126 
00131 void
00132 ProphetNode::update_age()
00133 {
00134     // new p_value_ is P_(A,B), previous p_value is P_(A,B)_old
00135     // params_->gamma_ is gamma
00136     // timeunits is k
00137     ASSERT(p_value_ >= 0.0 && p_value_ <= 1.0);
00138     double gfactor = params_->gamma_;
00139 
00140     oasys::Time now;
00141     now.get_time();
00142     u_int timeunits = time_to_units(now - age_);
00143     age_.get_time();
00144     
00145     if ( timeunits == 0 )
00146         gfactor = 1;
00147     else if ( timeunits > 1 )
00148         gfactor = pow( params_->gamma_, timeunits );
00149     double prev = p_value_;
00150     (void)prev;
00151     p_value_ *= gfactor;
00152     log_debug("update_age: timeunits %u before %.2f after %.2f",
00153               timeunits,prev,p_value_);
00154 }
00155 
00156 u_int
00157 ProphetNode::time_to_units(oasys::Time diff)
00158 {
00159     // params_->kappa_ is in units of milliseconds per timeunit
00160     u_int retval = (u_int) diff.sec_ * 1000 / params_->kappa_;
00161     retval += (u_int) diff.usec_ / (1000 * params_->kappa_);
00162     log_debug("time_to_units: diff sec %u usec %u timeunits %u",
00163               diff.sec_,diff.usec_,retval);
00164     return retval;
00165 }
00166 
00167 ProphetAck::ProphetAck()
00168     : dest_id_(EndpointID::NULL_EID()), cts_(0)
00169 {
00170 }
00171 
00172 ProphetAck::ProphetAck(const EndpointID& eid,
00173                        u_int32_t cts,
00174                        u_int32_t ets)
00175     : dest_id_(eid), cts_(cts), ets_(ets)
00176 {
00177 }
00178 
00179 ProphetAck::ProphetAck(const ProphetAck& p)
00180     : dest_id_(p.dest_id_), cts_(p.cts_)
00181 {
00182     ASSERT(!dest_id_.equals(EndpointID::NULL_EID()));
00183 }
00184 
00185 bool
00186 ProphetAck::operator<(const ProphetAck& p) const
00187 {
00188     if(dest_id_.equals(p.dest_id_))
00189         return (cts_ < p.cts_);
00190     return (dest_id_.str() < p.dest_id_.str());
00191 }
00192 
00193 ProphetAck&
00194 ProphetAck::operator=(const ProphetAck& p) 
00195 {
00196     dest_id_ = p.dest_id_;
00197     cts_ = p.cts_;
00198     return *this;
00199 }
00200 
00201 } // namespace dtn

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