blob: 530a9d47136dbf6a9a6237e1e647f23e70e11400 [file] [log] [blame]
Nick Gordon94719252016-10-20 20:48:01 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Nick Gordon94719252016-10-20 20:48:01 +00004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "nfd-rib-readvertise-destination.hpp"
Junxiao Shi63b67e22017-03-06 19:46:02 +000027#include "core/logger.hpp"
28
29#include <ndn-cxx/mgmt/nfd/command-options.hpp>
30#include <ndn-cxx/mgmt/nfd/control-command.hpp>
31#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
32#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Nick Gordon94719252016-10-20 20:48:01 +000033
34namespace nfd {
35namespace rib {
36
Davide Pesaventoa3148082018-04-12 18:21:54 -040037NFD_LOG_INIT(NfdRibReadvertiseDestination);
Junxiao Shi63b67e22017-03-06 19:46:02 +000038
39using ndn::nfd::CommandOptions;
Nick Gordon94719252016-10-20 20:48:01 +000040using ndn::nfd::ControlParameters;
41using ndn::nfd::ControlResponse;
42
43NfdRibReadvertiseDestination::NfdRibReadvertiseDestination(ndn::nfd::Controller& controller,
Junxiao Shi63b67e22017-03-06 19:46:02 +000044 const Name& commandPrefix,
Nick Gordon04262d92017-01-31 12:27:06 -060045 Rib& rib)
Nick Gordon94719252016-10-20 20:48:01 +000046 : m_controller(controller)
47 , m_commandPrefix(commandPrefix)
48{
Junxiao Shi63b67e22017-03-06 19:46:02 +000049 m_ribInsertConn = rib.afterInsertEntry.connect(
50 std::bind(&NfdRibReadvertiseDestination::handleRibInsert, this, _1));
51 m_ribEraseConn = rib.afterEraseEntry.connect(
52 std::bind(&NfdRibReadvertiseDestination::handleRibErase, this, _1));
Nick Gordon94719252016-10-20 20:48:01 +000053}
54
55void
Junxiao Shi63b67e22017-03-06 19:46:02 +000056NfdRibReadvertiseDestination::advertise(const nfd::rib::ReadvertisedRoute& rr,
Nick Gordon94719252016-10-20 20:48:01 +000057 std::function<void()> successCb,
58 std::function<void(const std::string&)> failureCb)
59{
Junxiao Shi63b67e22017-03-06 19:46:02 +000060 NFD_LOG_DEBUG("advertise " << rr.prefix << " on " << m_commandPrefix);
61
62 m_controller.start<ndn::nfd::RibRegisterCommand>(
63 ControlParameters().setName(rr.prefix).setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT),
Nick Gordon94719252016-10-20 20:48:01 +000064 [=] (const ControlParameters& cp) { successCb(); },
Junxiao Shi63b67e22017-03-06 19:46:02 +000065 [=] (const ControlResponse& cr) { failureCb(cr.getText()); },
66 CommandOptions().setPrefix(m_commandPrefix).setSigningInfo(rr.signer));
Nick Gordon94719252016-10-20 20:48:01 +000067}
68
69void
Junxiao Shi63b67e22017-03-06 19:46:02 +000070NfdRibReadvertiseDestination::withdraw(const nfd::rib::ReadvertisedRoute& rr,
Nick Gordon94719252016-10-20 20:48:01 +000071 std::function<void()> successCb,
72 std::function<void(const std::string&)> failureCb)
73{
Junxiao Shi63b67e22017-03-06 19:46:02 +000074 NFD_LOG_DEBUG("withdraw " << rr.prefix << " on " << m_commandPrefix);
75
76 m_controller.start<ndn::nfd::RibUnregisterCommand>(
77 ControlParameters().setName(rr.prefix).setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT),
Nick Gordon94719252016-10-20 20:48:01 +000078 [=] (const ControlParameters& cp) { successCb(); },
Junxiao Shi63b67e22017-03-06 19:46:02 +000079 [=] (const ControlResponse& cr) { failureCb(cr.getText()); },
80 CommandOptions().setPrefix(m_commandPrefix).setSigningInfo(rr.signer));
Nick Gordon94719252016-10-20 20:48:01 +000081}
82
Nick Gordon04262d92017-01-31 12:27:06 -060083void
Junxiao Shi63b67e22017-03-06 19:46:02 +000084NfdRibReadvertiseDestination::handleRibInsert(const ndn::Name& name)
Nick Gordon04262d92017-01-31 12:27:06 -060085{
Junxiao Shi63b67e22017-03-06 19:46:02 +000086 if (name.isPrefixOf(m_commandPrefix)) {
Nick Gordon04262d92017-01-31 12:27:06 -060087 setAvailability(true);
88 }
89}
90
91void
Junxiao Shi63b67e22017-03-06 19:46:02 +000092NfdRibReadvertiseDestination::handleRibErase(const ndn::Name& name)
Nick Gordon04262d92017-01-31 12:27:06 -060093{
Junxiao Shi63b67e22017-03-06 19:46:02 +000094 if (name.isPrefixOf(m_commandPrefix)) {
Nick Gordon04262d92017-01-31 12:27:06 -060095 setAvailability(false);
96 }
97}
98
Nick Gordon94719252016-10-20 20:48:01 +000099} // namespace rib
100} // namespace nfd