blob: 18c063d7e3a27c40fe5df10fd97339415f05197d [file] [log] [blame]
hilatadd50ada2014-03-13 12:48:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include <ndn-cpp-dev/face.hpp>
8#include <ndn-cpp-dev/security/key-chain.hpp>
9
10
11void
12usage(const char* programName)
13{
14 std::cout << "Usage:\n" << programName << " [-h] Uri \n"
15 " -h print usage and exit\n"
16 "\n"
17 " Uri - a FaceMgmt URI\n"
18 << std::endl;
19}
20
21using namespace ndn;
22
23class NdnAutoconfigServer
24{
25public:
26 explicit
27 NdnAutoconfigServer(const std::string& uri)
28 : m_faceMgmtUri(uri)
29 {
30 }
31
32 void
33 onInterest(const Name& name, const Interest& interest)
34 {
hilatadd50ada2014-03-13 12:48:47 -050035 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070036 data.setFreshnessPeriod(time::hours(1)); // 1 hour
hilatadd50ada2014-03-13 12:48:47 -050037
38 // create and encode uri block
39 Block uriBlock = dataBlock(tlv::nfd::Uri,
40 reinterpret_cast<const uint8_t*>(m_faceMgmtUri.c_str()),
41 m_faceMgmtUri.size());
42 data.setContent(uriBlock);
43 m_keyChain.sign(data);
44 m_face.put(data);
45 }
46
47 void
48 onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
49 {
50 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" <<
51 reason << ")" << std::endl;
52 m_face.shutdown();
53 }
54
55 void
56 listen()
57 {
58 m_face.setInterestFilter("/localhop/ndn-autoconf/hub",
59 ndn::bind(&NdnAutoconfigServer::onInterest, this, _1, _2),
60 ndn::bind(&NdnAutoconfigServer::onRegisterFailed, this, _1, _2));
61 m_face.processEvents();
62 }
63
64private:
65 ndn::Face m_face;
66 KeyChain m_keyChain;
67 std::string m_faceMgmtUri;
68
69};
70
71int
72main(int argc, char** argv)
73{
74 int opt;
75 const char* programName = argv[0];
76
77 while ((opt = getopt(argc, argv, "h")) != -1)
78 {
79 switch (opt)
80 {
81 case 'h':
82 usage(programName);
83 return 0;
84
85 default:
86 usage(programName);
87 return 1;
88 }
89 }
90
91 if (argc != optind + 1)
92 {
93 usage(programName);
94 return 1;
95 }
96 // get the configured face managment uri
97 NdnAutoconfigServer producer(argv[optind]);
98
99 try
100 {
101 producer.listen();
102 }
103 catch (std::exception& error)
104 {
105 std::cerr << "ERROR: " << error.what() << std::endl;
106 }
107 return 0;
108}