blob: cab365b64c988748d63a563c6fadbdabd8fb33f7 [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventob48bbda2020-07-27 19:41:37 -04002/*
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -04003 * Copyright (c) 2017-2022, Regents of the University of California.
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert 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 General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
Zhiyi Zhangb041d442020-10-22 21:57:11 -070021#include "detail/ca-memory.hpp"
Davide Pesaventob48bbda2020-07-27 19:41:37 -040022
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040023namespace ndncert::ca {
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080024
Davide Pesavento0dc02012021-11-23 22:55:03 -050025const std::string CaMemory::STORAGE_TYPE = "ca-storage-memory";
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080026NDNCERT_REGISTER_CA_STORAGE(CaMemory);
27
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040028CaMemory::CaMemory(const Name&, const std::string&)
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070029 : CaStorage()
30{
31}
32
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070033RequestState
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070034CaMemory::getRequest(const RequestId& requestId)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080035{
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040036 auto it = m_requests.find(requestId);
37 if (it == m_requests.end()) {
38 NDN_THROW(std::runtime_error("Request " + ndn::toHex(requestId) + " does not exist"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080039 }
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040040 return it->second;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080041}
42
43void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070044CaMemory::addRequest(const RequestState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080045{
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040046 auto result = m_requests.insert({request.requestId, request});
47 if (!result.second) {
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -040048 NDN_THROW(std::runtime_error("Request " + ndn::toHex(request.requestId) + " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080049 }
50}
51
52void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070053CaMemory::updateRequest(const RequestState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080054{
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040055 m_requests.insert_or_assign(request.requestId, request);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080056}
57
58void
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070059CaMemory::deleteRequest(const RequestId& requestId)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080060{
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040061 m_requests.erase(requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080062}
63
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070064std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070065CaMemory::listAllRequests()
66{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070067 std::list<RequestState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070068 for (const auto& entry : m_requests) {
69 result.push_back(entry.second);
70 }
71 return result;
72}
73
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070074std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070075CaMemory::listAllRequests(const Name& caName)
76{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070077 std::list<RequestState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070078 for (const auto& entry : m_requests) {
tylerliu7b9185c2020-11-24 12:15:18 -080079 if (entry.second.caPrefix == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070080 result.push_back(entry.second);
81 }
82 }
83 return result;
84}
85
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040086} // namespace ndncert::ca