blob: d604c9d5154b035950a3b9b2c2dd410cd4b69f4d [file] [log] [blame]
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "version.hpp"
27
28#include "multicast-discovery.hpp"
29#include "guess-from-search-domains.hpp"
30#include "guess-from-identity-name.hpp"
31
32#include <boost/noncopyable.hpp>
33
34namespace ndn {
35namespace tools {
36
37class NdnAutoconfig : boost::noncopyable
38{
39public:
40 class Error : public std::runtime_error
41 {
42 public:
43 explicit
44 Error(const std::string& what)
45 : std::runtime_error(what)
46 {
47 }
48 };
49
50 NdnAutoconfig()
51 : m_stage1(m_face, m_keyChain,
52 [&] (const std::string& errorMessage) {
53 std::cerr << "Stage 1 failed: " << errorMessage << std::endl;
54 m_stage2.start();
55 })
56 , m_stage2(m_face, m_keyChain,
57 [&] (const std::string& errorMessage) {
58 std::cerr << "Stage 2 failed: " << errorMessage << std::endl;
59 m_stage3.start();
60 })
61 , m_stage3(m_face, m_keyChain,
62 [&] (const std::string& errorMessage) {
63 std::cerr << "Stage 3 failed: " << errorMessage << std::endl;
64 throw Error("No more stages, automatic discovery failed");
65 })
66 {
67 m_stage1.start();
68 }
69
70 void
71 run()
72 {
73 m_face.processEvents();
74 }
75
76 static void
77 usage(const char* programName)
78 {
79 std::cout << "Usage:\n"
80 << " " << programName << " [options]\n"
81 << "\n"
82 << "Options:\n"
83 << " [-h] - print usage and exit\n"
84 << " [-V] - print version number and exit\n"
85 << std::endl;
86 }
87
88private:
89 Face m_face;
90 KeyChain m_keyChain;
91
92 autoconfig::MulticastDiscovery m_stage1;
93 autoconfig::GuessFromSearchDomains m_stage2;
94 autoconfig::GuessFromIdentityName m_stage3;
95};
96
97} // namespace tools
98} // namespace ndn
99
100int
101main(int argc, char** argv)
102{
103 int opt;
104 const char* programName = argv[0];
105
106 while ((opt = getopt(argc, argv, "hV")) != -1) {
107 switch (opt) {
108 case 'h':
109 ndn::tools::NdnAutoconfig::usage(programName);
110 return 0;
111 case 'V':
112 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
113 return 0;
114 }
115 }
116
117 try {
118 ndn::tools::NdnAutoconfig autoConfigInstance;
119 autoConfigInstance.run();
120 }
121 catch (const std::exception& error) {
122 std::cerr << "ERROR: " << error.what() << std::endl;
123 return 1;
124 }
125 return 0;
126}