blob: 0de5fe2fa63c636bb9d5150dc8dc2c872bbc0d25 [file] [log] [blame]
Chengyu Fan36dca992014-09-25 13:42:03 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento56bd3ee2017-02-26 16:05:16 -05003 * Copyright (c) 2013-2017 Regents of the University of California.
Chengyu Fan36dca992014-09-25 13:42:03 -06004 *
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 Shi65f1a712014-11-20 14:59:36 -070022#include "nfd-constants.hpp"
Davide Pesaventoe78eeca2017-02-23 23:22:32 -050023#include "util/string-helper.hpp"
Davide Pesavento56bd3ee2017-02-26 16:05:16 -050024
Junxiao Shi6336c5f2017-04-04 22:16:03 +000025#include <boost/algorithm/string.hpp>
26#include <boost/lexical_cast.hpp>
27#include <istream>
Junxiao Shi18ef4de2015-10-17 16:58:23 +000028#include <map>
Davide Pesaventoe78eeca2017-02-23 23:22:32 -050029#include <ostream>
Chengyu Fan36dca992014-09-25 13:42:03 -060030
31namespace ndn {
32namespace nfd {
33
34std::ostream&
35operator<<(std::ostream& os, FaceScope faceScope)
36{
37 switch (faceScope) {
Junxiao Shi18ef4de2015-10-17 16:58:23 +000038 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 Fan36dca992014-09-25 13:42:03 -060044 }
Davide Pesaventoe55589c2017-02-20 03:02:51 -050045 return os << static_cast<unsigned>(faceScope);
Chengyu Fan36dca992014-09-25 13:42:03 -060046}
47
48std::ostream&
49operator<<(std::ostream& os, FacePersistency facePersistency)
50{
51 switch (facePersistency) {
Junxiao Shi18ef4de2015-10-17 16:58:23 +000052 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 Fan36dca992014-09-25 13:42:03 -060060 }
Davide Pesaventoe55589c2017-02-20 03:02:51 -050061 return os << static_cast<unsigned>(facePersistency);
Chengyu Fan36dca992014-09-25 13:42:03 -060062}
63
64std::ostream&
65operator<<(std::ostream& os, LinkType linkType)
66{
67 switch (linkType) {
Junxiao Shi18ef4de2015-10-17 16:58:23 +000068 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 Liangb4ecadc2017-03-25 10:55:40 -050074 case LINK_TYPE_AD_HOC:
75 return os << "adhoc";
Chengyu Fan36dca992014-09-25 13:42:03 -060076 }
Davide Pesaventoe55589c2017-02-20 03:02:51 -050077 return os << static_cast<unsigned>(linkType);
78}
79
80std::ostream&
81operator<<(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 Shi18ef4de2015-10-17 16:58:23 +000096}
97
Junxiao Shi6336c5f2017-04-04 22:16:03 +000098std::istream&
99operator>>(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;
118 else if (iequals(s, "static"))
119 routeOrigin = ROUTE_ORIGIN_STATIC;
120 else {
121 // To reject negative numbers, we parse as a wider signed type, and compare with the range.
122 static_assert(std::numeric_limits<std::underlying_type<RouteOrigin>::type>::max() <=
123 std::numeric_limits<int>::max(), "");
124
125 int v = -1;
126 try {
127 v = boost::lexical_cast<int>(s);
128 }
129 catch (const boost::bad_lexical_cast&) {
130 }
131
132 if (v >= std::numeric_limits<std::underlying_type<RouteOrigin>::type>::min() &&
133 v <= std::numeric_limits<std::underlying_type<RouteOrigin>::type>::max()) {
134 routeOrigin = static_cast<RouteOrigin>(v);
135 }
136 else {
137 routeOrigin = ROUTE_ORIGIN_NONE;
138 is.setstate(std::ios::failbit);
139 }
140 }
141
142 return is;
143}
144
Junxiao Shi18ef4de2015-10-17 16:58:23 +0000145std::ostream&
146operator<<(std::ostream& os, RouteOrigin routeOrigin)
147{
148 switch (routeOrigin) {
149 case ROUTE_ORIGIN_NONE:
150 return os << "none";
151 case ROUTE_ORIGIN_APP:
152 return os << "app";
153 case ROUTE_ORIGIN_AUTOREG:
154 return os << "autoreg";
155 case ROUTE_ORIGIN_CLIENT:
156 return os << "client";
157 case ROUTE_ORIGIN_AUTOCONF:
158 return os << "autoconf";
159 case ROUTE_ORIGIN_NLSR:
160 return os << "nlsr";
161 case ROUTE_ORIGIN_STATIC:
162 return os << "static";
Junxiao Shi18ef4de2015-10-17 16:58:23 +0000163 }
Davide Pesaventoe55589c2017-02-20 03:02:51 -0500164 return os << static_cast<unsigned>(routeOrigin);
Junxiao Shi18ef4de2015-10-17 16:58:23 +0000165}
166
167std::ostream&
168operator<<(std::ostream& os, RouteFlags routeFlags)
169{
170 if (routeFlags == ROUTE_FLAGS_NONE) {
171 return os << "none";
172 }
173
Junxiao Shi18ef4de2015-10-17 16:58:23 +0000174 static const std::map<RouteFlags, std::string> knownBits = {
175 {ROUTE_FLAG_CHILD_INHERIT, "child-inherit"},
Davide Pesaventocf415762017-02-25 23:46:47 -0500176 {ROUTE_FLAG_CAPTURE, "capture"}
177 };
178
179 auto join = make_ostream_joiner(os, '|');
Junxiao Shi18ef4de2015-10-17 16:58:23 +0000180 for (const auto& pair : knownBits) {
181 RouteFlags bit = ROUTE_FLAGS_NONE;
182 std::string token;
183 std::tie(bit, token) = pair;
184
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500185 if ((routeFlags & bit) != 0) {
Davide Pesaventocf415762017-02-25 23:46:47 -0500186 join = token;
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500187 routeFlags = static_cast<RouteFlags>(routeFlags & ~bit);
Junxiao Shi18ef4de2015-10-17 16:58:23 +0000188 }
Junxiao Shi18ef4de2015-10-17 16:58:23 +0000189 }
190
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500191 if (routeFlags != ROUTE_FLAGS_NONE) {
Davide Pesaventocf415762017-02-25 23:46:47 -0500192 join = AsHex{routeFlags};
Junxiao Shi18ef4de2015-10-17 16:58:23 +0000193 }
194
Chengyu Fan36dca992014-09-25 13:42:03 -0600195 return os;
196}
197
198} // namespace nfd
199} // namespace ndn