00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <dtn-config.h>
00019 #endif
00020
00021 #include <oasys/util/OptParser.h>
00022 #include <oasys/util/StringBuffer.h>
00023 #include "BundleRouter.h"
00024 #include "RouteEntry.h"
00025 #include "naming/EndpointIDOpt.h"
00026
00027 namespace dtn {
00028
00029
00030 RouteEntry::RouteEntry(const EndpointIDPattern& dest_pattern,
00031 const LinkRef& link)
00032 : dest_pattern_(dest_pattern),
00033 source_pattern_(EndpointIDPattern::WILDCARD_EID()),
00034 bundle_cos_((1 << Bundle::COS_BULK) |
00035 (1 << Bundle::COS_NORMAL) |
00036 (1 << Bundle::COS_EXPEDITED)),
00037 priority_(BundleRouter::config_.default_priority_),
00038 link_(link.object(), "RouteEntry"),
00039 route_to_(),
00040 action_(ForwardingInfo::FORWARD_ACTION),
00041 custody_spec_(),
00042 info_(NULL)
00043 {
00044 }
00045
00046
00047 RouteEntry::RouteEntry(const EndpointIDPattern& dest_pattern,
00048 const EndpointIDPattern& route_to)
00049 : dest_pattern_(dest_pattern),
00050 source_pattern_(EndpointIDPattern::WILDCARD_EID()),
00051 bundle_cos_((1 << Bundle::COS_BULK) |
00052 (1 << Bundle::COS_NORMAL) |
00053 (1 << Bundle::COS_EXPEDITED)),
00054 priority_(BundleRouter::config_.default_priority_),
00055 link_("RouteEntry"),
00056 route_to_(route_to),
00057 action_(ForwardingInfo::FORWARD_ACTION),
00058 custody_spec_(),
00059 info_(NULL)
00060 {
00061 }
00062
00063
00064 RouteEntry::~RouteEntry()
00065 {
00066 if (info_)
00067 delete info_;
00068 }
00069
00070
00071 int
00072 RouteEntry::parse_options(int argc, const char** argv, const char** invalidp)
00073 {
00074 int num = custody_spec_.parse_options(argc, argv, invalidp);
00075 if (num == -1) {
00076 return -1;
00077 }
00078
00079 argc -= num;
00080
00081 oasys::OptParser p;
00082
00083 p.addopt(new EndpointIDOpt("source_eid", &source_pattern_));
00084 p.addopt(new oasys::UIntOpt("route_priority", &priority_));
00085 p.addopt(new oasys::UIntOpt("cos_flags", &bundle_cos_));
00086 oasys::EnumOpt::Case fwdopts[] = {
00087 {"forward", ForwardingInfo::FORWARD_ACTION},
00088 {"copy", ForwardingInfo::COPY_ACTION},
00089 {0, 0}
00090 };
00091
00092
00093 int action = action_;
00094 p.addopt(new oasys::EnumOpt("action", fwdopts, &action));
00095
00096 int num2 = p.parse_and_shift(argc, argv, invalidp);
00097 if (num2 == -1) {
00098 return -1;
00099 }
00100
00101
00102
00103 action_ = action;
00104
00105 if ((bundle_cos_ == 0) || (bundle_cos_ >= (1 << 3))) {
00106 static const char* s = "invalid cos flags";
00107 invalidp = &s;
00108 return -1;
00109 }
00110
00111 return num + num2;
00112 }
00113
00114
00115 int
00116 RouteEntry::format(char* bp, size_t sz) const
00117 {
00118
00119
00120 return snprintf(bp, sz, "%s -> %s (%s)",
00121 dest_pattern().c_str(),
00122 next_hop_str().c_str(),
00123 ForwardingInfo::action_to_str(action()));
00124 }
00125
00126 static const char* DASHES =
00127 "---------------------------------------------"
00128 "---------------------------------------------"
00129 "---------------------------------------------"
00130 "---------------------------------------------"
00131 "---------------------------------------------"
00132 "---------------------------------------------";
00133
00134
00135 void
00136 RouteEntry::dump_header(oasys::StringBuffer* buf,
00137 int dest_eid_width,
00138 int source_eid_width,
00139 int next_hop_width)
00140 {
00141
00142
00143
00144
00145 buf->appendf("%-*.*s %-*.*s %3s %-*.*s %-7s %5s [%-15s]\n"
00146 "%-*.*s %-*.*s %3s %-*.*s %-7s %5s [%-5s %-3s %-5s]\n"
00147 "%.*s\n",
00148 dest_eid_width, dest_eid_width, "destination",
00149 source_eid_width, source_eid_width, "source",
00150 "COS",
00151 next_hop_width, next_hop_width, "next hop",
00152 " fwd ",
00153 "route",
00154 "custody timeout",
00155
00156 dest_eid_width, dest_eid_width, "endpoint id",
00157 source_eid_width, source_eid_width, " eid",
00158 "BNE",
00159 next_hop_width, next_hop_width, "link/eid",
00160 "action",
00161 "prio",
00162 "min",
00163 "pct",
00164 " max",
00165
00166 dest_eid_width + source_eid_width + next_hop_width + 51,
00167 DASHES);
00168 }
00169
00170
00171 void
00172 RouteEntry::append_long_string(oasys::StringBuffer* buf,
00173 oasys::StringVector* long_strings,
00174 int width, const std::string& str)
00175 {
00176 size_t tmplen;
00177 char tmp[64];
00178
00179 if (str.length() <= (size_t)width) {
00180 buf->appendf("%-*.*s ", width, width, str.c_str());
00181 } else {
00182 size_t index;
00183 for (index = 0; index < long_strings->size(); ++index) {
00184 if ((*long_strings)[index] == str) break;
00185 }
00186 if (index == long_strings->size()) {
00187 long_strings->push_back(str);
00188 }
00189
00190 tmplen = snprintf(tmp, sizeof(tmp), "[%zu] ", index);
00191 buf->appendf("%.*s... %s",
00192 width - 3 - (int)tmplen, str.c_str(), tmp);
00193
00194 }
00195 }
00196
00197
00198 void
00199 RouteEntry::dump(oasys::StringBuffer* buf,
00200 oasys::StringVector* long_strings,
00201 int dest_eid_width,
00202 int source_eid_width,
00203 int next_hop_width) const
00204 {
00205 append_long_string(buf, long_strings, dest_eid_width, dest_pattern().str());
00206 append_long_string(buf, long_strings, source_eid_width, source_pattern().str());
00207
00208 buf->appendf("%c%c%c -> ",
00209 (bundle_cos_ & (1 << Bundle::COS_BULK)) ? '1' : '0',
00210 (bundle_cos_ & (1 << Bundle::COS_NORMAL)) ? '1' : '0',
00211 (bundle_cos_ & (1 << Bundle::COS_EXPEDITED)) ? '1' : '0');
00212
00213 append_long_string(buf, long_strings, next_hop_width, next_hop_str());
00214
00215 buf->appendf("%7s %5d [%-5d %3d %5d]\n",
00216 ForwardingInfo::action_to_str(action()),
00217 priority(),
00218 custody_spec().min_,
00219 custody_spec().lifetime_pct_,
00220 custody_spec().max_);
00221 }
00222
00223
00224 void
00225 RouteEntry::serialize(oasys::SerializeAction *a)
00226 {
00227 a->process("dest_pattern", &dest_pattern_);
00228 a->process("source_pattern", &source_pattern_);
00229 a->process("bundle_cos", &bundle_cos_);
00230 a->process("priority", &priority_);
00231 a->process("link", const_cast<std::string *>(&link_->name_str()));
00232 a->process("route_to", &route_to_);
00233 a->process("action", &action_);
00234 a->process("custody_spec", &custody_spec_);
00235
00236
00237 }
00238
00239 }