Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 7 | #ifndef NDN_MANAGEMENT_NFD_CONTROLLER_HPP |
| 8 | #define NDN_MANAGEMENT_NFD_CONTROLLER_HPP |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 9 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 10 | #include "controller.hpp" |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 11 | #include "nfd-control-command.hpp" |
| 12 | #include "../face.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 13 | |
| 14 | namespace ndn { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 15 | namespace nfd { |
| 16 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 17 | /** \brief NFD Management protocol - ControlCommand client |
| 18 | */ |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 19 | class Controller : public ndn::Controller |
| 20 | { |
| 21 | public: |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 22 | /** \brief a callback on command success |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 23 | */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 24 | 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 Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 31 | Controller(Face& face); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 32 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 33 | /** \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 | |
| 41 | public: // selfreg |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 42 | virtual void |
| 43 | selfRegisterPrefix(const Name& prefixToRegister, |
| 44 | const SuccessCallback& onSuccess, |
| 45 | const FailCallback& onFail); |
| 46 | |
| 47 | virtual void |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 48 | selfDeregisterPrefix(const Name& prefixToDeRegister, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 49 | const SuccessCallback& onSuccess, |
| 50 | const FailCallback& onFail); |
| 51 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 52 | public: |
| 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 Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 63 | /** |
| 64 | * \brief Adds a nexthop to an existing or new FIB entry |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 65 | * \deprecated use startCommand instead |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 66 | * |
| 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, |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 80 | const FibCommandSucceedCallback& onSuccess, |
| 81 | const FailCallback& onFail); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * \brief Remove a nexthop from FIB entry |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 85 | * \deprecated use startCommand instead |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 86 | * |
| 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, |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 99 | const FibCommandSucceedCallback& onSuccess, |
| 100 | const FailCallback& onFail); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 101 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 102 | protected: |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 103 | /** \deprecated use startCommand instead |
| 104 | */ |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 105 | void |
| 106 | startFibCommand(const std::string& command, |
| 107 | const FibManagementOptions& options, |
| 108 | const FibCommandSucceedCallback& onSuccess, |
| 109 | const FailCallback& onFailure); |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 110 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 111 | /** \deprecated use startCommand instead |
| 112 | */ |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 113 | void |
| 114 | startFaceCommand(const std::string& command, |
| 115 | const FaceManagementOptions& options, |
| 116 | const FaceCommandSucceedCallback& onSuccess, |
| 117 | const FailCallback& onFailure); |
| 118 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 119 | /** \deprecated use startCommand instead |
| 120 | */ |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 121 | void |
| 122 | startStrategyChoiceCommand(const std::string& command, |
| 123 | const StrategyChoiceOptions& options, |
| 124 | const StrategyChoiceCommandSucceedCallback& onSuccess, |
| 125 | const FailCallback& onFailure); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 127 | private: |
| 128 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 129 | processCommandResponse(const Data& data, |
| 130 | const shared_ptr<ControlCommand>& command, |
| 131 | const CommandSucceedCallback& onSuccess, |
| 132 | const CommandFailCallback& onFailure); |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 133 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 134 | protected: |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 135 | Face& m_face; |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 136 | CommandInterestGenerator m_commandInterestGenerator; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 139 | |
| 140 | template<typename Command> |
| 141 | void |
| 142 | Controller::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 Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 158 | } // namespace nfd |
| 159 | } // namespace ndn |
| 160 | |
Alexander Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 161 | #endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP |