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 UPDATE_HANDLER_H |
| 23 | #define UPDATE_HANDLER_H |
| 24 | |
| 25 | #include "stats-table.hpp" |
| 26 | |
| 27 | #include <ndn-cxx/face.hpp> |
| 28 | #include <ndn-cxx/interest.hpp> |
| 29 | #include <ndn-cxx/security/key-chain.hpp> |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace ntorrent { |
| 33 | |
| 34 | class UpdateHandler { |
| 35 | public: |
| 36 | class Error : public tlv::Error |
| 37 | { |
| 38 | public: |
| 39 | explicit |
| 40 | Error(const std::string& what) |
| 41 | : tlv::Error(what) |
| 42 | { |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | UpdateHandler(Name torrentName, shared_ptr<KeyChain> keyChain, |
| 47 | shared_ptr<StatsTable> statsTable, shared_ptr<Face> face); |
| 48 | |
| 49 | ~UpdateHandler(); |
| 50 | |
| 51 | /** |
| 52 | * @brief Send an ALIVE Interest |
| 53 | * @param routablePrefix The routable prefix to be included in the LINK object attached |
| 54 | * to this Interest |
| 55 | */ |
| 56 | void |
| 57 | sendAliveInterest(StatsTable::iterator iter); |
| 58 | |
| 59 | /** |
| 60 | * @brief Check whether we need to send out an "ALIVE" interest |
| 61 | * @return True if an "ALIVE" interest should be sent out, otherwise false |
| 62 | * |
| 63 | * Returns true if we have less than MIN_NUM_OF_ROUTABLE_NAMES prefixes in the stats table |
| 64 | * or all the routable prefixes has success rate less than 0.5. Otherwise, it returns false |
| 65 | */ |
| 66 | bool |
| 67 | needsUpdate(); |
| 68 | |
| 69 | enum { |
| 70 | // Maximum number of names to be encoded as a response to an "ALIVE" Interest |
| 71 | MAX_NUM_OF_ENCODED_NAMES = 5, |
| 72 | // Minimum number of routable prefixes that the peer would like to have |
| 73 | MIN_NUM_OF_ROUTABLE_NAMES = 5, |
| 74 | }; |
| 75 | |
| 76 | protected: |
| 77 | // Used for testing purposes |
| 78 | const Name& |
| 79 | getOwnRoutablePrefix(); |
| 80 | |
| 81 | private: |
| 82 | template<encoding::Tag TAG> |
| 83 | size_t |
| 84 | encodeContent(EncodingImpl<TAG>& encoder) const; |
| 85 | |
| 86 | void |
| 87 | onInterestReceived(const InterestFilter& filter, const Interest& interest); |
| 88 | |
| 89 | void |
| 90 | onRegisterFailed(const Name& prefix, const std::string& reason); |
| 91 | |
| 92 | /** |
| 93 | * @brief Encode the first MAX_NUM_OF_ENCODED_NAMES prefixes of the table into a data packet |
| 94 | * @param name The name of the data packet |
| 95 | * @return A shared pointer to the created data packet |
| 96 | * |
| 97 | */ |
| 98 | shared_ptr<Data> |
| 99 | createDataPacket(const Name& name); |
| 100 | |
| 101 | /** |
| 102 | * @brief Given a received data packet, decode the contained routable name prefixes |
| 103 | * and insert them to the table (if not already there) |
| 104 | * @param interest The interest that retrieved the data packet |
| 105 | * @param data A shared pointer to the received data packet |
| 106 | * |
| 107 | */ |
| 108 | void |
| 109 | decodeDataPacketContent(const Interest& interest, const Data& data); |
| 110 | |
| 111 | /** |
| 112 | * @brief Send an Interest to the local NFD to get the routable prefixes under which the |
| 113 | * published data is available |
| 114 | */ |
| 115 | void |
| 116 | learnOwnRoutablePrefix(); |
| 117 | |
| 118 | void |
| 119 | tryNextRoutablePrefix(const Interest& interest); |
| 120 | |
| 121 | private: |
| 122 | Name m_torrentName; |
| 123 | shared_ptr<KeyChain> m_keyChain; |
| 124 | shared_ptr<StatsTable> m_statsTable; |
| 125 | shared_ptr<Face> m_face; |
| 126 | Name m_ownRoutablePrefix; |
| 127 | }; |
| 128 | |
| 129 | inline |
| 130 | UpdateHandler::UpdateHandler(Name torrentName, shared_ptr<KeyChain> keyChain, |
| 131 | shared_ptr<StatsTable> statsTable, shared_ptr<Face> face) |
| 132 | : m_torrentName(torrentName) |
| 133 | , m_keyChain(keyChain) |
| 134 | , m_statsTable(statsTable) |
| 135 | , m_face(face) |
| 136 | { |
| 137 | this->learnOwnRoutablePrefix(); |
| 138 | m_face->setInterestFilter(Name("/NTORRENT" + m_torrentName.toUri() + "/ALIVE"), |
| 139 | bind(&UpdateHandler::onInterestReceived, this, _1, _2), |
| 140 | RegisterPrefixSuccessCallback(), |
| 141 | bind(&UpdateHandler::onRegisterFailed, this, _1, _2)); |
| 142 | } |
| 143 | |
| 144 | inline |
| 145 | UpdateHandler::~UpdateHandler() |
| 146 | { |
| 147 | } |
| 148 | |
| 149 | inline const Name& |
| 150 | UpdateHandler::getOwnRoutablePrefix() |
| 151 | { |
| 152 | return m_ownRoutablePrefix; |
| 153 | } |
| 154 | |
| 155 | } // namespace ntorrent |
| 156 | } // namespace ndn |
| 157 | |
| 158 | #endif // UPDATE_HANDLER_H |