blob: bf99148ff9769501648e399d18359980b9fc0447 [file] [log] [blame]
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -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
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -08007#include "nrd-controller.hpp"
8#include "nrd-prefix-reg-options.hpp"
Junxiao Shi5f6c74f2014-04-18 16:29:44 -07009#include "nfd-control-response.hpp" // used in deprecated function only
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080010
11namespace ndn {
12namespace nrd {
13
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070014using nfd::ControlParameters;
15using nfd::RibRegisterCommand;
16using nfd::RibUnregisterCommand;
17
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080018Controller::Controller(Face& face)
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070019 : nfd::Controller(face)
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080020{
21}
22
23void
24Controller::selfRegisterPrefix(const Name& prefixToRegister,
25 const SuccessCallback& onSuccess,
26 const FailCallback& onFail)
27{
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070028 ControlParameters parameters;
29 parameters.setName(prefixToRegister);
30
31 this->start<RibRegisterCommand>(parameters,
32 bind(onSuccess),
33 bind(onFail, _2));
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080034}
35
36void
37Controller::selfDeregisterPrefix(const Name& prefixToRegister,
38 const SuccessCallback& onSuccess,
39 const FailCallback& onFail)
40{
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070041 ControlParameters parameters;
42 parameters.setName(prefixToRegister);
43
44 this->start<RibUnregisterCommand>(parameters,
45 bind(onSuccess),
46 bind(onFail, _2));
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080047}
48
49void
Obaid6e7f5f12014-03-11 14:46:10 -050050Controller::registerPrefix(const PrefixRegOptions& options,
51 const CommandSucceedCallback& onSuccess,
52 const FailCallback& onFail)
53{
54 startCommand("register", options, onSuccess, onFail);
55}
56
57void
58Controller::unregisterPrefix(const PrefixRegOptions& options,
59 const CommandSucceedCallback& onSuccess,
60 const FailCallback& onFail)
61{
62 startCommand("unregister", options, onSuccess, onFail);
63}
64
65void
66Controller::advertisePrefix(const PrefixRegOptions& options,
67 const CommandSucceedCallback& onSuccess,
68 const FailCallback& onFail)
69{
70 startCommand("advertise", options, onSuccess, onFail);
71}
72
73void
74Controller::withdrawPrefix(const PrefixRegOptions& options,
75 const CommandSucceedCallback& onSuccess,
76 const FailCallback& onFail)
77{
78 startCommand("withdraw", options, onSuccess, onFail);
79}
80
81void
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080082Controller::startCommand(const std::string& command,
83 const PrefixRegOptions& options,
84 const CommandSucceedCallback& onSuccess,
85 const FailCallback& onFail)
86{
87 Name commandInterestName("/localhost/nrd");
88 commandInterestName
89 .append(command)
90 .append(options.wireEncode());
91
92 Interest commandInterest(commandInterestName);
Obaid6e7f5f12014-03-11 14:46:10 -050093 m_commandInterestGenerator.generate(commandInterest);
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080094
95 m_face.expressInterest(commandInterest,
96 bind(&Controller::processCommandResponse, this, _2,
97 onSuccess, onFail),
98 bind(onFail, "Command Interest timed out"));
99}
100
101void
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800102Controller::processCommandResponse(Data& data,
103 const CommandSucceedCallback& onSuccess,
104 const FailCallback& onFail)
105{
Obaid6e7f5f12014-03-11 14:46:10 -0500106 /// \todo Add validation of incoming Data
107
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800108 try
109 {
110 nfd::ControlResponse response(data.getContent().blockFromValue());
111 if (response.getCode() != 200)
112 return onFail(response.getText());
113
114 PrefixRegOptions options(response.getBody());
115 return onSuccess(options);
116 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700117 catch (ndn::Tlv::Error& e)
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800118 {
119 if (static_cast<bool>(onFail))
120 return onFail(e.what());
121 }
122}
123
124} // namespace nrd
125} // namespace ndn