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 | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame^] | 25 | #include <ndn-cxx/face.hpp> |
| 26 | #include <ndn-cxx/security/key-chain.hpp> |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 27 | |
| 28 | |
| 29 | void |
| 30 | usage(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 | |
| 39 | using namespace ndn; |
| 40 | |
| 41 | class NdnAutoconfigServer |
| 42 | { |
| 43 | public: |
| 44 | explicit |
| 45 | NdnAutoconfigServer(const std::string& uri) |
| 46 | : m_faceMgmtUri(uri) |
| 47 | { |
| 48 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 49 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 50 | void |
| 51 | onInterest(const Name& name, const Interest& interest) |
| 52 | { |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 53 | ndn::Data data(ndn::Name(interest.getName()).appendVersion()); |
Alexander Afanasyev | 85b6b01 | 2014-04-21 18:12:57 -0700 | [diff] [blame] | 54 | data.setFreshnessPeriod(ndn::time::hours(1)); // 1 hour |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 55 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 56 | // 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 72 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 73 | 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 | |
| 82 | private: |
| 83 | ndn::Face m_face; |
| 84 | KeyChain m_keyChain; |
| 85 | std::string m_faceMgmtUri; |
| 86 | |
| 87 | }; |
| 88 | |
| 89 | int |
| 90 | main(int argc, char** argv) |
| 91 | { |
| 92 | int opt; |
| 93 | const char* programName = argv[0]; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 94 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 95 | while ((opt = getopt(argc, argv, "h")) != -1) |
| 96 | { |
| 97 | switch (opt) |
| 98 | { |
| 99 | case 'h': |
| 100 | usage(programName); |
| 101 | return 0; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 102 | |
hilata | dd50ada | 2014-03-13 12:48:47 -0500 | [diff] [blame] | 103 | 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 | } |