blob: 62778cd4e85009f9d755703c8d689bd68a6e91e3 [file] [log] [blame]
Vince Lehman27f1add2014-10-16 17:14:46 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
Vince Lehman27f1add2014-10-16 17:14:46 -05005 *
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 Lehman0a7da612014-10-29 14:39:29 -050023
24#include "common.hpp"
Vince Lehman27f1add2014-10-16 17:14:46 -050025#include "logger.hpp"
26
27namespace nlsr {
28namespace util {
29
30INIT_LOGGER("FaceController");
31
32using ndn::util::FaceUri;
33
34const ndn::time::seconds FaceController::TIME_ALLOWED_FOR_CANONIZATION = ndn::time::seconds(4);
35
36void
37FaceController::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");
dmcoomes9f936662017-03-02 10:33:09 -060044 uri.canonize(std::bind(&FaceController::onCanonizeSuccess, this, _1, onSuccess, onFailure, uri),
45 std::bind(&FaceController::onCanonizeFailure, this, _1, onSuccess, onFailure, uri),
Vince Lehman27f1add2014-10-16 17:14:46 -050046 m_ioService, TIME_ALLOWED_FOR_CANONIZATION);
47}
48
49void
50FaceController::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);
Ashlesh Gawande218aaef2017-02-25 10:04:09 -060058 m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters, onSuccess,
59 [onSuccess, onFailure, uri] (const ndn::nfd::ControlResponse& response) {
60 ndn::nfd::ControlParameters faceParams(response.getBody());
61 if (response.getCode() == 409 && faceParams.getUri() == uri.toString()) {
62 _LOG_DEBUG("Got 409 - treating as success");
63 onSuccess(faceParams);
64 }
65 else {
66 onFailure(response);
67 }
68 }
69 );
Vince Lehman27f1add2014-10-16 17:14:46 -050070}
71
72void
73FaceController::onCanonizeSuccess(const FaceUri& uri,
74 const CommandSuccessCallback& onSuccess,
75 const CommandFailureCallback& onFailure,
76 const FaceUri& request)
77{
78 _LOG_DEBUG("Converted " << request << " to canonical form: " << uri);
79
80 createFaceInNfd(uri, onSuccess, onFailure);
81}
82
83void
84FaceController::onCanonizeFailure(const std::string& reason,
85 const CommandSuccessCallback& onSuccess,
86 const CommandFailureCallback& onFailure,
87 const FaceUri& request)
88{
89 _LOG_WARN("Could not convert " << request << " to canonical form: " << reason);
Junxiao Shi63bd0342016-08-17 16:57:14 +000090 onFailure(ndn::nfd::ControlResponse(CANONIZE_ERROR_CODE,
91 "Could not canonize face-uri: " + request.toString()));
Vince Lehman27f1add2014-10-16 17:14:46 -050092}
93
94} // namespace util
95} // namespace nlsr