blob: bc038e3d5345309bdbc8e1d280bef3f0d41d4a07 [file] [log] [blame]
hilatadd50ada2014-03-13 12:48:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology
9 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
hilatadd50ada2014-03-13 12:48:47 -050024
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070025#include "version.hpp"
Alexander Afanasyev4a771362014-04-24 21:29:33 -070026#include <ndn-cxx/face.hpp>
27#include <ndn-cxx/security/key-chain.hpp>
hilatadd50ada2014-03-13 12:48:47 -050028
29
30void
31usage(const char* programName)
32{
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070033 std::cout << "Usage:\n" << programName << " [-h] [-V] Uri \n"
34 << " -h - print usage and exit\n"
35 << " -V - print version number and exit\n"
36 << "\n"
37 << " Uri - a FaceMgmt URI\n"
38 << std::endl;
hilatadd50ada2014-03-13 12:48:47 -050039}
40
41using namespace ndn;
42
43class NdnAutoconfigServer
44{
45public:
46 explicit
47 NdnAutoconfigServer(const std::string& uri)
48 : m_faceMgmtUri(uri)
49 {
50 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070051
hilatadd50ada2014-03-13 12:48:47 -050052 void
53 onInterest(const Name& name, const Interest& interest)
54 {
hilatadd50ada2014-03-13 12:48:47 -050055 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
Alexander Afanasyev85b6b012014-04-21 18:12:57 -070056 data.setFreshnessPeriod(ndn::time::hours(1)); // 1 hour
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070057
hilatadd50ada2014-03-13 12:48:47 -050058 // create and encode uri block
59 Block uriBlock = dataBlock(tlv::nfd::Uri,
60 reinterpret_cast<const uint8_t*>(m_faceMgmtUri.c_str()),
61 m_faceMgmtUri.size());
62 data.setContent(uriBlock);
63 m_keyChain.sign(data);
64 m_face.put(data);
65 }
66
67 void
68 onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
69 {
70 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" <<
71 reason << ")" << std::endl;
72 m_face.shutdown();
73 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070074
hilatadd50ada2014-03-13 12:48:47 -050075 void
76 listen()
77 {
78 m_face.setInterestFilter("/localhop/ndn-autoconf/hub",
79 ndn::bind(&NdnAutoconfigServer::onInterest, this, _1, _2),
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070080 ndn::RegisterPrefixSuccessCallback(),
hilatadd50ada2014-03-13 12:48:47 -050081 ndn::bind(&NdnAutoconfigServer::onRegisterFailed, this, _1, _2));
82 m_face.processEvents();
83 }
84
85private:
86 ndn::Face m_face;
87 KeyChain m_keyChain;
88 std::string m_faceMgmtUri;
89
90};
91
92int
93main(int argc, char** argv)
94{
95 int opt;
96 const char* programName = argv[0];
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070097
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070098 while ((opt = getopt(argc, argv, "hV")) != -1) {
99 switch (opt) {
100 case 'h':
101 usage(programName);
102 return 0;
103 case 'V':
104 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
105 return 0;
106 default:
107 usage(programName);
108 return 1;
hilatadd50ada2014-03-13 12:48:47 -0500109 }
110 }
111
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700112 if (argc != optind + 1) {
hilatadd50ada2014-03-13 12:48:47 -0500113 usage(programName);
114 return 1;
115 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700116 // get the configured face management uri
hilatadd50ada2014-03-13 12:48:47 -0500117 NdnAutoconfigServer producer(argv[optind]);
118
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700119 try {
hilatadd50ada2014-03-13 12:48:47 -0500120 producer.listen();
121 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700122 catch (const std::exception& error) {
hilatadd50ada2014-03-13 12:48:47 -0500123 std::cerr << "ERROR: " << error.what() << std::endl;
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700124 return 1;
hilatadd50ada2014-03-13 12:48:47 -0500125 }
126 return 0;
127}