blob: 91025dec5bcd77d3d52df646d0990cd2c50f7561 [file] [log] [blame]
Alexander Afanasyeve289b532014-02-09 22:14:44 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * See COPYING for copyright and distribution information.
5 */
6
Alexander Afanasyev26c24d22014-03-20 09:31:21 -07007#ifndef NDN_MANAGEMENT_NFD_CONTROLLER_HPP
8#define NDN_MANAGEMENT_NFD_CONTROLLER_HPP
Alexander Afanasyeve289b532014-02-09 22:14:44 -08009
Alexander Afanasyeve289b532014-02-09 22:14:44 -080010#include "controller.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070011#include "nfd-control-command.hpp"
12#include "../face.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080013
14namespace ndn {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080015namespace nfd {
16
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070017/** \brief NFD Management protocol - ControlCommand client
18 */
Alexander Afanasyeve289b532014-02-09 22:14:44 -080019class Controller : public ndn::Controller
20{
21public:
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070022 /** \brief a callback on command success
Alexander Afanasyeve289b532014-02-09 22:14:44 -080023 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070024 typedef function<void(const ControlParameters&)> CommandSucceedCallback;
25
26 /** \brief a callback on command failure
27 */
28 typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback;
29
30 explicit
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080031 Controller(Face& face);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080032
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070033 /** \brief start command execution
34 */
35 template<typename Command>
36 void
37 start(const ControlParameters& parameters,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070038 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070039 const CommandFailCallback& onFailure,
40 time::milliseconds timeout = getDefaultCommandTimeout());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070041
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070042public: // selfreg using FIB Management commands
Alexander Afanasyeve289b532014-02-09 22:14:44 -080043 virtual void
44 selfRegisterPrefix(const Name& prefixToRegister,
45 const SuccessCallback& onSuccess,
46 const FailCallback& onFail);
47
48 virtual void
Alexander Afanasyev884280c2014-03-07 09:40:39 +000049 selfDeregisterPrefix(const Name& prefixToDeRegister,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080050 const SuccessCallback& onSuccess,
51 const FailCallback& onFail);
52
Alexander Afanasyeve289b532014-02-09 22:14:44 -080053private:
54 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070055 processCommandResponse(const Data& data,
56 const shared_ptr<ControlCommand>& command,
57 const CommandSucceedCallback& onSuccess,
58 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -050059
Junxiao Shi5c785d62014-04-20 18:10:20 -070060public:
61 static time::milliseconds
62 getDefaultCommandTimeout()
63 {
64 return time::milliseconds(10000);
65 }
66
hilataa99e37e2014-02-15 23:52:46 -060067protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080068 Face& m_face;
Alexander Afanasyev884280c2014-03-07 09:40:39 +000069 CommandInterestGenerator m_commandInterestGenerator;
Alexander Afanasyeve289b532014-02-09 22:14:44 -080070};
71
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070072
73template<typename Command>
74void
75Controller::start(const ControlParameters& parameters,
76 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070077 const CommandFailCallback& onFailure,
78 time::milliseconds timeout)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070079{
Junxiao Shi5c785d62014-04-20 18:10:20 -070080 BOOST_ASSERT(timeout > time::milliseconds::zero());
81
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070082 shared_ptr<ControlCommand> command = make_shared<Command>();
83
84 Interest commandInterest = command->makeCommandInterest(parameters, m_commandInterestGenerator);
Junxiao Shi5c785d62014-04-20 18:10:20 -070085 commandInterest.setInterestLifetime(timeout);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070086
87 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx
88 const uint32_t timeoutCode = 10060;
89 m_face.expressInterest(commandInterest,
90 bind(&Controller::processCommandResponse, this, _2,
91 command, onSuccess, onFailure),
92 bind(onFailure, timeoutCode, "Command Interest timed out"));
93}
94
Alexander Afanasyeve289b532014-02-09 22:14:44 -080095} // namespace nfd
96} // namespace ndn
97
Alexander Afanasyev26c24d22014-03-20 09:31:21 -070098#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP