blob: 7df409b2835ba8085c47cae13b56417cbe9db5ae [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
20#ifndef REPO_NDN_HANDLE_BASE_HANDLE_HPP
21#define REPO_NDN_HANDLE_BASE_HANDLE_HPP
22
23#include "ndn-handle-common.hpp"
24
25namespace repo {
26
27class BaseHandle : noncopyable
28{
29
30public:
Shuo Chen29c77fe2014-03-18 11:29:41 -070031 class Error : std::runtime_error
32 {
33 public:
34 explicit
35 Error(const std::string& what)
36 : std::runtime_error(what)
37 {
38 }
39 };
40
41public:
42 BaseHandle(Face& face, StorageHandle& storageHandle, KeyChain& keyChain, Scheduler& scheduler)
Shuo Chen9c2477f2014-03-13 15:01:06 -070043 : m_face(face)
44 , m_storageHandle(storageHandle)
Shuo Chen29c77fe2014-03-18 11:29:41 -070045 , m_keyChain(keyChain)
46 , m_scheduler(scheduler)
Shuo Chen9c2477f2014-03-13 15:01:06 -070047 {
48 }
49
50 virtual void
51 listen(const Name& prefix) = 0;
52
Shuo Chen29c77fe2014-03-18 11:29:41 -070053protected:
54
55 inline Face&
Shuo Chen9c2477f2014-03-13 15:01:06 -070056 getFace()
57 {
58 return m_face;
59 }
60
Shuo Chen29c77fe2014-03-18 11:29:41 -070061 inline StorageHandle&
Shuo Chen9c2477f2014-03-13 15:01:06 -070062 getStorageHandle()
63 {
64 return m_storageHandle;
65 }
66
Shuo Chen29c77fe2014-03-18 11:29:41 -070067 inline Scheduler&
68 getScheduler()
69 {
70 return m_scheduler;
71 }
72
73 uint64_t
74 generateProcessId();
75
76 void
77 reply(const Interest& commandInterest, const RepoCommandResponse& response);
78
79
80 /**
81 * @brief extract RepoCommandParameter from a command Interest.
82 * @param interest command Interest
83 * @param prefix Name prefix up to command-verb
84 * @param[out] parameter parsed parameter
85 * @throw RepoCommandParameter::Error parse error
86 */
87 void
88 extractParameter(const Interest& interest, const Name& prefix, RepoCommandParameter& parameter);
89
Shuo Chen9c2477f2014-03-13 15:01:06 -070090private:
Shuo Chen29c77fe2014-03-18 11:29:41 -070091
92 Face& m_face;
93 StorageHandle& m_storageHandle;
94 KeyChain& m_keyChain;
95 Scheduler& m_scheduler;
Shuo Chen9c2477f2014-03-13 15:01:06 -070096};
97
Shuo Chen29c77fe2014-03-18 11:29:41 -070098inline void
99BaseHandle::reply(const Interest& commandInterest, const RepoCommandResponse& response)
100{
101 Data rdata(commandInterest.getName());
102 rdata.setContent(response.wireEncode());
103 m_keyChain.sign(rdata);
104 m_face.put(rdata);
105}
106
107inline void
108BaseHandle::extractParameter(const Interest& interest, const Name& prefix,
109 RepoCommandParameter& parameter)
110{
111 parameter.wireDecode(interest.getName().get(prefix.size()).blockFromValue());
112}
113
Shuo Chen9c2477f2014-03-13 15:01:06 -0700114} //namespace repo
115
116#endif // REPO_NDN_HANDLE_BASE_HANDLE_HPP