blob: 66156d2093b0a16da2431a6e2544f56d146c3897 [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
7#include "common.hpp"
8#include "../face.hpp"
9
10// NRD
11#include "nrd-controller.hpp"
12#include "nrd-prefix-reg-options.hpp"
13
14// NFD
15#include "nfd-control-response.hpp"
16
17namespace ndn {
18namespace nrd {
19
20Controller::Controller(Face& face)
21 : m_face(face)
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080022{
23}
24
25void
26Controller::selfRegisterPrefix(const Name& prefixToRegister,
27 const SuccessCallback& onSuccess,
28 const FailCallback& onFail)
29{
30 startCommand("register",
31 PrefixRegOptions()
32 .setName(prefixToRegister)
33 .setFaceId(0) // self-registration
34 .setCost(0),
Obaid6e7f5f12014-03-11 14:46:10 -050035 bind(onSuccess), onFail);
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080036}
37
38void
39Controller::selfDeregisterPrefix(const Name& prefixToRegister,
40 const SuccessCallback& onSuccess,
41 const FailCallback& onFail)
42{
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080043 startCommand("unregister",
44 PrefixRegOptions()
45 .setName(prefixToRegister)
Obaid6e7f5f12014-03-11 14:46:10 -050046 .setFaceId(0), // self-registration
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080047 bind(onSuccess), onFail);
48}
49
50void
Obaid6e7f5f12014-03-11 14:46:10 -050051Controller::registerPrefix(const PrefixRegOptions& options,
52 const CommandSucceedCallback& onSuccess,
53 const FailCallback& onFail)
54{
55 startCommand("register", options, onSuccess, onFail);
56}
57
58void
59Controller::unregisterPrefix(const PrefixRegOptions& options,
60 const CommandSucceedCallback& onSuccess,
61 const FailCallback& onFail)
62{
63 startCommand("unregister", options, onSuccess, onFail);
64}
65
66void
67Controller::advertisePrefix(const PrefixRegOptions& options,
68 const CommandSucceedCallback& onSuccess,
69 const FailCallback& onFail)
70{
71 startCommand("advertise", options, onSuccess, onFail);
72}
73
74void
75Controller::withdrawPrefix(const PrefixRegOptions& options,
76 const CommandSucceedCallback& onSuccess,
77 const FailCallback& onFail)
78{
79 startCommand("withdraw", options, onSuccess, onFail);
80}
81
82void
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080083Controller::startCommand(const std::string& command,
84 const PrefixRegOptions& options,
85 const CommandSucceedCallback& onSuccess,
86 const FailCallback& onFail)
87{
88 Name commandInterestName("/localhost/nrd");
89 commandInterestName
90 .append(command)
91 .append(options.wireEncode());
92
93 Interest commandInterest(commandInterestName);
Obaid6e7f5f12014-03-11 14:46:10 -050094 m_commandInterestGenerator.generate(commandInterest);
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080095
96 m_face.expressInterest(commandInterest,
97 bind(&Controller::processCommandResponse, this, _2,
98 onSuccess, onFail),
99 bind(onFail, "Command Interest timed out"));
100}
101
102void
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800103Controller::processCommandResponse(Data& data,
104 const CommandSucceedCallback& onSuccess,
105 const FailCallback& onFail)
106{
Obaid6e7f5f12014-03-11 14:46:10 -0500107 /// \todo Add validation of incoming Data
108
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800109 try
110 {
111 nfd::ControlResponse response(data.getContent().blockFromValue());
112 if (response.getCode() != 200)
113 return onFail(response.getText());
114
115 PrefixRegOptions options(response.getBody());
116 return onSuccess(options);
117 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700118 catch (ndn::Tlv::Error& e)
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800119 {
120 if (static_cast<bool>(onFail))
121 return onFail(e.what());
122 }
123}
124
125} // namespace nrd
126} // namespace ndn