Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 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 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 26 | #include "daemon/global.hpp" |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 27 | #include "face/face.hpp" |
| 28 | #include "face/tcp-channel.hpp" |
| 29 | #include "face/udp-channel.hpp" |
| 30 | |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 31 | #include <boost/exception/diagnostic_information.hpp> |
| 32 | |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 33 | #include <fstream> |
| 34 | #include <iostream> |
| 35 | |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 36 | #ifdef HAVE_VALGRIND |
| 37 | #include <valgrind/callgrind.h> |
| 38 | #endif |
| 39 | |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 40 | namespace nfd { |
| 41 | namespace tests { |
| 42 | |
| 43 | class FaceBenchmark |
| 44 | { |
| 45 | public: |
| 46 | FaceBenchmark(const char* configFileName) |
| 47 | : m_terminationSignalSet{getGlobalIoService()} |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 48 | , m_tcpChannel{tcp::Endpoint{boost::asio::ip::tcp::v4(), 6363}, false, |
| 49 | bind([] { return ndn::nfd::FACE_SCOPE_NON_LOCAL; })} |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 50 | , m_udpChannel{udp::Endpoint{boost::asio::ip::udp::v4(), 6363}, time::minutes{10}, false} |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 51 | { |
| 52 | m_terminationSignalSet.add(SIGINT); |
| 53 | m_terminationSignalSet.add(SIGTERM); |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 54 | m_terminationSignalSet.async_wait([] (const auto& error, int) { |
| 55 | if (!error) |
| 56 | getGlobalIoService().stop(); |
| 57 | }); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 58 | |
| 59 | parseConfig(configFileName); |
| 60 | |
| 61 | m_tcpChannel.listen(bind(&FaceBenchmark::onLeftFaceCreated, this, _1), |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 62 | bind(&FaceBenchmark::onFaceCreationFailed, _1, _2)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 63 | std::clog << "Listening on " << m_tcpChannel.getUri() << std::endl; |
| 64 | |
| 65 | m_udpChannel.listen(bind(&FaceBenchmark::onLeftFaceCreated, this, _1), |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 66 | bind(&FaceBenchmark::onFaceCreationFailed, _1, _2)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 67 | std::clog << "Listening on " << m_udpChannel.getUri() << std::endl; |
| 68 | } |
| 69 | |
| 70 | private: |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 71 | void |
| 72 | parseConfig(const char* configFileName) |
| 73 | { |
| 74 | std::ifstream file{configFileName}; |
| 75 | std::string uriStrL; |
| 76 | std::string uriStrR; |
| 77 | |
| 78 | while (file >> uriStrL >> uriStrR) { |
| 79 | FaceUri uriL{uriStrL}; |
| 80 | FaceUri uriR{uriStrR}; |
| 81 | |
| 82 | if (uriL.getScheme() != "tcp4" && uriL.getScheme() != "udp4") { |
| 83 | std::clog << "Unsupported protocol '" << uriL.getScheme() << "'" << std::endl; |
| 84 | } |
| 85 | else if (uriR.getScheme() != "tcp4" && uriR.getScheme() != "udp4") { |
| 86 | std::clog << "Unsupported protocol '" << uriR.getScheme() << "'" << std::endl; |
| 87 | } |
| 88 | else { |
| 89 | m_faceUris.push_back(std::make_pair(uriL, uriR)); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (m_faceUris.empty()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 94 | NDN_THROW(std::runtime_error("No supported FaceUri pairs found in config file")); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | onLeftFaceCreated(const shared_ptr<Face>& faceL) |
| 100 | { |
| 101 | std::clog << "Left face created: remote=" << faceL->getRemoteUri() |
| 102 | << " local=" << faceL->getLocalUri() << std::endl; |
| 103 | |
| 104 | // find a matching right uri |
| 105 | FaceUri uriR; |
| 106 | for (const auto& pair : m_faceUris) { |
| 107 | if (pair.first.getHost() == faceL->getRemoteUri().getHost() && |
| 108 | pair.first.getScheme() == faceL->getRemoteUri().getScheme()) { |
| 109 | uriR = pair.second; |
| 110 | } |
| 111 | else if (pair.second.getHost() == faceL->getRemoteUri().getHost() && |
| 112 | pair.second.getScheme() == faceL->getRemoteUri().getScheme()) { |
| 113 | uriR = pair.first; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (uriR == FaceUri()) { |
| 118 | std::clog << "No FaceUri matched, ignoring..." << std::endl; |
| 119 | faceL->close(); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | // create the right face |
Davide Pesavento | 9c33b90 | 2018-05-20 01:30:29 -0400 | [diff] [blame] | 124 | auto addr = boost::asio::ip::address::from_string(uriR.getHost()); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 125 | auto port = boost::lexical_cast<uint16_t>(uriR.getPort()); |
| 126 | if (uriR.getScheme() == "tcp4") { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 127 | m_tcpChannel.connect(tcp::Endpoint(addr, port), {}, |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 128 | bind(&FaceBenchmark::onRightFaceCreated, this, faceL, _1), |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 129 | bind(&FaceBenchmark::onFaceCreationFailed, _1, _2)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 130 | } |
| 131 | else if (uriR.getScheme() == "udp4") { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 132 | m_udpChannel.connect(udp::Endpoint(addr, port), {}, |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 133 | bind(&FaceBenchmark::onRightFaceCreated, this, faceL, _1), |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 134 | bind(&FaceBenchmark::onFaceCreationFailed, _1, _2)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | onRightFaceCreated(const shared_ptr<Face>& faceL, const shared_ptr<Face>& faceR) |
| 140 | { |
| 141 | std::clog << "Right face created: remote=" << faceR->getRemoteUri() |
| 142 | << " local=" << faceR->getLocalUri() << std::endl; |
| 143 | |
| 144 | tieFaces(faceR, faceL); |
| 145 | tieFaces(faceL, faceR); |
| 146 | } |
| 147 | |
| 148 | static void |
| 149 | tieFaces(const shared_ptr<Face>& face1, const shared_ptr<Face>& face2) |
| 150 | { |
| 151 | face1->afterReceiveInterest.connect([face2] (const Interest& interest) { face2->sendInterest(interest); }); |
| 152 | face1->afterReceiveData.connect([face2] (const Data& data) { face2->sendData(data); }); |
| 153 | face1->afterReceiveNack.connect([face2] (const ndn::lp::Nack& nack) { face2->sendNack(nack); }); |
| 154 | } |
| 155 | |
| 156 | static void |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 157 | onFaceCreationFailed(uint32_t status, const std::string& reason) |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 158 | { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 159 | NDN_THROW(std::runtime_error("Failed to create face: [" + to_string(status) + "] " + reason)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | private: |
| 163 | boost::asio::signal_set m_terminationSignalSet; |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 164 | face::TcpChannel m_tcpChannel; |
| 165 | face::UdpChannel m_udpChannel; |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 166 | std::vector<std::pair<FaceUri, FaceUri>> m_faceUris; |
| 167 | }; |
| 168 | |
| 169 | } // namespace tests |
| 170 | } // namespace nfd |
| 171 | |
| 172 | int |
| 173 | main(int argc, char** argv) |
| 174 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 175 | #ifdef _DEBUG |
| 176 | std::cerr << "Benchmark compiled in debug mode is unreliable, please compile in release mode.\n"; |
| 177 | #endif |
| 178 | |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 179 | if (argc != 2) { |
| 180 | std::cerr << "Usage: " << argv[0] << " <config-file>" << std::endl; |
| 181 | return 2; |
| 182 | } |
| 183 | |
| 184 | try { |
| 185 | nfd::tests::FaceBenchmark bench{argv[1]}; |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 186 | #ifdef HAVE_VALGRIND |
| 187 | CALLGRIND_START_INSTRUMENTATION; |
| 188 | #endif |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 189 | nfd::getGlobalIoService().run(); |
| 190 | } |
| 191 | catch (const std::exception& e) { |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 192 | std::cerr << "ERROR: " << boost::diagnostic_information(e); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 193 | return 1; |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |