blob: afd4cf703abb4c158453d1cfb5b238a06e4924fc [file] [log] [blame]
Weiwei Liu4f1afac2016-04-02 19:08:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, Regents of the University of California,
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
35namespace nfd {
36namespace tests {
37
38class FaceBenchmark
39{
40public:
41 FaceBenchmark(const char* configFileName)
42 : m_terminationSignalSet{getGlobalIoService()}
43 , m_tcpChannel{tcp::Endpoint{boost::asio::ip::tcp::v4(), 6363}}
44 , m_udpChannel{udp::Endpoint{boost::asio::ip::udp::v4(), 6363}, time::minutes{10}}
45 {
46 m_terminationSignalSet.add(SIGINT);
47 m_terminationSignalSet.add(SIGTERM);
48 m_terminationSignalSet.async_wait(bind(&FaceBenchmark::terminate, _1, _2));
49
50 parseConfig(configFileName);
51
52 m_tcpChannel.listen(bind(&FaceBenchmark::onLeftFaceCreated, this, _1),
53 bind(&FaceBenchmark::onFaceCreationFailed, _1));
54 std::clog << "Listening on " << m_tcpChannel.getUri() << std::endl;
55
56 m_udpChannel.listen(bind(&FaceBenchmark::onLeftFaceCreated, this, _1),
57 bind(&FaceBenchmark::onFaceCreationFailed, _1));
58 std::clog << "Listening on " << m_udpChannel.getUri() << std::endl;
59 }
60
61private:
62 static void
63 terminate(const boost::system::error_code& error, int signalNo)
64 {
65 if (error)
66 return;
67 getGlobalIoService().stop();
68 }
69
70 void
71 parseConfig(const char* configFileName)
72 {
73 std::ifstream file{configFileName};
74 std::string uriStrL;
75 std::string uriStrR;
76
77 while (file >> uriStrL >> uriStrR) {
78 FaceUri uriL{uriStrL};
79 FaceUri uriR{uriStrR};
80
81 if (uriL.getScheme() != "tcp4" && uriL.getScheme() != "udp4") {
82 std::clog << "Unsupported protocol '" << uriL.getScheme() << "'" << std::endl;
83 }
84 else if (uriR.getScheme() != "tcp4" && uriR.getScheme() != "udp4") {
85 std::clog << "Unsupported protocol '" << uriR.getScheme() << "'" << std::endl;
86 }
87 else {
88 m_faceUris.push_back(std::make_pair(uriL, uriR));
89 }
90 }
91
92 if (m_faceUris.empty()) {
93 BOOST_THROW_EXCEPTION(std::runtime_error("No supported FaceUri pairs found in config file"));
94 }
95 }
96
97 void
98 onLeftFaceCreated(const shared_ptr<Face>& faceL)
99 {
100 std::clog << "Left face created: remote=" << faceL->getRemoteUri()
101 << " local=" << faceL->getLocalUri() << std::endl;
102
103 // find a matching right uri
104 FaceUri uriR;
105 for (const auto& pair : m_faceUris) {
106 if (pair.first.getHost() == faceL->getRemoteUri().getHost() &&
107 pair.first.getScheme() == faceL->getRemoteUri().getScheme()) {
108 uriR = pair.second;
109 }
110 else if (pair.second.getHost() == faceL->getRemoteUri().getHost() &&
111 pair.second.getScheme() == faceL->getRemoteUri().getScheme()) {
112 uriR = pair.first;
113 }
114 }
115
116 if (uriR == FaceUri()) {
117 std::clog << "No FaceUri matched, ignoring..." << std::endl;
118 faceL->close();
119 return;
120 }
121
122 // create the right face
123 auto addr = boost::asio::ip::address::from_string(uriR.getHost());
124 auto port = boost::lexical_cast<uint16_t>(uriR.getPort());
125 if (uriR.getScheme() == "tcp4") {
126 m_tcpChannel.connect(tcp::Endpoint(addr, port),
127 bind(&FaceBenchmark::onRightFaceCreated, this, faceL, _1),
128 bind(&FaceBenchmark::onFaceCreationFailed, _1));
129 }
130 else if (uriR.getScheme() == "udp4") {
131 m_udpChannel.connect(udp::Endpoint(addr, port),
132 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
133 bind(&FaceBenchmark::onRightFaceCreated, this, faceL, _1),
134 bind(&FaceBenchmark::onFaceCreationFailed, _1));
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
157 onFaceCreationFailed(const std::string& reason)
158 {
159 BOOST_THROW_EXCEPTION(std::runtime_error("Failed to create face: " + reason));
160 }
161
162private:
163 boost::asio::signal_set m_terminationSignalSet;
164 TcpChannel m_tcpChannel;
165 UdpChannel m_udpChannel;
166 std::vector<std::pair<FaceUri, FaceUri>> m_faceUris;
167};
168
169} // namespace tests
170} // namespace nfd
171
172int
173main(int argc, char** argv)
174{
175 if (argc != 2) {
176 std::cerr << "Usage: " << argv[0] << " <config-file>" << std::endl;
177 return 2;
178 }
179
180 try {
181 nfd::tests::FaceBenchmark bench{argv[1]};
182 nfd::getGlobalIoService().run();
183 }
184 catch (const std::exception& e) {
185 std::cerr << "FATAL: " << nfd::getExtendedErrorMessage(e) << std::endl;
186 return 1;
187 }
188
189 return 0;
190}