shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * query.cpp |
| 3 | * |
| 4 | * Created on: 18 Jul, 2014 |
| 5 | * Author: shock |
| 6 | */ |
| 7 | |
| 8 | #include "query.h" |
| 9 | |
| 10 | namespace ndn { |
| 11 | |
| 12 | Query::Query() |
| 13 | : m_interestLifetime(time::milliseconds(4000)) |
| 14 | { |
| 15 | // TODO Auto-generated constructor stub |
| 16 | |
| 17 | } |
| 18 | |
| 19 | Query::~Query() { |
| 20 | // TODO Auto-generated destructor stub |
| 21 | } |
| 22 | |
| 23 | |
| 24 | void |
| 25 | Query::fromWire(const Name &name, const Interest &interest) |
| 26 | { |
| 27 | Name interestName; |
| 28 | interestName = interest.getName(); |
| 29 | |
| 30 | int qtflag = -1; |
| 31 | size_t len = interestName.size(); |
| 32 | for (size_t i=0; i<len; i++) |
| 33 | { |
| 34 | string comp = interestName.get(i).toEscapedString(); |
| 35 | if (comp == ndn::toString(QueryType::DNS) || comp == ndn::toString(QueryType::DNS_R)) |
| 36 | { |
| 37 | qtflag = i; |
| 38 | break; |
| 39 | } |
| 40 | }//for |
| 41 | |
| 42 | if (qtflag == -1) |
| 43 | { |
| 44 | cerr<<"There is no QueryType in the Interest Name: "<<interestName<<endl; |
| 45 | return; |
| 46 | } |
| 47 | this->m_queryType = ndn::toQueryType(interestName.get(qtflag).toEscapedString()); |
| 48 | this->m_rrType = ndn::toRRType(interestName.get(len-1).toEscapedString()); |
| 49 | this->m_authorityZone = interestName.getPrefix(qtflag); //the DNS/DNS-R is not included |
| 50 | this->m_interestLifetime = interest.getInterestLifetime(); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | template<bool T> |
| 55 | size_t |
| 56 | Query::wireEncode(EncodingImpl<T>& block) const |
| 57 | { |
| 58 | size_t totalLength = 0; |
| 59 | totalLength += 0; |
| 60 | |
| 61 | |
| 62 | size_t totalLength = prependByteArrayBlock(block, tlv::Bytes, |
| 63 | m_key.get().buf(), m_key.get().size()); |
| 64 | totalLength += m_keyName.wireEncode(block); |
| 65 | totalLength += block.prependVarNumber(totalLength); |
| 66 | totalLength += block.prependVarNumber(tlv::PublicKey); |
| 67 | |
| 68 | return totalLength; |
| 69 | } |
| 70 | |
| 71 | Interest |
| 72 | Query::toWire() const |
| 73 | { |
| 74 | Name name = this->m_authorityZone; |
| 75 | name.append(ndn::toString(this->m_queryType)); |
| 76 | name.append(this->m_rrLabel); |
| 77 | name.append(ndn::toString(this->m_rrType)); |
| 78 | Selectors selector; |
| 79 | //selector.setMustBeFresh(true); |
| 80 | |
| 81 | Interest interest = Interest(name, selector, -1, this->m_interestLifetime); |
| 82 | return interest; |
| 83 | } |
| 84 | |
| 85 | } /* namespace ndn */ |