shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * myping.cc |
| 3 | * |
| 4 | * Created on: 21 Jun, 2014 |
| 5 | * Author: shock |
| 6 | */ |
| 7 | |
| 8 | #include <boost/noncopyable.hpp> |
| 9 | #include <boost/asio.hpp> |
| 10 | #include <ndn-cxx/face.hpp> |
| 11 | #include <ndn-cxx/name.hpp> |
| 12 | #include <ndn-cxx/interest.hpp> |
| 13 | #include <ndn-cxx/selectors.hpp> |
| 14 | |
| 15 | #include <query.h> |
| 16 | #include <rr.h> |
| 17 | |
| 18 | #include <unistd.h> |
| 19 | #include <stdlib.h> //atoi |
| 20 | |
| 21 | |
| 22 | |
| 23 | #include <map> |
| 24 | #include <vector> |
| 25 | #include <sstream> |
| 26 | |
| 27 | //using std::cout; |
| 28 | //using std::cin; |
| 29 | //using std::cerr; |
| 30 | //using std::endl; |
| 31 | //using std::map; |
| 32 | //using std::vector; |
| 33 | //using std::exception; |
| 34 | //using std::pair; |
| 35 | //using std::string; |
| 36 | |
| 37 | using namespace std; |
| 38 | |
| 39 | namespace ndn{ |
| 40 | |
| 41 | |
| 42 | class Resolver : boost::noncopyable |
| 43 | { |
| 44 | |
| 45 | class RequestInfo |
| 46 | { |
| 47 | public: |
| 48 | RequestInfo(unsigned long seq) |
| 49 | : m_seq(seq) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | SendInterest() |
| 55 | { |
| 56 | //m_sentT = time::system_clock::now(); |
| 57 | } |
| 58 | void |
| 59 | GetData() |
| 60 | { |
| 61 | //this->m_receT = time::system_clock::now(); |
| 62 | } |
| 63 | |
| 64 | string |
| 65 | toString() |
| 66 | { |
| 67 | stringstream str; |
| 68 | str<<"m_seq="<<m_seq; |
| 69 | //s +="m_seq="+m_seq+" m_sentT="+" m_receT="; |
| 70 | return str.str(); |
| 71 | } |
| 72 | |
| 73 | public: |
| 74 | unsigned long m_seq; |
| 75 | time::milliseconds m_sentT; |
| 76 | time::milliseconds m_receT; |
| 77 | }; |
| 78 | |
| 79 | public: |
| 80 | Resolver(char *programName, char *scope, char *domain) |
| 81 | : m_programName (programName) |
| 82 | , m_authorityZone (scope) |
| 83 | , m_rrLabel(domain) |
| 84 | , m_interestLifetime (time::milliseconds(4000)) |
| 85 | , m_face (m_ioService) |
| 86 | { |
| 87 | cout<<"Create "<<m_programName<<endl; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | onInterest(const Name &name, const Interest &interest) |
| 92 | { |
| 93 | |
| 94 | cout<<"-> Interest: "<<name<<std::endl; |
| 95 | |
| 96 | } |
| 97 | void |
| 98 | onData(const ndn::Interest& interest, Data& data) |
| 99 | { |
| 100 | Response response(data); |
| 101 | response |
| 102 | } |
| 103 | void |
| 104 | onTimeout(const ndn::Interest& interest) |
| 105 | { |
| 106 | cout<<"!- Interest Timeout"<<interest.getName()<<endl; |
| 107 | Name iName = interest.getName(); |
| 108 | unsigned long seq = (unsigned long)iName.get(iName.size()-1).toNumber(); |
| 109 | |
| 110 | this->tryExpress(); |
| 111 | |
| 112 | |
| 113 | } |
| 114 | |
| 115 | |
| 116 | void |
| 117 | doExpress(Interest interest) |
| 118 | { |
| 119 | m_face.expressInterest(interest, boost::bind(&onData, this, _1, _2), |
| 120 | boost::bind(&onTimeout, this, _1)); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | tryExpress() |
| 125 | { |
| 126 | |
| 127 | this->doExpress(); |
| 128 | |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void |
| 133 | singalHandler() |
| 134 | { |
| 135 | this->stop(); |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | stop() |
| 140 | { |
| 141 | cout<<"resolve stops"<<endl; |
| 142 | this->m_face.shutdown(); |
| 143 | this->m_ioService.stop(); |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | run() |
| 148 | { |
| 149 | |
| 150 | boost::asio::signal_set signalSet(m_ioService, SIGINT, SIGTERM); |
| 151 | signalSet.async_wait(boost::bind(&singalHandler, this)); |
| 152 | |
| 153 | this->m_query.setAuthorityZone(this->m_authorityZone); |
| 154 | this->m_query.setRrLabel(this->m_rrLabel); |
| 155 | this->m_query.setRrType(RRType::NS); |
| 156 | |
| 157 | Interest interest = m_query.toWire(); |
| 158 | this->doExpress(interest); |
| 159 | |
| 160 | try{ |
| 161 | m_face.processEvents(); |
| 162 | } catch (exception &e){ |
| 163 | cerr<<"Error: "<<e.what()<<endl; |
| 164 | this->stop(); |
| 165 | } |
| 166 | |
| 167 | } |
| 168 | |
| 169 | |
| 170 | void |
| 171 | resolve() |
| 172 | { |
| 173 | |
| 174 | } |
| 175 | |
| 176 | |
| 177 | private: |
| 178 | char *m_programName; |
| 179 | Name m_authorityZone; |
| 180 | Name m_rrLabel; |
| 181 | //Name m_name; |
| 182 | time::milliseconds m_interestLifetime; |
| 183 | |
| 184 | |
| 185 | Query m_query; |
| 186 | |
| 187 | boost::asio::io_service m_ioService; |
| 188 | //This line MUST be before m_face declaration, or leads to segment error: 11 |
| 189 | |
| 190 | Face m_face; |
| 191 | |
| 192 | |
| 193 | };//class MyPing |
| 194 | |
| 195 | }//namespace |
| 196 | |
| 197 | |
| 198 | void |
| 199 | usage() |
| 200 | { |
| 201 | cout<<"-h to show the hint"<<endl; |
| 202 | } |
| 203 | |
| 204 | int |
| 205 | main(int argc, char *argv[]) |
| 206 | { |
| 207 | |
| 208 | if (argc < 2) |
| 209 | { |
| 210 | cout<<"You must have at least one parameter: resolve domain [scope]"<<endl; |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | char * domain = argv[1]; |
| 216 | char * scope; |
| 217 | if (argc == 3) |
| 218 | scope = argv[2]; |
| 219 | else |
| 220 | scope = "/"; |
| 221 | |
| 222 | |
| 223 | ndn::Resolver resolver(argv[0], scope, domain); |
| 224 | resolver.run(); |
| 225 | |
| 226 | return 0; |
| 227 | }//main |
| 228 | |
| 229 | |