blob: c6b322bf7a7ec373d978177963c72cf74bbf2c38 [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_PUBLISH_PUBLISH_ADAPTER_HPP
20#define ATMOS_PUBLISH_PUBLISH_ADAPTER_HPP
21
22#include "util/catalog-adapter.hpp"
23#include "util/mysql-util.hpp"
24
25#include <json/reader.h>
26#include <json/value.h>
27#include <json/writer.h>
28
29#include <ndn-cxx/face.hpp>
30#include <ndn-cxx/interest.hpp>
31#include <ndn-cxx/interest-filter.hpp>
32#include <ndn-cxx/name.hpp>
33#include <ndn-cxx/security/key-chain.hpp>
34
35#include "mysql/mysql.h"
36
37#include <memory>
38#include <string>
39
40namespace atmos {
41namespace publish {
42
43/**
44 * PublishAdapter handles the Publish usecases for the catalog
45 */
46template <typename DatabaseHandler>
47class PublishAdapter : public atmos::util::CatalogAdapter<DatabaseHandler> {
48public:
49 /**
50 * Constructor
51 *
52 * @param face: Face that will be used for NDN communications
53 * @param keyChain: KeyChain to sign query responses and evaluate the incoming publish
54 * and ChronoSync requests against
55 * @param databaseHandler: <typename DatabaseHandler> to the database that stores our catalog
56 * @oaram prefix: Name that will define the prefix to all publish requests
57 * that will be routed to this specific Catalog Instance
58 */
59 PublishAdapter(std::shared_ptr<ndn::Face> face, std::shared_ptr<ndn::KeyChain> keyChain,
60 std::shared_ptr<DatabaseHandler> databaseHandler, const ndn::Name& prefix);
61
62
63 /**
64 * Destructor
65 */
66 virtual
67 ~PublishAdapter();
68
69protected:
70 /**
71 * Initial "please publish this" Interests
72 *
73 * @param filter: InterestFilter that caused this Interest to be routed
74 * @param interest: Interest that needs to be handled
75 */
76 virtual void
77 onInterest(const ndn::InterestFilter& filter, const ndn::Interest& interest);
78
79 /**
80 * Data containing the actual thing we need to publish
81 *
82 * @param interest: Interest that caused this Data to be routed
83 * @param data: Data that needs to be handled
84 */
85 virtual void
86 onData(const ndn::Interest& interest, const ndn::Data& data);
87
88 // @todo: Should we do anything special with the timeouts for the publish requests?
89 // If so, overwrite onTimeout()
90
91};
92
93template <typename DatabaseHandler>
94PublishAdapter<DatabaseHandler>::PublishAdapter(std::shared_ptr<ndn::Face> face,
95 std::shared_ptr<ndn::KeyChain> keyChain,
96 std::shared_ptr<DatabaseHandler> databaseHandler,
97 const ndn::Name& prefix)
98 : atmos::util::CatalogAdapter<DatabaseHandler>(face, keyChain, databaseHandler,
99 ndn::Name(prefix).append("/publish"))
100{
101 face->setInterestFilter(ndn::InterestFilter(ndn::Name(prefix).append("/publish")),
102 bind(&atmos::publish::PublishAdapter<DatabaseHandler>::onInterest,
103 this, _1, _2),
104 bind(&atmos::publish::PublishAdapter<DatabaseHandler>::onRegisterSuccess,
105 this, _1),
106 bind(&atmos::publish::PublishAdapter<DatabaseHandler>::onRegisterFailure,
107 this, _1, _2));
108
109 std::shared_ptr<ndn::Interest> request(std::make_shared<ndn::Interest>(ndn::Name(prefix).append("/publish")));
110 atmos::util::CatalogAdapter<DatabaseHandler>::m_face->expressInterest(*request,
111 bind(&atmos::publish::PublishAdapter<DatabaseHandler>::onData,
112 this, _1, _2),
113 bind(&atmos::publish::PublishAdapter<DatabaseHandler>::onTimeout,
114 this, _1));
115}
116
117template <typename DatabaseHandler>
118PublishAdapter<DatabaseHandler>::~PublishAdapter()
119{
120 // empty
121}
122
123template <typename DatabaseHandler>
124void
125PublishAdapter<DatabaseHandler>::onInterest(const ndn::InterestFilter& filter, const ndn::Interest& interest)
126{
127 // @todo: Request the data for publish
128}
129
130template <typename DatabaseHandler>
131void
132PublishAdapter<DatabaseHandler>::onData(const ndn::Interest& interest, const ndn::Data& data)
133{
134 // @todo handle publishing the data
135}
136
137} // namespace publish
138} // namespace atmos
139#endif //ATMOS_PUBLISH_PUBLISH_ADAPTER_HPP