blob: 536994c982b9c1ca0124a592deabbd4bc67984e5 [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,
38 const CommandSucceedCallback& onSuccess,
39 const CommandFailCallback& onFailure);
40
41public: // selfreg
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
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070052public:
53 /** \deprecated use CommandSucceedCallback instead
54 */
55 typedef function<void(const FibManagementOptions&)> FibCommandSucceedCallback;
56 /** \deprecated use CommandSucceedCallback instead
57 */
58 typedef function<void(const FaceManagementOptions&)> FaceCommandSucceedCallback;
59 /** \deprecated use CommandSucceedCallback instead
60 */
61 typedef function<void(const StrategyChoiceOptions&)> StrategyChoiceCommandSucceedCallback;
62
Alexander Afanasyev884280c2014-03-07 09:40:39 +000063 /**
64 * \brief Adds a nexthop to an existing or new FIB entry
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070065 * \deprecated use startCommand instead
Alexander Afanasyev884280c2014-03-07 09:40:39 +000066 *
67 * If FIB entry for the specified prefix does not exist, it will be automatically created.
68 *
69 * \param prefix Prefix of the FIB entry
70 * \param faceId ID of the face which should be added as a next hop for prefix FIB entry.
71 * If a nexthop of same FaceId exists on the FIB entry, its cost is updated.
72 * If FaceId is set to zero, it is implied as the face of the entity sending
73 * this command.
74 * \param cost Cost that should be associated with the next hop
75 * \param onSuccess Callback that will be called when operation succeeds
76 * \param onFail Callback that will be called when operation fails
77 */
78 void
79 fibAddNextHop(const Name& prefix, uint64_t faceId, int cost,
Obaid6e7f5f12014-03-11 14:46:10 -050080 const FibCommandSucceedCallback& onSuccess,
81 const FailCallback& onFail);
Alexander Afanasyev884280c2014-03-07 09:40:39 +000082
83 /**
84 * \brief Remove a nexthop from FIB entry
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070085 * \deprecated use startCommand instead
Alexander Afanasyev884280c2014-03-07 09:40:39 +000086 *
87 * If after removal of the nexthop FIB entry has zero next hops, this FIB entry will
88 * be automatically deleted.
89 *
90 * \param prefix Prefix of the FIB entry
91 * \param faceId ID of the face which should be removed FIB entry.
92 * If FaceId is set to zero, it is implied as the face of the entity sending
93 * this command.
94 * \param onSuccess Callback that will be called when operation succeeds
95 * \param onFail Callback that will be called when operation fails
96 */
97 void
98 fibRemoveNextHop(const Name& prefix, uint64_t faceId,
Obaid6e7f5f12014-03-11 14:46:10 -050099 const FibCommandSucceedCallback& onSuccess,
100 const FailCallback& onFail);
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000101
hilataa99e37e2014-02-15 23:52:46 -0600102protected:
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700103 /** \deprecated use startCommand instead
104 */
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800105 void
106 startFibCommand(const std::string& command,
107 const FibManagementOptions& options,
108 const FibCommandSucceedCallback& onSuccess,
109 const FailCallback& onFailure);
hilataa99e37e2014-02-15 23:52:46 -0600110
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700111 /** \deprecated use startCommand instead
112 */
hilataa99e37e2014-02-15 23:52:46 -0600113 void
114 startFaceCommand(const std::string& command,
115 const FaceManagementOptions& options,
116 const FaceCommandSucceedCallback& onSuccess,
117 const FailCallback& onFailure);
118
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700119 /** \deprecated use startCommand instead
120 */
hilata7d160f22014-03-13 19:51:42 -0500121 void
122 startStrategyChoiceCommand(const std::string& command,
123 const StrategyChoiceOptions& options,
124 const StrategyChoiceCommandSucceedCallback& onSuccess,
125 const FailCallback& onFailure);
Junxiao Shibc19b372014-03-23 16:59:25 -0700126
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800127private:
128 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700129 processCommandResponse(const Data& data,
130 const shared_ptr<ControlCommand>& command,
131 const CommandSucceedCallback& onSuccess,
132 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -0500133
hilataa99e37e2014-02-15 23:52:46 -0600134protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800135 Face& m_face;
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000136 CommandInterestGenerator m_commandInterestGenerator;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800137};
138
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700139
140template<typename Command>
141void
142Controller::start(const ControlParameters& parameters,
143 const CommandSucceedCallback& onSuccess,
144 const CommandFailCallback& onFailure)
145{
146 shared_ptr<ControlCommand> command = make_shared<Command>();
147
148 Interest commandInterest = command->makeCommandInterest(parameters, m_commandInterestGenerator);
149
150 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx
151 const uint32_t timeoutCode = 10060;
152 m_face.expressInterest(commandInterest,
153 bind(&Controller::processCommandResponse, this, _2,
154 command, onSuccess, onFailure),
155 bind(onFailure, timeoutCode, "Command Interest timed out"));
156}
157
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800158} // namespace nfd
159} // namespace ndn
160
Alexander Afanasyev26c24d22014-03-20 09:31:21 -0700161#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP