Vince Lehman | 27f1add | 2014-10-16 17:14:46 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 University of Memphis, |
| 4 | * Regents of the University of California |
| 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | **/ |
| 21 | |
| 22 | #include "face-controller.hpp" |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 23 | |
| 24 | #include "common.hpp" |
Vince Lehman | 27f1add | 2014-10-16 17:14:46 -0500 | [diff] [blame] | 25 | #include "logger.hpp" |
| 26 | |
| 27 | namespace nlsr { |
| 28 | namespace util { |
| 29 | |
| 30 | INIT_LOGGER("FaceController"); |
| 31 | |
| 32 | using ndn::util::FaceUri; |
| 33 | |
| 34 | const ndn::time::seconds FaceController::TIME_ALLOWED_FOR_CANONIZATION = ndn::time::seconds(4); |
| 35 | |
| 36 | void |
| 37 | FaceController::createFace(const std::string& request, |
| 38 | const CommandSuccessCallback& onSuccess, |
| 39 | const CommandFailureCallback& onFailure) |
| 40 | { |
| 41 | FaceUri uri(request); |
| 42 | |
| 43 | _LOG_TRACE("Converting " << uri << " to canonical form"); |
| 44 | uri.canonize(bind(&FaceController::onCanonizeSuccess, this, _1, onSuccess, onFailure, uri), |
| 45 | bind(&FaceController::onCanonizeFailure, this, _1, onSuccess, onFailure, uri), |
| 46 | m_ioService, TIME_ALLOWED_FOR_CANONIZATION); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | FaceController::createFaceInNfd(const FaceUri& uri, |
| 51 | const CommandSuccessCallback& onSuccess, |
| 52 | const CommandFailureCallback& onFailure) |
| 53 | { |
| 54 | ndn::nfd::ControlParameters faceParameters; |
| 55 | faceParameters.setUri(uri.toString()); |
| 56 | |
| 57 | _LOG_DEBUG("Creating Face in NFD with face-uri: " << uri); |
| 58 | m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters, onSuccess, onFailure); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | FaceController::onCanonizeSuccess(const FaceUri& uri, |
| 63 | const CommandSuccessCallback& onSuccess, |
| 64 | const CommandFailureCallback& onFailure, |
| 65 | const FaceUri& request) |
| 66 | { |
| 67 | _LOG_DEBUG("Converted " << request << " to canonical form: " << uri); |
| 68 | |
| 69 | createFaceInNfd(uri, onSuccess, onFailure); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | FaceController::onCanonizeFailure(const std::string& reason, |
| 74 | const CommandSuccessCallback& onSuccess, |
| 75 | const CommandFailureCallback& onFailure, |
| 76 | const FaceUri& request) |
| 77 | { |
| 78 | _LOG_WARN("Could not convert " << request << " to canonical form: " << reason); |
| 79 | onFailure(CANONIZE_ERROR_CODE, "Could not canonize face-uri: " + request.toString()); |
| 80 | } |
| 81 | |
| 82 | } // namespace util |
| 83 | } // namespace nlsr |