ForwardingLog.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) 2005 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 
00039 #include <oasys/thread/SpinLock.h>
00040 #include <oasys/util/StringBuffer.h>
00041 #include "conv_layers/ConvergenceLayer.h"
00042 #include "contacts/Link.h"
00043 #include "ForwardingLog.h"
00044 
00045 namespace dtn {
00046 
00047 //----------------------------------------------------------------------
00048 ForwardingLog::ForwardingLog(oasys::SpinLock* lock)
00049     : lock_(lock)
00050 {
00051 }
00052 
00053 //----------------------------------------------------------------------
00054 bool
00055 ForwardingLog::get_latest_entry(Link* link, ForwardingInfo* info) const
00056 {
00057     oasys::ScopeLock l(lock_, "ForwardingLog::get_latest_state");
00058     
00059     Log::const_reverse_iterator iter;
00060     for (iter = log_.rbegin(); iter != log_.rend(); ++iter)
00061     {
00062         if (iter->nexthop_ == link->nexthop() &&
00063             iter->clayer_  == link->clayer()->name())
00064         {
00065             *info = *iter;
00066             return true;
00067         }
00068     }
00069 
00070     return false;
00071 }
00072 
00073 //----------------------------------------------------------------------
00074 ForwardingLog::state_t
00075 ForwardingLog::get_latest_entry(Link* link) const
00076 {
00077     ForwardingInfo info;
00078     if (! get_latest_entry(link, &info)) {
00079         return ForwardingInfo::NONE;
00080     }
00081 
00082     return info.state_;
00083 }
00084 
00085 //----------------------------------------------------------------------
00086 size_t
00087 ForwardingLog::get_transmission_count(ForwardingInfo::action_t action,
00088                                       bool include_inflight) const
00089 {
00090     size_t ret = 0;
00091     
00092     oasys::ScopeLock l(lock_, "ForwardingLog::get_transmission_count");
00093     
00094     Log::const_iterator iter;
00095     for (iter = log_.begin(); iter != log_.end(); ++iter)
00096     {
00097         if (iter->state_ == ForwardingInfo::TRANSMITTED ||
00098             (include_inflight && (iter->state_ == ForwardingInfo::IN_FLIGHT)))
00099         {
00100             if ((action == iter->action_) ||
00101                 (action == ForwardingInfo::INVALID_ACTION))
00102             {
00103                 ++ret;
00104             }
00105         }
00106     }
00107     
00108     return ret;
00109 }
00110 
00111 //----------------------------------------------------------------------
00112 size_t
00113 ForwardingLog::get_count(state_t state) const
00114 {
00115     size_t ret = 0;
00116 
00117     oasys::ScopeLock l(lock_, "ForwardingLog::get_count");
00118     
00119     Log::const_iterator iter;
00120     for (iter = log_.begin(); iter != log_.end(); ++iter)
00121     {
00122         if (iter->state_ == state) {
00123             ++ret;
00124         }
00125     }
00126 
00127     return ret;
00128 }
00129 
00130 //----------------------------------------------------------------------
00131 void
00132 ForwardingLog::dump(oasys::StringBuffer* buf) const
00133 {
00134     oasys::ScopeLock l(lock_, "ForwardingLog::dump");
00135     buf->appendf("forwarding log:\n");
00136     Log::const_iterator iter;
00137     for (iter = log_.begin(); iter != log_.end(); ++iter)
00138     {
00139         const ForwardingInfo* info = &(*iter);
00140         
00141         buf->appendf("\t%s -> %s %u.%u [%s cl:%s] [custody min %d pct %d max %d]\n",
00142                      ForwardingInfo::state_to_str(info->state_),
00143                      info->clayer_.c_str(),
00144                      (u_int)info->timestamp_.tv_sec,
00145                      (u_int)info->timestamp_.tv_usec,
00146                      ForwardingInfo::action_to_str(info->action_),
00147                      info->nexthop_.c_str(),
00148                      info->custody_timer_.min_,
00149                      info->custody_timer_.lifetime_pct_,
00150                      info->custody_timer_.max_);
00151     }
00152 }
00153     
00154 //----------------------------------------------------------------------
00155 void
00156 ForwardingLog::add_entry(Link* link,
00157                          ForwardingInfo::action_t action,
00158                          state_t state,
00159                          const CustodyTimerSpec& custody_timer)
00160 {
00161     log_.push_back(ForwardingInfo(state, action, link->clayer()->name(),
00162                                   link->nexthop(), custody_timer));
00163 }
00164 
00165 //----------------------------------------------------------------------
00166 bool
00167 ForwardingLog::update(Link* link, state_t state)
00168 {
00169     oasys::ScopeLock l(lock_, "ForwardingLog::update");
00170     
00171     Log::reverse_iterator iter;
00172     for (iter = log_.rbegin(); iter != log_.rend(); ++iter)
00173     {
00174         if (iter->nexthop_ == link->nexthop() &&
00175             iter->clayer_  == link->clayer()->name())
00176         {
00177             iter->set_state(state);
00178             return true;
00179         }
00180     }
00181     
00182     return false;
00183 }
00184 
00185 } // namespace dtn

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