00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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
00113
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
00125
00126
00127
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 }