NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
nfd-constants.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
25 
26 #include <boost/algorithm/string.hpp>
27 #include <boost/lexical_cast.hpp>
28 #include <istream>
29 #include <map>
30 #include <ostream>
31 
32 namespace ndn {
33 namespace nfd {
34 
35 std::ostream&
36 operator<<(std::ostream& os, FaceScope faceScope)
37 {
38  switch (faceScope) {
39  case FACE_SCOPE_NONE:
40  return os << "none";
42  return os << "non-local";
43  case FACE_SCOPE_LOCAL:
44  return os << "local";
45  }
46  return os << static_cast<unsigned>(faceScope);
47 }
48 
49 std::ostream&
50 operator<<(std::ostream& os, FacePersistency facePersistency)
51 {
52  switch (facePersistency) {
54  return os << "none";
56  return os << "persistent";
58  return os << "on-demand";
60  return os << "permanent";
61  }
62  return os << static_cast<unsigned>(facePersistency);
63 }
64 
65 std::ostream&
66 operator<<(std::ostream& os, LinkType linkType)
67 {
68  switch (linkType) {
69  case LINK_TYPE_NONE:
70  return os << "none";
72  return os << "point-to-point";
74  return os << "multi-access";
75  case LINK_TYPE_AD_HOC:
76  return os << "adhoc";
77  }
78  return os << static_cast<unsigned>(linkType);
79 }
80 
81 std::ostream&
82 operator<<(std::ostream& os, FaceEventKind faceEventKind)
83 {
84  switch (faceEventKind) {
85  case FACE_EVENT_NONE:
86  return os << "none";
87  case FACE_EVENT_CREATED:
88  return os << "created";
90  return os << "destroyed";
91  case FACE_EVENT_UP:
92  return os << "up";
93  case FACE_EVENT_DOWN:
94  return os << "down";
95  }
96  return os << static_cast<unsigned>(faceEventKind);
97 }
98 
99 std::istream&
100 operator>>(std::istream& is, RouteOrigin& routeOrigin)
101 {
102  using boost::algorithm::iequals;
103 
104  std::string s;
105  is >> s;
106 
107  if (iequals(s, "none"))
108  routeOrigin = ROUTE_ORIGIN_NONE;
109  else if (iequals(s, "app"))
110  routeOrigin = ROUTE_ORIGIN_APP;
111  else if (iequals(s, "autoreg"))
112  routeOrigin = ROUTE_ORIGIN_AUTOREG;
113  else if (iequals(s, "client"))
114  routeOrigin = ROUTE_ORIGIN_CLIENT;
115  else if (iequals(s, "autoconf"))
116  routeOrigin = ROUTE_ORIGIN_AUTOCONF;
117  else if (iequals(s, "nlsr"))
118  routeOrigin = ROUTE_ORIGIN_NLSR;
119  else if (iequals(s, "prefixann"))
120  routeOrigin = ROUTE_ORIGIN_PREFIXANN;
121  else if (iequals(s, "static"))
122  routeOrigin = ROUTE_ORIGIN_STATIC;
123  else {
124  // To reject negative numbers, we parse as a wider signed type, and compare with the range.
125  static_assert(std::numeric_limits<std::underlying_type_t<RouteOrigin>>::max() <=
126  std::numeric_limits<int>::max(), "");
127 
128  int v = -1;
129  try {
130  v = boost::lexical_cast<int>(s);
131  }
132  catch (const boost::bad_lexical_cast&) {
133  }
134 
135  if (v >= std::numeric_limits<std::underlying_type_t<RouteOrigin>>::min() &&
136  v <= std::numeric_limits<std::underlying_type_t<RouteOrigin>>::max()) {
137  routeOrigin = static_cast<RouteOrigin>(v);
138  }
139  else {
140  routeOrigin = ROUTE_ORIGIN_NONE;
141  is.setstate(std::ios::failbit);
142  }
143  }
144 
145  return is;
146 }
147 
148 std::ostream&
149 operator<<(std::ostream& os, RouteOrigin routeOrigin)
150 {
151  switch (routeOrigin) {
152  case ROUTE_ORIGIN_NONE:
153  return os << "none";
154  case ROUTE_ORIGIN_APP:
155  return os << "app";
157  return os << "autoreg";
158  case ROUTE_ORIGIN_CLIENT:
159  return os << "client";
161  return os << "autoconf";
162  case ROUTE_ORIGIN_NLSR:
163  return os << "nlsr";
165  return os << "prefixann";
166  case ROUTE_ORIGIN_STATIC:
167  return os << "static";
168  }
169  return os << static_cast<unsigned>(routeOrigin);
170 }
171 
172 std::ostream&
173 operator<<(std::ostream& os, RouteFlags routeFlags)
174 {
175  if (routeFlags == ROUTE_FLAGS_NONE) {
176  return os << "none";
177  }
178 
179  static const std::map<RouteFlags, std::string> knownBits = {
180  {ROUTE_FLAG_CHILD_INHERIT, "child-inherit"},
181  {ROUTE_FLAG_CAPTURE, "capture"}
182  };
183 
184  auto join = make_ostream_joiner(os, '|');
185  for (const auto& pair : knownBits) {
187  std::string token;
188  std::tie(bit, token) = pair;
189 
190  if ((routeFlags & bit) != 0) {
191  join = token;
192  routeFlags = static_cast<RouteFlags>(routeFlags & ~bit);
193  }
194  }
195 
196  if (routeFlags != ROUTE_FLAGS_NONE) {
197  join = AsHex{routeFlags};
198  }
199 
200  return os;
201 }
202 
203 } // namespace nfd
204 } // namespace ndn
ndn::nfd::FACE_EVENT_DOWN
@ FACE_EVENT_DOWN
face went DOWN (from UP state)
Definition: nfd-constants.hpp:82
ndn::nfd::LINK_TYPE_NONE
@ LINK_TYPE_NONE
Definition: nfd-constants.hpp:58
ndn::nfd::ROUTE_ORIGIN_NLSR
@ ROUTE_ORIGIN_NLSR
Definition: nfd-constants.hpp:105
string-helper.hpp
ndn::nfd::FACE_SCOPE_NON_LOCAL
@ FACE_SCOPE_NON_LOCAL
face is non-local
Definition: nfd-constants.hpp:36
ndn::nfd::ROUTE_ORIGIN_APP
@ ROUTE_ORIGIN_APP
Definition: nfd-constants.hpp:101
ndn::nfd::LINK_TYPE_POINT_TO_POINT
@ LINK_TYPE_POINT_TO_POINT
link is point-to-point
Definition: nfd-constants.hpp:59
ndn::nfd::FACE_PERSISTENCY_PERMANENT
@ FACE_PERSISTENCY_PERMANENT
face is permanent
Definition: nfd-constants.hpp:49
ndn::nfd::operator<<
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
Definition: nfd-constants.cpp:36
ndn::AsHex
Helper class to convert a number to hexadecimal format, for use with stream insertion operators.
Definition: string-helper.hpp:49
ndn::nfd::RouteFlags
RouteFlags
Definition: nfd-constants.hpp:123
ndn::nfd::FACE_EVENT_DESTROYED
@ FACE_EVENT_DESTROYED
face was destroyed
Definition: nfd-constants.hpp:80
ndn::nfd::LINK_TYPE_AD_HOC
@ LINK_TYPE_AD_HOC
link is ad hoc
Definition: nfd-constants.hpp:61
ndn::nfd::ROUTE_ORIGIN_STATIC
@ ROUTE_ORIGIN_STATIC
Definition: nfd-constants.hpp:107
ndn::nfd::LinkType
LinkType
Definition: nfd-constants.hpp:57
ndn::nfd::ROUTE_FLAGS_NONE
@ ROUTE_FLAGS_NONE
Definition: nfd-constants.hpp:124
ndn::make_ostream_joiner
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
Definition: ostream-joiner.hpp:117
ndn::nfd::RouteOrigin
RouteOrigin
Definition: nfd-constants.hpp:99
ndn::nfd::ROUTE_ORIGIN_AUTOREG
@ ROUTE_ORIGIN_AUTOREG
Definition: nfd-constants.hpp:102
ndn::nfd::FacePersistency
FacePersistency
Definition: nfd-constants.hpp:45
ndn::nfd::LINK_TYPE_MULTI_ACCESS
@ LINK_TYPE_MULTI_ACCESS
link is multi-access
Definition: nfd-constants.hpp:60
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
ndn::nfd::operator>>
std::istream & operator>>(std::istream &is, RouteOrigin &routeOrigin)
extract RouteOrigin from stream
Definition: nfd-constants.cpp:100
ostream-joiner.hpp
Backport of ostream_joiner from the Library Fundamentals v2 TS.
ndn::nfd::ROUTE_FLAG_CAPTURE
@ ROUTE_FLAG_CAPTURE
Definition: nfd-constants.hpp:126
ndn::nfd::FACE_SCOPE_NONE
@ FACE_SCOPE_NONE
Definition: nfd-constants.hpp:35
ndn::nfd::FACE_PERSISTENCY_NONE
@ FACE_PERSISTENCY_NONE
Definition: nfd-constants.hpp:46
ndn::nfd::FACE_EVENT_UP
@ FACE_EVENT_UP
face went UP (from DOWN state)
Definition: nfd-constants.hpp:81
ndn::nfd::FACE_EVENT_NONE
@ FACE_EVENT_NONE
Definition: nfd-constants.hpp:78
ndn::nfd::ROUTE_ORIGIN_CLIENT
@ ROUTE_ORIGIN_CLIENT
Definition: nfd-constants.hpp:103
ndn::nfd::FaceEventKind
FaceEventKind
Definition: nfd-constants.hpp:77
ndn::nfd::FACE_PERSISTENCY_ON_DEMAND
@ FACE_PERSISTENCY_ON_DEMAND
face is on-demand
Definition: nfd-constants.hpp:48
ndn::nfd::FACE_EVENT_CREATED
@ FACE_EVENT_CREATED
face was created
Definition: nfd-constants.hpp:79
ndn::nfd::FACE_SCOPE_LOCAL
@ FACE_SCOPE_LOCAL
face is local
Definition: nfd-constants.hpp:37
ndn::nfd::ROUTE_ORIGIN_PREFIXANN
@ ROUTE_ORIGIN_PREFIXANN
Definition: nfd-constants.hpp:106
ndn::nfd::ROUTE_ORIGIN_AUTOCONF
@ ROUTE_ORIGIN_AUTOCONF
Definition: nfd-constants.hpp:104
ndn::nfd::ROUTE_ORIGIN_NONE
@ ROUTE_ORIGIN_NONE
Definition: nfd-constants.hpp:100
nfd-constants.hpp
ndn::nfd::FaceScope
FaceScope
Definition: nfd-constants.hpp:34
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::nfd::ROUTE_FLAG_CHILD_INHERIT
@ ROUTE_FLAG_CHILD_INHERIT
Definition: nfd-constants.hpp:125
ndn::nfd::FACE_PERSISTENCY_PERSISTENT
@ FACE_PERSISTENCY_PERSISTENT
face is persistent
Definition: nfd-constants.hpp:47