blob: 32239406bd7af414baccd4a628447367ce02f209 [file] [log] [blame]
Shuo Chen9c2477f2014-03-13 15:01:06 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -07003 * Copyright (c) 2014, Regents of the University of California.
4 *
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
25#include "storage/storage-handle.hpp"
26#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{
33
34public:
Shuo Chen29c77fe2014-03-18 11:29:41 -070035 class Error : std::runtime_error
36 {
37 public:
38 explicit
39 Error(const std::string& what)
40 : std::runtime_error(what)
41 {
42 }
43 };
44
45public:
46 BaseHandle(Face& face, StorageHandle& storageHandle, KeyChain& keyChain, Scheduler& scheduler)
Shuo Chen9c2477f2014-03-13 15:01:06 -070047 : m_face(face)
48 , m_storageHandle(storageHandle)
Shuo Chen29c77fe2014-03-18 11:29:41 -070049 , m_keyChain(keyChain)
50 , m_scheduler(scheduler)
Shuo Chen9c2477f2014-03-13 15:01:06 -070051 {
52 }
53
54 virtual void
55 listen(const Name& prefix) = 0;
56
Shuo Chen29c77fe2014-03-18 11:29:41 -070057protected:
58
59 inline Face&
Shuo Chen9c2477f2014-03-13 15:01:06 -070060 getFace()
61 {
62 return m_face;
63 }
64
Shuo Chen29c77fe2014-03-18 11:29:41 -070065 inline StorageHandle&
Shuo Chen9c2477f2014-03-13 15:01:06 -070066 getStorageHandle()
67 {
68 return m_storageHandle;
69 }
70
Shuo Chen29c77fe2014-03-18 11:29:41 -070071 inline Scheduler&
72 getScheduler()
73 {
74 return m_scheduler;
75 }
76
77 uint64_t
78 generateProcessId();
79
80 void
81 reply(const Interest& commandInterest, const RepoCommandResponse& response);
82
83
84 /**
85 * @brief extract RepoCommandParameter from a command Interest.
86 * @param interest command Interest
87 * @param prefix Name prefix up to command-verb
88 * @param[out] parameter parsed parameter
89 * @throw RepoCommandParameter::Error parse error
90 */
91 void
92 extractParameter(const Interest& interest, const Name& prefix, RepoCommandParameter& parameter);
93
Shuo Chen9c2477f2014-03-13 15:01:06 -070094private:
Shuo Chen29c77fe2014-03-18 11:29:41 -070095
96 Face& m_face;
97 StorageHandle& m_storageHandle;
98 KeyChain& m_keyChain;
99 Scheduler& m_scheduler;
Shuo Chen9c2477f2014-03-13 15:01:06 -0700100};
101
Shuo Chen29c77fe2014-03-18 11:29:41 -0700102inline void
103BaseHandle::reply(const Interest& commandInterest, const RepoCommandResponse& response)
104{
105 Data rdata(commandInterest.getName());
106 rdata.setContent(response.wireEncode());
107 m_keyChain.sign(rdata);
108 m_face.put(rdata);
109}
110
111inline void
112BaseHandle::extractParameter(const Interest& interest, const Name& prefix,
113 RepoCommandParameter& parameter)
114{
115 parameter.wireDecode(interest.getName().get(prefix.size()).blockFromValue());
116}
117
Alexander Afanasyev39d98072014-05-04 12:46:29 -0700118} // namespace repo
Shuo Chen9c2477f2014-03-13 15:01:06 -0700119
Alexander Afanasyev39d98072014-05-04 12:46:29 -0700120#endif // REPO_HANDLES_BASE_HANDLE_HPP