Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Teng Liang | c93c2ce | 2018-04-29 14:19:27 -0700 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 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 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 22 | #include "nfd-constants.hpp" |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 23 | #include "util/string-helper.hpp" |
Davide Pesavento | 56bd3ee | 2017-02-26 16:05:16 -0500 | [diff] [blame] | 24 | |
Junxiao Shi | 6336c5f | 2017-04-04 22:16:03 +0000 | [diff] [blame] | 25 | #include <boost/algorithm/string.hpp> |
| 26 | #include <boost/lexical_cast.hpp> |
| 27 | #include <istream> |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 28 | #include <map> |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 29 | #include <ostream> |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | namespace nfd { |
| 33 | |
| 34 | std::ostream& |
| 35 | operator<<(std::ostream& os, FaceScope faceScope) |
| 36 | { |
| 37 | switch (faceScope) { |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 38 | case FACE_SCOPE_NONE: |
| 39 | return os << "none"; |
| 40 | case FACE_SCOPE_NON_LOCAL: |
| 41 | return os << "non-local"; |
| 42 | case FACE_SCOPE_LOCAL: |
| 43 | return os << "local"; |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 44 | } |
Davide Pesavento | e55589c | 2017-02-20 03:02:51 -0500 | [diff] [blame] | 45 | return os << static_cast<unsigned>(faceScope); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | std::ostream& |
| 49 | operator<<(std::ostream& os, FacePersistency facePersistency) |
| 50 | { |
| 51 | switch (facePersistency) { |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 52 | case FACE_PERSISTENCY_NONE: |
| 53 | return os << "none"; |
| 54 | case FACE_PERSISTENCY_PERSISTENT: |
| 55 | return os << "persistent"; |
| 56 | case FACE_PERSISTENCY_ON_DEMAND: |
| 57 | return os << "on-demand"; |
| 58 | case FACE_PERSISTENCY_PERMANENT: |
| 59 | return os << "permanent"; |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 60 | } |
Davide Pesavento | e55589c | 2017-02-20 03:02:51 -0500 | [diff] [blame] | 61 | return os << static_cast<unsigned>(facePersistency); |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | std::ostream& |
| 65 | operator<<(std::ostream& os, LinkType linkType) |
| 66 | { |
| 67 | switch (linkType) { |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 68 | case LINK_TYPE_NONE: |
| 69 | return os << "none"; |
| 70 | case LINK_TYPE_POINT_TO_POINT: |
| 71 | return os << "point-to-point"; |
| 72 | case LINK_TYPE_MULTI_ACCESS: |
| 73 | return os << "multi-access"; |
Teng Liang | b4ecadc | 2017-03-25 10:55:40 -0500 | [diff] [blame] | 74 | case LINK_TYPE_AD_HOC: |
| 75 | return os << "adhoc"; |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 76 | } |
Davide Pesavento | e55589c | 2017-02-20 03:02:51 -0500 | [diff] [blame] | 77 | return os << static_cast<unsigned>(linkType); |
| 78 | } |
| 79 | |
| 80 | std::ostream& |
| 81 | operator<<(std::ostream& os, FaceEventKind faceEventKind) |
| 82 | { |
| 83 | switch (faceEventKind) { |
| 84 | case FACE_EVENT_NONE: |
| 85 | return os << "none"; |
| 86 | case FACE_EVENT_CREATED: |
| 87 | return os << "created"; |
| 88 | case FACE_EVENT_DESTROYED: |
| 89 | return os << "destroyed"; |
| 90 | case FACE_EVENT_UP: |
| 91 | return os << "up"; |
| 92 | case FACE_EVENT_DOWN: |
| 93 | return os << "down"; |
| 94 | } |
| 95 | return os << static_cast<unsigned>(faceEventKind); |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Junxiao Shi | 6336c5f | 2017-04-04 22:16:03 +0000 | [diff] [blame] | 98 | std::istream& |
| 99 | operator>>(std::istream& is, RouteOrigin& routeOrigin) |
| 100 | { |
| 101 | using boost::algorithm::iequals; |
| 102 | |
| 103 | std::string s; |
| 104 | is >> s; |
| 105 | |
| 106 | if (iequals(s, "none")) |
| 107 | routeOrigin = ROUTE_ORIGIN_NONE; |
| 108 | else if (iequals(s, "app")) |
| 109 | routeOrigin = ROUTE_ORIGIN_APP; |
| 110 | else if (iequals(s, "autoreg")) |
| 111 | routeOrigin = ROUTE_ORIGIN_AUTOREG; |
| 112 | else if (iequals(s, "client")) |
| 113 | routeOrigin = ROUTE_ORIGIN_CLIENT; |
| 114 | else if (iequals(s, "autoconf")) |
| 115 | routeOrigin = ROUTE_ORIGIN_AUTOCONF; |
| 116 | else if (iequals(s, "nlsr")) |
| 117 | routeOrigin = ROUTE_ORIGIN_NLSR; |
Teng Liang | c93c2ce | 2018-04-29 14:19:27 -0700 | [diff] [blame^] | 118 | else if (iequals(s, "selflearning")) |
| 119 | routeOrigin = ROUTE_ORIGIN_SELFLEARNING; |
Junxiao Shi | 6336c5f | 2017-04-04 22:16:03 +0000 | [diff] [blame] | 120 | else if (iequals(s, "static")) |
| 121 | routeOrigin = ROUTE_ORIGIN_STATIC; |
| 122 | else { |
| 123 | // To reject negative numbers, we parse as a wider signed type, and compare with the range. |
| 124 | static_assert(std::numeric_limits<std::underlying_type<RouteOrigin>::type>::max() <= |
| 125 | std::numeric_limits<int>::max(), ""); |
| 126 | |
| 127 | int v = -1; |
| 128 | try { |
| 129 | v = boost::lexical_cast<int>(s); |
| 130 | } |
| 131 | catch (const boost::bad_lexical_cast&) { |
| 132 | } |
| 133 | |
| 134 | if (v >= std::numeric_limits<std::underlying_type<RouteOrigin>::type>::min() && |
| 135 | v <= std::numeric_limits<std::underlying_type<RouteOrigin>::type>::max()) { |
| 136 | routeOrigin = static_cast<RouteOrigin>(v); |
| 137 | } |
| 138 | else { |
| 139 | routeOrigin = ROUTE_ORIGIN_NONE; |
| 140 | is.setstate(std::ios::failbit); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return is; |
| 145 | } |
| 146 | |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 147 | std::ostream& |
| 148 | operator<<(std::ostream& os, RouteOrigin routeOrigin) |
| 149 | { |
| 150 | switch (routeOrigin) { |
| 151 | case ROUTE_ORIGIN_NONE: |
| 152 | return os << "none"; |
| 153 | case ROUTE_ORIGIN_APP: |
| 154 | return os << "app"; |
| 155 | case ROUTE_ORIGIN_AUTOREG: |
| 156 | return os << "autoreg"; |
| 157 | case ROUTE_ORIGIN_CLIENT: |
| 158 | return os << "client"; |
| 159 | case ROUTE_ORIGIN_AUTOCONF: |
| 160 | return os << "autoconf"; |
| 161 | case ROUTE_ORIGIN_NLSR: |
| 162 | return os << "nlsr"; |
Teng Liang | c93c2ce | 2018-04-29 14:19:27 -0700 | [diff] [blame^] | 163 | case ROUTE_ORIGIN_SELFLEARNING: |
| 164 | return os << "selflearning"; |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 165 | case ROUTE_ORIGIN_STATIC: |
| 166 | return os << "static"; |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 167 | } |
Davide Pesavento | e55589c | 2017-02-20 03:02:51 -0500 | [diff] [blame] | 168 | return os << static_cast<unsigned>(routeOrigin); |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | std::ostream& |
| 172 | operator<<(std::ostream& os, RouteFlags routeFlags) |
| 173 | { |
| 174 | if (routeFlags == ROUTE_FLAGS_NONE) { |
| 175 | return os << "none"; |
| 176 | } |
| 177 | |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 178 | static const std::map<RouteFlags, std::string> knownBits = { |
| 179 | {ROUTE_FLAG_CHILD_INHERIT, "child-inherit"}, |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 180 | {ROUTE_FLAG_CAPTURE, "capture"} |
| 181 | }; |
| 182 | |
| 183 | auto join = make_ostream_joiner(os, '|'); |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 184 | for (const auto& pair : knownBits) { |
| 185 | RouteFlags bit = ROUTE_FLAGS_NONE; |
| 186 | std::string token; |
| 187 | std::tie(bit, token) = pair; |
| 188 | |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 189 | if ((routeFlags & bit) != 0) { |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 190 | join = token; |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 191 | routeFlags = static_cast<RouteFlags>(routeFlags & ~bit); |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 192 | } |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 195 | if (routeFlags != ROUTE_FLAGS_NONE) { |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 196 | join = AsHex{routeFlags}; |
Junxiao Shi | 18ef4de | 2015-10-17 16:58:23 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Chengyu Fan | 36dca99 | 2014-09-25 13:42:03 -0600 | [diff] [blame] | 199 | return os; |
| 200 | } |
| 201 | |
| 202 | } // namespace nfd |
| 203 | } // namespace ndn |