blob: 52b37a6f6c73eda8e1ad53fd2468a6bd71c25986 [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
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070022/** \brief NFD Management protocol - ControlCommand client
23 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070024class Controller : noncopyable
Alexander Afanasyeve289b532014-02-09 22:14:44 -080025{
26public:
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070027 /** \brief a callback on command success
Alexander Afanasyeve289b532014-02-09 22:14:44 -080028 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070029 typedef function<void(const ControlParameters&)> CommandSucceedCallback;
30
31 /** \brief a callback on command failure
32 */
33 typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback;
34
Yingdi Yue66bf2a2014-04-28 17:07:36 -070035 /** \brief a callback on signing command interest
36 */
37 typedef function<void(Interest&)> Sign;
38
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070039 explicit
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080040 Controller(Face& face);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080041
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070042 /** \brief start command execution
43 */
44 template<typename Command>
45 void
46 start(const ControlParameters& parameters,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070047 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070048 const CommandFailCallback& onFailure,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070049 const IdentityCertificate& certificate = IdentityCertificate(),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070050 const time::milliseconds& timeout = getDefaultCommandTimeout())
51 {
52 start<Command>(parameters, onSuccess, onFailure,
53 bind(&CommandInterestGenerator::generate,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070054 &m_commandInterestGenerator, _1, cref(certificate.getName())),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070055 timeout);
56 }
57
58 template<typename Command>
59 void
60 start(const ControlParameters& parameters,
61 const CommandSucceedCallback& onSuccess,
62 const CommandFailCallback& onFailure,
63 const Name& identity,
64 const time::milliseconds& timeout = getDefaultCommandTimeout())
65 {
66 start<Command>(parameters, onSuccess, onFailure,
67 bind(&CommandInterestGenerator::generateWithIdentity,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070068 &m_commandInterestGenerator, _1, cref(identity)),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070069 timeout);
70 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070071
Yingdi Yue66bf2a2014-04-28 17:07:36 -070072 template<typename Command>
73 void
74 start(const ControlParameters& parameters,
75 const CommandSucceedCallback& onSuccess,
76 const CommandFailCallback& onFailure,
77 const Sign& sign,
78 const time::milliseconds& timeout = getDefaultCommandTimeout());
79
Alexander Afanasyeve289b532014-02-09 22:14:44 -080080private:
81 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070082 processCommandResponse(const Data& data,
83 const shared_ptr<ControlCommand>& command,
84 const CommandSucceedCallback& onSuccess,
85 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -050086
Junxiao Shi5c785d62014-04-20 18:10:20 -070087public:
88 static time::milliseconds
89 getDefaultCommandTimeout()
90 {
91 return time::milliseconds(10000);
92 }
93
hilataa99e37e2014-02-15 23:52:46 -060094protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080095 Face& m_face;
Alexander Afanasyev884280c2014-03-07 09:40:39 +000096 CommandInterestGenerator m_commandInterestGenerator;
Alexander Afanasyeve289b532014-02-09 22:14:44 -080097};
98
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070099template<typename Command>
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700100inline void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700101Controller::start(const ControlParameters& parameters,
102 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -0700103 const CommandFailCallback& onFailure,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700104 const Sign& sign,
105 const time::milliseconds& timeout)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700106{
Junxiao Shi5c785d62014-04-20 18:10:20 -0700107 BOOST_ASSERT(timeout > time::milliseconds::zero());
108
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700109 shared_ptr<ControlCommand> command = make_shared<Command>();
110
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700111 Interest commandInterest = command->makeCommandInterest(parameters, sign);
112
Junxiao Shi5c785d62014-04-20 18:10:20 -0700113 commandInterest.setInterestLifetime(timeout);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700114
115 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx
116 const uint32_t timeoutCode = 10060;
117 m_face.expressInterest(commandInterest,
118 bind(&Controller::processCommandResponse, this, _2,
119 command, onSuccess, onFailure),
120 bind(onFailure, timeoutCode, "Command Interest timed out"));
121}
122
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800123} // namespace nfd
124} // namespace ndn
125
Alexander Afanasyev26c24d22014-03-20 09:31:21 -0700126#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP