blob: bf195c6ab367c5e09f9162a11b8c1b094cdd110f [file] [log] [blame]
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#ifndef NFD_TOOLS_NDN_AUTOCONFIG_BASE_HPP
27#define NFD_TOOLS_NDN_AUTOCONFIG_BASE_HPP
28
29#include "common.hpp"
30
31#include <boost/noncopyable.hpp>
32
33#include <ndn-cxx/face.hpp>
34#include <ndn-cxx/security/key-chain.hpp>
35#include <ndn-cxx/management/nfd-controller.hpp>
36#include <ndn-cxx/management/nfd-face-status.hpp>
37#include <ndn-cxx/encoding/buffer-stream.hpp>
38#include <ndn-cxx/util/face-uri.hpp>
39
40namespace ndn {
41namespace tools {
42namespace autoconfig {
43
44/**
45 * @brief Base class for discovery stages
46 */
47class Base : boost::noncopyable
48{
49public:
50 class Error : public std::runtime_error
51 {
52 public:
53 explicit
54 Error(const std::string& what)
55 : std::runtime_error(what)
56 {
57 }
58 };
59
60 /**
61 * @brief Callback to be called when the stage fails
62 */
63 typedef std::function<void(const std::string&)> NextStageCallback;
64
65 /**
66 * @brief Start the stage
67 */
68 virtual void
69 start() = 0;
70
71protected:
72 /**
73 * @brief Initialize variables and create nfd::Controller instance
74 * @param face Face to be used for all operations (e.g., will send registration commands)
75 * @param keyChain KeyChain object
76 * @param nextStageOnFailure Callback to be called after the stage failed
77 */
78 Base(Face& face, KeyChain& keyChain, const NextStageCallback& nextStageOnFailure);
79
80 /**
81 * @brief Attempt to connect to local hub using the \p uri FaceUri
82 * @throw Base::Error when failed to establish the tunnel
83 */
84 void
85 connectToHub(const std::string& uri);
86
87private:
88 void
89 onCanonizeSuccess(const util::FaceUri& canonicalUri);
90
91 void
92 onCanonizeFailure(const std::string& reason);
93
94 void
95 onHubConnectSuccess(const nfd::ControlParameters& resp);
96
97 void
98 onHubConnectError(uint32_t code, const std::string& error);
99
100 void
101 registerPrefix(const Name& prefix, uint64_t faceId);
102
103 void
104 onPrefixRegistrationSuccess(const nfd::ControlParameters& commandSuccessResult);
105
106 void
107 onPrefixRegistrationError(uint32_t code, const std::string& error);
108
109protected:
110 Face& m_face;
111 KeyChain& m_keyChain;
112 nfd::Controller m_controller;
113 NextStageCallback m_nextStageOnFailure;
114};
115
116} // namespace autoconfig
117} // namespace tools
118} // namespace ndn
119
120#endif // NFD_TOOLS_NDN_AUTOCONFIG_BASE_HPP