26 #include "core/version.hpp"
27 #include <ndn-cxx/encoding/tlv-nfd.hpp>
28 #include <ndn-cxx/face.hpp>
29 #include <ndn-cxx/security/key-chain.hpp>
39 std::cout <<
"Usage:\n" << programName <<
" [-h] [-V] [-p prefix] [-p prefix] ... Uri \n"
40 <<
" -h - print usage and exit\n"
41 <<
" -V - print version number and exit\n"
42 <<
" -p prefix - the local prefix of the hub\n"
44 <<
" Uri - a FaceMgmt URI\n"
48 class PrefixCollection : noncopyable
54 return m_prefixes.empty();
58 add(
const Name& prefix)
60 m_prefixes.push_back(prefix);
65 wireEncode(EncodingImpl<T>& encoder)
const
67 size_t totalLength = 0;
69 for (std::vector<Name>::const_reverse_iterator i = m_prefixes.rbegin();
70 i != m_prefixes.rend(); ++i) {
71 totalLength += i->wireEncode(encoder);
74 totalLength += encoder.prependVarNumber(totalLength);
75 totalLength += encoder.prependVarNumber(tlv::Content);
84 EncodingEstimator estimator;
85 size_t estimatedSize = wireEncode(estimator);
87 EncodingBuffer buffer(estimatedSize);
90 return buffer.block();
94 std::vector<Name> m_prefixes;
97 class NdnAutoconfigServer : noncopyable
100 NdnAutoconfigServer(
const std::string& hubFaceUri,
const PrefixCollection& routablePrefixes)
105 m_hubData = make_shared<Data>(Name(LOCALHOP_HUB).appendVersion());
106 m_hubData->setFreshnessPeriod(time::hours(1));
107 m_hubData->setContent(makeBinaryBlock(tlv::nfd::Uri,
108 reinterpret_cast<const uint8_t*>(hubFaceUri.c_str()),
110 m_keyChain.sign(*m_hubData);
113 if (!routablePrefixes.empty()) {
114 Name routablePrefixesDataName(LOCALHOP_ROUTABLE_PREFIXES);
115 routablePrefixesDataName.appendVersion();
116 routablePrefixesDataName.appendSegment(0);
117 m_routablePrefixesData = make_shared<Data>(routablePrefixesDataName);
118 m_routablePrefixesData->setContent(routablePrefixes.wireEncode());
119 m_routablePrefixesData->setFinalBlockId(routablePrefixesDataName.get(-1));
120 m_routablePrefixesData->setFreshnessPeriod(time::seconds(5));
121 m_keyChain.sign(*m_routablePrefixesData);
126 onHubInterest(
const Name& name,
const Interest& interest)
128 m_face.put(*m_hubData);
132 onRoutablePrefixesInterest(
const Name& name,
const Interest& interest)
134 m_face.put(*m_routablePrefixesData);
138 onRegisterFailed(
const Name& prefix,
const std::string& reason)
140 std::cerr <<
"ERROR: Failed to register prefix in local hub's daemon (" <<
141 reason <<
")" << std::endl;
148 m_face.setInterestFilter(LOCALHOP_HUB,
149 bind(&NdnAutoconfigServer::onHubInterest,
this, _1, _2),
150 RegisterPrefixSuccessCallback(),
151 bind(&NdnAutoconfigServer::onRegisterFailed,
this, _1, _2));
153 if (static_cast<bool>(m_routablePrefixesData)) {
154 m_face.setInterestFilter(LOCALHOP_ROUTABLE_PREFIXES,
155 bind(&NdnAutoconfigServer::onRoutablePrefixesInterest,
this, _1, _2),
156 RegisterPrefixSuccessCallback(),
157 bind(&NdnAutoconfigServer::onRegisterFailed,
this, _1, _2));
160 m_face.processEvents();
166 shared_ptr<Data> m_hubData;
167 shared_ptr<Data> m_routablePrefixesData;
173 const char* programName = argv[0];
175 PrefixCollection routablePrefixes;
178 while ((opt = getopt(argc, argv,
"hVp:")) != -1) {
184 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
187 routablePrefixes.add(Name(optarg));
195 if (argc != optind + 1) {
200 std::string hubFaceUri = argv[optind];
201 NdnAutoconfigServer instance(hubFaceUri, routablePrefixes);
206 catch (
const std::exception& error) {
207 std::cerr <<
"ERROR: " << error.what() << std::endl;
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
static void usage(const char *programName)
int main(int argc, char **argv)
static const Name LOCALHOP_ROUTABLE_PREFIXES
int main(int argc, char **argv)
static const Name LOCALHOP_HUB