Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Regents of the University of California. |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef REPO_NDN_HANDLE_BASE_HANDLE_HPP |
| 8 | #define REPO_NDN_HANDLE_BASE_HANDLE_HPP |
| 9 | |
| 10 | #include "ndn-handle-common.hpp" |
| 11 | |
| 12 | namespace repo { |
| 13 | |
| 14 | class BaseHandle : noncopyable |
| 15 | { |
| 16 | |
| 17 | public: |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame^] | 18 | class Error : std::runtime_error |
| 19 | { |
| 20 | public: |
| 21 | explicit |
| 22 | Error(const std::string& what) |
| 23 | : std::runtime_error(what) |
| 24 | { |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | public: |
| 29 | BaseHandle(Face& face, StorageHandle& storageHandle, KeyChain& keyChain, Scheduler& scheduler) |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 30 | : m_face(face) |
| 31 | , m_storageHandle(storageHandle) |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame^] | 32 | , m_keyChain(keyChain) |
| 33 | , m_scheduler(scheduler) |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
| 37 | virtual void |
| 38 | listen(const Name& prefix) = 0; |
| 39 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame^] | 40 | protected: |
| 41 | |
| 42 | inline Face& |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 43 | getFace() |
| 44 | { |
| 45 | return m_face; |
| 46 | } |
| 47 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame^] | 48 | inline StorageHandle& |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 49 | getStorageHandle() |
| 50 | { |
| 51 | return m_storageHandle; |
| 52 | } |
| 53 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame^] | 54 | inline Scheduler& |
| 55 | getScheduler() |
| 56 | { |
| 57 | return m_scheduler; |
| 58 | } |
| 59 | |
| 60 | uint64_t |
| 61 | generateProcessId(); |
| 62 | |
| 63 | void |
| 64 | reply(const Interest& commandInterest, const RepoCommandResponse& response); |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * @brief extract RepoCommandParameter from a command Interest. |
| 69 | * @param interest command Interest |
| 70 | * @param prefix Name prefix up to command-verb |
| 71 | * @param[out] parameter parsed parameter |
| 72 | * @throw RepoCommandParameter::Error parse error |
| 73 | */ |
| 74 | void |
| 75 | extractParameter(const Interest& interest, const Name& prefix, RepoCommandParameter& parameter); |
| 76 | |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 77 | private: |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame^] | 78 | |
| 79 | Face& m_face; |
| 80 | StorageHandle& m_storageHandle; |
| 81 | KeyChain& m_keyChain; |
| 82 | Scheduler& m_scheduler; |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame^] | 85 | inline void |
| 86 | BaseHandle::reply(const Interest& commandInterest, const RepoCommandResponse& response) |
| 87 | { |
| 88 | Data rdata(commandInterest.getName()); |
| 89 | rdata.setContent(response.wireEncode()); |
| 90 | m_keyChain.sign(rdata); |
| 91 | m_face.put(rdata); |
| 92 | } |
| 93 | |
| 94 | inline void |
| 95 | BaseHandle::extractParameter(const Interest& interest, const Name& prefix, |
| 96 | RepoCommandParameter& parameter) |
| 97 | { |
| 98 | parameter.wireDecode(interest.getName().get(prefix.size()).blockFromValue()); |
| 99 | } |
| 100 | |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 101 | } //namespace repo |
| 102 | |
| 103 | #endif // REPO_NDN_HANDLE_BASE_HANDLE_HPP |