blob: 205b8844ebe6e2bc16d70a5132526178a9f1e632 [file] [log] [blame]
weijia yuan82cf9142018-10-21 12:25:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2018, 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/>.
18 */
19
20#ifndef REPO_HANDLES_COMMAND_BASE_HANDLE_HPP
21#define REPO_HANDLES_COMMAND_BASE_HANDLE_HPP
22
23#include "common.hpp"
24
25#include "storage/repo-storage.hpp"
26#include "repo-command-response.hpp"
27#include "repo-command-parameter.hpp"
28#include "repo-command.hpp"
29
30#include <ndn-cxx/mgmt/dispatcher.hpp>
31
32namespace repo {
33
34class CommandBaseHandle
35{
36public:
37 class Error : public std::runtime_error
38 {
39 public:
40 explicit
41 Error(const std::string& what)
42 : std::runtime_error(what)
43 {
44 }
45 };
46
47public:
48 CommandBaseHandle(Face& face, RepoStorage& storageHandle,
49 Scheduler& scheduler, Validator& validator);
50
51 virtual
52 ~CommandBaseHandle() = default;
53
54 ndn::mgmt::Authorization
55 makeAuthorization();
56
57 template<typename T>
58 bool
59 validateParameters(const ndn::mgmt::ControlParameters& parameters)
60 {
61 const RepoCommandParameter* castParams =
62 dynamic_cast<const RepoCommandParameter*>(&parameters);
63 BOOST_ASSERT(castParams != nullptr);
64 T command;
65 try {
66 command.validateRequest(*castParams);
67 }
68 catch (const RepoCommand::ArgumentError& ae) {
69 return false;
70 }
71 return true;
72 }
73
74protected:
75 Face& face;
76 RepoStorage& storageHandle;
77 Scheduler& scheduler;
78
79private:
80 Validator& m_validator;
81};
82} // namespace repo
83
84#endif // REPO_HANDLES_COMMAND_BASE_HANDLE_HPP