Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California. |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /** |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 21 | * @file This file demonstrates how to generate data to be stored in a repo using |
| 22 | * the repo watch protocol and repo insertion protocol. |
| 23 | * |
| 24 | * The details of the protocols can be found here: |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 25 | * <https://redmine.named-data.net/projects/repo-ng/wiki/Watched_Prefix_Insertion_Protocol> |
| 26 | * <https://redmine.named-data.net/projects/repo-ng/wiki/Basic_Repo_Insertion_Protocol> |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 27 | * |
| 28 | * This file is used for debugging purpose. There are two modes for users to assign |
| 29 | * names for the data. |
| 30 | * 1)read the data name from a specific file |
| 31 | * 2)input a prefix and a random version number will be added automatically |
| 32 | * Users need to run nfd and repo-ng and set up specific repo protocols mentioned |
| 33 | * above before running this program. |
| 34 | * The description of command parameter can be found in the function usage(). |
| 35 | */ |
| 36 | |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 37 | #include <boost/asio/io_context.hpp> |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 38 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 39 | |
| 40 | #include <ndn-cxx/data.hpp> |
| 41 | #include <ndn-cxx/face.hpp> |
| 42 | #include <ndn-cxx/name.hpp> |
| 43 | #include <ndn-cxx/util/random.hpp> |
| 44 | #include <ndn-cxx/util/scheduler.hpp> |
| 45 | #include <ndn-cxx/util/time.hpp> |
| 46 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 47 | #include <fstream> |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 48 | #include <iostream> |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 49 | #include <random> |
| 50 | #include <string> |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 51 | |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 52 | namespace repo::examples { |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 53 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 54 | enum Mode { |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 55 | AUTO, |
| 56 | READFILE |
| 57 | }; |
| 58 | |
| 59 | class Publisher |
| 60 | { |
| 61 | public: |
| 62 | class Error : public std::runtime_error |
| 63 | { |
| 64 | public: |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 65 | using std::runtime_error::runtime_error; |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | public: |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 69 | void |
| 70 | run(); |
| 71 | |
| 72 | void |
| 73 | autoGenerate(); |
| 74 | |
| 75 | void |
| 76 | generateFromFile(); |
| 77 | |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 78 | static std::shared_ptr<ndn::Data> |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 79 | createData(const ndn::Name& name); |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 80 | |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 81 | public: |
| 82 | std::ifstream insertStream; |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 83 | Mode mode{AUTO}; |
| 84 | ndn::Name dataPrefix{"/example/data"}; |
| 85 | ndn::time::milliseconds timeInterval{2000}; |
| 86 | ndn::time::milliseconds duration{0}; |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | ndn::Face m_face; |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 90 | ndn::Scheduler m_scheduler{m_face.getIoContext()}; |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 91 | std::uniform_int_distribution<> m_randomDist{200, 1000}; |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | void |
| 95 | Publisher::run() |
| 96 | { |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 97 | if (mode == AUTO) { |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 98 | m_scheduler.schedule(timeInterval, [this] { autoGenerate(); }); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 99 | } |
| 100 | else { |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 101 | m_scheduler.schedule(timeInterval, [this] { generateFromFile(); }); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 102 | } |
| 103 | m_face.processEvents(duration); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | Publisher::autoGenerate() |
| 108 | { |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 109 | ndn::Name name = dataPrefix; |
| 110 | name.appendNumber(m_randomDist(ndn::random::getRandomNumberEngine())); |
| 111 | auto data = createData(name); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 112 | m_face.put(*data); |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 113 | |
| 114 | m_scheduler.schedule(timeInterval, [this] { autoGenerate(); }); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void |
| 118 | Publisher::generateFromFile() |
| 119 | { |
| 120 | if (insertStream.eof()) { |
Davide Pesavento | 164ae3b | 2023-11-11 22:00:23 -0500 | [diff] [blame] | 121 | m_face.getIoContext().stop(); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 122 | return; |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 125 | std::string name; |
| 126 | getline(insertStream, name); |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 127 | auto data = createData(name); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 128 | m_face.put(*data); |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 129 | |
| 130 | m_scheduler.schedule(timeInterval, [this] { generateFromFile(); }); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 133 | std::shared_ptr<ndn::Data> |
| 134 | Publisher::createData(const ndn::Name& name) |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 135 | { |
| 136 | static ndn::KeyChain keyChain; |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 137 | static const std::vector<uint8_t> content(1500, '-'); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 138 | |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 139 | auto data = std::make_shared<ndn::Data>(name); |
| 140 | data->setContent(content); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 141 | keyChain.sign(*data); |
| 142 | return data; |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | usage() |
| 147 | { |
| 148 | std::cerr |
| 149 | << " Publisher [-d dataPrefix] [-f filename] [-s duration time] [-t generate time interval] \n" |
| 150 | << " -d: specify the data prefix publisher generate\n" |
| 151 | << " -f: specify filename that publish would read from\n" |
| 152 | << " -s: specify the time duration of generate data\n" |
| 153 | << " -t: specify the time interval between two data generated\n" |
| 154 | << std::endl; |
| 155 | exit(1); |
| 156 | } |
| 157 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 158 | static int |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 159 | main(int argc, char* argv[]) |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 160 | { |
| 161 | Publisher generator; |
| 162 | bool isAuto = false; |
| 163 | bool isRead = false; |
| 164 | int opt; |
| 165 | while ((opt = getopt(argc, argv, "d:f:s:t:")) != -1) { |
| 166 | switch (opt) { |
| 167 | case 'd': |
| 168 | { |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 169 | generator.dataPrefix = ndn::Name(std::string(optarg)); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 170 | generator.mode = AUTO; |
| 171 | isAuto = true; |
| 172 | } |
| 173 | break; |
| 174 | case 'f': |
| 175 | { |
| 176 | isRead = true; |
| 177 | generator.mode = READFILE; |
| 178 | std::string str = std::string(optarg); |
| 179 | generator.insertStream.open(str.c_str()); |
| 180 | if (!generator.insertStream.is_open()) { |
| 181 | std::cerr << "ERROR: cannot open " << std::string(optarg) << std::endl; |
| 182 | return 1; |
| 183 | } |
| 184 | } |
| 185 | break; |
| 186 | case 's': |
| 187 | try { |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 188 | generator.duration = ndn::time::milliseconds(boost::lexical_cast<uint64_t>(optarg)); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 189 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 190 | catch (const boost::bad_lexical_cast&) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 191 | std::cerr << "-s option should be an integer" << std::endl; |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 192 | return 1; |
| 193 | } |
| 194 | break; |
| 195 | case 't': |
| 196 | try { |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 197 | generator.timeInterval = ndn::time::milliseconds(boost::lexical_cast<uint64_t>(optarg)); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 198 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 199 | catch (const boost::bad_lexical_cast&) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 200 | std::cerr << "-t option should be an integer" << std::endl; |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 201 | return 1; |
| 202 | } |
| 203 | break; |
| 204 | default: |
| 205 | usage(); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | argc -= optind; |
| 211 | argv += optind; |
| 212 | |
| 213 | if (argc != 0) |
| 214 | usage(); |
| 215 | |
| 216 | if (isAuto && isRead) |
| 217 | usage(); |
| 218 | |
| 219 | generator.run(); |
| 220 | return 0; |
| 221 | } |
| 222 | |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 223 | } // namespace repo::examples |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 224 | |
| 225 | int |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 226 | main(int argc, char* argv[]) |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 227 | { |
| 228 | try { |
Davide Pesavento | d8521aa | 2023-09-17 14:21:27 -0400 | [diff] [blame] | 229 | return repo::examples::main(argc, argv); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 230 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 231 | catch (const std::exception& e) { |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 232 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 233 | return 2; |
| 234 | } |
| 235 | } |