blob: 916b0e475cf99220d850e8a50d87f04c104b8fba [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))
38 , m_ioService(new boost::asio::io_service)
39 , m_face(m_ioService)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080040 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070041 m_instanceId = boost::lexical_cast<std::string>(std::rand());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080042 }
43
44 class DataTrafficConfiguration
45 {
46 public:
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080047 DataTrafficConfiguration()
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070048 : m_contentType(-1)
49 , m_freshnessPeriod(time::milliseconds(-1))
50 , m_contentBytes(-1)
51 , m_contentDelay(time::milliseconds(-1))
52 , m_nInterestsReceived(0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080053 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080054 }
55
56 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070057 printTrafficConfiguration(Logger& logger)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080058 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070059 std::string detail = "";
60 if (m_name != "")
61 detail += "Name=" + m_name + ", ";
62 if (m_contentType >= 0)
63 detail += "ContentType=" + boost::lexical_cast<std::string>(m_contentType) + ", ";
64 if (m_freshnessPeriod >= time::milliseconds(0))
65 detail += "FreshnessPeriod=" +
66 boost::lexical_cast<std::string>(static_cast<int>(m_freshnessPeriod.count())) + ", ";
67 if (m_contentBytes >= 0)
68 detail += "ContentBytes=" + boost::lexical_cast<std::string>(m_contentBytes) + ", ";
69 if (m_contentDelay >= time::milliseconds(0))
70 detail += "ContentDelay=" +
71 boost::lexical_cast<std::string>(m_contentDelay.count()) + ", ";
72 if (m_content != "")
73 detail += "Content=" + m_content + ", ";
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080074 if (detail.length() >= 2)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070075 detail = detail.substr(0, detail.length() - 2);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080076 logger.log(detail, false, false);
77 }
78
79
80 bool
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070081 extractParameterValue(const std::string& detail,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070082 std::string& parameter,
83 std::string& value)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080084 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080085 std::string allowedCharacters = ":/+._-%";
86 parameter = "";
87 value = "";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070088 int i = 0;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080089 while (detail[i] != '=' && i < detail.length())
90 {
91 parameter += detail[i];
92 i++;
93 }
94 if (i == detail.length())
95 return false;
96 i++;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070097 while ((std::isalnum(detail[i]) ||
98 allowedCharacters.find(detail[i]) != std::string::npos) &&
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070099 i < detail.length())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800100 {
101 value += detail[i];
102 i++;
103 }
104 if(parameter == "" || value == "")
105 return false;
106 return true;
107 }
108
109 bool
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700110 processConfigurationDetail(const std::string& detail,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700111 Logger& logger,
112 int lineNumber)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800113 {
114 std::string parameter, value;
115 if (extractParameterValue(detail, parameter, value))
116 {
117 if (parameter == "Name")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700118 m_name = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800119 else if (parameter == "ContentType")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700120 m_contentType = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800121 else if (parameter == "FreshnessPeriod")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700122 m_freshnessPeriod = time::milliseconds(boost::lexical_cast<int>(value));
123 else if (parameter == "ContentDelay")
124 m_contentDelay = time::milliseconds(boost::lexical_cast<int>(value));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800125 else if (parameter == "ContentBytes")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700126 m_contentBytes = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800127 else if (parameter == "Content")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700128 m_content = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800129 else
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700130 logger.log("Line " + boost::lexical_cast<std::string>(lineNumber) +
131 " \t- Invalid Parameter='" + parameter + "'", false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800132 }
133 else
134 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700135 logger.log("Line " + boost::lexical_cast<std::string>(lineNumber) +
136 " \t- Improper Traffic Configuration Line - " + detail, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800137 return false;
138 }
139 return true;
140 }
141
142 bool
143 checkTrafficDetailCorrectness()
144 {
145 return true;
146 }
147
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700148 std::string m_name;
149 int m_contentType;
150 time::milliseconds m_freshnessPeriod;
151 int m_contentBytes;
152 time::milliseconds m_contentDelay;
153 std::string m_content;
154 int m_nInterestsReceived;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800155
156 };
157
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800158 void
159 usage()
160 {
161
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700162 std::cout << "\nUsage: " << m_programName << " [options] <Traffic_Configuration_File>\n"
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800163 "Respond to Interest as per provided Traffic Configuration File\n"
164 "Multiple Prefixes can be configured for handling.\n"
165 "Set environment variable NDN_TRAFFIC_LOGFOLDER for redirecting output to a log.\n"
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700166 " [-d interval] - set delay before responding to interest in milliseconds\n"
167 " [-c count] - specify maximum number of interests to be satisfied\n"
jeraldabraham420dbf02014-04-25 22:58:31 -0700168 " [-q] - quiet logging - no interest reception/data generation messages\n"
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700169 " [-h] - print help and exit\n\n";
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800170 exit(1);
171
172 }
173
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700174 void
175 setMaximumInterests(int maximumInterests)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800176 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700177 if (maximumInterests < 0)
178 usage();
179 m_nMaximumInterests = maximumInterests;
180 }
181
182 bool
183 hasError() const
184 {
185 return m_hasError;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800186 }
187
188 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700189 setContentDelay(int contentDelay)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800190 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700191 if (contentDelay < 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800192 usage();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700193 m_contentDelay = time::milliseconds(contentDelay);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800194 }
195
196 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700197 setConfigurationFile(char* configurationFile)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800198 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700199 m_configurationFile = configurationFile;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800200 }
201
202 void
jeraldabraham420dbf02014-04-25 22:58:31 -0700203 setQuietLogging()
204 {
205 m_hasQuietLogging = true;
206 }
207
208 void
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800209 signalHandler()
210 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800211 logStatistics();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700212 m_logger.shutdownLogger();
213 m_face.shutdown();
214 m_ioService->stop();
215 if (m_hasError)
216 exit(1);
217 else
218 exit(0);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800219 }
220
221 void
222 logStatistics()
223 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800224 m_logger.log("\n\n== Interest Traffic Report ==\n", false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700225 m_logger.log("Total Traffic Pattern Types = " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700226 boost::lexical_cast<std::string>(m_trafficPatterns.size()), false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700227 m_logger.log("Total Interests Received = " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700228 boost::lexical_cast<std::string>(m_nInterestsReceived), false, true);
229 if (m_nInterestsReceived < m_nMaximumInterests)
230 m_hasError = true;
231 for (int patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800232 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700233 m_logger.log("\nTraffic Pattern Type #" +
234 boost::lexical_cast<std::string>(patternId + 1), false, true);
235 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700236 m_logger.log("Total Interests Received = " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700237 boost::lexical_cast<std::string>(
238 m_trafficPatterns[patternId].m_nInterestsReceived) + "\n", false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800239 }
240 }
241
242 bool
243 checkTrafficPatternCorrectness()
244 {
245 return true;
246 }
247
248 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700249 parseConfigurationFile()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800250 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800251 std::string patternLine;
252 std::ifstream patternFile;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700253 m_logger.log("Analyzing Traffic Configuration File: " + m_configurationFile, true, true);
254 patternFile.open(m_configurationFile.c_str());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800255 if (patternFile.is_open())
256 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700257 int patternId = 0;
258 int lineNumber = 0;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800259 while (getline(patternFile, patternLine))
260 {
261 lineNumber++;
262 if (std::isalpha(patternLine[0]))
263 {
264 DataTrafficConfiguration dataData;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700265 bool shouldSkipLine = false;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800266 patternId++;
267 if (dataData.processConfigurationDetail(patternLine, m_logger, lineNumber))
268 {
269 while (getline(patternFile, patternLine) && std::isalpha(patternLine[0]))
270 {
271 lineNumber++;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700272 if (!dataData.processConfigurationDetail(patternLine,
273 m_logger,
274 lineNumber))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800275 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700276 shouldSkipLine = true;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800277 break;
278 }
279 }
280 lineNumber++;
281 }
282 else
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700283 shouldSkipLine = true;
284 if (!shouldSkipLine)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800285 {
286 if (dataData.checkTrafficDetailCorrectness())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700287 m_trafficPatterns.push_back(dataData);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800288 }
289 }
290 }
291 patternFile.close();
292 if (!checkTrafficPatternCorrectness())
293 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700294 m_logger.log("ERROR - Traffic Configuration Provided Is Not Proper- " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700295 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800296 m_logger.shutdownLogger();
297 exit(1);
298 }
299 m_logger.log("Traffic Configuration File Processing Completed\n", true, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700300 for (patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800301 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700302 m_logger.log("Traffic Pattern Type #" +
303 boost::lexical_cast<std::string>(patternId + 1), false, false);
304 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800305 m_logger.log("", false, false);
306 }
307 }
308 else
309 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700310 m_logger.log("ERROR - Unable To Open Traffic Configuration File: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700311 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800312 m_logger.shutdownLogger();
313 exit(1);
314 }
315 }
316
317 void
318 initializeTrafficConfiguration()
319 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700320 if (boost::filesystem::exists(boost::filesystem::path(m_configurationFile)))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800321 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700322 if (boost::filesystem::is_regular_file(boost::filesystem::path(m_configurationFile)))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800323 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700324 parseConfigurationFile();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800325 }
326 else
327 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700328 m_logger.log("ERROR - Traffic Configuration File Is Not A Regular File: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700329 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800330 m_logger.shutdownLogger();
331 exit(1);
332 }
333 }
334 else
335 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700336 m_logger.log("ERROR - Traffic Configuration File Does Not Exist: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700337 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800338 m_logger.shutdownLogger();
339 exit(1);
340 }
341 }
342
343 static std::string
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700344 getRandomByteString(int randomSize)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800345 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700346 std::string randomString;
347 for (int i = 0; i < randomSize; i++)
348 randomString += static_cast<char>(std::rand() % 128);
349 return randomString;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800350 }
351
352 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700353 onInterest(const Name& name, const Interest& interest, int patternId)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800354 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700355 if (m_nMaximumInterests < 0 || m_nInterestsReceived < m_nMaximumInterests)
356 {
357 Data data(interest.getName());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800358
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700359 if (m_trafficPatterns[patternId].m_contentType >= 0)
360 data.setContentType(m_trafficPatterns[patternId].m_contentType);
361
362 if (m_trafficPatterns[patternId].m_freshnessPeriod >= time::milliseconds(0))
363 data.setFreshnessPeriod(m_trafficPatterns[patternId].m_freshnessPeriod);
364
365 std::string content = "";
366 if (m_trafficPatterns[patternId].m_contentBytes >= 0)
367 content = getRandomByteString(m_trafficPatterns[patternId].m_contentBytes);
368 if (m_trafficPatterns[patternId].m_content != "")
369 content = m_trafficPatterns[patternId].m_content;
370
371 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.length());
372 m_keyChain.sign(data);
373 m_nInterestsReceived++;
374 m_trafficPatterns[patternId].m_nInterestsReceived++;
375 std::string logLine = "Interest Received - PatternType=" +
376 boost::lexical_cast<std::string>(patternId + 1);
377 logLine += ", GlobalID=" + boost::lexical_cast<std::string>(m_nInterestsReceived);
378 logLine += ", LocalID=" +
379 boost::lexical_cast<std::string>(m_trafficPatterns[patternId].m_nInterestsReceived);
380 logLine += ", Name=" + m_trafficPatterns[patternId].m_name;
jeraldabraham420dbf02014-04-25 22:58:31 -0700381 if (!m_hasQuietLogging)
382 m_logger.log(logLine, true, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700383 if (m_trafficPatterns[patternId].m_contentDelay > time::milliseconds(-1))
384 usleep(m_trafficPatterns[patternId].m_contentDelay.count() * 1000);
385 if (m_contentDelay > time::milliseconds(-1))
386 usleep(m_contentDelay.count() * 1000);
387 m_face.put(data);
388 }
389 if (m_nMaximumInterests >= 0 && m_nInterestsReceived == m_nMaximumInterests)
390 {
391 logStatistics();
392 m_logger.shutdownLogger();
393 m_face.shutdown();
394 m_ioService->stop();
395 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800396 }
397
398 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700399 onRegisterFailed(const ndn::Name& prefix, const std::string& reason, int patternId)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800400 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700401 std::string logLine = "";
402 logLine += "Prefix Registration Failed - PatternType=" +
403 boost::lexical_cast<std::string>(patternId + 1);
404 logLine += ", Name=" + m_trafficPatterns[patternId].m_name;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800405 m_logger.log(logLine, true, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700406 m_nRegistrationsFailed++;
407 if (m_nRegistrationsFailed == m_trafficPatterns.size())
408 {
409 m_hasError = true;
410 signalHandler();
411 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800412 }
413
414 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700415 run()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800416 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700417 boost::asio::signal_set signalSet(*m_ioService, SIGINT, SIGTERM);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800418 signalSet.async_wait(boost::bind(&NdnTrafficServer::signalHandler, this));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700419 m_logger.initializeLog(m_instanceId);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800420 initializeTrafficConfiguration();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700421 if (m_nMaximumInterests == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800422 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700423 logStatistics();
424 m_logger.shutdownLogger();
425 return;
426 }
427
428 for (int patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
429 {
430 m_face.setInterestFilter(m_trafficPatterns[patternId].m_name,
431 bind(&NdnTrafficServer::onInterest,
432 this, _1, _2,
433 patternId),
434 bind(&NdnTrafficServer::onRegisterFailed,
435 this, _1, _2,
436 patternId));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800437 }
438
439 try {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700440 m_face.processEvents();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800441 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700442 catch (std::exception& e) {
443 m_logger.log("ERROR: " + static_cast<std::string>(e.what()), true, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800444 m_logger.shutdownLogger();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700445 m_hasError = true;
446 m_ioService->stop();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800447 }
448 }
449
450private:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700451 KeyChain m_keyChain;
452 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 std::string m_instanceId;
456 time::milliseconds m_contentDelay;
457 int m_nRegistrationsFailed;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800458 Logger m_logger;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700459 std::string m_configurationFile;
460 shared_ptr<boost::asio::io_service> m_ioService;
461 Face m_face;
462 std::vector<DataTrafficConfiguration> m_trafficPatterns;
463 int m_nMaximumInterests;
464 int m_nInterestsReceived;
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}