blob: 1b46c76c8ea3a35a1789049d0fbf55f7b5f658dc [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,
39 const CommandFailCallback& onFailure);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070040
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070041public: // selfreg using FIB Management commands
Alexander Afanasyeve289b532014-02-09 22:14:44 -080042 virtual void
43 selfRegisterPrefix(const Name& prefixToRegister,
44 const SuccessCallback& onSuccess,
45 const FailCallback& onFail);
46
47 virtual void
Alexander Afanasyev884280c2014-03-07 09:40:39 +000048 selfDeregisterPrefix(const Name& prefixToDeRegister,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080049 const SuccessCallback& onSuccess,
50 const FailCallback& onFail);
51
Alexander Afanasyeve289b532014-02-09 22:14:44 -080052private:
53 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070054 processCommandResponse(const Data& data,
55 const shared_ptr<ControlCommand>& command,
56 const CommandSucceedCallback& onSuccess,
57 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -050058
hilataa99e37e2014-02-15 23:52:46 -060059protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080060 Face& m_face;
Alexander Afanasyev884280c2014-03-07 09:40:39 +000061 CommandInterestGenerator m_commandInterestGenerator;
Alexander Afanasyeve289b532014-02-09 22:14:44 -080062};
63
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070064
65template<typename Command>
66void
67Controller::start(const ControlParameters& parameters,
68 const CommandSucceedCallback& onSuccess,
69 const CommandFailCallback& onFailure)
70{
71 shared_ptr<ControlCommand> command = make_shared<Command>();
72
73 Interest commandInterest = command->makeCommandInterest(parameters, m_commandInterestGenerator);
74
75 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx
76 const uint32_t timeoutCode = 10060;
77 m_face.expressInterest(commandInterest,
78 bind(&Controller::processCommandResponse, this, _2,
79 command, onSuccess, onFailure),
80 bind(onFailure, timeoutCode, "Command Interest timed out"));
81}
82
Alexander Afanasyeve289b532014-02-09 22:14:44 -080083} // namespace nfd
84} // namespace ndn
85
Alexander Afanasyev26c24d22014-03-20 09:31:21 -070086#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP