blob: abcdf5cac2be16df44caa18a447fdf84720210f4 [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 Afanasyev4a771362014-04-24 21:29:33 -070025#include <ndn-cxx/face.hpp>
26#include <ndn-cxx/security/key-chain.hpp>
hilatadd50ada2014-03-13 12:48:47 -050027
28
29void
30usage(const char* programName)
31{
32 std::cout << "Usage:\n" << programName << " [-h] Uri \n"
33 " -h print usage and exit\n"
34 "\n"
35 " Uri - a FaceMgmt URI\n"
36 << std::endl;
37}
38
39using namespace ndn;
40
41class NdnAutoconfigServer
42{
43public:
44 explicit
45 NdnAutoconfigServer(const std::string& uri)
46 : m_faceMgmtUri(uri)
47 {
48 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070049
hilatadd50ada2014-03-13 12:48:47 -050050 void
51 onInterest(const Name& name, const Interest& interest)
52 {
hilatadd50ada2014-03-13 12:48:47 -050053 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
Alexander Afanasyev85b6b012014-04-21 18:12:57 -070054 data.setFreshnessPeriod(ndn::time::hours(1)); // 1 hour
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070055
hilatadd50ada2014-03-13 12:48:47 -050056 // create and encode uri block
57 Block uriBlock = dataBlock(tlv::nfd::Uri,
58 reinterpret_cast<const uint8_t*>(m_faceMgmtUri.c_str()),
59 m_faceMgmtUri.size());
60 data.setContent(uriBlock);
61 m_keyChain.sign(data);
62 m_face.put(data);
63 }
64
65 void
66 onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
67 {
68 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" <<
69 reason << ")" << std::endl;
70 m_face.shutdown();
71 }
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070072
hilatadd50ada2014-03-13 12:48:47 -050073 void
74 listen()
75 {
76 m_face.setInterestFilter("/localhop/ndn-autoconf/hub",
77 ndn::bind(&NdnAutoconfigServer::onInterest, this, _1, _2),
78 ndn::bind(&NdnAutoconfigServer::onRegisterFailed, this, _1, _2));
79 m_face.processEvents();
80 }
81
82private:
83 ndn::Face m_face;
84 KeyChain m_keyChain;
85 std::string m_faceMgmtUri;
86
87};
88
89int
90main(int argc, char** argv)
91{
92 int opt;
93 const char* programName = argv[0];
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070094
hilatadd50ada2014-03-13 12:48:47 -050095 while ((opt = getopt(argc, argv, "h")) != -1)
96 {
97 switch (opt)
98 {
99 case 'h':
100 usage(programName);
101 return 0;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700102
hilatadd50ada2014-03-13 12:48:47 -0500103 default:
104 usage(programName);
105 return 1;
106 }
107 }
108
109 if (argc != optind + 1)
110 {
111 usage(programName);
112 return 1;
113 }
114 // get the configured face managment uri
115 NdnAutoconfigServer producer(argv[optind]);
116
117 try
118 {
119 producer.listen();
120 }
121 catch (std::exception& error)
122 {
123 std::cerr << "ERROR: " << error.what() << std::endl;
124 }
125 return 0;
126}