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  FaceUri faceUri(uri);
44  std::cerr << "About to connect to: " << uri << std::endl;
45 
46  faceUri.canonize(bind(&Base::onCanonizeSuccess, this, _1),
47  bind(&Base::onCanonizeFailure, this, _1),
48  m_face.getIoService(), time::seconds(4));
49 
50 }
51 
52 
53 void
54 Base::onCanonizeSuccess(const FaceUri& canonicalUri)
55 {
56  m_controller.start<ndn::nfd::FaceCreateCommand>(
57  ControlParameters().setUri(canonicalUri.toString()),
58  bind(&Base::onHubConnectSuccess, this, _1),
59  bind(&Base::onHubConnectError, this, _1));
60 }
61 
62 void
63 Base::onCanonizeFailure(const std::string& reason)
64 {
65  BOOST_THROW_EXCEPTION(Error("FaceUri canonization failed: " + reason));
66 }
67 
68 void
69 Base::onHubConnectSuccess(const ControlParameters& resp)
70 {
71  std::cerr << "Successfully created face: " << resp << std::endl;
72 
73  static const Name TESTBED_PREFIX = "/ndn";
74  registerPrefix(TESTBED_PREFIX, resp.getFaceId());
75 
76  static const Name LOCALHOP_NFD_PREFIX = "/localhop/nfd";
77  registerPrefix(LOCALHOP_NFD_PREFIX, resp.getFaceId());
78 }
79 
80 void
81 Base::onHubConnectError(const ControlResponse& response)
82 {
83  std::ostringstream os;
84  os << "Failed to create face: " << response.getText() << " (code: " << response.getCode() << ")";
85  BOOST_THROW_EXCEPTION(Error(os.str()));
86 }
87 
88 void
89 Base::registerPrefix(const Name& prefix, uint64_t faceId)
90 {
91  // Register a prefix in RIB
92  m_controller.start<ndn::nfd::RibRegisterCommand>(
93  ControlParameters()
94  .setName(prefix)
95  .setFaceId(faceId)
96  .setOrigin(ndn::nfd::ROUTE_ORIGIN_AUTOCONF)
97  .setCost(100)
98  .setExpirationPeriod(time::milliseconds::max()),
99  bind(&Base::onPrefixRegistrationSuccess, this, _1),
100  bind(&Base::onPrefixRegistrationError, this, _1));
101 }
102 
103 void
104 Base::onPrefixRegistrationSuccess(const ControlParameters& commandSuccessResult)
105 {
106  std::cerr << "Successful in name registration: " << commandSuccessResult << std::endl;
107 }
108 
109 void
110 Base::onPrefixRegistrationError(const ControlResponse& response)
111 {
112  BOOST_THROW_EXCEPTION(Error("Failed in name registration, " + response.getText() +
113  " (code: " + to_string(response.getCode()) + ")"));
114 }
115 
116 
117 } // namespace autoconfig
118 } // namespace tools
119 } // 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 Controller instance.
Definition: base.cpp:32
void connectToHub(const std::string &uri)
Attempt to connect to local hub using the uri FaceUri.
Definition: base.cpp:41
ndn::nfd::Controller m_controller
Definition: base.hpp:113
std::function< void(const std::string &)> NextStageCallback
Callback to be called when the stage fails.
Definition: base.hpp:64