blob: 9873bde4f84c81b9454ee9ae854c939f51ba1c2c [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),
80 ndn::bind(&NdnAutoconfigServer::onRegisterFailed, this, _1, _2));
81 m_face.processEvents();
82 }
83
84private:
85 ndn::Face m_face;
86 KeyChain m_keyChain;
87 std::string m_faceMgmtUri;
88
89};
90
91int
92main(int argc, char** argv)
93{
94 int opt;
95 const char* programName = argv[0];
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070096
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070097 while ((opt = getopt(argc, argv, "hV")) != -1) {
98 switch (opt) {
99 case 'h':
100 usage(programName);
101 return 0;
102 case 'V':
103 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
104 return 0;
105 default:
106 usage(programName);
107 return 1;
hilatadd50ada2014-03-13 12:48:47 -0500108 }
109 }
110
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700111 if (argc != optind + 1) {
hilatadd50ada2014-03-13 12:48:47 -0500112 usage(programName);
113 return 1;
114 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700115 // get the configured face management uri
hilatadd50ada2014-03-13 12:48:47 -0500116 NdnAutoconfigServer producer(argv[optind]);
117
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700118 try {
hilatadd50ada2014-03-13 12:48:47 -0500119 producer.listen();
120 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700121 catch (const std::exception& error) {
hilatadd50ada2014-03-13 12:48:47 -0500122 std::cerr << "ERROR: " << error.what() << std::endl;
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700123 return 1;
hilatadd50ada2014-03-13 12:48:47 -0500124 }
125 return 0;
126}