blob: b0232775d3aaf20bd58f83c69c40567850bdbab8 [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
Yingdi Yue66bf2a2014-04-28 17:07:36 -070016#include "../security/identity-certificate.hpp"
17
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080018
19namespace ndn {
20namespace nrd {
21
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070022using nfd::ControlParameters;
23using nfd::RibRegisterCommand;
24using nfd::RibUnregisterCommand;
25
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080026Controller::Controller(Face& face)
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070027 : nfd::Controller(face)
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080028{
29}
30
31void
32Controller::selfRegisterPrefix(const Name& prefixToRegister,
33 const SuccessCallback& onSuccess,
Yingdi Yue66bf2a2014-04-28 17:07:36 -070034 const FailCallback& onFail,
35 const Sign& sign)
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080036{
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070037 ControlParameters parameters;
38 parameters.setName(prefixToRegister);
39
40 this->start<RibRegisterCommand>(parameters,
41 bind(onSuccess),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070042 bind(onFail, _2),
43 sign);
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080044}
45
46void
47Controller::selfDeregisterPrefix(const Name& prefixToRegister,
48 const SuccessCallback& onSuccess,
Yingdi Yue66bf2a2014-04-28 17:07:36 -070049 const FailCallback& onFail,
50 const Sign& sign)
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080051{
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070052 ControlParameters parameters;
53 parameters.setName(prefixToRegister);
54
55 this->start<RibUnregisterCommand>(parameters,
56 bind(onSuccess),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070057 bind(onFail, _2),
58 sign);
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080059}
60
61void
Obaid6e7f5f12014-03-11 14:46:10 -050062Controller::registerPrefix(const PrefixRegOptions& options,
63 const CommandSucceedCallback& onSuccess,
64 const FailCallback& onFail)
65{
66 startCommand("register", options, onSuccess, onFail);
67}
68
69void
70Controller::unregisterPrefix(const PrefixRegOptions& options,
71 const CommandSucceedCallback& onSuccess,
72 const FailCallback& onFail)
73{
74 startCommand("unregister", options, onSuccess, onFail);
75}
76
77void
78Controller::advertisePrefix(const PrefixRegOptions& options,
79 const CommandSucceedCallback& onSuccess,
80 const FailCallback& onFail)
81{
82 startCommand("advertise", options, onSuccess, onFail);
83}
84
85void
86Controller::withdrawPrefix(const PrefixRegOptions& options,
87 const CommandSucceedCallback& onSuccess,
88 const FailCallback& onFail)
89{
90 startCommand("withdraw", options, onSuccess, onFail);
91}
92
93void
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080094Controller::startCommand(const std::string& command,
95 const PrefixRegOptions& options,
96 const CommandSucceedCallback& onSuccess,
97 const FailCallback& onFail)
98{
99 Name commandInterestName("/localhost/nrd");
100 commandInterestName
101 .append(command)
102 .append(options.wireEncode());
103
104 Interest commandInterest(commandInterestName);
Obaid6e7f5f12014-03-11 14:46:10 -0500105 m_commandInterestGenerator.generate(commandInterest);
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800106
107 m_face.expressInterest(commandInterest,
108 bind(&Controller::processCommandResponse, this, _2,
109 onSuccess, onFail),
110 bind(onFail, "Command Interest timed out"));
111}
112
113void
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800114Controller::processCommandResponse(Data& data,
115 const CommandSucceedCallback& onSuccess,
116 const FailCallback& onFail)
117{
Obaid6e7f5f12014-03-11 14:46:10 -0500118 /// \todo Add validation of incoming Data
119
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800120 try
121 {
122 nfd::ControlResponse response(data.getContent().blockFromValue());
123 if (response.getCode() != 200)
124 return onFail(response.getText());
125
126 PrefixRegOptions options(response.getBody());
127 return onSuccess(options);
128 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700129 catch (ndn::Tlv::Error& e)
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800130 {
131 if (static_cast<bool>(onFail))
132 return onFail(e.what());
133 }
134}
135
136} // namespace nrd
137} // namespace ndn