blob: e7b997580d55493e592bc89ea9ef85aa1ebbdf27 [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 {
35 size_t total_len = 0;
36 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
37 data.setFreshnessPeriod(1000); // 1 sec
38
39 // create and encode uri block
40 Block uriBlock = dataBlock(tlv::nfd::Uri,
41 reinterpret_cast<const uint8_t*>(m_faceMgmtUri.c_str()),
42 m_faceMgmtUri.size());
43 data.setContent(uriBlock);
44 m_keyChain.sign(data);
45 m_face.put(data);
46 }
47
48 void
49 onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
50 {
51 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" <<
52 reason << ")" << std::endl;
53 m_face.shutdown();
54 }
55
56 void
57 listen()
58 {
59 m_face.setInterestFilter("/localhop/ndn-autoconf/hub",
60 ndn::bind(&NdnAutoconfigServer::onInterest, this, _1, _2),
61 ndn::bind(&NdnAutoconfigServer::onRegisterFailed, this, _1, _2));
62 m_face.processEvents();
63 }
64
65private:
66 ndn::Face m_face;
67 KeyChain m_keyChain;
68 std::string m_faceMgmtUri;
69
70};
71
72int
73main(int argc, char** argv)
74{
75 int opt;
76 const char* programName = argv[0];
77
78 while ((opt = getopt(argc, argv, "h")) != -1)
79 {
80 switch (opt)
81 {
82 case 'h':
83 usage(programName);
84 return 0;
85
86 default:
87 usage(programName);
88 return 1;
89 }
90 }
91
92 if (argc != optind + 1)
93 {
94 usage(programName);
95 return 1;
96 }
97 // get the configured face managment uri
98 NdnAutoconfigServer producer(argv[optind]);
99
100 try
101 {
102 producer.listen();
103 }
104 catch (std::exception& error)
105 {
106 std::cerr << "ERROR: " << error.what() << std::endl;
107 }
108 return 0;
109}