blob: 52f21d2d4c5c45473905a80ec4a51a8c40ebe474 [file] [log] [blame]
Alison Craig2a4d5282015-04-10 12:00:02 -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#ifndef ATMOS_UTIL_CATALOG_ADAPTER_HPP
20#define ATMOS_UTIL_CATALOG_ADAPTER_HPP
21
22#include <ndn-cxx/data.hpp>
23#include <ndn-cxx/face.hpp>
24#include <ndn-cxx/interest.hpp>
25#include <ndn-cxx/name.hpp>
26#include <ndn-cxx/security/key-chain.hpp>
27
28#include <memory>
29#include <string>
30
31
32#include <ndn-cxx/encoding/block.hpp>
33
34#include <iostream>
35
36namespace atmos {
37namespace util {
38
39/**
40 * Catalog Adapter acts as a common interface for Classes that need to register as Interest Filters
41 *
42 * Both QueryAdapter and PublisherAdapter use this as a template to allow consistancy between
43 * their designs and flow-control
44 */
45template <typename DatabaseHandler>
46class CatalogAdapter {
47public:
48 /**
49 * Constructor
50 * @param face: Face that will be used for NDN communications
51 * @param keyChain: KeyChain to sign query responses and evaluate the incoming publish
52 * and ChronoSync requests against
53 * @param databaseHandler: <typename DatabaseHandler> to the database that stores our catalog
54 * @oaram prefix: Name that will define the prefix to all queries and publish requests
55 * that will be routed to this specific Catalog Instance
56 */
57 CatalogAdapter(std::shared_ptr<ndn::Face> face, std::shared_ptr<ndn::KeyChain> keyChain,
58 std::shared_ptr<DatabaseHandler> databaseHandler, const ndn::Name& prefix);
59
60 /**
61 * Destructor
62 */
63 virtual
64 ~CatalogAdapter();
65
66protected:
67 // @{ (onData and onTimeout) and/or onInterest should be overwritten at a minimum
68
69
70 /**
71 * Data that is routed to this class based on the Interest
72 *
73 * @param interest: Interest that caused this Data to be routed
74 * @param data: Data that needs to be handled
75 */
76 virtual void
77 onData(const ndn::Interest& interest, const ndn::Data& data);
78
79 /**
80 * Timeout from a Data request
81 *
82 * @param interest: Interest that failed to be satisfied (within the timelimit)
83 */
84 virtual void
85 onTimeout(const ndn::Interest& interest);
86
87
88 /**
89 * Interest that is routed to this class based on the InterestFilter
90 *
91 * @param filter: InterestFilter that caused this Interest to be routed
92 * @param interest: Interest that needs to be handled
93 */
94 virtual void
95 onInterest(const ndn::InterestFilter& filter, const ndn::Interest& interest);
96 // @}
97
98 /**
99 * Callback that should/can be used to evaluate that the Interest Filter has been correctly set up
100 *
101 * @param prefix: Name that will be routed to this class
102 */
103 virtual void
104 onRegisterSuccess(const ndn::Name& prefix);
105
106 /**
107 * Callback that should/can be used to evaluate that the Interest Filter has been correctly set up
108 *
109 * @param prefix: Name that failed to route
110 * @param reason: String explanation as to why the failure occured
111 */
112 virtual void
113 onRegisterFailure(const ndn::Name& prefix, const std::string& reason);
114
115
116 // Face to communicate with
117 std::shared_ptr<ndn::Face> m_face;
118 // KeyChain used for security
119 std::shared_ptr<ndn::KeyChain> m_keyChain;
120 // Handle to the Catalog's database
121 std::shared_ptr<DatabaseHandler> m_databaseHandler;
122 // Prefix for our namespace
123 ndn::Name m_prefix;
124}; // class CatalogAdapter
125
126template <typename DatabaseHandler>
127CatalogAdapter<DatabaseHandler>::CatalogAdapter(std::shared_ptr<ndn::Face> face,
128 std::shared_ptr<ndn::KeyChain> keyChain,
129 std::shared_ptr<DatabaseHandler> databaseHandler,
130 const ndn::Name& prefix)
131 : m_face(face), m_keyChain(keyChain), m_databaseHandler(databaseHandler), m_prefix(prefix)
132{
133 // empty
134}
135
136template <typename DatabaseHandler>
137CatalogAdapter<DatabaseHandler>::~CatalogAdapter()
138{
139 // empty
140}
141
142template <typename DatabaseHandler>
143void
144CatalogAdapter<DatabaseHandler>::onRegisterSuccess(const ndn::Name& prefix)
145{
146 // std::cout << "Successfully registered " << prefix << std::endl;
147}
148
149template <typename DatabaseHandler>
150void
151CatalogAdapter<DatabaseHandler>::onRegisterFailure(const ndn::Name& prefix, const std::string& reason)
152{
153 // std::cout << "Failed to register prefix " << prefix << ": " << reason << std::endl;
154}
155
156
157template <typename DatabaseHandler>
158void
159CatalogAdapter<DatabaseHandler>::onData(const ndn::Interest& interest, const ndn::Data& data)
160{
161 // At this point we need to get the ndn::Block out of data.getContent()
162}
163
164template <typename DatabaseHandler>
165void
166CatalogAdapter<DatabaseHandler>::onInterest(const ndn::InterestFilter& filter, const ndn::Interest& interest)
167{
168 // At this point we need to use the filter to either:
169 // a) Request the Data for the Interest, or
170 // b) Use the Filter to ID where in the Interest the Interest's "Content" is, and grab that out
171}
172
173
174template <typename DatabaseHandler>
175void
176CatalogAdapter<DatabaseHandler>::onTimeout(const ndn::Interest& interest)
177{
178 // At this point, probably should do a retry
179}
180
181} // namespace util
182} // namespace atmos
183
184#endif //ATMOS_UTIL_CATALOG_ADAPTER_HPP