base.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "base.hpp"
27 
28 namespace ndn {
29 namespace tools {
30 namespace autoconfig {
31 
32 Base::Base(Face& face, KeyChain& keyChain, const NextStageCallback& nextStageOnFailure)
33  : m_face(face)
34  , m_keyChain(keyChain)
35  , m_controller(face, keyChain)
36  , m_nextStageOnFailure(nextStageOnFailure)
37 {
38 }
39 
40 void
41 Base::connectToHub(const std::string& uri)
42 {
43  util::FaceUri faceUri(uri);
44 
45  faceUri.canonize(bind(&Base::onCanonizeSuccess, this, _1),
46  bind(&Base::onCanonizeFailure, this, _1),
47  m_face.getIoService(), time::seconds(4));
48 
49 }
50 
51 
52 void
53 Base::onCanonizeSuccess(const util::FaceUri& canonicalUri)
54 {
55  std::cerr << "About to connect to: " << canonicalUri.toString() << std::endl;
56 
57  m_controller.start<nfd::FaceCreateCommand>(nfd::ControlParameters()
58  .setUri(canonicalUri.toString()),
59  bind(&Base::onHubConnectSuccess, this, _1),
60  bind(&Base::onHubConnectError, this, _1));
61 }
62 
63 void
64 Base::onCanonizeFailure(const std::string& reason)
65 {
66  std::ostringstream os;
67  os << "FaceUri canonization failed: " << reason;
68  BOOST_THROW_EXCEPTION(Error(os.str()));
69 }
70 
71 void
72 Base::onHubConnectSuccess(const nfd::ControlParameters& resp)
73 {
74  std::cerr << "Successfully created face: " << resp << std::endl;
75 
76  static const Name TESTBED_PREFIX = "/ndn";
77  registerPrefix(TESTBED_PREFIX, resp.getFaceId());
78 
79  static const Name LOCALHOP_NFD_PREFIX = "/localhop/nfd";
80  registerPrefix(LOCALHOP_NFD_PREFIX, resp.getFaceId());
81 }
82 
83 void
84 Base::onHubConnectError(const nfd::ControlResponse& response)
85 {
86  std::ostringstream os;
87  os << "Failed to create face: " << response.getText() << " (code: " << response.getCode() << ")";
88  BOOST_THROW_EXCEPTION(Error(os.str()));
89 }
90 
91 void
92 Base::registerPrefix(const Name& prefix, uint64_t faceId)
93 {
94  // Register a prefix in RIB
95  m_controller.start<nfd::RibRegisterCommand>(nfd::ControlParameters()
96  .setName(prefix)
97  .setFaceId(faceId)
98  .setOrigin(nfd::ROUTE_ORIGIN_AUTOCONF)
99  .setCost(100)
100  .setExpirationPeriod(time::milliseconds::max()),
101  bind(&Base::onPrefixRegistrationSuccess, this, _1),
102  bind(&Base::onPrefixRegistrationError, this, _1));
103 }
104 
105 void
106 Base::onPrefixRegistrationSuccess(const nfd::ControlParameters& commandSuccessResult)
107 {
108  std::cerr << "Successful in name registration: " << commandSuccessResult << std::endl;
109 }
110 
111 void
112 Base::onPrefixRegistrationError(const nfd::ControlResponse& response)
113 {
114  std::ostringstream os;
115  os << "Failed in name registration, " << response.getText() << " (code: " << response.getCode() << ")";
116  BOOST_THROW_EXCEPTION(Error(os.str()));
117 }
118 
119 
120 } // namespace autoconfig
121 } // namespace tools
122 } // namespace ndn
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
Definition: nfd.hpp:35
Base(Face &face, KeyChain &keyChain, const NextStageCallback &nextStageOnFailure)
Initialize variables and create nfd::Controller instance.
Definition: base.cpp:32
nfd::Controller m_controller
Definition: base.hpp:110
void connectToHub(const std::string &uri)
Attempt to connect to local hub using the uri FaceUri.
Definition: base.cpp:41
std::function< void(const std::string &)> NextStageCallback
Callback to be called when the stage fails.
Definition: base.hpp:61