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 |
|
jeraldabraham | 662266d | 2014-02-17 10:26:12 -0700 | [diff] [blame^] | 21 | NdnTrafficServer( char* programName ) : ioService_(new boost::asio::io_service), face_(ioService_)
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 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_ = "";
|
| 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 |
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 52 | void
|
| 53 | setContentDelayTime( int contentDelayTime )
|
| 54 | {
|
| 55 | if (contentDelayTime < 0)
|
| 56 | usage();
|
| 57 | contentDelayTime_ = contentDelayTime;
|
| 58 | }
|
| 59 |
|
| 60 | void
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 61 | setConfigurationFile( char* configurationFile )
|
| 62 | {
|
| 63 | configurationFile_ = configurationFile;
|
| 64 | }
|
| 65 |
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame] | 66 | 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 |
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 118 | private:
|
| 119 |
|
| 120 | KeyChain keyChain_;
|
| 121 | std::string programName_;
|
| 122 | int contentDelayTime_;
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame] | 123 | std::string logLocation_;
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 124 | std::string configurationFile_;
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame] | 125 | ptr_lib::shared_ptr<boost::asio::io_service> ioService_;
|
| 126 | Face face_;
|
| 127 | std::string instanceId_;
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 128 |
|
| 129 | };
|
| 130 |
|
| 131 | int main( int argc, char* argv[] )
|
| 132 | {
|
| 133 | int option;
|
| 134 | NdnTrafficServer ndnTrafficServer (argv[0]);
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame] | 135 | while ((option = getopt(argc, argv, "hd:")) != -1) {
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 136 | switch (option) {
|
| 137 | case 'h' :
|
| 138 | ndnTrafficServer.usage();
|
| 139 | break;
|
| 140 | case 'd' :
|
| 141 | ndnTrafficServer.setContentDelayTime(atoi(optarg));
|
| 142 | break;
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 143 | default :
|
| 144 | ndnTrafficServer.usage();
|
| 145 | break;
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | argc -= optind;
|
| 150 | argv += optind;
|
| 151 |
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame] | 152 | if (argv[0] == NULL)
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 153 | ndnTrafficServer.usage();
|
| 154 |
|
| 155 | ndnTrafficServer.setConfigurationFile(argv[0]);
|
jeraldabraham | e891ac9 | 2014-02-16 11:04:01 -0700 | [diff] [blame] | 156 | ndnTrafficServer.initialize();
|
jeraldabraham | f9543a4 | 2014-02-11 06:37:34 -0700 | [diff] [blame] | 157 |
|
| 158 | return 0;
|
| 159 | }
|