blob: 7cdfccad3e4f522f9b88416e430a8b3691ee4758 [file] [log] [blame]
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi9f5b01d2016-08-05 03:54:28 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08004 * 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
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000029#include "core/common.hpp"
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080030
31#include <ndn-cxx/face.hpp>
Junxiao Shi52fa45c2016-11-29 21:18:13 +000032#include <ndn-cxx/encoding/buffer-stream.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000033#include <ndn-cxx/mgmt/nfd/controller.hpp>
34#include <ndn-cxx/mgmt/nfd/face-status.hpp>
Junxiao Shi52fa45c2016-11-29 21:18:13 +000035#include <ndn-cxx/security/key-chain.hpp>
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080036#include <ndn-cxx/util/face-uri.hpp>
37
38namespace ndn {
39namespace tools {
40namespace autoconfig {
41
Junxiao Shi52fa45c2016-11-29 21:18:13 +000042using ndn::nfd::ControlParameters;
43using ndn::nfd::ControlResponse;
44using ndn::util::FaceUri;
45
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080046/**
47 * @brief Base class for discovery stages
48 */
49class Base : boost::noncopyable
50{
51public:
52 class Error : public std::runtime_error
53 {
54 public:
55 explicit
56 Error(const std::string& what)
57 : std::runtime_error(what)
58 {
59 }
60 };
61
62 /**
63 * @brief Callback to be called when the stage fails
64 */
65 typedef std::function<void(const std::string&)> NextStageCallback;
66
67 /**
68 * @brief Start the stage
69 */
70 virtual void
71 start() = 0;
72
73protected:
74 /**
Junxiao Shi52fa45c2016-11-29 21:18:13 +000075 * @brief Initialize variables and create Controller instance
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080076 * @param face Face to be used for all operations (e.g., will send registration commands)
77 * @param keyChain KeyChain object
78 * @param nextStageOnFailure Callback to be called after the stage failed
79 */
80 Base(Face& face, KeyChain& keyChain, const NextStageCallback& nextStageOnFailure);
81
82 /**
83 * @brief Attempt to connect to local hub using the \p uri FaceUri
84 * @throw Base::Error when failed to establish the tunnel
85 */
86 void
87 connectToHub(const std::string& uri);
88
89private:
90 void
Junxiao Shi52fa45c2016-11-29 21:18:13 +000091 onCanonizeSuccess(const FaceUri& canonicalUri);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080092
93 void
94 onCanonizeFailure(const std::string& reason);
95
96 void
Junxiao Shi52fa45c2016-11-29 21:18:13 +000097 onHubConnectSuccess(const ControlParameters& resp);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080098
99 void
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000100 onHubConnectError(const ControlResponse& response);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800101
102 void
103 registerPrefix(const Name& prefix, uint64_t faceId);
104
105 void
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000106 onPrefixRegistrationSuccess(const ControlParameters& commandSuccessResult);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800107
108 void
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000109 onPrefixRegistrationError(const ControlResponse& response);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800110
111protected:
112 Face& m_face;
113 KeyChain& m_keyChain;
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000114 ndn::nfd::Controller m_controller;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800115 NextStageCallback m_nextStageOnFailure;
116};
117
118} // namespace autoconfig
119} // namespace tools
120} // namespace ndn
121
122#endif // NFD_TOOLS_NDN_AUTOCONFIG_BASE_HPP