blob: ed051e1f10d8fdb7dc6f7116baad240964d9dd05 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014-2015, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base.hpp"
namespace ndn {
namespace tools {
namespace autoconfig {
Base::Base(Face& face, KeyChain& keyChain, const NextStageCallback& nextStageOnFailure)
: m_face(face)
, m_keyChain(keyChain)
, m_controller(face, keyChain)
, m_nextStageOnFailure(nextStageOnFailure)
{
}
void
Base::connectToHub(const std::string& uri)
{
util::FaceUri faceUri(uri);
faceUri.canonize(bind(&Base::onCanonizeSuccess, this, _1),
bind(&Base::onCanonizeFailure, this, _1),
m_face.getIoService(), time::seconds(4));
}
void
Base::onCanonizeSuccess(const util::FaceUri& canonicalUri)
{
std::cerr << "About to connect to: " << canonicalUri.toString() << std::endl;
m_controller.start<nfd::FaceCreateCommand>(nfd::ControlParameters()
.setUri(canonicalUri.toString()),
bind(&Base::onHubConnectSuccess, this, _1),
bind(&Base::onHubConnectError, this, _1, _2));
}
void
Base::onCanonizeFailure(const std::string& reason)
{
std::ostringstream os;
os << "FaceUri canonization failed: " << reason;
throw Error(os.str());
}
void
Base::onHubConnectSuccess(const nfd::ControlParameters& resp)
{
std::cerr << "Successfully created face: " << resp << std::endl;
static const Name TESTBED_PREFIX = "/ndn";
registerPrefix(TESTBED_PREFIX, resp.getFaceId());
}
void
Base::onHubConnectError(uint32_t code, const std::string& error)
{
std::ostringstream os;
os << "Failed to create face: " << error << " (code: " << code << ")";
throw Error(os.str());
}
void
Base::registerPrefix(const Name& prefix, uint64_t faceId)
{
// Register a prefix in RIB
m_controller.start<nfd::RibRegisterCommand>(nfd::ControlParameters()
.setName(prefix)
.setFaceId(faceId)
.setOrigin(nfd::ROUTE_ORIGIN_AUTOCONF)
.setCost(100)
.setExpirationPeriod(time::milliseconds::max()),
bind(&Base::onPrefixRegistrationSuccess, this, _1),
bind(&Base::onPrefixRegistrationError, this, _1, _2));
}
void
Base::onPrefixRegistrationSuccess(const nfd::ControlParameters& commandSuccessResult)
{
std::cerr << "Successful in name registration: " << commandSuccessResult << std::endl;
}
void
Base::onPrefixRegistrationError(uint32_t code, const std::string& error)
{
std::ostringstream os;
os << "Failed in name registration, " << error << " (code: " << code << ")";
throw Error(os.str());
}
} // namespace autoconfig
} // namespace tools
} // namespace ndn