Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2015, Arizona Board of Regents. |
| 4 | * |
| 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 7 | * |
| 8 | * ndn-tools 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 | * ndn-tools 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 |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Eric Newberry <enewberry@email.arizona.edu> |
| 20 | * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 21 | */ |
| 22 | |
| 23 | #include "core/common.hpp" |
| 24 | #include "core/version.hpp" |
| 25 | |
| 26 | #include "ping-server.hpp" |
| 27 | #include "tracer.hpp" |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace ping { |
| 31 | namespace server { |
| 32 | |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 33 | class Runner : noncopyable |
| 34 | { |
| 35 | public: |
| 36 | explicit |
| 37 | Runner(const Options& options) |
| 38 | : m_options(options) |
| 39 | , m_pingServer(m_face, m_keyChain, options) |
| 40 | , m_tracer(m_pingServer, options) |
| 41 | , m_signalSetInt(m_face.getIoService(), SIGINT) |
| 42 | { |
| 43 | m_signalSetInt.async_wait(bind(&Runner::afterIntSignal, this, _1)); |
| 44 | |
| 45 | m_pingServer.afterFinish.connect([this] { |
| 46 | this->cancel(); |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | int |
| 51 | run() |
| 52 | { |
| 53 | try { |
| 54 | m_pingServer.start(); |
| 55 | m_face.processEvents(); |
| 56 | } |
| 57 | catch (std::exception& e) { |
| 58 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | std::cout << "\n--- ping server " << m_options.prefix << " ---" << std::endl; |
| 63 | std::cout << m_pingServer.getNPings() << " packets processed" << std::endl; |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | void |
| 70 | cancel() |
| 71 | { |
| 72 | m_signalSetInt.cancel(); |
| 73 | m_pingServer.stop(); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | afterIntSignal(const boost::system::error_code& errorCode) |
| 78 | { |
| 79 | if (errorCode == boost::asio::error::operation_aborted) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | cancel(); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | const Options& m_options; |
| 88 | Face m_face; |
| 89 | KeyChain m_keyChain; |
| 90 | PingServer m_pingServer; |
| 91 | Tracer m_tracer; |
| 92 | |
| 93 | boost::asio::signal_set m_signalSetInt; |
| 94 | }; |
| 95 | |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 96 | static time::milliseconds |
| 97 | getMinimumFreshnessPeriod() |
| 98 | { |
| 99 | return time::milliseconds(1000); |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | usage(const boost::program_options::options_description& options) |
| 104 | { |
| 105 | std::cout << "Usage: ndnpingserver [options] ndn:/name/prefix\n" |
| 106 | "\n" |
Eric Newberry | a6b4bbd | 2015-05-30 22:09:40 -0700 | [diff] [blame] | 107 | "Starts a NDN ping server that responds to Interests under name " |
| 108 | "ndn:/name/prefix/ping.\n" |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 109 | "\n"; |
| 110 | std::cout << options; |
| 111 | exit(2); |
| 112 | } |
| 113 | |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 114 | int |
| 115 | main(int argc, char* argv[]) |
| 116 | { |
| 117 | Options options; |
| 118 | options.freshnessPeriod = getMinimumFreshnessPeriod(); |
| 119 | options.shouldLimitSatisfied = false; |
| 120 | options.nMaxPings = 0; |
| 121 | options.shouldPrintTimestamp = false; |
Eric Newberry | 62f7f71 | 2015-05-17 12:15:52 -0700 | [diff] [blame] | 122 | options.payloadSize = 0; |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 123 | |
| 124 | namespace po = boost::program_options; |
| 125 | |
| 126 | po::options_description visibleOptDesc("Allowed options"); |
| 127 | visibleOptDesc.add_options() |
| 128 | ("help,h", "print this message and exit") |
| 129 | ("version,V", "display version and exit") |
| 130 | ("freshness,x", po::value<int>(), |
| 131 | ("set freshness period in milliseconds (minimum " + |
| 132 | std::to_string(getMinimumFreshnessPeriod().count()) + " ms)").c_str()) |
| 133 | ("satisfy,p", po::value<int>(&options.nMaxPings), "set maximum number of pings to be satisfied") |
| 134 | ("timestamp,t", "log timestamp with responses") |
Eric Newberry | 62f7f71 | 2015-05-17 12:15:52 -0700 | [diff] [blame] | 135 | ("size,s", po::value<int>(&options.payloadSize), "specify size of response payload") |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 136 | ; |
| 137 | po::options_description hiddenOptDesc("Hidden options"); |
| 138 | hiddenOptDesc.add_options() |
| 139 | ("prefix", po::value<std::string>(), "prefix to register") |
| 140 | ; |
| 141 | |
| 142 | po::options_description optDesc("Allowed options"); |
| 143 | optDesc.add(visibleOptDesc).add(hiddenOptDesc); |
| 144 | |
| 145 | try { |
| 146 | po::positional_options_description optPos; |
| 147 | optPos.add("prefix", -1); |
| 148 | |
| 149 | po::variables_map optVm; |
| 150 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), optVm); |
| 151 | po::notify(optVm); |
| 152 | |
| 153 | if (optVm.count("help") > 0) { |
| 154 | usage(visibleOptDesc); |
| 155 | } |
| 156 | |
| 157 | if (optVm.count("version") > 0) { |
| 158 | std::cout << "ndnpingserver " << tools::VERSION << std::endl; |
| 159 | exit(0); |
| 160 | } |
| 161 | |
| 162 | if (optVm.count("prefix") > 0) { |
| 163 | options.prefix = Name(optVm["prefix"].as<std::string>()); |
| 164 | } |
| 165 | else { |
| 166 | std::cerr << "ERROR: No prefix specified" << std::endl; |
| 167 | usage(visibleOptDesc); |
| 168 | } |
| 169 | |
| 170 | if (optVm.count("freshness") > 0) { |
| 171 | options.freshnessPeriod = time::milliseconds(optVm["freshness"].as<int>()); |
| 172 | |
| 173 | if (options.freshnessPeriod.count() < getMinimumFreshnessPeriod().count()) { |
| 174 | std::cerr << "ERROR: Specified FreshnessPeriod is less than the minimum " |
| 175 | << getMinimumFreshnessPeriod() << std::endl; |
| 176 | usage(visibleOptDesc); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (optVm.count("satisfy") > 0) { |
| 181 | options.shouldLimitSatisfied = true; |
| 182 | |
| 183 | if (options.nMaxPings < 1) { |
| 184 | std::cerr << "ERROR: Maximum number of pings to satisfy must be greater than 0" << std::endl; |
| 185 | usage(visibleOptDesc); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (optVm.count("timestamp") > 0) { |
| 190 | options.shouldPrintTimestamp = true; |
| 191 | } |
Eric Newberry | 62f7f71 | 2015-05-17 12:15:52 -0700 | [diff] [blame] | 192 | |
| 193 | if (optVm.count("size") > 0) { |
| 194 | if (options.payloadSize < 0) { |
| 195 | std::cerr << "ERROR: Payload size must be greater than or equal to 0" << std::endl; |
| 196 | usage(visibleOptDesc); |
| 197 | } |
| 198 | } |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 199 | } |
| 200 | catch (const po::error& e) { |
| 201 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 202 | usage(visibleOptDesc); |
| 203 | } |
| 204 | |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 205 | std::cout << "PING SERVER " << options.prefix << std::endl; |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 206 | return Runner(options).run(); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | } // namespace server |
| 210 | } // namespace ping |
| 211 | } // namespace ndn |
| 212 | |
| 213 | int |
| 214 | main(int argc, char** argv) |
| 215 | { |
| 216 | return ndn::ping::server::main(argc, argv); |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 217 | } |