Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
| 26 | #include "core/extended-error-message.hpp" |
| 27 | #include "core/global-io.hpp" |
| 28 | #include "face/face.hpp" |
| 29 | #include "face/tcp-channel.hpp" |
| 30 | #include "face/udp-channel.hpp" |
| 31 | |
| 32 | #include <fstream> |
| 33 | #include <iostream> |
| 34 | |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 35 | #ifdef HAVE_VALGRIND |
| 36 | #include <valgrind/callgrind.h> |
| 37 | #endif |
| 38 | |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 39 | namespace nfd { |
| 40 | namespace tests { |
| 41 | |
| 42 | class FaceBenchmark |
| 43 | { |
| 44 | public: |
| 45 | FaceBenchmark(const char* configFileName) |
| 46 | : m_terminationSignalSet{getGlobalIoService()} |
| 47 | , m_tcpChannel{tcp::Endpoint{boost::asio::ip::tcp::v4(), 6363}} |
| 48 | , m_udpChannel{udp::Endpoint{boost::asio::ip::udp::v4(), 6363}, time::minutes{10}} |
| 49 | { |
| 50 | m_terminationSignalSet.add(SIGINT); |
| 51 | m_terminationSignalSet.add(SIGTERM); |
| 52 | m_terminationSignalSet.async_wait(bind(&FaceBenchmark::terminate, _1, _2)); |
| 53 | |
| 54 | parseConfig(configFileName); |
| 55 | |
| 56 | m_tcpChannel.listen(bind(&FaceBenchmark::onLeftFaceCreated, this, _1), |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 57 | bind(&FaceBenchmark::onFaceCreationFailed, _1, _2)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 58 | std::clog << "Listening on " << m_tcpChannel.getUri() << std::endl; |
| 59 | |
| 60 | m_udpChannel.listen(bind(&FaceBenchmark::onLeftFaceCreated, this, _1), |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 61 | bind(&FaceBenchmark::onFaceCreationFailed, _1, _2)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 62 | std::clog << "Listening on " << m_udpChannel.getUri() << std::endl; |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | static void |
| 67 | terminate(const boost::system::error_code& error, int signalNo) |
| 68 | { |
| 69 | if (error) |
| 70 | return; |
| 71 | getGlobalIoService().stop(); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | parseConfig(const char* configFileName) |
| 76 | { |
| 77 | std::ifstream file{configFileName}; |
| 78 | std::string uriStrL; |
| 79 | std::string uriStrR; |
| 80 | |
| 81 | while (file >> uriStrL >> uriStrR) { |
| 82 | FaceUri uriL{uriStrL}; |
| 83 | FaceUri uriR{uriStrR}; |
| 84 | |
| 85 | if (uriL.getScheme() != "tcp4" && uriL.getScheme() != "udp4") { |
| 86 | std::clog << "Unsupported protocol '" << uriL.getScheme() << "'" << std::endl; |
| 87 | } |
| 88 | else if (uriR.getScheme() != "tcp4" && uriR.getScheme() != "udp4") { |
| 89 | std::clog << "Unsupported protocol '" << uriR.getScheme() << "'" << std::endl; |
| 90 | } |
| 91 | else { |
| 92 | m_faceUris.push_back(std::make_pair(uriL, uriR)); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (m_faceUris.empty()) { |
| 97 | BOOST_THROW_EXCEPTION(std::runtime_error("No supported FaceUri pairs found in config file")); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | onLeftFaceCreated(const shared_ptr<Face>& faceL) |
| 103 | { |
| 104 | std::clog << "Left face created: remote=" << faceL->getRemoteUri() |
| 105 | << " local=" << faceL->getLocalUri() << std::endl; |
| 106 | |
| 107 | // find a matching right uri |
| 108 | FaceUri uriR; |
| 109 | for (const auto& pair : m_faceUris) { |
| 110 | if (pair.first.getHost() == faceL->getRemoteUri().getHost() && |
| 111 | pair.first.getScheme() == faceL->getRemoteUri().getScheme()) { |
| 112 | uriR = pair.second; |
| 113 | } |
| 114 | else if (pair.second.getHost() == faceL->getRemoteUri().getHost() && |
| 115 | pair.second.getScheme() == faceL->getRemoteUri().getScheme()) { |
| 116 | uriR = pair.first; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (uriR == FaceUri()) { |
| 121 | std::clog << "No FaceUri matched, ignoring..." << std::endl; |
| 122 | faceL->close(); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | // create the right face |
| 127 | auto addr = boost::asio::ip::address::from_string(uriR.getHost()); |
| 128 | auto port = boost::lexical_cast<uint16_t>(uriR.getPort()); |
| 129 | if (uriR.getScheme() == "tcp4") { |
| 130 | m_tcpChannel.connect(tcp::Endpoint(addr, port), |
Davide Pesavento | a3c9ddb | 2017-04-10 22:15:24 -0400 | [diff] [blame] | 131 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 132 | false, |
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 | else if (uriR.getScheme() == "udp4") { |
| 137 | m_udpChannel.connect(udp::Endpoint(addr, port), |
| 138 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 139 | bind(&FaceBenchmark::onRightFaceCreated, this, faceL, _1), |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 140 | bind(&FaceBenchmark::onFaceCreationFailed, _1, _2)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | onRightFaceCreated(const shared_ptr<Face>& faceL, const shared_ptr<Face>& faceR) |
| 146 | { |
| 147 | std::clog << "Right face created: remote=" << faceR->getRemoteUri() |
| 148 | << " local=" << faceR->getLocalUri() << std::endl; |
| 149 | |
| 150 | tieFaces(faceR, faceL); |
| 151 | tieFaces(faceL, faceR); |
| 152 | } |
| 153 | |
| 154 | static void |
| 155 | tieFaces(const shared_ptr<Face>& face1, const shared_ptr<Face>& face2) |
| 156 | { |
| 157 | face1->afterReceiveInterest.connect([face2] (const Interest& interest) { face2->sendInterest(interest); }); |
| 158 | face1->afterReceiveData.connect([face2] (const Data& data) { face2->sendData(data); }); |
| 159 | face1->afterReceiveNack.connect([face2] (const ndn::lp::Nack& nack) { face2->sendNack(nack); }); |
| 160 | } |
| 161 | |
| 162 | static void |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 163 | onFaceCreationFailed(uint32_t status, const std::string& reason) |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 164 | { |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 165 | BOOST_THROW_EXCEPTION(std::runtime_error("Failed to create face: " + to_string(status) + ": " + reason)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | private: |
| 169 | boost::asio::signal_set m_terminationSignalSet; |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 170 | face::TcpChannel m_tcpChannel; |
| 171 | face::UdpChannel m_udpChannel; |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 172 | std::vector<std::pair<FaceUri, FaceUri>> m_faceUris; |
| 173 | }; |
| 174 | |
| 175 | } // namespace tests |
| 176 | } // namespace nfd |
| 177 | |
| 178 | int |
| 179 | main(int argc, char** argv) |
| 180 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 181 | #ifdef _DEBUG |
| 182 | std::cerr << "Benchmark compiled in debug mode is unreliable, please compile in release mode.\n"; |
| 183 | #endif |
| 184 | |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 185 | if (argc != 2) { |
| 186 | std::cerr << "Usage: " << argv[0] << " <config-file>" << std::endl; |
| 187 | return 2; |
| 188 | } |
| 189 | |
| 190 | try { |
| 191 | nfd::tests::FaceBenchmark bench{argv[1]}; |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 192 | #ifdef HAVE_VALGRIND |
| 193 | CALLGRIND_START_INSTRUMENTATION; |
| 194 | #endif |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 195 | nfd::getGlobalIoService().run(); |
| 196 | } |
| 197 | catch (const std::exception& e) { |
| 198 | std::cerr << "FATAL: " << nfd::getExtendedErrorMessage(e) << std::endl; |
| 199 | return 1; |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |