blob: ff56fe0a15a0f7434d77705ae7ff310528ca6264 [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>
11#include <boost/asio.hpp>
12#include <boost/filesystem.hpp>
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070013#include <boost/lexical_cast.hpp>
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080014
15#include <ndn-cpp-dev/face.hpp>
16#include <ndn-cpp-dev/security/key-chain.hpp>
17
18#include "logger.hpp"
19
20namespace ndn {
21
22class NdnTrafficServer
23{
24public:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070025
26 explicit
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070027 NdnTrafficServer(char* programName)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070028 : m_logger("NdnTrafficServer")
29 , m_programName(programName)
30 , m_hasError(false)
31 , m_nRegistrationsFailed(0)
32 , m_nMaximumInterests(-1)
33 , m_nInterestsReceived(0)
34 , m_contentDelay(time::milliseconds(-1))
35 , m_ioService(new boost::asio::io_service)
36 , m_face(m_ioService)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080037 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070038 m_instanceId = boost::lexical_cast<std::string>(std::rand());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080039 }
40
41 class DataTrafficConfiguration
42 {
43 public:
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080044 DataTrafficConfiguration()
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070045 : m_contentType(-1)
46 , m_freshnessPeriod(time::milliseconds(-1))
47 , m_contentBytes(-1)
48 , m_contentDelay(time::milliseconds(-1))
49 , m_nInterestsReceived(0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080050 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080051 }
52
53 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070054 printTrafficConfiguration(Logger& logger)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080055 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070056 std::string detail = "";
57 if (m_name != "")
58 detail += "Name=" + m_name + ", ";
59 if (m_contentType >= 0)
60 detail += "ContentType=" + boost::lexical_cast<std::string>(m_contentType) + ", ";
61 if (m_freshnessPeriod >= time::milliseconds(0))
62 detail += "FreshnessPeriod=" +
63 boost::lexical_cast<std::string>(static_cast<int>(m_freshnessPeriod.count())) + ", ";
64 if (m_contentBytes >= 0)
65 detail += "ContentBytes=" + boost::lexical_cast<std::string>(m_contentBytes) + ", ";
66 if (m_contentDelay >= time::milliseconds(0))
67 detail += "ContentDelay=" +
68 boost::lexical_cast<std::string>(m_contentDelay.count()) + ", ";
69 if (m_content != "")
70 detail += "Content=" + m_content + ", ";
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080071 if (detail.length() >= 2)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070072 detail = detail.substr(0, detail.length() - 2);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080073 logger.log(detail, false, false);
74 }
75
76
77 bool
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070078 extractParameterValue(const std::string& detail,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070079 std::string& parameter,
80 std::string& value)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080081 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080082 std::string allowedCharacters = ":/+._-%";
83 parameter = "";
84 value = "";
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070085 int i = 0;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080086 while (detail[i] != '=' && i < detail.length())
87 {
88 parameter += detail[i];
89 i++;
90 }
91 if (i == detail.length())
92 return false;
93 i++;
Alexander Afanasyevfda32a32014-03-20 10:50:00 -070094 while ((std::isalnum(detail[i]) ||
95 allowedCharacters.find(detail[i]) != std::string::npos) &&
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070096 i < detail.length())
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080097 {
98 value += detail[i];
99 i++;
100 }
101 if(parameter == "" || value == "")
102 return false;
103 return true;
104 }
105
106 bool
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700107 processConfigurationDetail(const std::string& detail,
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700108 Logger& logger,
109 int lineNumber)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800110 {
111 std::string parameter, value;
112 if (extractParameterValue(detail, parameter, value))
113 {
114 if (parameter == "Name")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700115 m_name = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800116 else if (parameter == "ContentType")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700117 m_contentType = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800118 else if (parameter == "FreshnessPeriod")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700119 m_freshnessPeriod = time::milliseconds(boost::lexical_cast<int>(value));
120 else if (parameter == "ContentDelay")
121 m_contentDelay = time::milliseconds(boost::lexical_cast<int>(value));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800122 else if (parameter == "ContentBytes")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700123 m_contentBytes = boost::lexical_cast<int>(value);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800124 else if (parameter == "Content")
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700125 m_content = value;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800126 else
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700127 logger.log("Line " + boost::lexical_cast<std::string>(lineNumber) +
128 " \t- Invalid Parameter='" + parameter + "'", false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800129 }
130 else
131 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700132 logger.log("Line " + boost::lexical_cast<std::string>(lineNumber) +
133 " \t- Improper Traffic Configuration Line - " + detail, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800134 return false;
135 }
136 return true;
137 }
138
139 bool
140 checkTrafficDetailCorrectness()
141 {
142 return true;
143 }
144
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700145 std::string m_name;
146 int m_contentType;
147 time::milliseconds m_freshnessPeriod;
148 int m_contentBytes;
149 time::milliseconds m_contentDelay;
150 std::string m_content;
151 int m_nInterestsReceived;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800152
153 };
154
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800155 void
156 usage()
157 {
158
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700159 std::cout << "\nUsage: " << m_programName << " [options] <Traffic_Configuration_File>\n"
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800160 "Respond to Interest as per provided Traffic Configuration File\n"
161 "Multiple Prefixes can be configured for handling.\n"
162 "Set environment variable NDN_TRAFFIC_LOGFOLDER for redirecting output to a log.\n"
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700163 " [-d interval] - set delay before responding to interest in milliseconds\n"
164 " [-c count] - specify maximum number of interests to be satisfied\n"
165 " [-h] - print help and exit\n\n";
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800166 exit(1);
167
168 }
169
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700170 void
171 setMaximumInterests(int maximumInterests)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800172 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700173 if (maximumInterests < 0)
174 usage();
175 m_nMaximumInterests = maximumInterests;
176 }
177
178 bool
179 hasError() const
180 {
181 return m_hasError;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800182 }
183
184 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700185 setContentDelay(int contentDelay)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800186 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700187 if (contentDelay < 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800188 usage();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700189 m_contentDelay = time::milliseconds(contentDelay);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800190 }
191
192 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700193 setConfigurationFile(char* configurationFile)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800194 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700195 m_configurationFile = configurationFile;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800196 }
197
198 void
199 signalHandler()
200 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800201 logStatistics();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700202 m_logger.shutdownLogger();
203 m_face.shutdown();
204 m_ioService->stop();
205 if (m_hasError)
206 exit(1);
207 else
208 exit(0);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800209 }
210
211 void
212 logStatistics()
213 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800214 m_logger.log("\n\n== Interest Traffic Report ==\n", false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700215 m_logger.log("Total Traffic Pattern Types = " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700216 boost::lexical_cast<std::string>(m_trafficPatterns.size()), false, true);
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700217 m_logger.log("Total Interests Received = " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700218 boost::lexical_cast<std::string>(m_nInterestsReceived), false, true);
219 if (m_nInterestsReceived < m_nMaximumInterests)
220 m_hasError = true;
221 for (int patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800222 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700223 m_logger.log("\nTraffic Pattern Type #" +
224 boost::lexical_cast<std::string>(patternId + 1), false, true);
225 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
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>(
228 m_trafficPatterns[patternId].m_nInterestsReceived) + "\n", false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800229 }
230 }
231
232 bool
233 checkTrafficPatternCorrectness()
234 {
235 return true;
236 }
237
238 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700239 parseConfigurationFile()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800240 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800241 std::string patternLine;
242 std::ifstream patternFile;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700243 m_logger.log("Analyzing Traffic Configuration File: " + m_configurationFile, true, true);
244 patternFile.open(m_configurationFile.c_str());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800245 if (patternFile.is_open())
246 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700247 int patternId = 0;
248 int lineNumber = 0;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800249 while (getline(patternFile, patternLine))
250 {
251 lineNumber++;
252 if (std::isalpha(patternLine[0]))
253 {
254 DataTrafficConfiguration dataData;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700255 bool shouldSkipLine = false;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800256 patternId++;
257 if (dataData.processConfigurationDetail(patternLine, m_logger, lineNumber))
258 {
259 while (getline(patternFile, patternLine) && std::isalpha(patternLine[0]))
260 {
261 lineNumber++;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700262 if (!dataData.processConfigurationDetail(patternLine,
263 m_logger,
264 lineNumber))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800265 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700266 shouldSkipLine = true;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800267 break;
268 }
269 }
270 lineNumber++;
271 }
272 else
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700273 shouldSkipLine = true;
274 if (!shouldSkipLine)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800275 {
276 if (dataData.checkTrafficDetailCorrectness())
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700277 m_trafficPatterns.push_back(dataData);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800278 }
279 }
280 }
281 patternFile.close();
282 if (!checkTrafficPatternCorrectness())
283 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700284 m_logger.log("ERROR - Traffic Configuration Provided Is Not Proper- " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700285 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800286 m_logger.shutdownLogger();
287 exit(1);
288 }
289 m_logger.log("Traffic Configuration File Processing Completed\n", true, false);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700290 for (patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800291 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700292 m_logger.log("Traffic Pattern Type #" +
293 boost::lexical_cast<std::string>(patternId + 1), false, false);
294 m_trafficPatterns[patternId].printTrafficConfiguration(m_logger);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800295 m_logger.log("", false, false);
296 }
297 }
298 else
299 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700300 m_logger.log("ERROR - Unable To Open Traffic Configuration File: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700301 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800302 m_logger.shutdownLogger();
303 exit(1);
304 }
305 }
306
307 void
308 initializeTrafficConfiguration()
309 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700310 if (boost::filesystem::exists(boost::filesystem::path(m_configurationFile)))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800311 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700312 if (boost::filesystem::is_regular_file(boost::filesystem::path(m_configurationFile)))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800313 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700314 parseConfigurationFile();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800315 }
316 else
317 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700318 m_logger.log("ERROR - Traffic Configuration File Is Not A Regular File: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700319 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800320 m_logger.shutdownLogger();
321 exit(1);
322 }
323 }
324 else
325 {
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700326 m_logger.log("ERROR - Traffic Configuration File Does Not Exist: " +
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700327 m_configurationFile, false, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800328 m_logger.shutdownLogger();
329 exit(1);
330 }
331 }
332
333 static std::string
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700334 getRandomByteString(int randomSize)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800335 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700336 std::string randomString;
337 for (int i = 0; i < randomSize; i++)
338 randomString += static_cast<char>(std::rand() % 128);
339 return randomString;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800340 }
341
342 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700343 onInterest(const Name& name, const Interest& interest, int patternId)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800344 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700345 if (m_nMaximumInterests < 0 || m_nInterestsReceived < m_nMaximumInterests)
346 {
347 Data data(interest.getName());
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800348
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700349 if (m_trafficPatterns[patternId].m_contentType >= 0)
350 data.setContentType(m_trafficPatterns[patternId].m_contentType);
351
352 if (m_trafficPatterns[patternId].m_freshnessPeriod >= time::milliseconds(0))
353 data.setFreshnessPeriod(m_trafficPatterns[patternId].m_freshnessPeriod);
354
355 std::string content = "";
356 if (m_trafficPatterns[patternId].m_contentBytes >= 0)
357 content = getRandomByteString(m_trafficPatterns[patternId].m_contentBytes);
358 if (m_trafficPatterns[patternId].m_content != "")
359 content = m_trafficPatterns[patternId].m_content;
360
361 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.length());
362 m_keyChain.sign(data);
363 m_nInterestsReceived++;
364 m_trafficPatterns[patternId].m_nInterestsReceived++;
365 std::string logLine = "Interest Received - PatternType=" +
366 boost::lexical_cast<std::string>(patternId + 1);
367 logLine += ", GlobalID=" + boost::lexical_cast<std::string>(m_nInterestsReceived);
368 logLine += ", LocalID=" +
369 boost::lexical_cast<std::string>(m_trafficPatterns[patternId].m_nInterestsReceived);
370 logLine += ", Name=" + m_trafficPatterns[patternId].m_name;
371 m_logger.log(logLine, true, false);
372 if (m_trafficPatterns[patternId].m_contentDelay > time::milliseconds(-1))
373 usleep(m_trafficPatterns[patternId].m_contentDelay.count() * 1000);
374 if (m_contentDelay > time::milliseconds(-1))
375 usleep(m_contentDelay.count() * 1000);
376 m_face.put(data);
377 }
378 if (m_nMaximumInterests >= 0 && m_nInterestsReceived == m_nMaximumInterests)
379 {
380 logStatistics();
381 m_logger.shutdownLogger();
382 m_face.shutdown();
383 m_ioService->stop();
384 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800385 }
386
387 void
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700388 onRegisterFailed(const ndn::Name& prefix, const std::string& reason, int patternId)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800389 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700390 std::string logLine = "";
391 logLine += "Prefix Registration Failed - PatternType=" +
392 boost::lexical_cast<std::string>(patternId + 1);
393 logLine += ", Name=" + m_trafficPatterns[patternId].m_name;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800394 m_logger.log(logLine, true, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700395 m_nRegistrationsFailed++;
396 if (m_nRegistrationsFailed == m_trafficPatterns.size())
397 {
398 m_hasError = true;
399 signalHandler();
400 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800401 }
402
403 void
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700404 run()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800405 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700406 boost::asio::signal_set signalSet(*m_ioService, SIGINT, SIGTERM);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800407 signalSet.async_wait(boost::bind(&NdnTrafficServer::signalHandler, this));
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700408 m_logger.initializeLog(m_instanceId);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800409 initializeTrafficConfiguration();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700410 if (m_nMaximumInterests == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800411 {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700412 logStatistics();
413 m_logger.shutdownLogger();
414 return;
415 }
416
417 for (int patternId = 0; patternId < m_trafficPatterns.size(); patternId++)
418 {
419 m_face.setInterestFilter(m_trafficPatterns[patternId].m_name,
420 bind(&NdnTrafficServer::onInterest,
421 this, _1, _2,
422 patternId),
423 bind(&NdnTrafficServer::onRegisterFailed,
424 this, _1, _2,
425 patternId));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800426 }
427
428 try {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700429 m_face.processEvents();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800430 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700431 catch (std::exception& e) {
432 m_logger.log("ERROR: " + static_cast<std::string>(e.what()), true, true);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800433 m_logger.shutdownLogger();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700434 m_hasError = true;
435 m_ioService->stop();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800436 }
437 }
438
439private:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700440 KeyChain m_keyChain;
441 std::string m_programName;
442 bool m_hasError;
443 std::string m_instanceId;
444 time::milliseconds m_contentDelay;
445 int m_nRegistrationsFailed;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800446 Logger m_logger;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700447 std::string m_configurationFile;
448 shared_ptr<boost::asio::io_service> m_ioService;
449 Face m_face;
450 std::vector<DataTrafficConfiguration> m_trafficPatterns;
451 int m_nMaximumInterests;
452 int m_nInterestsReceived;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800453};
454
455} // namespace ndn
456
Alexander Afanasyevfda32a32014-03-20 10:50:00 -0700457int
458main(int argc, char* argv[])
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800459{
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700460 std::srand(std::time(0));
461 ndn::NdnTrafficServer ndnTrafficServer(argv[0]);
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800462 int option;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700463 while ((option = getopt(argc, argv, "hc:d:")) != -1) {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800464 switch (option) {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700465 case 'h':
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800466 ndnTrafficServer.usage();
467 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700468 case 'c':
469 ndnTrafficServer.setMaximumInterests(atoi(optarg));
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800470 break;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700471 case 'd':
472 ndnTrafficServer.setContentDelay(atoi(optarg));
473 break;
474 default:
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800475 ndnTrafficServer.usage();
476 break;
477 }
478 }
479
480 argc -= optind;
481 argv += optind;
482
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700483 if (argv[0] == 0)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800484 ndnTrafficServer.usage();
485
486 ndnTrafficServer.setConfigurationFile(argv[0]);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700487 ndnTrafficServer.run();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800488
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700489 if (ndnTrafficServer.hasError())
490 return 1;
491 else
492 return 0;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800493}