blob: d16049d043e7666e9196d5fa0c7c76a5fb2ff87e [file] [log] [blame]
Ivan Yeodb0052d2015-02-08 17:27:04 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2015 Regents of the University of California
4 *
5 * This file is part of NFD (Named Data Networking Forwarding Daemon) Android.
6 * See AUTHORS.md for complete list of NFD Android authors and contributors.
7 *
8 * NFD Android is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NFD Android is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NFD Android, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "nfd-wrapper.hpp"
Ivan Yeodb0052d2015-02-08 17:27:04 -080021
Alexander Afanasyev216df012015-02-10 17:35:46 -080022#include "daemon/nfd.hpp"
23#include "rib/nrd.hpp"
Ivan Yeodb0052d2015-02-08 17:27:04 -080024
Alexander Afanasyev216df012015-02-10 17:35:46 -080025#include "core/global-io.hpp"
26#include "core/config-file.hpp"
27#include "core/logger.hpp"
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080028#include "core/privilege-helper.hpp"
Alexander Afanasyev216df012015-02-10 17:35:46 -080029
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080030#include <stdlib.h>
Alexander Afanasyev216df012015-02-10 17:35:46 -080031#include <boost/property_tree/info_parser.hpp>
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080032#include <boost/thread.hpp>
33#include <mutex>
Alexander Afanasyev216df012015-02-10 17:35:46 -080034
35NFD_LOG_INIT("NfdWrapper");
36
37namespace nfd {
38
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080039
40// A little bit of cheating to make sure NFD can be properly restarted
41
42namespace scheduler {
43// defined in scheduler.cpp
44void
45resetGlobalScheduler();
46} // namespace scheduler
47
48void
49resetGlobalIoService();
50
51
Alexander Afanasyev216df012015-02-10 17:35:46 -080052class Runner
53{
54public:
55 Runner()
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080056 : m_io(nullptr)
Alexander Afanasyev216df012015-02-10 17:35:46 -080057 {
58 std::string initialConfig =
59 "general\n"
60 "{\n"
61 "}\n"
62 "\n"
63 "log\n"
64 "{\n"
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080065 " default_level ALL\n"
66 " NameTree INFO\n"
67 " BestRouteStrategy2 INFO\n"
68 " InternalFace INFO\n"
69 " Forwarder INFO\n"
70 " ContentStore INFO\n"
71 " DeadNonceList INFO\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -080072 "}\n"
73 "tables\n"
74 "{\n"
75 " cs_max_packets 100\n"
76 "\n"
77 " strategy_choice\n"
78 " {\n"
79 " / /localhost/nfd/strategy/best-route\n"
80 " /localhost /localhost/nfd/strategy/broadcast\n"
81 " /localhost/nfd /localhost/nfd/strategy/best-route\n"
82 " /ndn/broadcast /localhost/nfd/strategy/broadcast\n"
83 " }\n"
84 "}\n"
85 "\n"
86 "face_system\n"
87 "{\n"
88 " tcp\n"
89 " {\n"
90 " port 6363\n"
91 " }\n"
92 "\n"
93 " udp\n"
94 " {\n"
95 " port 6363\n"
96 " idle_timeout 600\n"
97 " keep_alive_interval 25\n"
98 " mcast no\n"
99 " }\n"
Alexander Afanasyevedf1e2b2015-04-19 19:31:17 -0700100 " websocket\n"
101 " {\n"
102 " listen yes\n"
103 " port 9696\n"
104 " enable_v4 yes\n"
105 " enable_v6 yes\n"
106 " }\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -0800107 "}\n"
108 "\n"
109 "authorizations\n"
110 "{\n"
111 " authorize\n"
112 " {\n"
113 " certfile any\n"
114 " privileges\n"
115 " {\n"
116 " faces\n"
117 " fib\n"
118 " strategy-choice\n"
119 " }\n"
120 " }\n"
121 "}\n"
122 "\n"
123 "rib\n"
124 "{\n"
125 " localhost_security\n"
126 " {\n"
127 " trust-anchor\n"
128 " {\n"
129 " type any\n"
130 " }\n"
131 " }\n"
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800132 "\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -0800133 " remote_register\n"
134 " {\n"
135 " cost 15\n"
136 " timeout 10000\n"
137 " retry 0\n"
138 " refresh_interval 300\n"
139 " }\n"
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800140 "}\n"
Alexander Afanasyev216df012015-02-10 17:35:46 -0800141 "\n";
142
143 std::istringstream input(initialConfig);
144 boost::property_tree::read_info(input, m_config);
145
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800146 std::unique_lock<std::mutex> lock(m_pointerMutex);
147 m_nfd.reset(new Nfd(m_config, m_keyChain));
148 m_nrd.reset(new rib::Nrd(m_config, m_keyChain));
Alexander Afanasyev216df012015-02-10 17:35:46 -0800149
150 m_nfd->initialize();
151 m_nrd->initialize();
152 }
153
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800154 ~Runner()
Alexander Afanasyev216df012015-02-10 17:35:46 -0800155 {
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800156 stop();
157 m_io->reset();
158 }
159
160 void
161 start()
162 {
163 {
164 std::unique_lock<std::mutex> lock(m_pointerMutex);
165 m_io = &getGlobalIoService();
166 }
167 m_io->run();
168 m_io->reset();
Alexander Afanasyev216df012015-02-10 17:35:46 -0800169 }
170
171 void
172 stop()
173 {
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800174 std::unique_lock<std::mutex> lock(m_pointerMutex);
175
176 m_io->post([this] {
177 m_io->stop();
178 this->m_nrd.reset();
179 this->m_nfd.reset();
180 });
Alexander Afanasyev216df012015-02-10 17:35:46 -0800181 }
182
183private:
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800184 std::mutex m_pointerMutex;
185 boost::asio::io_service* m_io;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800186 ndn::KeyChain m_keyChain;
187 unique_ptr<Nfd> m_nfd; // will use globalIoService
188 unique_ptr<rib::Nrd> m_nrd; // will use globalIoService
189
190 nfd::ConfigSection m_config;
191};
192
193static unique_ptr<Runner> g_runner;
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800194static boost::thread g_thread;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800195
196} // namespace nfd
Ivan Yeodb0052d2015-02-08 17:27:04 -0800197
198JNIEXPORT void JNICALL
Ivan Yeo6296dce2015-02-10 23:29:43 -0800199Java_net_named_1data_nfd_service_NfdService_startNfd(JNIEnv* env, jclass, jstring homePathJ)
Ivan Yeodb0052d2015-02-08 17:27:04 -0800200{
Alexander Afanasyev216df012015-02-10 17:35:46 -0800201 if (nfd::g_runner.get() == nullptr) {
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800202 // set/update HOME environment variable
203 const char* homePath = env->GetStringUTFChars(homePathJ, nullptr);
204 ::setenv("HOME", homePath, true);
205 env->ReleaseStringUTFChars(homePathJ, homePath);
206 NFD_LOG_INFO("Use [" << homePath << "] as a security storage");
Ivan Yeodb0052d2015-02-08 17:27:04 -0800207
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800208 nfd::g_thread = boost::thread([] {
209 NFD_LOG_INFO("Starting NFD...");
210 try {
211 nfd::g_runner.reset(new nfd::Runner());
212 nfd::g_runner->start();
213 }
214 catch (const std::exception& e) {
215 NFD_LOG_FATAL(e.what());
216 }
217 catch (const nfd::PrivilegeHelper::Error& e) {
218 NFD_LOG_FATAL("PrivilegeHelper: " << e.what());
219 }
220 catch (...) {
221 NFD_LOG_FATAL("Unknown fatal error");
222 }
Alexander Afanasyev216df012015-02-10 17:35:46 -0800223
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800224 nfd::g_runner.reset();
225 nfd::scheduler::resetGlobalScheduler();
226 nfd::resetGlobalIoService();
227 NFD_LOG_INFO("NFD stopped");
228 });
229 }
Ivan Yeodb0052d2015-02-08 17:27:04 -0800230}
231
232JNIEXPORT void JNICALL
Ivan Yeo6296dce2015-02-10 23:29:43 -0800233Java_net_named_1data_nfd_service_NfdService_stopNfd(JNIEnv*, jclass)
Ivan Yeodb0052d2015-02-08 17:27:04 -0800234{
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800235 if (nfd::g_runner.get() != nullptr) {
236 NFD_LOG_INFO("Stopping NFD...");
237 nfd::g_runner->stop();
238 // do not block anything
Alexander Afanasyev216df012015-02-10 17:35:46 -0800239 }
Ivan Yeodb0052d2015-02-08 17:27:04 -0800240}