blob: 44ac4a88fe23f0ea1f12473656258a0ee0ad9169 [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
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:
Shuo Chen29c77fe2014-03-18 11:29:41 -070034 class Error : std::runtime_error
35 {
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)
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)
Weiqi Shif0330d52014-07-09 10:54:27 -070051 // , m_storeindex(storeindex)
Shuo Chen9c2477f2014-03-13 15:01:06 -070052 {
53 }
54
55 virtual void
56 listen(const Name& prefix) = 0;
57
Shuo Chen29c77fe2014-03-18 11:29:41 -070058protected:
59
60 inline Face&
Shuo Chen9c2477f2014-03-13 15:01:06 -070061 getFace()
62 {
63 return m_face;
64 }
65
Weiqi Shif0330d52014-07-09 10:54:27 -070066 inline RepoStorage&
Shuo Chen9c2477f2014-03-13 15:01:06 -070067 getStorageHandle()
68 {
69 return m_storageHandle;
70 }
71
Shuo Chen29c77fe2014-03-18 11:29:41 -070072 inline Scheduler&
73 getScheduler()
74 {
75 return m_scheduler;
76 }
Alexander Afanasyevb7e8a812014-07-23 01:36:47 -070077
78 // inline RepoStorage&
79 // getStoreIndex()
80 // {
81 // return m_storeindex;
82 // }
83
Shuo Chen29c77fe2014-03-18 11:29:41 -070084 uint64_t
85 generateProcessId();
86
87 void
88 reply(const Interest& commandInterest, const RepoCommandResponse& response);
89
90
91 /**
92 * @brief extract RepoCommandParameter from a command Interest.
93 * @param interest command Interest
94 * @param prefix Name prefix up to command-verb
95 * @param[out] parameter parsed parameter
96 * @throw RepoCommandParameter::Error parse error
97 */
98 void
99 extractParameter(const Interest& interest, const Name& prefix, RepoCommandParameter& parameter);
100
Shuo Chen9c2477f2014-03-13 15:01:06 -0700101private:
Shuo Chen29c77fe2014-03-18 11:29:41 -0700102
103 Face& m_face;
Weiqi Shif0330d52014-07-09 10:54:27 -0700104 RepoStorage& m_storageHandle;
Shuo Chen29c77fe2014-03-18 11:29:41 -0700105 KeyChain& m_keyChain;
106 Scheduler& m_scheduler;
Weiqi Shif0330d52014-07-09 10:54:27 -0700107 // RepoStorage& m_storeindex;
Shuo Chen9c2477f2014-03-13 15:01:06 -0700108};
109
Shuo Chen29c77fe2014-03-18 11:29:41 -0700110inline void
111BaseHandle::reply(const Interest& commandInterest, const RepoCommandResponse& response)
112{
Weiqi Shi098f91c2014-07-23 17:41:35 -0700113 shared_ptr<Data> rdata = make_shared<Data>(commandInterest.getName());
114 rdata->setContent(response.wireEncode());
115 m_keyChain.sign(*rdata);
116 m_face.put(*rdata);
Shuo Chen29c77fe2014-03-18 11:29:41 -0700117}
118
119inline void
120BaseHandle::extractParameter(const Interest& interest, const Name& prefix,
121 RepoCommandParameter& parameter)
122{
123 parameter.wireDecode(interest.getName().get(prefix.size()).blockFromValue());
124}
125
Alexander Afanasyev39d98072014-05-04 12:46:29 -0700126} // namespace repo
Shuo Chen9c2477f2014-03-13 15:01:06 -0700127
Alexander Afanasyev39d98072014-05-04 12:46:29 -0700128#endif // REPO_HANDLES_BASE_HANDLE_HPP