shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * response.cpp |
| 3 | * |
| 4 | * Created on: 19 Jul, 2014 |
| 5 | * Author: shock |
| 6 | */ |
| 7 | |
| 8 | #include "response.h" |
| 9 | |
| 10 | |
| 11 | |
| 12 | namespace ndn { |
| 13 | |
| 14 | Response::Response() { |
| 15 | // TODO Auto-generated constructor stub |
| 16 | |
| 17 | } |
| 18 | |
| 19 | Response::~Response() { |
| 20 | // TODO Auto-generated destructor stub |
| 21 | } |
| 22 | |
| 23 | |
| 24 | |
| 25 | Data |
| 26 | Response::toWire() const |
| 27 | { |
| 28 | Name name = this->m_queryName; |
| 29 | name.append(this->m_serial); |
| 30 | |
| 31 | Data data(name); |
| 32 | |
| 33 | data.setFreshnessPeriod(this->m_freshness); |
| 34 | |
| 35 | string content = ""; |
| 36 | |
| 37 | size_t totalLen = 0; |
| 38 | Block block = Block(); |
| 39 | block.push_back |
| 40 | (nonNegativeIntegerBlock |
| 41 | (tlv::ndns::Type, static_cast<unsigned long>(this->m_responseType)) |
| 42 | ); |
| 43 | |
| 44 | block.push_back |
| 45 | (nonNegativeIntegerBlock |
| 46 | (tlv::ndns::Fressness, this->m_freshness.count()) |
| 47 | ); |
| 48 | |
| 49 | Block block2 = Block(tlv::ndns::ContentBlob); |
| 50 | block2.push_back |
| 51 | (nonNegativeIntegerBlock |
| 52 | (tlv::ndns::NumberOfRRData, this->m_numberOfRR) |
| 53 | ); |
| 54 | |
| 55 | for (int i=0; i<this->m_numberOfRR; i++) |
| 56 | { |
| 57 | RR rr = m_rrs[i]; |
| 58 | block2.push_back(rr.toWire()); |
| 59 | } |
| 60 | |
| 61 | block.push_back(block2); |
| 62 | |
| 63 | return data; |
| 64 | |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | Response::fromWire(const Interest &interest, const Data &data) |
| 69 | { |
| 70 | Name dataName; |
| 71 | dataName = data.getName(); |
| 72 | |
| 73 | int qtflag = -1; |
| 74 | size_t len = dataName.size(); |
| 75 | for (size_t i=0; i<len; i++) |
| 76 | { |
| 77 | string comp = dataName.get(i).toEscapedString(); |
| 78 | if (comp == ndn::toString(QueryType::DNS) || comp == ndn::toString(QueryType::DNS_R)) |
| 79 | { |
| 80 | qtflag = i; |
| 81 | break; |
| 82 | } |
| 83 | }//for |
| 84 | |
| 85 | if (qtflag == -1) |
| 86 | { |
| 87 | cerr<<"There is no QueryType in the Interest Name: "<<dataName<<endl; |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | this->m_queryName = dataName.getPrefix(-1); |
| 92 | |
| 93 | string last = dataName.get(len-1).toEscapedString(); |
| 94 | if (ndn::toRRType(last) == RRType::UNKNOWN) |
| 95 | { |
| 96 | this->m_serial = ""; |
| 97 | } else |
| 98 | { |
| 99 | this->m_serial = last; |
| 100 | } |
| 101 | |
| 102 | Block block = data.getContent(); |
| 103 | this->m_numberOfRR = readNonNegativeInteger(block.get(tlv::ndns::NumberOfRRData)); |
| 104 | |
| 105 | Block block2 = block.get(tlv::ndns::ContentBlob); |
| 106 | for (int i=0; i<this->m_numberOfRR; i++) |
| 107 | { |
| 108 | Block block3 = block2.get(tlv::ndns::RRData); |
| 109 | RR rr; |
| 110 | rr.fromWire(block3); |
| 111 | m_rrs.push_back(rr); |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | |
| 116 | |
| 117 | } /* namespace ndn */ |
| 118 | |
| 119 | |
| 120 | |
| 121 | |