blob: ebec2f9e2cc42f411f022031e3786985fd4984a8 [file] [log] [blame]
Chengyu Fanb25835b2015-04-28 17:09:35 -06001/** NDN-Atmos: Cataloging Service for distributed data originally developed
2 * for atmospheric science data
3 * Copyright (C) 2015 Colorado State University
4 *
5 * NDN-Atmos is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * NDN-Atmos is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NDN-Atmos. If not, see <http://www.gnu.org/licenses/>.
17**/
18
19#include "catalog.hpp"
20
21namespace atmos {
22namespace catalog {
23
24Catalog::Catalog(const std::shared_ptr<ndn::Face>& face,
25 const std::shared_ptr<ndn::KeyChain>& keyChain,
26 const std::string& configFileName)
27 : m_face(face)
28 , m_keyChain(keyChain)
29 , m_configFile(configFileName)
30{
31 // empty
32}
33
34Catalog::~Catalog()
35{
36 // empty
37}
38
39void
40Catalog::onConfig(const util::ConfigSection& configSection,
41 bool isDryRun,
42 const std::string& fileName)
43{
44 if (isDryRun) {
45 return;
46 }
47 for (auto i = configSection.begin();
48 i != configSection.end();
49 ++ i)
50 {
51 if (i->first == "prefix") {
52 m_prefix.clear();
53 m_prefix.append(i->second.get_value<std::string>());
54 if (m_prefix.empty()) {
55 throw Error("Empty value for \"prefix\""
56 " in \"general\" section");
57 }
58 }
59 }
60}
61
62void
63Catalog::addAdapter(std::unique_ptr<util::CatalogAdapter>& adapter)
64{
65 m_adapters.push_back(std::move(adapter));
66}
67
68void
69Catalog::initializeCatalog()
70{
71 util::ConfigFile config(&util::ConfigFile::ignoreUnknownSection);
72
73 config.addSectionHandler("general", bind(&Catalog::onConfig, this, _1, _2, _3));
74
75 config.parse(m_configFile, true);
76 config.parse(m_configFile, false);
77}
78
79void
80Catalog::initializeAdapters()
81{
82 util::ConfigFile config(&util::ConfigFile::ignoreUnknownSection);
83 for (auto i = m_adapters.begin();
84 i != m_adapters.end();
85 ++ i)
86 {
87 (*i)->setConfigFile(config, m_prefix);
88 }
89
90 config.parse(m_configFile, true);
91 config.parse(m_configFile, false);
92}
93
94void
95Catalog::initialize()
96{
97 initializeCatalog();
98 initializeAdapters();
99}
100
101} // namespace catalog
102} // namespace atmos