blob: 4b1251049ece734c2fc026c70d44ce0930fac09c [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
jeraldabraham662266d2014-02-17 10:26:12 -070021 NdnTrafficServer( char* programName ) : ioService_(new boost::asio::io_service), face_(ioService_)
jeraldabrahamf9543a42014-02-11 06:37:34 -070022 {
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_ = "";
31 }
32
33 NdnTrafficServer()
34 : keyChain_()
35 {
36 }
37
38 void
39 usage()
40 {
41 std::cout << "\nUsage: " << programName_ << " Printing Usage"
42 "\n\n";
43 exit(1);
44 }
45
46 int
47 getDefaultContentDelayTime()
48 {
49 return 0;
50 }
51
jeraldabrahamf9543a42014-02-11 06:37:34 -070052 void
53 setContentDelayTime( int contentDelayTime )
54 {
55 if (contentDelayTime < 0)
56 usage();
57 contentDelayTime_ = contentDelayTime;
58 }
59
60 void
jeraldabrahamf9543a42014-02-11 06:37:34 -070061 setConfigurationFile( char* configurationFile )
62 {
63 configurationFile_ = configurationFile;
64 }
65
jeraldabrahame891ac92014-02-16 11:04:01 -070066 void
67 signalHandler()
68 {
69 face_.shutdown();
70 ioService_.reset();
71 exit(1);
72 }
73
74 void
75 initializeLog()
76 {
77 char* variableValue = std::getenv("NDN_TRAFFIC_LOGFOLDER");
78 if (variableValue != NULL)
79 logLocation_ = variableValue;
80
81 if (boost::filesystem::exists(boost::filesystem::path(logLocation_)))
82 {
83 if (boost::filesystem::is_directory(boost::filesystem::path(logLocation_)))
84 {
85 logLocation_ += "/NDNTrafficServer_"+instanceId_+".log";
86 std::cout << "Log File Initialized: " << logLocation_ << std::endl;
87 }
88 else
89 {
90 std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Should Be A Folder.\n"
91 "Using Default Output For Logging." << std::endl;
92 logLocation_ = "";
93 }
94 }
95 else
96 {
97 std::cout << "Environment Variable NDN_TRAFFIC_LOGFOLDER Not Set.\n"
98 "Using Default Output For Logging." << std::endl;
99 logLocation_ = "";
100 }
101 }
102
103 void
104 initializeTrafficConfiguration()
105 {
106 std::cout << "Traffic Configuration File: " << configurationFile_ << std::endl;
107 }
108
109 void
110 initialize()
111 {
112 boost::asio::signal_set signalSet(*ioService_, SIGINT, SIGTERM);
113 signalSet.async_wait(boost::bind(&NdnTrafficServer::signalHandler, this));
114 initializeLog();
115 initializeTrafficConfiguration();
116 }
117
jeraldabrahamf9543a42014-02-11 06:37:34 -0700118private:
119
120 KeyChain keyChain_;
121 std::string programName_;
122 int contentDelayTime_;
jeraldabrahame891ac92014-02-16 11:04:01 -0700123 std::string logLocation_;
jeraldabrahamf9543a42014-02-11 06:37:34 -0700124 std::string configurationFile_;
jeraldabrahame891ac92014-02-16 11:04:01 -0700125 ptr_lib::shared_ptr<boost::asio::io_service> ioService_;
126 Face face_;
127 std::string instanceId_;
jeraldabrahamf9543a42014-02-11 06:37:34 -0700128
129};
130
131int main( int argc, char* argv[] )
132{
133 int option;
134 NdnTrafficServer ndnTrafficServer (argv[0]);
jeraldabrahame891ac92014-02-16 11:04:01 -0700135 while ((option = getopt(argc, argv, "hd:")) != -1) {
jeraldabrahamf9543a42014-02-11 06:37:34 -0700136 switch (option) {
137 case 'h' :
138 ndnTrafficServer.usage();
139 break;
140 case 'd' :
141 ndnTrafficServer.setContentDelayTime(atoi(optarg));
142 break;
jeraldabrahamf9543a42014-02-11 06:37:34 -0700143 default :
144 ndnTrafficServer.usage();
145 break;
146 }
147 }
148
149 argc -= optind;
150 argv += optind;
151
jeraldabrahame891ac92014-02-16 11:04:01 -0700152 if (argv[0] == NULL)
jeraldabrahamf9543a42014-02-11 06:37:34 -0700153 ndnTrafficServer.usage();
154
155 ndnTrafficServer.setConfigurationFile(argv[0]);
jeraldabrahame891ac92014-02-16 11:04:01 -0700156 ndnTrafficServer.initialize();
jeraldabrahamf9543a42014-02-11 06:37:34 -0700157
158 return 0;
159}