Alexander Afanasyev | 04fa37a | 2014-03-19 18:06:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_MANAGEMENT_NFD_FACE_STATUS_HPP |
| 8 | #define NDN_MANAGEMENT_NFD_FACE_STATUS_HPP |
| 9 | |
| 10 | #include "../encoding/encoding-buffer.hpp" |
| 11 | #include "../encoding/tlv-nfd.hpp" |
| 12 | |
| 13 | namespace ndn { |
| 14 | namespace nfd { |
| 15 | |
| 16 | class FaceStatus |
| 17 | { |
| 18 | public: |
| 19 | class Error : public Tlv::Error |
| 20 | { |
| 21 | public: |
| 22 | Error(const std::string& what) : Tlv::Error(what) { } |
| 23 | }; |
| 24 | |
| 25 | FaceStatus(const uint64_t faceId, |
| 26 | const std::string& uri, |
| 27 | uint64_t inInterest, uint64_t inData, uint64_t outInterest, uint64_t outData); |
| 28 | |
| 29 | explicit |
| 30 | FaceStatus(const Block& block); |
| 31 | |
| 32 | uint64_t |
| 33 | getFaceId() const |
| 34 | { |
| 35 | return m_faceId; |
| 36 | } |
| 37 | |
| 38 | const std::string& |
| 39 | getUri() const |
| 40 | { |
| 41 | return m_uri; |
| 42 | } |
| 43 | |
| 44 | const uint64_t |
| 45 | getInInterest() const |
| 46 | { |
| 47 | return m_inInterest; |
| 48 | } |
| 49 | |
| 50 | const uint64_t |
| 51 | getInData() const |
| 52 | { |
| 53 | return m_inData; |
| 54 | } |
| 55 | |
| 56 | const uint64_t |
| 57 | getOutInterest() const |
| 58 | { |
| 59 | return m_outInterest; |
| 60 | } |
| 61 | |
| 62 | const uint64_t |
| 63 | getOutData() const |
| 64 | { |
| 65 | return m_outData; |
| 66 | } |
| 67 | |
| 68 | template<bool T> |
| 69 | size_t |
| 70 | wireEncode(EncodingImpl<T>& buffer) const; |
| 71 | |
| 72 | const Block& |
| 73 | wireEncode() const; |
| 74 | |
| 75 | void |
| 76 | wireDecode(const Block& wire); |
| 77 | |
| 78 | private: |
| 79 | uint64_t m_faceId; |
| 80 | std::string m_uri; |
| 81 | uint64_t m_inInterest; |
| 82 | uint64_t m_inData; |
| 83 | uint64_t m_outInterest; |
| 84 | uint64_t m_outData; |
| 85 | |
| 86 | mutable Block m_wire; |
| 87 | }; |
| 88 | |
| 89 | inline |
| 90 | FaceStatus::FaceStatus(const uint64_t faceId, |
| 91 | const std::string& uri, |
| 92 | uint64_t inInterest, uint64_t inData, uint64_t outInterest, uint64_t outData) |
| 93 | : m_faceId(faceId) |
| 94 | , m_uri(uri) |
| 95 | , m_inInterest(inInterest) |
| 96 | , m_inData(inData) |
| 97 | , m_outInterest(outInterest) |
| 98 | , m_outData(outData) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | inline |
| 103 | FaceStatus::FaceStatus(const Block& block) |
| 104 | { |
| 105 | wireDecode(block); |
| 106 | } |
| 107 | |
| 108 | template<bool T> |
| 109 | size_t |
| 110 | FaceStatus::wireEncode(EncodingImpl<T>& buffer) const |
| 111 | { |
| 112 | size_t totalLength = 0; |
| 113 | |
| 114 | totalLength += prependNonNegativeIntegerBlock(buffer, |
| 115 | tlv::nfd::TotalOutgoingDataCounter, |
| 116 | m_outData); |
| 117 | |
| 118 | totalLength += prependNonNegativeIntegerBlock(buffer, |
| 119 | tlv::nfd::TotalOutgoingInterestCounter, |
| 120 | m_outInterest); |
| 121 | |
| 122 | totalLength += prependNonNegativeIntegerBlock(buffer, |
| 123 | tlv::nfd::TotalIncomingDataCounter, |
| 124 | m_inData); |
| 125 | |
| 126 | totalLength += prependNonNegativeIntegerBlock(buffer, |
| 127 | tlv::nfd::TotalIncomingInterestCounter, |
| 128 | m_inInterest); |
| 129 | |
| 130 | totalLength += prependByteArrayBlock(buffer, |
| 131 | tlv::nfd::Uri, |
| 132 | reinterpret_cast<const uint8_t*>(m_uri.c_str()), |
| 133 | m_uri.size()); |
| 134 | |
| 135 | totalLength += prependNonNegativeIntegerBlock(buffer, |
| 136 | tlv::nfd::FaceId, |
| 137 | m_faceId); |
| 138 | |
| 139 | totalLength += buffer.prependVarNumber(totalLength); |
| 140 | totalLength += buffer.prependVarNumber(tlv::nfd::FaceStatus); |
| 141 | |
| 142 | return totalLength; |
| 143 | } |
| 144 | |
| 145 | const Block& |
| 146 | FaceStatus::wireEncode() const |
| 147 | { |
| 148 | if (m_wire.hasWire ()) |
| 149 | return m_wire; |
| 150 | |
| 151 | EncodingEstimator estimator; |
| 152 | size_t estimatedSize = wireEncode(estimator); |
| 153 | |
| 154 | EncodingBuffer buffer(estimatedSize, 0); |
| 155 | wireEncode(buffer); |
| 156 | |
| 157 | m_wire = buffer.block(); |
| 158 | return m_wire; |
| 159 | } |
| 160 | |
| 161 | inline void |
| 162 | FaceStatus::wireDecode(const Block &wire) |
| 163 | { |
| 164 | m_wire = wire; |
| 165 | |
| 166 | if (m_wire.type() != tlv::nfd::FaceStatus) |
| 167 | throw Error("Requested decoding of FaceStatus, but Block is of different type"); |
| 168 | |
| 169 | m_wire.parse(); |
| 170 | |
| 171 | // FaceKind |
| 172 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 173 | if (val == m_wire.elements_end() || val->type() != tlv::nfd::FaceId) |
| 174 | throw Error("Missing required FaceId block"); |
| 175 | m_faceId = readNonNegativeInteger(*val); |
| 176 | |
| 177 | // URI |
| 178 | ++val; |
| 179 | if (val == m_wire.elements_end() || val->type() != tlv::nfd::Uri) |
| 180 | throw Error("Missing required Uri block"); |
| 181 | m_uri = std::string(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 182 | |
| 183 | // TotalIncomingInterestCounter |
| 184 | ++val; |
| 185 | if (val == m_wire.elements_end() || val->type() != tlv::nfd::TotalIncomingInterestCounter) |
| 186 | throw Error("Missing required FaceId block"); |
| 187 | m_inInterest = readNonNegativeInteger(*val); |
| 188 | |
| 189 | // TotalIncomingDataCounter |
| 190 | ++val; |
| 191 | if (val == m_wire.elements_end() || val->type() != tlv::nfd::TotalIncomingDataCounter) |
| 192 | throw Error("Missing required FaceId block"); |
| 193 | m_inData = readNonNegativeInteger(*val); |
| 194 | |
| 195 | // TotalOutgoingInterestCounter |
| 196 | ++val; |
| 197 | if (val == m_wire.elements_end() || val->type() != tlv::nfd::TotalOutgoingInterestCounter) |
| 198 | throw Error("Missing required FaceId block"); |
| 199 | m_outInterest = readNonNegativeInteger(*val); |
| 200 | |
| 201 | // TotalOutgoingDataCounter |
| 202 | ++val; |
| 203 | if (val == m_wire.elements_end() || val->type() != tlv::nfd::TotalOutgoingDataCounter) |
| 204 | throw Error("Missing required FaceId block"); |
| 205 | m_outData = readNonNegativeInteger(*val); |
| 206 | } |
| 207 | |
| 208 | inline std::ostream& |
| 209 | operator << (std::ostream& os, const FaceStatus& status) |
| 210 | { |
| 211 | os << "FaceStatus("; |
| 212 | |
| 213 | // FaceID |
| 214 | os << "FaceID: " << status.getFaceId() << ", "; |
| 215 | |
| 216 | // URI |
| 217 | os << "Uri: " << status.getUri() << ", "; |
| 218 | |
| 219 | os << "Counters: " << status.getInInterest() << "|" << status.getInData() |
| 220 | << "|" << status.getOutInterest() << "|" << status.getOutData(); |
| 221 | |
| 222 | os << ")"; |
| 223 | return os; |
| 224 | } |
| 225 | |
| 226 | } // namespace nfd |
| 227 | } // namespace ndn |
| 228 | |
| 229 | #endif // NDN_MANAGEMENT_NFD_FACE_EVENT_STATUS_HPP |