blob: f9ca99964f7d52730930e63e05158f2bf975461c [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
Alexander Afanasyeve289b532014-02-09 22:14:44 -080016#include "controller.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070017#include "nfd-control-command.hpp"
18#include "../face.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080019
20namespace ndn {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080021namespace nfd {
22
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070023/** \brief NFD Management protocol - ControlCommand client
24 */
Alexander Afanasyeve289b532014-02-09 22:14:44 -080025class Controller : public ndn::Controller
26{
27public:
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070028 /** \brief a callback on command success
Alexander Afanasyeve289b532014-02-09 22:14:44 -080029 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070030 typedef function<void(const ControlParameters&)> CommandSucceedCallback;
31
32 /** \brief a callback on command failure
33 */
34 typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback;
35
36 explicit
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080037 Controller(Face& face);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080038
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070039 /** \brief start command execution
40 */
41 template<typename Command>
42 void
43 start(const ControlParameters& parameters,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070044 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070045 const CommandFailCallback& onFailure,
46 time::milliseconds timeout = getDefaultCommandTimeout());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070047
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070048public: // selfreg using FIB Management commands
Alexander Afanasyeve289b532014-02-09 22:14:44 -080049 virtual void
50 selfRegisterPrefix(const Name& prefixToRegister,
51 const SuccessCallback& onSuccess,
52 const FailCallback& onFail);
53
54 virtual void
Alexander Afanasyev884280c2014-03-07 09:40:39 +000055 selfDeregisterPrefix(const Name& prefixToDeRegister,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080056 const SuccessCallback& onSuccess,
57 const FailCallback& onFail);
58
Alexander Afanasyeve289b532014-02-09 22:14:44 -080059private:
60 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070061 processCommandResponse(const Data& data,
62 const shared_ptr<ControlCommand>& command,
63 const CommandSucceedCallback& onSuccess,
64 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -050065
Junxiao Shi5c785d62014-04-20 18:10:20 -070066public:
67 static time::milliseconds
68 getDefaultCommandTimeout()
69 {
70 return time::milliseconds(10000);
71 }
72
hilataa99e37e2014-02-15 23:52:46 -060073protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080074 Face& m_face;
Alexander Afanasyev884280c2014-03-07 09:40:39 +000075 CommandInterestGenerator m_commandInterestGenerator;
Alexander Afanasyeve289b532014-02-09 22:14:44 -080076};
77
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070078
79template<typename Command>
80void
81Controller::start(const ControlParameters& parameters,
82 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070083 const CommandFailCallback& onFailure,
84 time::milliseconds timeout)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070085{
Junxiao Shi5c785d62014-04-20 18:10:20 -070086 BOOST_ASSERT(timeout > time::milliseconds::zero());
87
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070088 shared_ptr<ControlCommand> command = make_shared<Command>();
89
90 Interest commandInterest = command->makeCommandInterest(parameters, m_commandInterestGenerator);
Junxiao Shi5c785d62014-04-20 18:10:20 -070091 commandInterest.setInterestLifetime(timeout);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070092
93 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx
94 const uint32_t timeoutCode = 10060;
95 m_face.expressInterest(commandInterest,
96 bind(&Controller::processCommandResponse, this, _2,
97 command, onSuccess, onFailure),
98 bind(onFailure, timeoutCode, "Command Interest timed out"));
99}
100
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800101} // namespace nfd
102} // namespace ndn
103
Alexander Afanasyev26c24d22014-03-20 09:31:21 -0700104#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP