Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 4 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 7 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 11 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 15 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 18 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 19 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "response-cache.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace pib { |
| 26 | |
| 27 | using std::map; |
| 28 | |
| 29 | ResponseCache::ResponseCache() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | |
| 34 | shared_ptr<const Data> |
| 35 | ResponseCache::find(const Name& dataName, bool hasVersion) const |
| 36 | { |
| 37 | if (!hasVersion) { |
| 38 | Storage::const_iterator it = m_storage.find(dataName); |
| 39 | if (it != m_storage.end()) |
| 40 | return it->second; |
| 41 | else |
| 42 | return shared_ptr<const Data>(); |
| 43 | } |
| 44 | else { |
| 45 | Storage::const_iterator it = m_storage.find(dataName.getPrefix(-1)); |
| 46 | if (it != m_storage.end() && it->second->getName() == dataName) |
| 47 | return it->second; |
| 48 | else |
| 49 | return shared_ptr<const Data>(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | ResponseCache::insert(const Data& data) |
| 55 | { |
| 56 | data.getName().at(-1).toVersion(); // ensures last component is version |
| 57 | m_storage[data.getName().getPrefix(-1)] = data.shared_from_this(); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | ResponseCache::erase(const Name& dataNameWithoutVersion) |
| 62 | { |
| 63 | m_storage.erase(dataNameWithoutVersion); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | ResponseCache::clear() |
| 68 | { |
| 69 | m_storage.clear(); |
| 70 | } |
| 71 | |
| 72 | } // namespace pib |
| 73 | } // namespace ndn |