jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 1 | /**
|
| 2 | *
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame^] | 3 | * Copyright (C) 2014 University of Arizona.
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 4 | * @author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
|
| 5 | *
|
| 6 | */
|
| 7 |
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame^] | 8 | #include <sstream>
|
| 9 | #include <boost/asio.hpp>
|
| 10 | #include <boost/filesystem.hpp>
|
| 11 |
|
| 12 | #include <ndn-cpp-dev/face.hpp>
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 13 | #include <ndn-cpp-dev/security/key-chain.hpp>
|
| 14 |
|
| 15 | using namespace ndn;
|
| 16 |
|
| 17 | class NdnTrafficServer
|
| 18 | {
|
| 19 | public:
|
| 20 |
|
| 21 | NdnTrafficServer( char* programName )
|
| 22 | {
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame^] | 23 | std::stringstream randomId;
|
| 24 | std::srand(std::time(0));
|
| 25 | randomId << std::rand();
|
| 26 | instanceId_ = randomId.str();
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 27 | programName_ = programName;
|
| 28 | contentDelayTime_ = getDefaultContentDelayTime();
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame^] | 29 | logLocation_ = "";
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 30 | configurationFile_ = "";
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame^] | 31 | ioService_ = ptr_lib::make_shared<boost::asio::io_service>();
|
| 32 | face_ = Face(ioService_);
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 33 | }
|
| 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 |
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 54 | void
|
| 55 | setContentDelayTime( int contentDelayTime )
|
| 56 | {
|
| 57 | if (contentDelayTime < 0)
|
| 58 | usage();
|
| 59 | contentDelayTime_ = contentDelayTime;
|
| 60 | }
|
| 61 |
|
| 62 | void
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 63 | setConfigurationFile( char* configurationFile )
|
| 64 | {
|
| 65 | configurationFile_ = configurationFile;
|
| 66 | }
|
| 67 |
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame^] | 68 | 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 |
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 120 | private:
|
| 121 |
|
| 122 | KeyChain keyChain_;
|
| 123 | std::string programName_;
|
| 124 | int contentDelayTime_;
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame^] | 125 | std::string logLocation_;
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 126 | std::string configurationFile_;
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame^] | 127 | ptr_lib::shared_ptr<boost::asio::io_service> ioService_;
|
| 128 | Face face_;
|
| 129 | std::string instanceId_;
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 130 |
|
| 131 | };
|
| 132 |
|
| 133 | int main( int argc, char* argv[] )
|
| 134 | {
|
| 135 | int option;
|
| 136 | NdnTrafficServer ndnTrafficServer (argv[0]);
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame^] | 137 | while ((option = getopt(argc, argv, "hd:")) != -1) {
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 138 | switch (option) {
|
| 139 | case 'h' :
|
| 140 | ndnTrafficServer.usage();
|
| 141 | break;
|
| 142 | case 'd' :
|
| 143 | ndnTrafficServer.setContentDelayTime(atoi(optarg));
|
| 144 | break;
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 145 | default :
|
| 146 | ndnTrafficServer.usage();
|
| 147 | break;
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | argc -= optind;
|
| 152 | argv += optind;
|
| 153 |
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame^] | 154 | if (argv[0] == NULL)
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 155 | ndnTrafficServer.usage();
|
| 156 |
|
| 157 | ndnTrafficServer.setConfigurationFile(argv[0]);
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame^] | 158 | ndnTrafficServer.initialize();
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 159 |
|
| 160 | return 0;
|
| 161 | }
|