blob: 558f05623c4c92633af95709cff6e361f8010726 [file] [log] [blame]
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080011 */
12
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080013#include "nrd-controller.hpp"
14#include "nrd-prefix-reg-options.hpp"
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070015#include "nfd-control-response.hpp" // used in deprecated function only
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080016
17namespace ndn {
18namespace nrd {
19
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070020using nfd::ControlParameters;
21using nfd::RibRegisterCommand;
22using nfd::RibUnregisterCommand;
23
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080024Controller::Controller(Face& face)
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070025 : nfd::Controller(face)
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080026{
27}
28
29void
30Controller::selfRegisterPrefix(const Name& prefixToRegister,
31 const SuccessCallback& onSuccess,
32 const FailCallback& onFail)
33{
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070034 ControlParameters parameters;
35 parameters.setName(prefixToRegister);
36
37 this->start<RibRegisterCommand>(parameters,
38 bind(onSuccess),
39 bind(onFail, _2));
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080040}
41
42void
43Controller::selfDeregisterPrefix(const Name& prefixToRegister,
44 const SuccessCallback& onSuccess,
45 const FailCallback& onFail)
46{
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070047 ControlParameters parameters;
48 parameters.setName(prefixToRegister);
49
50 this->start<RibUnregisterCommand>(parameters,
51 bind(onSuccess),
52 bind(onFail, _2));
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080053}
54
55void
Obaid6e7f5f12014-03-11 14:46:10 -050056Controller::registerPrefix(const PrefixRegOptions& options,
57 const CommandSucceedCallback& onSuccess,
58 const FailCallback& onFail)
59{
60 startCommand("register", options, onSuccess, onFail);
61}
62
63void
64Controller::unregisterPrefix(const PrefixRegOptions& options,
65 const CommandSucceedCallback& onSuccess,
66 const FailCallback& onFail)
67{
68 startCommand("unregister", options, onSuccess, onFail);
69}
70
71void
72Controller::advertisePrefix(const PrefixRegOptions& options,
73 const CommandSucceedCallback& onSuccess,
74 const FailCallback& onFail)
75{
76 startCommand("advertise", options, onSuccess, onFail);
77}
78
79void
80Controller::withdrawPrefix(const PrefixRegOptions& options,
81 const CommandSucceedCallback& onSuccess,
82 const FailCallback& onFail)
83{
84 startCommand("withdraw", options, onSuccess, onFail);
85}
86
87void
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080088Controller::startCommand(const std::string& command,
89 const PrefixRegOptions& options,
90 const CommandSucceedCallback& onSuccess,
91 const FailCallback& onFail)
92{
93 Name commandInterestName("/localhost/nrd");
94 commandInterestName
95 .append(command)
96 .append(options.wireEncode());
97
98 Interest commandInterest(commandInterestName);
Obaid6e7f5f12014-03-11 14:46:10 -050099 m_commandInterestGenerator.generate(commandInterest);
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800100
101 m_face.expressInterest(commandInterest,
102 bind(&Controller::processCommandResponse, this, _2,
103 onSuccess, onFail),
104 bind(onFail, "Command Interest timed out"));
105}
106
107void
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800108Controller::processCommandResponse(Data& data,
109 const CommandSucceedCallback& onSuccess,
110 const FailCallback& onFail)
111{
Obaid6e7f5f12014-03-11 14:46:10 -0500112 /// \todo Add validation of incoming Data
113
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800114 try
115 {
116 nfd::ControlResponse response(data.getContent().blockFromValue());
117 if (response.getCode() != 200)
118 return onFail(response.getText());
119
120 PrefixRegOptions options(response.getBody());
121 return onSuccess(options);
122 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123 catch (ndn::Tlv::Error& e)
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800124 {
125 if (static_cast<bool>(onFail))
126 return onFail(e.what());
127 }
128}
129
130} // namespace nrd
131} // namespace ndn