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 | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, 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 | d45e62b | 2018-01-01 23:33:18 -0500 | [diff] [blame] | 37 | #include <boost/asio/io_service.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 | |
| 52 | namespace repo { |
| 53 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 54 | using ndn::time::milliseconds; |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 55 | |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 56 | const milliseconds DEFAULT_TIME_INTERVAL(2000); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 57 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 58 | enum Mode { |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 59 | AUTO, |
| 60 | READFILE |
| 61 | }; |
| 62 | |
| 63 | class Publisher |
| 64 | { |
| 65 | public: |
| 66 | class Error : public std::runtime_error |
| 67 | { |
| 68 | public: |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 69 | using std::runtime_error::runtime_error; |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | public: |
| 73 | Publisher() |
| 74 | : mode(AUTO) |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 75 | , dataPrefix("/example/data") |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 76 | , timeInterval(DEFAULT_TIME_INTERVAL) |
| 77 | , duration(0) |
| 78 | , m_scheduler(m_face.getIoService()) |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 79 | , m_randomDist(200, 1000) |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 80 | { |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | run(); |
| 85 | |
| 86 | void |
| 87 | autoGenerate(); |
| 88 | |
| 89 | void |
| 90 | generateFromFile(); |
| 91 | |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 92 | static std::shared_ptr<ndn::Data> |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 93 | createData(const ndn::Name& name); |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 94 | |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 95 | public: |
| 96 | std::ifstream insertStream; |
| 97 | Mode mode; |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 98 | ndn::Name dataPrefix; |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 99 | milliseconds timeInterval; |
| 100 | milliseconds duration; |
| 101 | |
| 102 | private: |
| 103 | ndn::Face m_face; |
| 104 | ndn::Scheduler m_scheduler; |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 105 | std::uniform_int_distribution<> m_randomDist; |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | void |
| 109 | Publisher::run() |
| 110 | { |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 111 | if (mode == AUTO) { |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 112 | m_scheduler.schedule(timeInterval, [this] { autoGenerate(); }); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 113 | } |
| 114 | else { |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 115 | m_scheduler.schedule(timeInterval, [this] { generateFromFile(); }); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 116 | } |
| 117 | m_face.processEvents(duration); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | Publisher::autoGenerate() |
| 122 | { |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 123 | ndn::Name name = dataPrefix; |
| 124 | name.appendNumber(m_randomDist(ndn::random::getRandomNumberEngine())); |
| 125 | auto data = createData(name); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 126 | m_face.put(*data); |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 127 | |
| 128 | m_scheduler.schedule(timeInterval, [this] { autoGenerate(); }); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void |
| 132 | Publisher::generateFromFile() |
| 133 | { |
| 134 | if (insertStream.eof()) { |
| 135 | m_face.getIoService().stop(); |
| 136 | return; |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 139 | std::string name; |
| 140 | getline(insertStream, name); |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 141 | auto data = createData(name); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 142 | m_face.put(*data); |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 143 | |
| 144 | m_scheduler.schedule(timeInterval, [this] { generateFromFile(); }); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 147 | std::shared_ptr<ndn::Data> |
| 148 | Publisher::createData(const ndn::Name& name) |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 149 | { |
| 150 | static ndn::KeyChain keyChain; |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 151 | static const std::vector<uint8_t> content(1500, '-'); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 152 | |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 153 | auto data = std::make_shared<ndn::Data>(name); |
| 154 | data->setContent(content); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 155 | keyChain.sign(*data); |
| 156 | return data; |
| 157 | } |
| 158 | |
| 159 | static void |
| 160 | usage() |
| 161 | { |
| 162 | std::cerr |
| 163 | << " Publisher [-d dataPrefix] [-f filename] [-s duration time] [-t generate time interval] \n" |
| 164 | << " -d: specify the data prefix publisher generate\n" |
| 165 | << " -f: specify filename that publish would read from\n" |
| 166 | << " -s: specify the time duration of generate data\n" |
| 167 | << " -t: specify the time interval between two data generated\n" |
| 168 | << std::endl; |
| 169 | exit(1); |
| 170 | } |
| 171 | |
Davide Pesavento | 49f3a5f | 2017-09-23 01:36:33 -0400 | [diff] [blame] | 172 | static int |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 173 | main(int argc, char* argv[]) |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 174 | { |
| 175 | Publisher generator; |
| 176 | bool isAuto = false; |
| 177 | bool isRead = false; |
| 178 | int opt; |
| 179 | while ((opt = getopt(argc, argv, "d:f:s:t:")) != -1) { |
| 180 | switch (opt) { |
| 181 | case 'd': |
| 182 | { |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 183 | generator.dataPrefix = ndn::Name(std::string(optarg)); |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 184 | generator.mode = AUTO; |
| 185 | isAuto = true; |
| 186 | } |
| 187 | break; |
| 188 | case 'f': |
| 189 | { |
| 190 | isRead = true; |
| 191 | generator.mode = READFILE; |
| 192 | std::string str = std::string(optarg); |
| 193 | generator.insertStream.open(str.c_str()); |
| 194 | if (!generator.insertStream.is_open()) { |
| 195 | std::cerr << "ERROR: cannot open " << std::string(optarg) << std::endl; |
| 196 | return 1; |
| 197 | } |
| 198 | } |
| 199 | break; |
| 200 | case 's': |
| 201 | try { |
| 202 | generator.duration = milliseconds(boost::lexical_cast<uint64_t>(optarg)); |
| 203 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 204 | catch (const boost::bad_lexical_cast&) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 205 | std::cerr << "-s option should be an integer" << std::endl; |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 206 | return 1; |
| 207 | } |
| 208 | break; |
| 209 | case 't': |
| 210 | try { |
| 211 | generator.timeInterval = milliseconds(boost::lexical_cast<uint64_t>(optarg)); |
| 212 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 213 | catch (const boost::bad_lexical_cast&) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame^] | 214 | std::cerr << "-t option should be an integer" << std::endl; |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 215 | return 1; |
| 216 | } |
| 217 | break; |
| 218 | default: |
| 219 | usage(); |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | argc -= optind; |
| 225 | argv += optind; |
| 226 | |
| 227 | if (argc != 0) |
| 228 | usage(); |
| 229 | |
| 230 | if (isAuto && isRead) |
| 231 | usage(); |
| 232 | |
| 233 | generator.run(); |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | } // namespace repo |
| 238 | |
| 239 | int |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 240 | main(int argc, char* argv[]) |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 241 | { |
| 242 | try { |
| 243 | return repo::main(argc, argv); |
| 244 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 245 | catch (const std::exception& e) { |
Weiqi Shi | 68f2cf6 | 2014-08-26 12:35:45 -0700 | [diff] [blame] | 246 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 247 | return 2; |
| 248 | } |
| 249 | } |