blob: 2c78dba331badbcee7715ee4b11ee776bd7e5f74 [file] [log] [blame]
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2014 University of Arizona.
4 *
5 * GNU 3.0 License, see the LICENSE file for more information
6 *
7 * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
8 */
9
10#include <sstream>
jeraldabraham420dbf02014-04-25 22:58:31 -070011
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080012#include <boost/asio.hpp>
13#include <boost/filesystem.hpp>
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070014#include <boost/lexical_cast.hpp>
jeraldabraham420dbf02014-04-25 22:58:31 -070015#include <boost/noncopyable.hpp>
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080016
jeraldabraham420dbf02014-04-25 22:58:31 -070017#include <ndn-cxx/face.hpp>
18#include <ndn-cxx/security/key-chain.hpp>
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080019
20#include "logger.hpp"
21
22namespace ndn {
23
jeraldabraham420dbf02014-04-25 22:58:31 -070024class NdnTrafficServer : boost::noncopyable
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080025{
26public:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070027
28 explicit
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070029 NdnTrafficServer(char* programName)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070030 : m_logger("NdnTrafficServer")
31 , m_programName(programName)
32 , m_hasError(false)
jeraldabraham420dbf02014-04-25 22:58:31 -070033 , m_hasQuietLogging(false)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070034 , m_nRegistrationsFailed(0)
35 , m_nMaximumInterests(-1)
36 , m_nInterestsReceived(0)
37 , m_contentDelay(time::milliseconds(-1))
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070038 , m_face(m_ioService)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080039 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070040 m_instanceId = boost::lexical_cast<std::string>(std::rand());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080041 }
42
43 class DataTrafficConfiguration
44 {
45 public:
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080046 DataTrafficConfiguration()
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070047 : m_contentType(-1)
48 , m_freshnessPeriod(time::milliseconds(-1))
49 , m_contentBytes(-1)
50 , m_contentDelay(time::milliseconds(-1))
51 , m_nInterestsReceived(0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080052 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080053 }
54
55 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070056 printTrafficConfiguration(Logger& logger)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080057 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070058 std::string detail = "";
59 if (m_name != "")
60 detail += "Name=" + m_name + ", ";
61 if (m_contentType >= 0)
62 detail += "ContentType=" + boost::lexical_cast<std::string>(m_contentType) + ", ";
63 if (m_freshnessPeriod >= time::milliseconds(0))
64 detail += "FreshnessPeriod=" +
65 boost::lexical_cast<std::string>(static_cast<int>(m_freshnessPeriod.count())) + ", ";
66 if (m_contentBytes >= 0)
67 detail += "ContentBytes=" + boost::lexical_cast<std::string>(m_contentBytes) + ", ";
68 if (m_contentDelay >= time::milliseconds(0))
69 detail += "ContentDelay=" +
70 boost::lexical_cast<std::string>(m_contentDelay.count()) + ", ";
71 if (m_content != "")
72 detail += "Content=" + m_content + ", ";
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080073 if (detail.length() >= 2)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070074 detail = detail.substr(0, detail.length() - 2);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080075 logger.log(detail, false, false);
76 }
77
78
79 bool
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070080 extractParameterValue(const std::string& detail,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070081 std::string& parameter,
82 std::string& value)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080083 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080084 std::string allowedCharacters = ":/+._-%";
85 parameter = "";
86 value = "";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070087 int i = 0;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080088 while (detail[i] != '=' && i < detail.length())
89 {
90 parameter += detail[i];
91 i++;
92 }
93 if (i == detail.length())
94 return false;
95 i++;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070096 while ((std::isalnum(detail[i]) ||
97 allowedCharacters.find(detail[i]) != std::string::npos) &&
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070098 i < detail.length())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080099 {
100 value += detail[i];
101 i++;
102 }
103 if(parameter == "" || value == "")
104 return false;
105 return true;
106 }
107
108 bool
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700109 processConfigurationDetail(const std::string& detail,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700110 Logger& logger,
111 int lineNumber)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800112 {
113 std::string parameter, value;
114 if (extractParameterValue(detail, parameter, value))
115 {
116 if (parameter == "Name")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700117 m_name = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800118 else if (parameter == "ContentType")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700119 m_contentType = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800120 else if (parameter == "FreshnessPeriod")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700121 m_freshnessPeriod = time::milliseconds(boost::lexical_cast<int>(value));
122 else if (parameter == "ContentDelay")
123 m_contentDelay = time::milliseconds(boost::lexical_cast<int>(value));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800124 else if (parameter == "ContentBytes")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700125 m_contentBytes = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800126 else if (parameter == "Content")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700127 m_content = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800128 else
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700129 logger.log("Line " + boost::lexical_cast<std::string>(lineNumber) +
130 " \t- Invalid Parameter='" + parameter + "'", false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800131 }
132 else
133 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700134 logger.log("Line " + boost::lexical_cast<std::string>(lineNumber) +
135 " \t- Improper Traffic Configuration Line - " + detail, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800136 return false;
137 }
138 return true;
139 }
140
141 bool
142 checkTrafficDetailCorrectness()
143 {
144 return true;
145 }
146
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700147 std::string m_name;
148 int m_contentType;
149 time::milliseconds m_freshnessPeriod;
150 int m_contentBytes;
151 time::milliseconds m_contentDelay;
152 std::string m_content;
153 int m_nInterestsReceived;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800154
155 };
156
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800157 void
158 usage()
159 {
160
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700161 std::cout << "\nUsage: " << m_programName << " [options] <Traffic_Configuration_File>\n"
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800162 "Respond to Interest as per provided Traffic Configuration File\n"
163 "Multiple Prefixes can be configured for handling.\n"
164 "Set environment variable NDN_TRAFFIC_LOGFOLDER for redirecting output to a log.\n"
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700165 " [-d interval] - set delay before responding to interest in milliseconds\n"
166 " [-c count] - specify maximum number of interests to be satisfied\n"
jeraldabraham420dbf02014-04-25 22:58:31 -0700167 " [-q] - quiet logging - no interest reception/data generation messages\n"
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700168 " [-h] - print help and exit\n\n";
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800169 exit(1);
170
171 }
172
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700173 void
174 setMaximumInterests(int maximumInterests)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800175 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700176 if (maximumInterests < 0)
177 usage();
178 m_nMaximumInterests = maximumInterests;
179 }
180
181 bool
182 hasError() const
183 {
184 return m_hasError;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800185 }
186
187 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700188 setContentDelay(int contentDelay)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800189 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700190 if (contentDelay < 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800191 usage();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700192 m_contentDelay = time::milliseconds(contentDelay);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800193 }
194
195 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700196 setConfigurationFile(char* configurationFile)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800197 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700198 m_configurationFile = configurationFile;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800199 }
200
201 void
jeraldabraham420dbf02014-04-25 22:58:31 -0700202 setQuietLogging()
203 {
204 m_hasQuietLogging = true;
205 }
206
207 void
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800208 signalHandler()
209 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800210 logStatistics();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700211 m_logger.shutdownLogger();
212 m_face.shutdown();
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300213 m_ioService.stop();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700214 if (m_hasError)
215 exit(1);
216 else
217 exit(0);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800218 }
219
220 void
221 logStatistics()
222 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800223 m_logger.log("\n\n== Interest Traffic Report ==\n", false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700224 m_logger.log("Total Traffic Pattern Types = " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700225 boost::lexical_cast<std::string>(m_trafficPatterns.size()), false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700226 m_logger.log("Total Interests Received = " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700227 boost::lexical_cast<std::string>(m_nInterestsReceived), false, true);
228 if (m_nInterestsReceived < m_nMaximumInterests)
229 m_hasError = true;
230 for (int patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800231 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700232 m_logger.log("\nTraffic Pattern Type #" +
233 boost::lexical_cast<std::string>(patternId + 1), false, true);
234 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700235 m_logger.log("Total Interests Received = " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700236 boost::lexical_cast<std::string>(
237 m_trafficPatterns[patternId].m_nInterestsReceived) + "\n", false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800238 }
239 }
240
241 bool
242 checkTrafficPatternCorrectness()
243 {
244 return true;
245 }
246
247 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700248 parseConfigurationFile()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800249 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800250 std::string patternLine;
251 std::ifstream patternFile;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700252 m_logger.log("Analyzing Traffic Configuration File: " + m_configurationFile, true, true);
253 patternFile.open(m_configurationFile.c_str());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800254 if (patternFile.is_open())
255 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700256 int patternId = 0;
257 int lineNumber = 0;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800258 while (getline(patternFile, patternLine))
259 {
260 lineNumber++;
261 if (std::isalpha(patternLine[0]))
262 {
263 DataTrafficConfiguration dataData;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700264 bool shouldSkipLine = false;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800265 patternId++;
266 if (dataData.processConfigurationDetail(patternLine, m_logger, lineNumber))
267 {
268 while (getline(patternFile, patternLine) && std::isalpha(patternLine[0]))
269 {
270 lineNumber++;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700271 if (!dataData.processConfigurationDetail(patternLine,
272 m_logger,
273 lineNumber))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800274 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700275 shouldSkipLine = true;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800276 break;
277 }
278 }
279 lineNumber++;
280 }
281 else
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700282 shouldSkipLine = true;
283 if (!shouldSkipLine)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800284 {
285 if (dataData.checkTrafficDetailCorrectness())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700286 m_trafficPatterns.push_back(dataData);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800287 }
288 }
289 }
290 patternFile.close();
291 if (!checkTrafficPatternCorrectness())
292 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700293 m_logger.log("ERROR - Traffic Configuration Provided Is Not Proper- " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700294 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800295 m_logger.shutdownLogger();
296 exit(1);
297 }
298 m_logger.log("Traffic Configuration File Processing Completed\n", true, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700299 for (patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800300 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700301 m_logger.log("Traffic Pattern Type #" +
302 boost::lexical_cast<std::string>(patternId + 1), false, false);
303 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800304 m_logger.log("", false, false);
305 }
306 }
307 else
308 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700309 m_logger.log("ERROR - Unable To Open Traffic Configuration File: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700310 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800311 m_logger.shutdownLogger();
312 exit(1);
313 }
314 }
315
316 void
317 initializeTrafficConfiguration()
318 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700319 if (boost::filesystem::exists(boost::filesystem::path(m_configurationFile)))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800320 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700321 if (boost::filesystem::is_regular_file(boost::filesystem::path(m_configurationFile)))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800322 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700323 parseConfigurationFile();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800324 }
325 else
326 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700327 m_logger.log("ERROR - Traffic Configuration File Is Not A Regular File: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700328 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800329 m_logger.shutdownLogger();
330 exit(1);
331 }
332 }
333 else
334 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700335 m_logger.log("ERROR - Traffic Configuration File Does Not Exist: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700336 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800337 m_logger.shutdownLogger();
338 exit(1);
339 }
340 }
341
342 static std::string
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700343 getRandomByteString(int randomSize)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800344 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700345 std::string randomString;
346 for (int i = 0; i < randomSize; i++)
347 randomString += static_cast<char>(std::rand() % 128);
348 return randomString;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800349 }
350
351 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700352 onInterest(const Name& name, const Interest& interest, int patternId)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800353 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700354 if (m_nMaximumInterests < 0 || m_nInterestsReceived < m_nMaximumInterests)
355 {
356 Data data(interest.getName());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800357
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700358 if (m_trafficPatterns[patternId].m_contentType >= 0)
359 data.setContentType(m_trafficPatterns[patternId].m_contentType);
360
361 if (m_trafficPatterns[patternId].m_freshnessPeriod >= time::milliseconds(0))
362 data.setFreshnessPeriod(m_trafficPatterns[patternId].m_freshnessPeriod);
363
364 std::string content = "";
365 if (m_trafficPatterns[patternId].m_contentBytes >= 0)
366 content = getRandomByteString(m_trafficPatterns[patternId].m_contentBytes);
367 if (m_trafficPatterns[patternId].m_content != "")
368 content = m_trafficPatterns[patternId].m_content;
369
370 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.length());
371 m_keyChain.sign(data);
372 m_nInterestsReceived++;
373 m_trafficPatterns[patternId].m_nInterestsReceived++;
374 std::string logLine = "Interest Received - PatternType=" +
375 boost::lexical_cast<std::string>(patternId + 1);
376 logLine += ", GlobalID=" + boost::lexical_cast<std::string>(m_nInterestsReceived);
377 logLine += ", LocalID=" +
378 boost::lexical_cast<std::string>(m_trafficPatterns[patternId].m_nInterestsReceived);
379 logLine += ", Name=" + m_trafficPatterns[patternId].m_name;
jeraldabraham420dbf02014-04-25 22:58:31 -0700380 if (!m_hasQuietLogging)
381 m_logger.log(logLine, true, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700382 if (m_trafficPatterns[patternId].m_contentDelay > time::milliseconds(-1))
383 usleep(m_trafficPatterns[patternId].m_contentDelay.count() * 1000);
384 if (m_contentDelay > time::milliseconds(-1))
385 usleep(m_contentDelay.count() * 1000);
386 m_face.put(data);
387 }
388 if (m_nMaximumInterests >= 0 && m_nInterestsReceived == m_nMaximumInterests)
389 {
390 logStatistics();
391 m_logger.shutdownLogger();
392 m_face.shutdown();
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300393 m_ioService.stop();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700394 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800395 }
396
397 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700398 onRegisterFailed(const ndn::Name& prefix, const std::string& reason, int patternId)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800399 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700400 std::string logLine = "";
401 logLine += "Prefix Registration Failed - PatternType=" +
402 boost::lexical_cast<std::string>(patternId + 1);
403 logLine += ", Name=" + m_trafficPatterns[patternId].m_name;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800404 m_logger.log(logLine, true, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700405 m_nRegistrationsFailed++;
406 if (m_nRegistrationsFailed == m_trafficPatterns.size())
407 {
408 m_hasError = true;
409 signalHandler();
410 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800411 }
412
413 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700414 run()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800415 {
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300416 boost::asio::signal_set signalSet(m_ioService, SIGINT, SIGTERM);
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700417 signalSet.async_wait(bind(&NdnTrafficServer::signalHandler, this));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700418 m_logger.initializeLog(m_instanceId);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800419 initializeTrafficConfiguration();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700420 if (m_nMaximumInterests == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800421 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700422 logStatistics();
423 m_logger.shutdownLogger();
424 return;
425 }
426
427 for (int patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
428 {
429 m_face.setInterestFilter(m_trafficPatterns[patternId].m_name,
430 bind(&NdnTrafficServer::onInterest,
431 this, _1, _2,
432 patternId),
433 bind(&NdnTrafficServer::onRegisterFailed,
434 this, _1, _2,
435 patternId));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800436 }
437
438 try {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700439 m_face.processEvents();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800440 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700441 catch (std::exception& e) {
442 m_logger.log("ERROR: " + static_cast<std::string>(e.what()), true, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800443 m_logger.shutdownLogger();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700444 m_hasError = true;
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300445 m_ioService.stop();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800446 }
447 }
448
449private:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700450 KeyChain m_keyChain;
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700451 Logger m_logger;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700452 std::string m_programName;
453 bool m_hasError;
jeraldabraham420dbf02014-04-25 22:58:31 -0700454 bool m_hasQuietLogging;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700455 int m_nRegistrationsFailed;
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700456 int m_nMaximumInterests;
457 int m_nInterestsReceived;
458 time::milliseconds m_contentDelay;
459 std::string m_instanceId;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700460 std::string m_configurationFile;
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700461
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300462 boost::asio::io_service m_ioService;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700463 Face m_face;
464 std::vector<DataTrafficConfiguration> m_trafficPatterns;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800465};
466
467} // namespace ndn
468
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700469int
470main(int argc, char* argv[])
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800471{
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700472 std::srand(std::time(0));
473 ndn::NdnTrafficServer ndnTrafficServer(argv[0]);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800474 int option;
jeraldabraham420dbf02014-04-25 22:58:31 -0700475 while ((option = getopt(argc, argv, "hqc:d:")) != -1) {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800476 switch (option) {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700477 case 'h':
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800478 ndnTrafficServer.usage();
479 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700480 case 'c':
481 ndnTrafficServer.setMaximumInterests(atoi(optarg));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800482 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700483 case 'd':
484 ndnTrafficServer.setContentDelay(atoi(optarg));
485 break;
jeraldabraham420dbf02014-04-25 22:58:31 -0700486 case 'q':
487 ndnTrafficServer.setQuietLogging();
488 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700489 default:
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800490 ndnTrafficServer.usage();
491 break;
492 }
493 }
494
495 argc -= optind;
496 argv += optind;
497
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700498 if (argv[0] == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800499 ndnTrafficServer.usage();
500
501 ndnTrafficServer.setConfigurationFile(argv[0]);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700502 ndnTrafficServer.run();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800503
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700504 if (ndnTrafficServer.hasError())
505 return 1;
506 else
507 return 0;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800508}