blob: e6c0a8b65841fada27d46bd8001bbc3a57ec3639 [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
Vince Lehman27f1add2014-10-16 17:14:46 -050034void
35FaceController::createFace(const std::string& request,
36 const CommandSuccessCallback& onSuccess,
37 const CommandFailureCallback& onFailure)
38{
39 FaceUri uri(request);
40
41 _LOG_TRACE("Converting " << uri << " to canonical form");
dmcoomes9f936662017-03-02 10:33:09 -060042 uri.canonize(std::bind(&FaceController::onCanonizeSuccess, this, _1, onSuccess, onFailure, uri),
43 std::bind(&FaceController::onCanonizeFailure, this, _1, onSuccess, onFailure, uri),
Vince Lehman27f1add2014-10-16 17:14:46 -050044 m_ioService, TIME_ALLOWED_FOR_CANONIZATION);
45}
46
47void
48FaceController::createFaceInNfd(const FaceUri& uri,
49 const CommandSuccessCallback& onSuccess,
50 const CommandFailureCallback& onFailure)
51{
52 ndn::nfd::ControlParameters faceParameters;
53 faceParameters.setUri(uri.toString());
54
55 _LOG_DEBUG("Creating Face in NFD with face-uri: " << uri);
Ashlesh Gawande218aaef2017-02-25 10:04:09 -060056 m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters, onSuccess,
57 [onSuccess, onFailure, uri] (const ndn::nfd::ControlResponse& response) {
58 ndn::nfd::ControlParameters faceParams(response.getBody());
59 if (response.getCode() == 409 && faceParams.getUri() == uri.toString()) {
60 _LOG_DEBUG("Got 409 - treating as success");
61 onSuccess(faceParams);
62 }
63 else {
64 onFailure(response);
65 }
66 }
67 );
Vince Lehman27f1add2014-10-16 17:14:46 -050068}
69
70void
71FaceController::onCanonizeSuccess(const FaceUri& uri,
72 const CommandSuccessCallback& onSuccess,
73 const CommandFailureCallback& onFailure,
74 const FaceUri& request)
75{
76 _LOG_DEBUG("Converted " << request << " to canonical form: " << uri);
77
78 createFaceInNfd(uri, onSuccess, onFailure);
79}
80
81void
82FaceController::onCanonizeFailure(const std::string& reason,
83 const CommandSuccessCallback& onSuccess,
84 const CommandFailureCallback& onFailure,
85 const FaceUri& request)
86{
87 _LOG_WARN("Could not convert " << request << " to canonical form: " << reason);
Junxiao Shi63bd0342016-08-17 16:57:14 +000088 onFailure(ndn::nfd::ControlResponse(CANONIZE_ERROR_CODE,
89 "Could not canonize face-uri: " + request.toString()));
Vince Lehman27f1add2014-10-16 17:14:46 -050090}
91
92} // namespace util
93} // namespace nlsr