spirosmastorakis | 4ff8c87 | 2016-04-14 09:51:38 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2016 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of the nTorrent codebase. |
| 6 | * |
| 7 | * nTorrent is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with nTorrent, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS for complete list of nTorrent authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_TESTS_UNIT_TESTS_DUMMY_PARSER_HPP |
| 23 | #define NDN_TESTS_UNIT_TESTS_DUMMY_PARSER_HPP |
| 24 | |
| 25 | #include <ndn-cxx/data.hpp> |
| 26 | |
| 27 | #include <vector> |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace ntorrent { |
| 31 | namespace tests { |
| 32 | |
| 33 | using std::vector; |
| 34 | |
| 35 | class DummyParser { |
| 36 | public: |
| 37 | DummyParser() |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | ~DummyParser() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | static shared_ptr<Data> |
| 46 | createDataPacket(const Name& packetName, const std::vector<Name>& vec) |
| 47 | { |
| 48 | shared_ptr<Data> data = make_shared<Data>(packetName); |
| 49 | |
| 50 | EncodingEstimator estimator; |
| 51 | size_t estimatedSize = encodeContent(estimator, vec); |
| 52 | |
| 53 | EncodingBuffer buffer(estimatedSize, 0); |
| 54 | encodeContent(buffer, vec); |
| 55 | |
| 56 | data->setContentType(tlv::ContentType_Blob); |
| 57 | data->setContent(buffer.block()); |
| 58 | |
| 59 | return data; |
| 60 | } |
| 61 | |
| 62 | static shared_ptr<vector<Name>> |
| 63 | decodeContent(const Block& content) |
| 64 | { |
| 65 | shared_ptr<vector<Name>> nameVec = make_shared<vector<Name>>(); |
| 66 | content.parse(); |
| 67 | // Decode the names (do not worry about the order) |
| 68 | for (auto element = content.elements_begin(); element != content.elements_end(); element++) { |
| 69 | element->parse(); |
| 70 | Name name(*element); |
| 71 | nameVec->push_back(name); |
| 72 | } |
| 73 | return nameVec; |
| 74 | } |
| 75 | private: |
| 76 | template<encoding::Tag TAG> |
| 77 | static size_t |
| 78 | encodeContent(EncodingImpl<TAG>& encoder, const std::vector<Name>& vec) |
| 79 | { |
| 80 | // Content ::= CONTENT-TYPE TLV-LENGTH |
| 81 | // RoutableName+ |
| 82 | |
| 83 | // RoutableName ::= NAME-TYPE TLV-LENGTH |
| 84 | // Name |
| 85 | |
| 86 | size_t totalLength = 0; |
| 87 | for (const auto& element : vec) { |
| 88 | size_t nameLength = 0; |
| 89 | nameLength += element.wireEncode(encoder); |
| 90 | totalLength += nameLength; |
| 91 | } |
| 92 | totalLength += encoder.prependVarNumber(totalLength); |
| 93 | totalLength += encoder.prependVarNumber(tlv::Content); |
| 94 | return totalLength; |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | } // namespace tests |
| 99 | } // namespace ntorrent |
| 100 | } // namespace ndn |
| 101 | |
| 102 | #endif // NDN_TESTS_UNIT_TESTS_DUMMY_PARSER_HPP |