blob: 3532bdbc53fb884a7ba9947a48f42806b1c34144 [file] [log] [blame]
Weiwei Liu4f1afac2016-04-02 19:08:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry2642cd22017-07-13 21:34:53 -04002/*
Davide Pesavento8fd15e62017-04-06 19:58:54 -04003 * Copyright (c) 2014-2017, Regents of the University of California,
Weiwei Liu4f1afac2016-04-02 19:08:38 -07004 * 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
Yanbiao Liafb20592017-08-03 16:20:46 -070032#include <ndn-cxx/net/address-converter.hpp>
Weiwei Liu4f1afac2016-04-02 19:08:38 -070033#include <fstream>
34#include <iostream>
35
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000036#ifdef HAVE_VALGRIND
37#include <valgrind/callgrind.h>
38#endif
39
Weiwei Liu4f1afac2016-04-02 19:08:38 -070040namespace nfd {
41namespace tests {
42
43class FaceBenchmark
44{
45public:
46 FaceBenchmark(const char* configFileName)
47 : m_terminationSignalSet{getGlobalIoService()}
48 , m_tcpChannel{tcp::Endpoint{boost::asio::ip::tcp::v4(), 6363}}
49 , m_udpChannel{udp::Endpoint{boost::asio::ip::udp::v4(), 6363}, time::minutes{10}}
50 {
51 m_terminationSignalSet.add(SIGINT);
52 m_terminationSignalSet.add(SIGTERM);
53 m_terminationSignalSet.async_wait(bind(&FaceBenchmark::terminate, _1, _2));
54
55 parseConfig(configFileName);
56
57 m_tcpChannel.listen(bind(&FaceBenchmark::onLeftFaceCreated, this, _1),
Eric Newberry42602412016-08-27 09:33:18 -070058 bind(&FaceBenchmark::onFaceCreationFailed, _1, _2));
Weiwei Liu4f1afac2016-04-02 19:08:38 -070059 std::clog << "Listening on " << m_tcpChannel.getUri() << std::endl;
60
61 m_udpChannel.listen(bind(&FaceBenchmark::onLeftFaceCreated, this, _1),
Eric Newberry42602412016-08-27 09:33:18 -070062 bind(&FaceBenchmark::onFaceCreationFailed, _1, _2));
Weiwei Liu4f1afac2016-04-02 19:08:38 -070063 std::clog << "Listening on " << m_udpChannel.getUri() << std::endl;
64 }
65
66private:
67 static void
68 terminate(const boost::system::error_code& error, int signalNo)
69 {
70 if (error)
71 return;
72 getGlobalIoService().stop();
73 }
74
75 void
76 parseConfig(const char* configFileName)
77 {
78 std::ifstream file{configFileName};
79 std::string uriStrL;
80 std::string uriStrR;
81
82 while (file >> uriStrL >> uriStrR) {
83 FaceUri uriL{uriStrL};
84 FaceUri uriR{uriStrR};
85
86 if (uriL.getScheme() != "tcp4" && uriL.getScheme() != "udp4") {
87 std::clog << "Unsupported protocol '" << uriL.getScheme() << "'" << std::endl;
88 }
89 else if (uriR.getScheme() != "tcp4" && uriR.getScheme() != "udp4") {
90 std::clog << "Unsupported protocol '" << uriR.getScheme() << "'" << std::endl;
91 }
92 else {
93 m_faceUris.push_back(std::make_pair(uriL, uriR));
94 }
95 }
96
97 if (m_faceUris.empty()) {
98 BOOST_THROW_EXCEPTION(std::runtime_error("No supported FaceUri pairs found in config file"));
99 }
100 }
101
102 void
103 onLeftFaceCreated(const shared_ptr<Face>& faceL)
104 {
105 std::clog << "Left face created: remote=" << faceL->getRemoteUri()
106 << " local=" << faceL->getLocalUri() << std::endl;
107
108 // find a matching right uri
109 FaceUri uriR;
110 for (const auto& pair : m_faceUris) {
111 if (pair.first.getHost() == faceL->getRemoteUri().getHost() &&
112 pair.first.getScheme() == faceL->getRemoteUri().getScheme()) {
113 uriR = pair.second;
114 }
115 else if (pair.second.getHost() == faceL->getRemoteUri().getHost() &&
116 pair.second.getScheme() == faceL->getRemoteUri().getScheme()) {
117 uriR = pair.first;
118 }
119 }
120
121 if (uriR == FaceUri()) {
122 std::clog << "No FaceUri matched, ignoring..." << std::endl;
123 faceL->close();
124 return;
125 }
126
127 // create the right face
Yanbiao Liafb20592017-08-03 16:20:46 -0700128 auto addr = ndn::ip::addressFromString(uriR.getHost());
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700129 auto port = boost::lexical_cast<uint16_t>(uriR.getPort());
130 if (uriR.getScheme() == "tcp4") {
131 m_tcpChannel.connect(tcp::Endpoint(addr, port),
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400132 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberryf40551a2016-09-05 15:41:16 -0700133 false,
Eric Newberry2642cd22017-07-13 21:34:53 -0400134 false,
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700135 bind(&FaceBenchmark::onRightFaceCreated, this, faceL, _1),
Eric Newberry42602412016-08-27 09:33:18 -0700136 bind(&FaceBenchmark::onFaceCreationFailed, _1, _2));
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700137 }
138 else if (uriR.getScheme() == "udp4") {
139 m_udpChannel.connect(udp::Endpoint(addr, port),
140 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
Eric Newberry2642cd22017-07-13 21:34:53 -0400141 false,
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700142 bind(&FaceBenchmark::onRightFaceCreated, this, faceL, _1),
Eric Newberry42602412016-08-27 09:33:18 -0700143 bind(&FaceBenchmark::onFaceCreationFailed, _1, _2));
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700144 }
145 }
146
147 void
148 onRightFaceCreated(const shared_ptr<Face>& faceL, const shared_ptr<Face>& faceR)
149 {
150 std::clog << "Right face created: remote=" << faceR->getRemoteUri()
151 << " local=" << faceR->getLocalUri() << std::endl;
152
153 tieFaces(faceR, faceL);
154 tieFaces(faceL, faceR);
155 }
156
157 static void
158 tieFaces(const shared_ptr<Face>& face1, const shared_ptr<Face>& face2)
159 {
160 face1->afterReceiveInterest.connect([face2] (const Interest& interest) { face2->sendInterest(interest); });
161 face1->afterReceiveData.connect([face2] (const Data& data) { face2->sendData(data); });
162 face1->afterReceiveNack.connect([face2] (const ndn::lp::Nack& nack) { face2->sendNack(nack); });
163 }
164
165 static void
Eric Newberry42602412016-08-27 09:33:18 -0700166 onFaceCreationFailed(uint32_t status, const std::string& reason)
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700167 {
Eric Newberry42602412016-08-27 09:33:18 -0700168 BOOST_THROW_EXCEPTION(std::runtime_error("Failed to create face: " + to_string(status) + ": " + reason));
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700169 }
170
171private:
172 boost::asio::signal_set m_terminationSignalSet;
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400173 face::TcpChannel m_tcpChannel;
174 face::UdpChannel m_udpChannel;
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700175 std::vector<std::pair<FaceUri, FaceUri>> m_faceUris;
176};
177
178} // namespace tests
179} // namespace nfd
180
181int
182main(int argc, char** argv)
183{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000184#ifdef _DEBUG
185 std::cerr << "Benchmark compiled in debug mode is unreliable, please compile in release mode.\n";
186#endif
187
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700188 if (argc != 2) {
189 std::cerr << "Usage: " << argv[0] << " <config-file>" << std::endl;
190 return 2;
191 }
192
193 try {
194 nfd::tests::FaceBenchmark bench{argv[1]};
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000195#ifdef HAVE_VALGRIND
196 CALLGRIND_START_INSTRUMENTATION;
197#endif
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700198 nfd::getGlobalIoService().run();
199 }
200 catch (const std::exception& e) {
201 std::cerr << "FATAL: " << nfd::getExtendedErrorMessage(e) << std::endl;
202 return 1;
203 }
204
205 return 0;
206}