face-id-fetcher.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "face-id-fetcher.hpp"
27 
28 #include <boost/lexical_cast.hpp>
29 #include <boost/regex.hpp>
30 
31 #include <ndn-cxx/mgmt/nfd/face-query-filter.hpp>
32 #include <ndn-cxx/mgmt/nfd/face-status.hpp>
33 #include <ndn-cxx/util/segment-fetcher.hpp>
34 
35 namespace nfd {
36 namespace tools {
37 namespace nfdc {
38 
39 FaceIdFetcher::FaceIdFetcher(ndn::Face& face,
40  ndn::nfd::Controller& controller,
41  bool allowCreate,
42  const SuccessCallback& onSucceed,
43  const FailureCallback& onFail)
44  : m_face(face)
45  , m_controller(controller)
46  , m_allowCreate(allowCreate)
47  , m_onSucceed(onSucceed)
48  , m_onFail(onFail)
49 {
50 }
51 
52 void
53 FaceIdFetcher::start(ndn::Face& face,
54  ndn::nfd::Controller& controller,
55  const std::string& input,
56  bool allowCreate,
57  const SuccessCallback& onSucceed,
58  const FailureCallback& onFail)
59 {
60  // 1. Try parse input as FaceId, if input is FaceId, succeed with parsed FaceId
61  // 2. Try parse input as FaceUri, if input is not FaceUri, fail
62  // 3. Canonize faceUri
63  // 4. If canonization fails, fail
64  // 5. Query for face
65  // 6. If query succeeds and finds a face, succeed with found FaceId
66  // 7. Create face
67  // 8. If face creation succeeds, succeed with created FaceId
68  // 9. Fail
69 
70  boost::regex e("^[a-z0-9]+\\:.*");
71  if (!boost::regex_match(input, e)) {
72  try {
73  uint32_t faceId = boost::lexical_cast<uint32_t>(input);
74  onSucceed(faceId);
75  return;
76  }
77  catch (const boost::bad_lexical_cast&) {
78  onFail("No valid faceId or faceUri is provided");
79  return;
80  }
81  }
82  else {
83  FaceUri faceUri;
84  if (!faceUri.parse(input)) {
85  onFail("FaceUri parse failed");
86  return;
87  }
88 
89  auto fetcher = new FaceIdFetcher(std::ref(face), std::ref(controller),
90  allowCreate, onSucceed, onFail);
91  fetcher->startGetFaceId(faceUri);
92  }
93 }
94 
95 void
96 FaceIdFetcher::startGetFaceId(const FaceUri& faceUri)
97 {
98  faceUri.canonize(bind(&FaceIdFetcher::onCanonizeSuccess, this, _1),
99  bind(&FaceIdFetcher::onCanonizeFailure, this, _1),
100  m_face.getIoService(), time::seconds(4));
101 }
102 
103 void
104 FaceIdFetcher::onCanonizeSuccess(const FaceUri& canonicalUri)
105 {
106  ndn::Name queryName("/localhost/nfd/faces/query");
107  ndn::nfd::FaceQueryFilter queryFilter;
108  queryFilter.setRemoteUri(canonicalUri.toString());
109  queryName.append(queryFilter.wireEncode());
110 
111  ndn::Interest interestPacket(queryName);
112  interestPacket.setMustBeFresh(true);
113  interestPacket.setInterestLifetime(time::milliseconds(4000));
114  auto interest = std::make_shared<ndn::Interest>(interestPacket);
115 
116  ndn::util::SegmentFetcher::fetch(
117  m_face, *interest, m_validator,
118  bind(&FaceIdFetcher::onQuerySuccess, this, _1, canonicalUri),
119  bind(&FaceIdFetcher::onQueryFailure, this, _1, canonicalUri));
120 }
121 
122 void
123 FaceIdFetcher::onCanonizeFailure(const std::string& reason)
124 {
125  fail("Canonize faceUri failed : " + reason);
126 }
127 
128 void
129 FaceIdFetcher::onQuerySuccess(const ndn::ConstBufferPtr& data,
130  const FaceUri& canonicalUri)
131 {
132  size_t offset = 0;
133  bool isOk = false;
134  ndn::Block block;
135  std::tie(isOk, block) = ndn::Block::fromBuffer(data, offset);
136 
137  if (!isOk) {
138  if (m_allowCreate) {
139  startFaceCreate(canonicalUri);
140  }
141  else {
142  fail("Fail to find faceId");
143  }
144  }
145  else {
146  try {
147  ndn::nfd::FaceStatus status(block);
148  succeed(status.getFaceId());
149  }
150  catch (const ndn::tlv::Error& e) {
151  std::string errorMessage(e.what());
152  fail("ERROR: " + errorMessage);
153  }
154  }
155 }
156 
157 void
158 FaceIdFetcher::onQueryFailure(uint32_t errorCode,
159  const FaceUri& canonicalUri)
160 {
161  std::stringstream ss;
162  ss << "Cannot fetch data (code " << errorCode << ")";
163  fail(ss.str());
164 }
165 
166 void
167 FaceIdFetcher::onFaceCreateError(const ndn::nfd::ControlResponse& response,
168  const std::string& message)
169 {
170  std::stringstream ss;
171  ss << message << " : " << response.getText() << " (code " << response.getCode() << ")";
172  fail(ss.str());
173 }
174 
175 void
176 FaceIdFetcher::startFaceCreate(const FaceUri& canonicalUri)
177 {
178  ndn::nfd::ControlParameters parameters;
179  parameters.setUri(canonicalUri.toString());
180 
181  m_controller.start<ndn::nfd::FaceCreateCommand>(parameters,
182  [this] (const ndn::nfd::ControlParameters& result) { succeed(result.getFaceId()); },
183  bind(&FaceIdFetcher::onFaceCreateError, this, _1, "Face creation failed"));
184 }
185 
186 void
187 FaceIdFetcher::succeed(uint32_t faceId)
188 {
189  m_onSucceed(faceId);
190  delete this;
191 }
192 
193 void
194 FaceIdFetcher::fail(const std::string& reason)
195 {
196  m_onFail(reason);
197  delete this;
198 }
199 
200 } // namespace nfdc
201 } // namespace tools
202 } // namespace nfd
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
std::function< void(const std::string &)> FailureCallback
std::function< void(uint32_t)> SuccessCallback