hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * 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 | **/ |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 24 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 25 | #include "version.hpp" |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 26 | #include <ndn-cxx/face.hpp> |
| 27 | #include <ndn-cxx/security/key-chain.hpp> |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 28 | |
| 29 | |
| 30 | void |
| 31 | usage(const char* programName) |
| 32 | { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 33 | 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; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | using namespace ndn; |
| 42 | |
| 43 | class NdnAutoconfigServer |
| 44 | { |
| 45 | public: |
| 46 | explicit |
| 47 | NdnAutoconfigServer(const std::string& uri) |
| 48 | : m_faceMgmtUri(uri) |
| 49 | { |
| 50 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 51 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 52 | void |
| 53 | onInterest(const Name& name, const Interest& interest) |
| 54 | { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 55 | ndn::Data data(ndn::Name(interest.getName()).appendVersion()); |
Alexander Afanasyev | 85b6b01 | 2014-04-21 18:12:57 -0700 | [diff] [blame] | 56 | data.setFreshnessPeriod(ndn::time::hours(1)); // 1 hour |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 57 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 58 | // 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 74 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 75 | void |
| 76 | listen() |
| 77 | { |
| 78 | m_face.setInterestFilter("/localhop/ndn-autoconf/hub", |
| 79 | ndn::bind(&NdnAutoconfigServer::onInterest, this, _1, _2), |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 80 | ndn::RegisterPrefixSuccessCallback(), |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 81 | ndn::bind(&NdnAutoconfigServer::onRegisterFailed, this, _1, _2)); |
| 82 | m_face.processEvents(); |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | ndn::Face m_face; |
| 87 | KeyChain m_keyChain; |
| 88 | std::string m_faceMgmtUri; |
| 89 | |
| 90 | }; |
| 91 | |
| 92 | int |
| 93 | main(int argc, char** argv) |
| 94 | { |
| 95 | int opt; |
| 96 | const char* programName = argv[0]; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 97 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 98 | 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; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 112 | if (argc != optind + 1) { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 113 | usage(programName); |
| 114 | return 1; |
| 115 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 116 | // get the configured face management uri |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 117 | NdnAutoconfigServer producer(argv[optind]); |
| 118 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 119 | try { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 120 | producer.listen(); |
| 121 | } |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 122 | catch (const std::exception& error) { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 123 | std::cerr << "ERROR: " << error.what() << std::endl; |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 124 | return 1; |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 125 | } |
| 126 | return 0; |
| 127 | } |