blob: 34b6e546fcd58bc39d77efe06b6439019b80a1c2 [file] [log] [blame]
Alexander Afanasyeve289b532014-02-09 22:14:44 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyeve289b532014-02-09 22:14:44 -080011 */
12
Alexander Afanasyev26c24d22014-03-20 09:31:21 -070013#ifndef NDN_MANAGEMENT_NFD_CONTROLLER_HPP
14#define NDN_MANAGEMENT_NFD_CONTROLLER_HPP
Alexander Afanasyeve289b532014-02-09 22:14:44 -080015
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070016#include "nfd-control-command.hpp"
17#include "../face.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080018
19namespace ndn {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080020namespace nfd {
21
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040022/**
23 * \defgroup management
24 * \brief Classes and data structures to manage NDN forwarder
25 */
26/**
27 * \ingroup management
28 * \brief NFD Management protocol - ControlCommand client
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070029 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070030class Controller : noncopyable
Alexander Afanasyeve289b532014-02-09 22:14:44 -080031{
32public:
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070033 /** \brief a callback on command success
Alexander Afanasyeve289b532014-02-09 22:14:44 -080034 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070035 typedef function<void(const ControlParameters&)> CommandSucceedCallback;
36
37 /** \brief a callback on command failure
38 */
39 typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback;
40
Yingdi Yue66bf2a2014-04-28 17:07:36 -070041 /** \brief a callback on signing command interest
42 */
43 typedef function<void(Interest&)> Sign;
44
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070045 explicit
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080046 Controller(Face& face);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080047
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070048 /** \brief start command execution
49 */
50 template<typename Command>
51 void
52 start(const ControlParameters& parameters,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070053 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070054 const CommandFailCallback& onFailure,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070055 const IdentityCertificate& certificate = IdentityCertificate(),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070056 const time::milliseconds& timeout = getDefaultCommandTimeout())
57 {
58 start<Command>(parameters, onSuccess, onFailure,
59 bind(&CommandInterestGenerator::generate,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070060 &m_commandInterestGenerator, _1, cref(certificate.getName())),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070061 timeout);
62 }
63
64 template<typename Command>
65 void
66 start(const ControlParameters& parameters,
67 const CommandSucceedCallback& onSuccess,
68 const CommandFailCallback& onFailure,
69 const Name& identity,
70 const time::milliseconds& timeout = getDefaultCommandTimeout())
71 {
72 start<Command>(parameters, onSuccess, onFailure,
73 bind(&CommandInterestGenerator::generateWithIdentity,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070074 &m_commandInterestGenerator, _1, cref(identity)),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070075 timeout);
76 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070077
Yingdi Yue66bf2a2014-04-28 17:07:36 -070078 template<typename Command>
79 void
80 start(const ControlParameters& parameters,
81 const CommandSucceedCallback& onSuccess,
82 const CommandFailCallback& onFailure,
83 const Sign& sign,
84 const time::milliseconds& timeout = getDefaultCommandTimeout());
85
Alexander Afanasyeve289b532014-02-09 22:14:44 -080086private:
87 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070088 processCommandResponse(const Data& data,
89 const shared_ptr<ControlCommand>& command,
90 const CommandSucceedCallback& onSuccess,
91 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -050092
Junxiao Shi5c785d62014-04-20 18:10:20 -070093public:
94 static time::milliseconds
95 getDefaultCommandTimeout()
96 {
97 return time::milliseconds(10000);
98 }
99
hilataa99e37e2014-02-15 23:52:46 -0600100protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800101 Face& m_face;
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000102 CommandInterestGenerator m_commandInterestGenerator;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800103};
104
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700105template<typename Command>
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700106inline void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700107Controller::start(const ControlParameters& parameters,
108 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -0700109 const CommandFailCallback& onFailure,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700110 const Sign& sign,
111 const time::milliseconds& timeout)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700112{
Junxiao Shi5c785d62014-04-20 18:10:20 -0700113 BOOST_ASSERT(timeout > time::milliseconds::zero());
114
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700115 shared_ptr<ControlCommand> command = make_shared<Command>();
116
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700117 Interest commandInterest = command->makeCommandInterest(parameters, sign);
118
Junxiao Shi5c785d62014-04-20 18:10:20 -0700119 commandInterest.setInterestLifetime(timeout);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700120
121 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx
122 const uint32_t timeoutCode = 10060;
123 m_face.expressInterest(commandInterest,
124 bind(&Controller::processCommandResponse, this, _2,
125 command, onSuccess, onFailure),
126 bind(onFailure, timeoutCode, "Command Interest timed out"));
127}
128
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800129} // namespace nfd
130} // namespace ndn
131
Alexander Afanasyev26c24d22014-03-20 09:31:21 -0700132#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP