shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * NameServer.cpp |
| 3 | * |
| 4 | * Created on: 18 Jul, 2014 |
| 5 | * Author: Xiaoke JIANG |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <boost/asio.hpp> |
| 10 | #include <boost/noncopyable.hpp> |
| 11 | |
| 12 | #include <ndn-cxx/face.hpp> |
| 13 | #include <ndn-cxx/name.hpp> |
| 14 | #include <ndn-cxx/security/key-chain.hpp> |
| 15 | |
| 16 | |
| 17 | |
| 18 | using namespace std; |
| 19 | using namespace ndn; |
| 20 | |
| 21 | |
| 22 | namespace ndn{ |
| 23 | class NameServer : boost::noncopyable |
| 24 | { |
| 25 | public: |
| 26 | explicit |
| 27 | NameServer(char *programName) |
| 28 | : m_programName(programName) |
| 29 | , m_hasError(false) |
| 30 | , m_freshnessPeriod(time::milliseconds(1000)) |
| 31 | , m_face(m_ioService) |
| 32 | { |
| 33 | |
| 34 | }//NameServer Construction |
| 35 | |
| 36 | |
| 37 | void |
| 38 | onInterest(const Name &name, const Interest &interest) |
| 39 | { |
| 40 | |
| 41 | cout<<"-> Interest: "<<name<<std::endl; |
| 42 | |
| 43 | } |
| 44 | void |
| 45 | onData(const ndn::Interest& interest, Data& data) |
| 46 | { |
| 47 | Name dName = data.getName(); |
| 48 | if (not m_name.isPrefixOf(dName) ) |
| 49 | { |
| 50 | cout<<"!! ILLEGAL data: "<<dName<<", which does not starts with "<<m_name<<endl; |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | cout<<"-> Data: "<<dName<<endl; |
| 55 | |
| 56 | Name iName = interest.getName(); |
| 57 | unsigned long seq = (unsigned long)iName.get(iName.size()-1).toNumber(); |
| 58 | |
| 59 | |
| 60 | this->tryExpress(); |
| 61 | |
| 62 | } |
| 63 | void |
| 64 | onTimeout(const ndn::Interest& interest) |
| 65 | { |
| 66 | cout<<"!- Interest Timeout"<<interest.getName()<<endl; |
| 67 | Name iName = interest.getName(); |
| 68 | unsigned long seq = (unsigned long)iName.get(iName.size()-1).toNumber(); |
| 69 | |
| 70 | |
| 71 | |
| 72 | this->tryExpress(); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | onRegisterFailed(const ndn::Name& prefix, const std::string& reason) |
| 77 | { |
| 78 | std::cerr << "ERROR: Failed to register prefix in local hub's daemon" << std::endl; |
| 79 | std::cerr << "REASON: " << reason << std::endl; |
| 80 | m_hasError = true; |
| 81 | this->stop(); |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | DoExpress() |
| 86 | { |
| 87 | Name name = this->m_name; |
| 88 | |
| 89 | |
| 90 | Selectors selector; |
| 91 | selector.setMustBeFresh(true); |
| 92 | |
| 93 | Interest interest = Interest(name, selector, -1, this->m_freshnessPeriod, 1); |
| 94 | |
| 95 | m_face.expressInterest(interest, boost::bind(&NameServer::onData, this, _1, _2), |
| 96 | boost::bind(&NameServer::onTimeout, this, _1)); |
| 97 | |
| 98 | |
| 99 | //m_face.expressInterest(interest, boost::bind(&MyPing::OnData, this, _1, _2), |
| 100 | // boost::bind(&MyPing::OnTimeout, this, _1)); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | tryExpress() |
| 105 | { |
| 106 | this->DoExpress(); |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | singalHandler() |
| 111 | { |
| 112 | cout<<"Fail to Register"<<endl; |
| 113 | this->stop(); |
| 114 | exit(1); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | stop() |
| 119 | { |
| 120 | cout<<"program "<<this->m_programName<<" stops"<<endl; |
| 121 | this->m_face.shutdown(); |
| 122 | this->m_ioService.stop(); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | void |
| 129 | run() |
| 130 | { |
| 131 | std::cout << "\n=== NDNS Server for Zone " << m_prefix <<" starts===\n" << std::endl; |
| 132 | |
| 133 | boost::asio::signal_set signalSet(m_ioService, SIGINT, SIGTERM); |
| 134 | signalSet.async_wait(bind(&NameServer::singalHandler, this)); |
| 135 | |
| 136 | m_name.set(m_prefix); |
| 137 | |
| 138 | m_face.setInterestFilter(m_name, |
| 139 | bind(&NameServer::onInterest, |
| 140 | this, _1, _2), |
| 141 | bind(&NameServer::onRegisterFailed, |
| 142 | this, _1,_2)); |
| 143 | try { |
| 144 | m_face.processEvents(); |
| 145 | } |
| 146 | catch (std::exception& e) { |
| 147 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 148 | m_hasError = true; |
| 149 | m_ioService.stop(); |
| 150 | } |
| 151 | |
| 152 | } |
| 153 | |
| 154 | bool hasError() const { |
| 155 | return m_hasError; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | public: |
| 160 | KeyChain m_keyChain; |
| 161 | bool m_hasError; |
| 162 | |
| 163 | time::milliseconds m_freshnessPeriod; |
| 164 | char* m_programName; |
| 165 | char* m_prefix; |
| 166 | Name m_name; |
| 167 | boost::asio::io_service m_ioService; |
| 168 | Face m_face; |
| 169 | |
| 170 | };//clcass NameServer |
| 171 | }//namespace ndn |
| 172 | |
| 173 | |
| 174 | int main(int argc, char * argv[]) |
| 175 | { |
| 176 | NameServer server; |
| 177 | server.run(); |
| 178 | |
| 179 | cout<<"the server ends with hasError="<<server.hasError()<<endl; |
| 180 | |
| 181 | if (server.hasError()){ |
| 182 | return 0; |
| 183 | } else { |
| 184 | return 1; |
| 185 | } |
| 186 | |
| 187 | } |
| 188 | |