blob: a6d8cf573066f0b97db683c637c693e14a383b62 [file] [log] [blame]
jeraldabrahamf9543a42014-02-11 06:37:34 -07001/**
2 *
jeraldabrahame891ac92014-02-16 11:04:01 -07003 * Copyright (C) 2014 University of Arizona.
jeraldabrahamf9543a42014-02-11 06:37:34 -07004 * @author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
5 *
6 */
7
jeraldabrahame891ac92014-02-16 11:04:01 -07008#include <sstream>
9#include <boost/asio.hpp>
10#include <boost/filesystem.hpp>
11
12#include <ndn-cpp-dev/face.hpp>
jeraldabrahamf9543a42014-02-11 06:37:34 -070013#include <ndn-cpp-dev/security/key-chain.hpp>
14
15using namespace ndn;
16
17class NdnTrafficServer
18{
19public:
20
21 NdnTrafficServer( char* programName )
22 {
jeraldabrahame891ac92014-02-16 11:04:01 -070023 std::stringstream randomId;
24 std::srand(std::time(0));
25 randomId << std::rand();
26 instanceId_ = randomId.str();
jeraldabrahamf9543a42014-02-11 06:37:34 -070027 programName_ = programName;
28 contentDelayTime_ = getDefaultContentDelayTime();
jeraldabrahame891ac92014-02-16 11:04:01 -070029 logLocation_ = "";
jeraldabrahamf9543a42014-02-11 06:37:34 -070030 configurationFile_ = "";
jeraldabrahame891ac92014-02-16 11:04:01 -070031 ioService_ = ptr_lib::make_shared<boost::asio::io_service>();
32 face_ = Face(ioService_);
jeraldabrahamf9543a42014-02-11 06:37:34 -070033 }
34
35 NdnTrafficServer()
36 : keyChain_()
37 {
38 }
39
40 void
41 usage()
42 {
43 std::cout << "\nUsage: " << programName_ << " Printing Usage"
44 "\n\n";
45 exit(1);
46 }
47
48 int
49 getDefaultContentDelayTime()
50 {
51 return 0;
52 }
53
jeraldabrahamf9543a42014-02-11 06:37:34 -070054 void
55 setContentDelayTime( int contentDelayTime )
56 {
57 if (contentDelayTime < 0)
58 usage();
59 contentDelayTime_ = contentDelayTime;
60 }
61
62 void
jeraldabrahamf9543a42014-02-11 06:37:34 -070063 setConfigurationFile( char* configurationFile )
64 {
65 configurationFile_ = configurationFile;
66 }
67
jeraldabrahame891ac92014-02-16 11:04:01 -070068 void
69 signalHandler()
70 {
71 face_.shutdown();
72 ioService_.reset();
73 exit(1);
74 }
75
76 void
77 initializeLog()
78 {
79 char* variableValue = std::getenv("NDN_TRAFFIC_LOGFOLDER");
80 if (variableValue != NULL)
81 logLocation_ = variableValue;
82
83 if (boost::filesystem::exists(boost::filesystem::path(logLocation_)))
84 {
85 if (boost::filesystem::is_directory(boost::filesystem::path(logLocation_)))
86 {
87 logLocation_ += "/NDNTrafficServer_"+instanceId_+".log";
88 std::cout << "Log File Initialized: " << logLocation_ << std::endl;
89 }
90 else
91 {
92 std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Should Be A Folder.\n"
93 "Using Default Output For Logging." << std::endl;
94 logLocation_ = "";
95 }
96 }
97 else
98 {
99 std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Not Set.\n"
100 "Using Default Output For Logging." << std::endl;
101 logLocation_ = "";
102 }
103 }
104
105 void
106 initializeTrafficConfiguration()
107 {
108 std::cout << "Traffic Configuration File: " << configurationFile_ << std::endl;
109 }
110
111 void
112 initialize()
113 {
114 boost::asio::signal_set signalSet(*ioService_, SIGINT, SIGTERM);
115 signalSet.async_wait(boost::bind(&NdnTrafficServer::signalHandler, this));
116 initializeLog();
117 initializeTrafficConfiguration();
118 }
119
jeraldabrahamf9543a42014-02-11 06:37:34 -0700120private:
121
122 KeyChain keyChain_;
123 std::string programName_;
124 int contentDelayTime_;
jeraldabrahame891ac92014-02-16 11:04:01 -0700125 std::string logLocation_;
jeraldabrahamf9543a42014-02-11 06:37:34 -0700126 std::string configurationFile_;
jeraldabrahame891ac92014-02-16 11:04:01 -0700127 ptr_lib::shared_ptr<boost::asio::io_service> ioService_;
128 Face face_;
129 std::string instanceId_;
jeraldabrahamf9543a42014-02-11 06:37:34 -0700130
131};
132
133int main( int argc, char* argv[] )
134{
135 int option;
136 NdnTrafficServer ndnTrafficServer (argv[0]);
jeraldabrahame891ac92014-02-16 11:04:01 -0700137 while ((option = getopt(argc, argv, "hd:")) != -1) {
jeraldabrahamf9543a42014-02-11 06:37:34 -0700138 switch (option) {
139 case 'h' :
140 ndnTrafficServer.usage();
141 break;
142 case 'd' :
143 ndnTrafficServer.setContentDelayTime(atoi(optarg));
144 break;
jeraldabrahamf9543a42014-02-11 06:37:34 -0700145 default :
146 ndnTrafficServer.usage();
147 break;
148 }
149 }
150
151 argc -= optind;
152 argv += optind;
153
jeraldabrahame891ac92014-02-16 11:04:01 -0700154 if (argv[0] == NULL)
jeraldabrahamf9543a42014-02-11 06:37:34 -0700155 ndnTrafficServer.usage();
156
157 ndnTrafficServer.setConfigurationFile(argv[0]);
jeraldabrahame891ac92014-02-16 11:04:01 -0700158 ndnTrafficServer.initialize();
jeraldabrahamf9543a42014-02-11 06:37:34 -0700159
160 return 0;
161}