RouteEntry.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) 2006 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/util/OptParser.h>
00040 #include <oasys/util/StringBuffer.h>
00041 #include "BundleRouter.h"
00042 #include "RouteEntry.h"
00043 #include "contacts/Link.h"
00044 #include "naming/EndpointIDOpt.h"
00045 
00046 namespace dtn {
00047 
00048 //----------------------------------------------------------------------
00049 RouteEntry::RouteEntry(const EndpointIDPattern& dest_pattern, Link* link)
00050     : dest_pattern_(dest_pattern),
00051       source_pattern_(EndpointID::WILDCARD_EID()),
00052       bundle_cos_((1 << Bundle::COS_BULK) |
00053                   (1 << Bundle::COS_NORMAL) |
00054                   (1 << Bundle::COS_EXPEDITED)),
00055       route_priority_(0),
00056       next_hop_(link),
00057       action_(ForwardingInfo::FORWARD_ACTION),
00058       custody_timeout_(),
00059       info_(NULL)
00060 {
00061     route_priority_ = BundleRouter::Config.default_priority_;
00062 }
00063 
00064 //----------------------------------------------------------------------
00065 RouteEntry::~RouteEntry()
00066 {
00067     if (info_)
00068         delete info_;
00069 }
00070 
00071 //----------------------------------------------------------------------
00072 int
00073 RouteEntry::parse_options(int argc, const char** argv, const char** invalidp)
00074 {
00075     int num = custody_timeout_.parse_options(argc, argv, invalidp);
00076     if (num == -1) {
00077         return -1;
00078     }
00079     
00080     argc -= num;
00081     
00082     oasys::OptParser p;
00083 
00084     p.addopt(new EndpointIDOpt("source_eid", &source_pattern_));
00085     p.addopt(new oasys::UIntOpt("route_priority", &route_priority_));
00086     p.addopt(new oasys::UIntOpt("cos_flags", &bundle_cos_));
00087     oasys::EnumOpt::Case fwdopts[] = {
00088         {"forward", ForwardingInfo::FORWARD_ACTION},
00089         {"copy",    ForwardingInfo::COPY_ACTION},
00090         {0, 0}
00091     };
00092     p.addopt(new oasys::EnumOpt("action", fwdopts, (int*)&action_));
00093 
00094     int num2 = p.parse_and_shift(argc, argv, invalidp);
00095     if (num2 == -1) {
00096         return -1;
00097     }
00098     
00099     if ((bundle_cos_ == 0) || (bundle_cos_ >= (1 << 3))) {
00100         static const char* s = "invalid cos flags";
00101         invalidp = &s;
00102         return -1;
00103     }
00104 
00105     return num + num2;
00106 }
00107 
00108 //----------------------------------------------------------------------
00109 int
00110 RouteEntry::format(char* bp, size_t sz) const
00111 {
00112     // XXX/demmer when the route table is serialized, add an integer
00113     // id for the route entry and include it here.
00114     return snprintf(bp, sz, "%s -> %s (%s)",
00115                     dest_pattern_.c_str(),
00116                     next_hop_->nexthop(),
00117                     ForwardingInfo::action_to_str(action_));
00118 }
00119 
00120 //----------------------------------------------------------------------
00121 void
00122 RouteEntry::dump_header(oasys::StringBuffer* buf)
00123 {
00124     // though this is a less efficient way of appending this data, it
00125     // makes it much easier to copy the format string to the ::dump
00126     // method, and given that it's only used for diagnostics, the
00127     // performance impact is negligible
00128     buf->appendf("%-15s %-10s %3s    %-13s %-7s %5s [%-15s]\n"
00129                  "%-15s %-10s %3s    %-13s %-7s %5s [%-5s %-3s %-5s]\n"
00130                  "-----------------------------------"
00131                  "-------------------------------------------\n",
00132                  "destination",
00133                  " source",
00134                  "COS",
00135                  "link",
00136                  " fwd  ",
00137                  "route",
00138                  "custody timeout",
00139                  " endpoint",
00140                  "endpoint",
00141                  "BNE",
00142                  "name",
00143                  "action",
00144                  "prio",
00145                  "min",
00146                  "pct",
00147                  "  max");
00148 }
00149 
00150 //----------------------------------------------------------------------
00151 void
00152 RouteEntry::dump(oasys::StringBuffer* buf, EndpointIDVector* long_eids) const
00153 {
00154     size_t len;
00155     if (dest_pattern_.length() <= 15) {
00156         buf->appendf("%-15s ", dest_pattern_.c_str());
00157     } else {
00158         len = buf->appendf("[%zu]", long_eids->size());
00159         buf->appendf("%.*s", 16 - (int)len, "               ");
00160         long_eids->push_back(dest_pattern_);
00161     }
00162     
00163     if (source_pattern_.length() <= 10) {
00164         buf->appendf("%-10s ", source_pattern_.c_str());
00165     } else {
00166         len = buf->appendf("[%zu]", long_eids->size());
00167         buf->appendf("%.*s", 11 - (int)len, "               ");
00168         long_eids->push_back(source_pattern_);
00169     }
00170     
00171     buf->appendf("%c%c%c -> %-13.13s %7s %5d [%-5d %3d %5d]\n",
00172                  (bundle_cos_ & (1 << Bundle::COS_BULK))      ? '1' : '0',
00173                  (bundle_cos_ & (1 << Bundle::COS_NORMAL))    ? '1' : '0',
00174                  (bundle_cos_ & (1 << Bundle::COS_EXPEDITED)) ? '1' : '0',
00175                  next_hop_->name(),
00176                  ForwardingInfo::action_to_str(action_),
00177                  route_priority_,
00178                  custody_timeout_.min_,
00179                  custody_timeout_.lifetime_pct_,
00180                  custody_timeout_.max_);
00181 }
00182 
00183 //----------------------------------------------------------------------
00187 struct RoutePriorityGT {
00188     bool operator() (RouteEntry* a, RouteEntry* b) {
00189         if (a->route_priority_ == b->route_priority_)
00190         {
00191             return (a->next_hop_->stats()->bytes_inflight_ <
00192                     b->next_hop_->stats()->bytes_inflight_);
00193         }
00194         
00195         return a->route_priority_ > b->route_priority_;
00196     }
00197 };
00198 
00199 //----------------------------------------------------------------------
00200 void
00201 RouteEntryVec::sort_by_priority()
00202 {
00203     std::sort(begin(), end(), RoutePriorityGT());
00204 }
00205 
00206 } // namespace dtn

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