blob: 05a207031d93773a9e5042b73e9bc43108a7310e [file] [log] [blame]
Shuo Chen9c2477f2014-03-13 15:01:06 -07001/* -*- 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
12namespace repo {
13
14class BaseHandle : noncopyable
15{
16
17public:
Shuo Chen29c77fe2014-03-18 11:29:41 -070018 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
28public:
29 BaseHandle(Face& face, StorageHandle& storageHandle, KeyChain& keyChain, Scheduler& scheduler)
Shuo Chen9c2477f2014-03-13 15:01:06 -070030 : m_face(face)
31 , m_storageHandle(storageHandle)
Shuo Chen29c77fe2014-03-18 11:29:41 -070032 , m_keyChain(keyChain)
33 , m_scheduler(scheduler)
Shuo Chen9c2477f2014-03-13 15:01:06 -070034 {
35 }
36
37 virtual void
38 listen(const Name& prefix) = 0;
39
Shuo Chen29c77fe2014-03-18 11:29:41 -070040protected:
41
42 inline Face&
Shuo Chen9c2477f2014-03-13 15:01:06 -070043 getFace()
44 {
45 return m_face;
46 }
47
Shuo Chen29c77fe2014-03-18 11:29:41 -070048 inline StorageHandle&
Shuo Chen9c2477f2014-03-13 15:01:06 -070049 getStorageHandle()
50 {
51 return m_storageHandle;
52 }
53
Shuo Chen29c77fe2014-03-18 11:29:41 -070054 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 Chen9c2477f2014-03-13 15:01:06 -070077private:
Shuo Chen29c77fe2014-03-18 11:29:41 -070078
79 Face& m_face;
80 StorageHandle& m_storageHandle;
81 KeyChain& m_keyChain;
82 Scheduler& m_scheduler;
Shuo Chen9c2477f2014-03-13 15:01:06 -070083};
84
Shuo Chen29c77fe2014-03-18 11:29:41 -070085inline void
86BaseHandle::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
94inline void
95BaseHandle::extractParameter(const Interest& interest, const Name& prefix,
96 RepoCommandParameter& parameter)
97{
98 parameter.wireDecode(interest.getName().get(prefix.size()).blockFromValue());
99}
100
Shuo Chen9c2477f2014-03-13 15:01:06 -0700101} //namespace repo
102
103#endif // REPO_NDN_HANDLE_BASE_HANDLE_HPP