blob: 19e98f860db6fbf7de4e7e96c7fa4bf60e597ea5 [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
7#include "common.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -08008#include "../face.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -08009
10#include "nfd-controller.hpp"
11#include "nfd-fib-management-options.hpp"
hilataa99e37e2014-02-15 23:52:46 -060012#include "nfd-face-management-options.hpp"
hilata7d160f22014-03-13 19:51:42 -050013#include "nfd-strategy-choice-options.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080014#include "nfd-control-response.hpp"
15
16namespace ndn {
17namespace nfd {
18
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080019Controller::Controller(Face& face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080020 : m_face(face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080021{
22}
23
24void
25Controller::selfRegisterPrefix(const Name& prefixToRegister,
26 const SuccessCallback& onSuccess,
27 const FailCallback& onFail)
28{
Obaid6e7f5f12014-03-11 14:46:10 -050029 fibAddNextHop(prefixToRegister, 0, 0, bind(onSuccess), onFail);
Alexander Afanasyev21abc102014-02-18 18:59:02 -080030}
31
32void
Alexander Afanasyev884280c2014-03-07 09:40:39 +000033Controller::selfDeregisterPrefix(const Name& prefixToDeRegister,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080034 const SuccessCallback& onSuccess,
35 const FailCallback& onFail)
36{
Obaid6e7f5f12014-03-11 14:46:10 -050037 fibRemoveNextHop(prefixToDeRegister, 0, bind(onSuccess), onFail);
Alexander Afanasyev884280c2014-03-07 09:40:39 +000038}
hilataa99e37e2014-02-15 23:52:46 -060039
Alexander Afanasyev884280c2014-03-07 09:40:39 +000040void
41Controller::fibAddNextHop(const Name& prefix, uint64_t faceId, int cost,
Obaid6e7f5f12014-03-11 14:46:10 -050042 const FibCommandSucceedCallback& onSuccess,
43 const FailCallback& onFail)
Alexander Afanasyev884280c2014-03-07 09:40:39 +000044{
45 startFibCommand("add-nexthop",
46 FibManagementOptions()
47 .setName(prefix)
48 .setFaceId(faceId)
49 .setCost(cost),
Obaid6e7f5f12014-03-11 14:46:10 -050050 onSuccess, onFail);
Alexander Afanasyev884280c2014-03-07 09:40:39 +000051}
52
53void
54Controller::fibRemoveNextHop(const Name& prefix, uint64_t faceId,
Obaid6e7f5f12014-03-11 14:46:10 -050055 const FibCommandSucceedCallback& onSuccess,
56 const FailCallback& onFail)
Alexander Afanasyev884280c2014-03-07 09:40:39 +000057{
Alexander Afanasyeve289b532014-02-09 22:14:44 -080058 startFibCommand("remove-nexthop",
59 FibManagementOptions()
Alexander Afanasyev884280c2014-03-07 09:40:39 +000060 .setName(prefix)
61 .setFaceId(faceId),
Obaid6e7f5f12014-03-11 14:46:10 -050062 onSuccess, onFail);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080063}
64
65void
66Controller::startFibCommand(const std::string& command,
67 const FibManagementOptions& options,
68 const FibCommandSucceedCallback& onSuccess,
69 const FailCallback& onFail)
70{
71 Name fibCommandInterestName("/localhost/nfd/fib");
72 fibCommandInterestName
73 .append(command)
74 .append(options.wireEncode());
75
76 Interest fibCommandInterest(fibCommandInterestName);
Alexander Afanasyev884280c2014-03-07 09:40:39 +000077 m_commandInterestGenerator.generate(fibCommandInterest);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080078
79 m_face.expressInterest(fibCommandInterest,
80 bind(&Controller::processFibCommandResponse, this, _2,
81 onSuccess, onFail),
82 bind(onFail, "Command Interest timed out"));
83}
84
85void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080086Controller::processFibCommandResponse(Data& data,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080087 const FibCommandSucceedCallback& onSuccess,
88 const FailCallback& onFail)
89{
Obaid6e7f5f12014-03-11 14:46:10 -050090 /// \todo Add validation of incoming Data
91
Alexander Afanasyeve289b532014-02-09 22:14:44 -080092 try
93 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080094 ControlResponse response(data.getContent().blockFromValue());
Alexander Afanasyeve289b532014-02-09 22:14:44 -080095 if (response.getCode() != 200)
96 return onFail(response.getText());
97
98 FibManagementOptions options(response.getBody());
99 return onSuccess(options);
100 }
101 catch(ndn::Tlv::Error& e)
102 {
103 if (static_cast<bool>(onFail))
104 return onFail(e.what());
105 }
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800106}
107
hilataa99e37e2014-02-15 23:52:46 -0600108void
109Controller::startFaceCommand(const std::string& command,
110 const FaceManagementOptions& options,
111 const FaceCommandSucceedCallback& onSuccess,
112 const FailCallback& onFail)
113{
114 Name faceCommandInterestName("/localhost/nfd/faces");
115 faceCommandInterestName
116 .append(command)
117 .append(options.wireEncode());
118
119 Interest faceCommandInterest(faceCommandInterestName);
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000120 m_commandInterestGenerator.generate(faceCommandInterest);
hilataa99e37e2014-02-15 23:52:46 -0600121
122 m_face.expressInterest(faceCommandInterest,
123 bind(&Controller::processFaceCommandResponse, this, _2,
124 onSuccess, onFail),
125 bind(onFail, "Command Interest timed out"));
126}
hilata7d160f22014-03-13 19:51:42 -0500127
hilataa99e37e2014-02-15 23:52:46 -0600128void
129Controller::processFaceCommandResponse(Data& data,
130 const FaceCommandSucceedCallback& onSuccess,
131 const FailCallback& onFail)
132{
Obaid6e7f5f12014-03-11 14:46:10 -0500133 /// \todo Add validation of incoming Data
hilata7d160f22014-03-13 19:51:42 -0500134
hilataa99e37e2014-02-15 23:52:46 -0600135 try
hilata7d160f22014-03-13 19:51:42 -0500136 {
137 ControlResponse response(data.getContent().blockFromValue());
138 if (response.getCode() != 200)
139 return onFail(response.getText());
140
141 FaceManagementOptions options(response.getBody());
142 return onSuccess(options);
143 }
hilataa99e37e2014-02-15 23:52:46 -0600144 catch(ndn::Tlv::Error& e)
hilata7d160f22014-03-13 19:51:42 -0500145 {
146 if (static_cast<bool>(onFail))
147 return onFail(e.what());
148 }
hilataa99e37e2014-02-15 23:52:46 -0600149}
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000150
hilata7d160f22014-03-13 19:51:42 -0500151void
152Controller::startStrategyChoiceCommand(const std::string& command,
153 const StrategyChoiceOptions& options,
154 const StrategyChoiceCommandSucceedCallback& onSuccess,
155 const FailCallback& onFail)
156{
157 Name strategyChoiceCommandInterestName("/localhost/nfd/strategy-choice");
158 strategyChoiceCommandInterestName
159 .append(command)
160 .append(options.wireEncode());
161
162 Interest strategyChoiceCommandInterest(strategyChoiceCommandInterestName);
163 m_commandInterestGenerator.generate(strategyChoiceCommandInterest);
164
165 m_face.expressInterest(strategyChoiceCommandInterest,
166 bind(&Controller::processStrategyChoiceCommandResponse, this, _2,
167 onSuccess, onFail),
168 bind(onFail, "Command Interest timed out"));
169}
170void
171Controller::processStrategyChoiceCommandResponse(
172 Data& data,
173 const StrategyChoiceCommandSucceedCallback& onSuccess,
174 const FailCallback& onFail)
175{
176 /// \todo Add validation of incoming Data
177
178 try
179 {
180 ControlResponse response(data.getContent().blockFromValue());
181 if (response.getCode() != 200)
182 return onFail(response.getText());
183
184 StrategyChoiceOptions options(response.getBody());
185 return onSuccess(options);
186 }
187 catch (ndn::Tlv::Error& error)
188 {
189 if (static_cast<bool>(onFail))
190 return onFail(error.what());
191 }
192}
193
194
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800195} // namespace nfd
196} // namespace ndn