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 "list-query-processor.hpp" |
| 23 | #include "encoding/pib-encoding.hpp" |
| 24 | #include <boost/lexical_cast.hpp> |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace pib { |
| 28 | |
| 29 | using std::string; |
| 30 | using std::vector; |
| 31 | |
| 32 | const size_t ListQueryProcessor::LIST_QUERY_LENGTH = 5; |
| 33 | |
| 34 | ListQueryProcessor::ListQueryProcessor(PibDb& db) |
| 35 | : m_db(db) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | std::pair<bool, Block> |
| 40 | ListQueryProcessor::operator()(const Interest& interest) |
| 41 | { |
| 42 | const Name& interestName = interest.getName(); |
| 43 | |
| 44 | // handle pib query: /localhost/pib/[UserName]/list/param |
| 45 | if (interestName.size() != MIN_PIB_INTEREST_SIZE) { |
| 46 | // malformed interest, discard |
| 47 | return std::make_pair(false, Block()); |
| 48 | } |
| 49 | |
| 50 | ListParam param; |
| 51 | |
| 52 | try { |
| 53 | param.wireDecode(interestName.get(OFFSET_PARAM).blockFromValue()); |
| 54 | } |
| 55 | catch (const tlv::Error& e) { |
| 56 | PibError error(ERR_WRONG_PARAM, "error in parsing param: " + string(e.what())); |
| 57 | return std::make_pair(true, error.wireEncode()); |
| 58 | } |
| 59 | |
| 60 | vector<Name> nameList; |
| 61 | switch (param.getOriginType()) { |
| 62 | case TYPE_USER: |
| 63 | { |
| 64 | nameList = m_db.listIdentities(); |
| 65 | break; |
| 66 | } |
| 67 | case TYPE_ID: |
| 68 | { |
| 69 | nameList = m_db.listKeyNamesOfIdentity(param.getOriginName()); |
| 70 | break; |
| 71 | } |
| 72 | case TYPE_KEY: |
| 73 | { |
| 74 | const Name& keyName = param.getOriginName(); |
| 75 | if (keyName.empty()) { |
| 76 | PibError error(ERR_WRONG_PARAM, |
| 77 | "key name must contain key id component"); |
| 78 | return std::make_pair(true, error.wireEncode()); |
| 79 | } |
| 80 | |
| 81 | nameList = m_db.listCertNamesOfKey(keyName); |
| 82 | break; |
| 83 | } |
| 84 | default: |
| 85 | { |
| 86 | PibError error(ERR_WRONG_PARAM, |
| 87 | "origin type is not supported: " + |
| 88 | boost::lexical_cast<string>(param.getOriginType())); |
| 89 | return std::make_pair(true, error.wireEncode()); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | PibNameList result(nameList); |
| 94 | return std::make_pair(true, result.wireEncode()); |
| 95 | } |
| 96 | |
| 97 | } // namespace pib |
| 98 | } // namespace ndn |