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 | |
| 25 | #include "storage/storage-handle.hpp" |
| 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 | { |
| 33 | |
| 34 | public: |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 35 | 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 | |
| 45 | public: |
| 46 | BaseHandle(Face& face, StorageHandle& storageHandle, KeyChain& keyChain, 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) |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 51 | { |
| 52 | } |
| 53 | |
| 54 | virtual void |
| 55 | listen(const Name& prefix) = 0; |
| 56 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 57 | protected: |
| 58 | |
| 59 | inline Face& |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 60 | getFace() |
| 61 | { |
| 62 | return m_face; |
| 63 | } |
| 64 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 65 | inline StorageHandle& |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 66 | getStorageHandle() |
| 67 | { |
| 68 | return m_storageHandle; |
| 69 | } |
| 70 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 71 | 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 Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 94 | private: |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 95 | |
| 96 | Face& m_face; |
| 97 | StorageHandle& m_storageHandle; |
| 98 | KeyChain& m_keyChain; |
| 99 | Scheduler& m_scheduler; |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 102 | inline void |
| 103 | BaseHandle::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 | |
| 111 | inline void |
| 112 | BaseHandle::extractParameter(const Interest& interest, const Name& prefix, |
| 113 | RepoCommandParameter& parameter) |
| 114 | { |
| 115 | parameter.wireDecode(interest.getName().get(prefix.size()).blockFromValue()); |
| 116 | } |
| 117 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame^] | 118 | } // namespace repo |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame^] | 120 | #endif // REPO_HANDLES_BASE_HANDLE_HPP |