blob: 5d12076c046a6ce0a9cd4f72c3c67ebe47ff0f6f [file] [log] [blame]
Shuo Chen9c2477f2014-03-13 15:01:06 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev42290b22017-03-09 12:58:29 -08003 * Copyright (c) 2014-2017, Regents of the University of California.
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -07004 *
5 * This file is part of NDN repo-ng (Next generation of NDN repository).
6 * See AUTHORS.md for complete list of repo-ng authors and contributors.
7 *
8 * repo-ng 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.
11 *
12 * repo-ng 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.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Shuo Chen9c2477f2014-03-13 15:01:06 -070018 */
19
Alexander Afanasyev39d98072014-05-04 12:46:29 -070020#ifndef REPO_HANDLES_BASE_HANDLE_HPP
21#define REPO_HANDLES_BASE_HANDLE_HPP
Shuo Chen9c2477f2014-03-13 15:01:06 -070022
Alexander Afanasyev39d98072014-05-04 12:46:29 -070023#include "common.hpp"
24
Weiqi Shif0330d52014-07-09 10:54:27 -070025#include "storage/repo-storage.hpp"
Alexander Afanasyev39d98072014-05-04 12:46:29 -070026#include "repo-command-response.hpp"
27#include "repo-command-parameter.hpp"
Shuo Chen9c2477f2014-03-13 15:01:06 -070028
29namespace repo {
30
31class BaseHandle : noncopyable
32{
Shuo Chen9c2477f2014-03-13 15:01:06 -070033public:
Alexander Afanasyev42290b22017-03-09 12:58:29 -080034 class Error : public std::runtime_error
Shuo Chen29c77fe2014-03-18 11:29:41 -070035 {
36 public:
37 explicit
38 Error(const std::string& what)
39 : std::runtime_error(what)
40 {
41 }
42 };
43
44public:
Weiqi Shif0330d52014-07-09 10:54:27 -070045 BaseHandle(Face& face, RepoStorage& storageHandle, KeyChain& keyChain,
46 Scheduler& scheduler)
Nick Gordon190e4dc2017-10-04 16:54:10 -050047 : m_storageHandle(storageHandle)
48 , m_face(face)
Shuo Chen29c77fe2014-03-18 11:29:41 -070049 , m_keyChain(keyChain)
50 , m_scheduler(scheduler)
Weiqi Shif0330d52014-07-09 10:54:27 -070051 // , m_storeindex(storeindex)
Shuo Chen9c2477f2014-03-13 15:01:06 -070052 {
53 }
54
Junxiao Shie1801312017-07-22 19:16:49 +000055 virtual
56 ~BaseHandle() = default;
57
Shuo Chen9c2477f2014-03-13 15:01:06 -070058 virtual void
59 listen(const Name& prefix) = 0;
60
Shuo Chen29c77fe2014-03-18 11:29:41 -070061protected:
62
63 inline Face&
Shuo Chen9c2477f2014-03-13 15:01:06 -070064 getFace()
65 {
66 return m_face;
67 }
68
Weiqi Shif0330d52014-07-09 10:54:27 -070069 inline RepoStorage&
Shuo Chen9c2477f2014-03-13 15:01:06 -070070 getStorageHandle()
71 {
72 return m_storageHandle;
73 }
74
Shuo Chen29c77fe2014-03-18 11:29:41 -070075 inline Scheduler&
76 getScheduler()
77 {
78 return m_scheduler;
79 }
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -070080
81 // inline RepoStorage&
82 // getStoreIndex()
83 // {
84 // return m_storeindex;
85 // }
86
Shuo Chen29c77fe2014-03-18 11:29:41 -070087 uint64_t
88 generateProcessId();
89
90 void
91 reply(const Interest& commandInterest, const RepoCommandResponse& response);
92
93
94 /**
95 * @brief extract RepoCommandParameter from a command Interest.
96 * @param interest command Interest
97 * @param prefix Name prefix up to command-verb
98 * @param[out] parameter parsed parameter
99 * @throw RepoCommandParameter::Error parse error
100 */
101 void
102 extractParameter(const Interest& interest, const Name& prefix, RepoCommandParameter& parameter);
103
Nick Gordon190e4dc2017-10-04 16:54:10 -0500104protected:
Weiqi Shif0330d52014-07-09 10:54:27 -0700105 RepoStorage& m_storageHandle;
Nick Gordon190e4dc2017-10-04 16:54:10 -0500106
107private:
108 Face& m_face;
Shuo Chen29c77fe2014-03-18 11:29:41 -0700109 KeyChain& m_keyChain;
110 Scheduler& m_scheduler;
Weiqi Shif0330d52014-07-09 10:54:27 -0700111 // RepoStorage& m_storeindex;
Shuo Chen9c2477f2014-03-13 15:01:06 -0700112};
113
Shuo Chen29c77fe2014-03-18 11:29:41 -0700114inline void
115BaseHandle::reply(const Interest& commandInterest, const RepoCommandResponse& response)
116{
Wentao Shanga8f3c402014-10-30 14:03:27 -0700117 std::shared_ptr<Data> rdata = std::make_shared<Data>(commandInterest.getName());
Weiqi Shi098f91c2014-07-23 17:41:35 -0700118 rdata->setContent(response.wireEncode());
119 m_keyChain.sign(*rdata);
120 m_face.put(*rdata);
Shuo Chen29c77fe2014-03-18 11:29:41 -0700121}
122
123inline void
124BaseHandle::extractParameter(const Interest& interest, const Name& prefix,
125 RepoCommandParameter& parameter)
126{
127 parameter.wireDecode(interest.getName().get(prefix.size()).blockFromValue());
128}
129
Alexander Afanasyev39d98072014-05-04 12:46:29 -0700130} // namespace repo
Shuo Chen9c2477f2014-03-13 15:01:06 -0700131
Alexander Afanasyev39d98072014-05-04 12:46:29 -0700132#endif // REPO_HANDLES_BASE_HANDLE_HPP