blob: ad4e2d535f7a3ff7c12b6b936fba038ff6aeabf7 [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library 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 * ndn-cxx library 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 ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "list-query-processor.hpp"
23#include "encoding/pib-encoding.hpp"
24#include <boost/lexical_cast.hpp>
25
26namespace ndn {
27namespace pib {
28
29using std::string;
30using std::vector;
31
32const size_t ListQueryProcessor::LIST_QUERY_LENGTH = 5;
33
34ListQueryProcessor::ListQueryProcessor(PibDb& db)
35 : m_db(db)
36{
37}
38
39std::pair<bool, Block>
40ListQueryProcessor::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