Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 3 | * 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 Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 20 | #ifndef REPO_HANDLES_BASE_HANDLE_HPP |
| 21 | #define REPO_HANDLES_BASE_HANDLE_HPP |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 23 | #include "common.hpp" |
| 24 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame^] | 25 | #include "storage/repo-storage.hpp" |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 26 | #include "repo-command-response.hpp" |
| 27 | #include "repo-command-parameter.hpp" |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 28 | |
| 29 | namespace repo { |
| 30 | |
| 31 | class BaseHandle : noncopyable |
| 32 | { |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 33 | public: |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 34 | 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 | |
| 44 | public: |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame^] | 45 | BaseHandle(Face& face, RepoStorage& storageHandle, KeyChain& keyChain, |
| 46 | Scheduler& scheduler) |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 47 | : m_face(face) |
| 48 | , m_storageHandle(storageHandle) |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 49 | , m_keyChain(keyChain) |
| 50 | , m_scheduler(scheduler) |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame^] | 51 | // , m_storeindex(storeindex) |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
| 55 | virtual void |
| 56 | listen(const Name& prefix) = 0; |
| 57 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 58 | protected: |
| 59 | |
| 60 | inline Face& |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 61 | getFace() |
| 62 | { |
| 63 | return m_face; |
| 64 | } |
| 65 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame^] | 66 | inline RepoStorage& |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 67 | getStorageHandle() |
| 68 | { |
| 69 | return m_storageHandle; |
| 70 | } |
| 71 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 72 | inline Scheduler& |
| 73 | getScheduler() |
| 74 | { |
| 75 | return m_scheduler; |
| 76 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame^] | 77 | /* |
| 78 | inline RepoStorage& |
| 79 | getStoreIndex() |
| 80 | { |
| 81 | return m_storeindex; |
| 82 | } |
| 83 | */ |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 84 | 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 Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 101 | private: |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 102 | |
| 103 | Face& m_face; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame^] | 104 | RepoStorage& m_storageHandle; |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 105 | KeyChain& m_keyChain; |
| 106 | Scheduler& m_scheduler; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame^] | 107 | // RepoStorage& m_storeindex; |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 110 | inline void |
| 111 | BaseHandle::reply(const Interest& commandInterest, const RepoCommandResponse& response) |
| 112 | { |
| 113 | Data rdata(commandInterest.getName()); |
| 114 | rdata.setContent(response.wireEncode()); |
| 115 | m_keyChain.sign(rdata); |
| 116 | m_face.put(rdata); |
| 117 | } |
| 118 | |
| 119 | inline void |
| 120 | BaseHandle::extractParameter(const Interest& interest, const Name& prefix, |
| 121 | RepoCommandParameter& parameter) |
| 122 | { |
| 123 | parameter.wireDecode(interest.getName().get(prefix.size()).blockFromValue()); |
| 124 | } |
| 125 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 126 | } // namespace repo |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 127 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 128 | #endif // REPO_HANDLES_BASE_HANDLE_HPP |