Skip to content

Commit 54dfc4a

Browse files
committed
mgmt: remove one level of indirection in status dataset processing
Change-Id: I46bf2f2788300a76d64d835c24fe890b32798d0c
1 parent e07923b commit 54dfc4a

File tree

3 files changed

+53
-77
lines changed

3 files changed

+53
-77
lines changed

ndn-cxx/mgmt/dispatcher.cpp

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ Dispatcher::processCommand(const Name& prefix,
166166
const Interest& interest,
167167
const ControlParametersParser& parse,
168168
const Authorization& authorize,
169-
const ValidateParameters& validateParams,
170-
const ControlCommandHandler& handler)
169+
ValidateParameters validate,
170+
ControlCommandHandler handler)
171171
{
172172
// /<prefix>/<relPrefix>/<parameters>
173173
size_t parametersLoc = prefix.size() + relPrefix.size();
@@ -181,8 +181,8 @@ Dispatcher::processCommand(const Name& prefix,
181181
return;
182182
}
183183

184-
AcceptContinuation accept = [=] (const auto& req) {
185-
processAuthorizedCommand(req, prefix, interest, parameters, validateParams, handler);
184+
AcceptContinuation accept = [=, v = std::move(validate), h = std::move(handler)] (const auto&) {
185+
processAuthorizedCommand(prefix, interest, parameters, v, h);
186186
};
187187
RejectContinuation reject = [=] (RejectReply reply) {
188188
afterAuthorizationRejected(reply, interest);
@@ -191,14 +191,13 @@ Dispatcher::processCommand(const Name& prefix,
191191
}
192192

193193
void
194-
Dispatcher::processAuthorizedCommand(const std::string& requester,
195-
const Name& prefix,
194+
Dispatcher::processAuthorizedCommand(const Name& prefix,
196195
const Interest& interest,
197196
const shared_ptr<ControlParameters>& parameters,
198-
const ValidateParameters& validateParams,
197+
const ValidateParameters& validate,
199198
const ControlCommandHandler& handler)
200199
{
201-
if (validateParams(*parameters)) {
200+
if (validate(*parameters)) {
202201
handler(prefix, interest, *parameters,
203202
[=] (const auto& resp) { sendControlResponse(resp, interest); });
204203
}
@@ -221,36 +220,28 @@ Dispatcher::sendControlResponse(const ControlResponse& resp, const Interest& int
221220

222221
void
223222
Dispatcher::addStatusDataset(const PartialName& relPrefix,
224-
Authorization auth,
225-
StatusDatasetHandler handler)
223+
Authorization authorize,
224+
StatusDatasetHandler handle)
226225
{
227226
checkPrefix(relPrefix);
228227

229-
AuthorizationAcceptedCallback accept =
230-
[this, handler = std::move(handler)] (auto&&, const auto& prefix, const auto& interest, auto&&) {
231-
processAuthorizedStatusDatasetInterest(prefix, interest, handler);
232-
};
233-
AuthorizationRejectedCallback reject =
234-
[this] (auto&&... args) {
235-
afterAuthorizationRejected(std::forward<decltype(args)>(args)...);
236-
};
237228
// follow the general path if storage is a miss
238-
InterestHandler missContinuation =
239-
[this, auth = std::move(auth), accept = std::move(accept), reject = std::move(reject)] (auto&&... args) {
240-
processStatusDatasetInterest(std::forward<decltype(args)>(args)..., auth, accept, reject);
241-
};
229+
InterestHandler afterMiss = [this,
230+
authorizer = std::move(authorize),
231+
handler = std::move(handle)] (const auto& prefix, const auto& interest) {
232+
processStatusDatasetInterest(prefix, interest, authorizer, std::move(handler));
233+
};
242234

243-
m_handlers[relPrefix] = [this, miss = std::move(missContinuation)] (auto&&... args) {
235+
m_handlers[relPrefix] = [this, miss = std::move(afterMiss)] (auto&&... args) {
244236
queryStorage(std::forward<decltype(args)>(args)..., miss);
245237
};
246238
}
247239

248240
void
249241
Dispatcher::processStatusDatasetInterest(const Name& prefix,
250242
const Interest& interest,
251-
const Authorization& authorization,
252-
const AuthorizationAcceptedCallback& accepted,
253-
const AuthorizationRejectedCallback& rejected)
243+
const Authorization& authorize,
244+
StatusDatasetHandler handler)
254245
{
255246
const Name& interestName = interest.getName();
256247
bool endsWithVersionOrSegment = interestName.size() >= 1 &&
@@ -259,9 +250,13 @@ Dispatcher::processStatusDatasetInterest(const Name& prefix,
259250
return;
260251
}
261252

262-
AcceptContinuation accept = [=] (const auto& req) { accepted(req, prefix, interest, nullptr); };
263-
RejectContinuation reject = [=] (RejectReply reply) { rejected(reply, interest); };
264-
authorization(prefix, interest, nullptr, accept, reject);
253+
AcceptContinuation accept = [=, h = std::move(handler)] (const auto&) {
254+
processAuthorizedStatusDatasetInterest(prefix, interest, h);
255+
};
256+
RejectContinuation reject = [=] (RejectReply reply) {
257+
afterAuthorizationRejected(reply, interest);
258+
};
259+
authorize(prefix, interest, nullptr, accept, reject);
265260
}
266261

267262
void

ndn-cxx/mgmt/dispatcher.hpp

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ makeAcceptAllAuthorization();
9393
*/
9494
using ValidateParameters = std::function<bool(const ControlParameters& params)>;
9595

96-
/** \brief A function to be called after ControlCommandHandler completes.
97-
* \param resp the response to be sent to requester
96+
/**
97+
* \brief A function to be called after a ControlCommandHandler completes.
98+
* \param resp The response that should be sent back to the requester.
9899
*/
99100
using CommandContinuation = std::function<void(const ControlResponse& resp)>;
100101

@@ -195,17 +196,17 @@ class Dispatcher : noncopyable
195196
* \throw std::out_of_range \p relPrefix overlaps with an existing relPrefix.
196197
* \throw std::domain_error One or more top-level prefixes have been added.
197198
*
198-
* Procedure for processing a ControlCommand:
199+
* Procedure for processing a ControlCommand registered through this function:
199200
* 1. Extract the NameComponent containing ControlParameters (the component after relPrefix),
200201
* and parse ControlParameters into ParametersType; if parsing fails, abort these steps.
201202
* 2. Perform authorization; if the authorization is rejected, perform the RejectReply action
202203
* and abort these steps.
203-
* 3. Validate ControlParameters; if validation fails, create a ControlResponse with
204+
* 3. Validate the ControlParameters; if validation fails, create a ControlResponse with
204205
* StatusCode 400 and go to step 5.
205206
* 4. Invoke the command handler, wait until CommandContinuation is called.
206207
* 5. Encode the ControlResponse into one Data packet.
207208
* 6. Sign the Data packet.
208-
* 7. If the Data packet is too large, abort these steps and log an error.
209+
* 7. If the Data packet is too large, log an error and abort these steps.
209210
* 8. Send the signed Data packet.
210211
*/
211212
template<typename ParametersType,
@@ -227,7 +228,8 @@ class Dispatcher : noncopyable
227228
authorize = std::move(authorize),
228229
validate = std::move(validate),
229230
handle = std::move(handle)] (const auto& prefix, const auto& interest) {
230-
processCommand(prefix, relPrefix, interest, parse, authorize, validate, handle);
231+
processCommand(prefix, relPrefix, interest, parse, authorize,
232+
std::move(validate), std::move(handle));
231233
};
232234
}
233235

@@ -296,13 +298,6 @@ class Dispatcher : noncopyable
296298
private:
297299
using InterestHandler = std::function<void(const Name& prefix, const Interest&)>;
298300

299-
using AuthorizationAcceptedCallback = std::function<void(const std::string& requester,
300-
const Name& prefix,
301-
const Interest&,
302-
const shared_ptr<ControlParameters>&)>;
303-
304-
using AuthorizationRejectedCallback = std::function<void(RejectReply, const Interest&)>;
305-
306301
/**
307302
* @brief The parser for extracting control parameters from a name component.
308303
* @return A shared pointer to the extracted ControlParameters.
@@ -374,30 +369,28 @@ class Dispatcher : noncopyable
374369
* @param parse function to extract the control parameters from the command
375370
* @param authorize function to determine whether the command is authorized
376371
* @param validate function to validate the command parameters
377-
* @param handler function to execute the command after authorization and validation
372+
* @param handler function to execute the command after it is authorized and validated
378373
*/
379374
void
380375
processCommand(const Name& prefix,
381376
const Name& relPrefix,
382377
const Interest& interest,
383378
const ControlParametersParser& parse,
384379
const Authorization& authorize,
385-
const ValidateParameters& validate,
386-
const ControlCommandHandler& handler);
380+
ValidateParameters validate,
381+
ControlCommandHandler handler);
387382

388383
/**
389384
* @brief Process an authorized control command.
390385
*
391-
* @param requester the requester
392386
* @param prefix the top-level prefix
393387
* @param interest the incoming Interest
394388
* @param parameters control parameters of this command
395389
* @param validate function to validate the command parameters
396-
* @param handler function to execute the command after authorization and validation
390+
* @param handler function to execute the command after its parameters are validated
397391
*/
398392
void
399-
processAuthorizedCommand(const std::string& requester,
400-
const Name& prefix,
393+
processAuthorizedCommand(const Name& prefix,
401394
const Interest& interest,
402395
const shared_ptr<ControlParameters>& parameters,
403396
const ValidateParameters& validate,
@@ -411,23 +404,21 @@ class Dispatcher : noncopyable
411404
*
412405
* @param prefix the top-level prefix
413406
* @param interest the incoming Interest
414-
* @param authorize function to process verification
415-
* @param accepted callback for successful authorization
416-
* @param rejected callback for failed authorization
407+
* @param authorize function to determine whether the request is authorized
408+
* @param handler function to continue processing the request after it is authorized
417409
*/
418410
void
419411
processStatusDatasetInterest(const Name& prefix,
420412
const Interest& interest,
421413
const Authorization& authorize,
422-
const AuthorizationAcceptedCallback& accepted,
423-
const AuthorizationRejectedCallback& rejected);
414+
StatusDatasetHandler handler);
424415

425416
/**
426417
* @brief Process an authorized StatusDataset request.
427418
*
428419
* @param prefix the top-level prefix
429420
* @param interest the incoming Interest
430-
* @param handler function to process this request
421+
* @param handler function to process the dataset request
431422
*/
432423
void
433424
processAuthorizedStatusDatasetInterest(const Name& prefix,

tests/unit/mgmt/dispatcher.t.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,10 @@ using namespace ndn::mgmt;
3232

3333
class DispatcherFixture : public IoKeyChainFixture
3434
{
35-
public:
36-
DispatcherFixture()
37-
: face(m_io, m_keyChain, {true, true})
38-
, dispatcher(face, m_keyChain, security::SigningInfo())
39-
, storage(dispatcher.m_storage)
40-
{
41-
}
42-
43-
public:
44-
DummyClientFace face;
45-
mgmt::Dispatcher dispatcher;
46-
InMemoryStorageFifo& storage;
35+
protected:
36+
DummyClientFace face{m_io, m_keyChain, {true, true}};
37+
Dispatcher dispatcher{face, m_keyChain};
38+
InMemoryStorageFifo& storage{dispatcher.m_storage};
4739
};
4840

4941
class VoidParameters : public mgmt::ControlParameters
@@ -266,16 +258,14 @@ BOOST_AUTO_TEST_CASE(ControlCommandAsyncAuthorization,
266258
* ut::description("test for bug #4059"))
267259
{
268260
AcceptContinuation authorizationAccept;
269-
auto authorization =
270-
[&authorizationAccept] (const Name&, const Interest&, const ControlParameters*,
271-
AcceptContinuation accept, RejectContinuation) {
272-
authorizationAccept = std::move(accept);
273-
};
274-
275-
auto validateParameters =
276-
[] (const ControlParameters& params) {
277-
return dynamic_cast<const StatefulParameters&>(params).check();
278-
};
261+
auto authorization = [&authorizationAccept] (const Name&, const Interest&, const ControlParameters*,
262+
AcceptContinuation accept, RejectContinuation) {
263+
authorizationAccept = std::move(accept);
264+
};
265+
266+
auto validateParameters = [] (const ControlParameters& params) {
267+
return dynamic_cast<const StatefulParameters&>(params).check();
268+
};
279269

280270
size_t nCallbackCalled = 0;
281271
dispatcher.addControlCommand<StatefulParameters>("test", authorization, validateParameters,

0 commit comments

Comments
 (0)