blob: 29190e43bb98197efd7a6cc57d7db119259942a8 [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu0a312e52015-07-22 13:14:53 -07003 * Copyright (c) 2014-2015, Regents of the University of California.
Yingdi Yu77627ab2015-07-21 16:13:49 -07004 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07005 * 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 Yu77627ab2015-07-21 16:13:49 -07007 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07008 * 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 Yu77627ab2015-07-21 16:13:49 -070011 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070012 * 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 Yu77627ab2015-07-21 16:13:49 -070015 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070016 * 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 Yu77627ab2015-07-21 16:13:49 -070018 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070019 * @author Yingdi Yu <yingdi@cs.ucla.edu>
Yingdi Yu77627ab2015-07-21 16:13:49 -070020 */
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