blob: 918efceac2b823cfe59e111a9e9747450c6c7925 [file] [log] [blame]
Ivan Yeodb0052d2015-02-08 17:27:04 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -04003 * Copyright (c) 2015-2018 Regents of the University of California
Ivan Yeodb0052d2015-02-08 17:27:04 -08004 *
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"
Alexander Afanasyeva8d404b2016-11-05 10:07:08 -060023#include "rib/service.hpp"
Ivan Yeodb0052d2015-02-08 17:27:04 -080024
Alexander Afanasyev216df012015-02-10 17:35:46 -080025#include "core/config-file.hpp"
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040026#include "core/global-io.hpp"
Alexander Afanasyev216df012015-02-10 17:35:46 -080027#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
30#include <boost/property_tree/info_parser.hpp>
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080031#include <boost/thread.hpp>
32#include <mutex>
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040033#include <ndn-cxx/util/logging.hpp>
34#include <stdlib.h>
Alexander Afanasyev216df012015-02-10 17:35:46 -080035
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040036NFD_LOG_INIT(NfdWrapper);
Alexander Afanasyev216df012015-02-10 17:35:46 -080037
38namespace nfd {
39
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -080040
41// A little bit of cheating to make sure NFD can be properly restarted
42
43namespace scheduler {
44// defined in scheduler.cpp
45void
46resetGlobalScheduler();
47} // namespace scheduler
48
49void
50resetGlobalIoService();
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 {
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -040058 std::string initialConfig = R"CONF(
59 log
60 {
61 default_level ALL
62 }
63 tables
64 {
65 cs_max_packets 100
66 strategy_choice
67 {
68 / /localhost/nfd/strategy/best-route
69 /localhost /localhost/nfd/strategy/multicast
70 /localhost/nfd /localhost/nfd/strategy/best-route
71 /ndn/broadcast /localhost/nfd/strategy/multicast
72 /ndn/multicast /localhost/nfd/strategy/multicast
73 }
74 }
75 face_system
76 {
77 tcp
78 udp
79 {
80 mcast no
81 }
82 websocket
83 }
84 authorizations
85 {
86 authorize
87 {
88 certfile any
89 privileges
90 {
91 faces
92 fib
93 cs
94 strategy-choice
95 }
96 }
97 }
98 rib
99 {
100 localhost_security
101 {
102 trust-anchor
103 {
104 type any
105 }
106 }
107 auto_prefix_propagate
108 {
109 refresh_interval 300
110 }
111 }
112 )CONF";
Alexander Afanasyev216df012015-02-10 17:35:46 -0800113
114 std::istringstream input(initialConfig);
115 boost::property_tree::read_info(input, m_config);
116
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800117 std::unique_lock<std::mutex> lock(m_pointerMutex);
118 m_nfd.reset(new Nfd(m_config, m_keyChain));
Alexander Afanasyeva8d404b2016-11-05 10:07:08 -0600119 m_nrd.reset(new rib::Service(m_config, m_keyChain));
Alexander Afanasyev216df012015-02-10 17:35:46 -0800120
121 m_nfd->initialize();
122 m_nrd->initialize();
123 }
124
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800125 ~Runner()
Alexander Afanasyev216df012015-02-10 17:35:46 -0800126 {
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800127 stop();
128 m_io->reset();
129 }
130
131 void
132 start()
133 {
134 {
135 std::unique_lock<std::mutex> lock(m_pointerMutex);
136 m_io = &getGlobalIoService();
137 }
Alexander Afanasyevf7b62362015-09-10 23:29:47 -0700138
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800139 m_io->run();
140 m_io->reset();
Alexander Afanasyev216df012015-02-10 17:35:46 -0800141 }
142
143 void
144 stop()
145 {
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800146 std::unique_lock<std::mutex> lock(m_pointerMutex);
147
148 m_io->post([this] {
149 m_io->stop();
150 this->m_nrd.reset();
151 this->m_nfd.reset();
152 });
Alexander Afanasyev216df012015-02-10 17:35:46 -0800153 }
154
155private:
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800156 std::mutex m_pointerMutex;
157 boost::asio::io_service* m_io;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800158 ndn::KeyChain m_keyChain;
159 unique_ptr<Nfd> m_nfd; // will use globalIoService
Alexander Afanasyeva8d404b2016-11-05 10:07:08 -0600160 unique_ptr<rib::Service> m_nrd; // will use globalIoService
Alexander Afanasyev216df012015-02-10 17:35:46 -0800161
162 nfd::ConfigSection m_config;
163};
164
165static unique_ptr<Runner> g_runner;
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800166static boost::thread g_thread;
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700167static std::map<std::string, std::string> g_params;
Alexander Afanasyev216df012015-02-10 17:35:46 -0800168
169} // namespace nfd
Ivan Yeodb0052d2015-02-08 17:27:04 -0800170
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700171
172std::map<std::string, std::string>
173getParams(JNIEnv* env, jobject jParams)
174{
175 std::map<std::string, std::string> params;
176
177 jclass jcMap = env->GetObjectClass(jParams);
178 jclass jcSet = env->FindClass("java/util/Set");
179 jclass jcIterator = env->FindClass("java/util/Iterator");
180 jclass jcMapEntry = env->FindClass("java/util/Map$Entry");
181
182 jmethodID jcMapEntrySet = env->GetMethodID(jcMap, "entrySet", "()Ljava/util/Set;");
183 jmethodID jcSetIterator = env->GetMethodID(jcSet, "iterator", "()Ljava/util/Iterator;");
184 jmethodID jcIteratorHasNext = env->GetMethodID(jcIterator, "hasNext", "()Z");
185 jmethodID jcIteratorNext = env->GetMethodID(jcIterator, "next", "()Ljava/lang/Object;");
186 jmethodID jcMapEntryGetKey = env->GetMethodID(jcMapEntry, "getKey", "()Ljava/lang/Object;");
187 jmethodID jcMapEntryGetValue = env->GetMethodID(jcMapEntry, "getValue", "()Ljava/lang/Object;");
188
189 jobject jParamsEntrySet = env->CallObjectMethod(jParams, jcMapEntrySet);
190 jobject jParamsIterator = env->CallObjectMethod(jParamsEntrySet, jcSetIterator);
191 jboolean bHasNext = env->CallBooleanMethod(jParamsIterator, jcIteratorHasNext);
192 while (bHasNext) {
193 jobject entry = env->CallObjectMethod(jParamsIterator, jcIteratorNext);
194
195 jstring jKey = (jstring)env->CallObjectMethod(entry, jcMapEntryGetKey);
196 jstring jValue = (jstring)env->CallObjectMethod(entry, jcMapEntryGetValue);
197
198 const char* cKey = env->GetStringUTFChars(jKey, nullptr);
199 const char* cValue = env->GetStringUTFChars(jValue, nullptr);
200
201 params.insert(std::make_pair(cKey, cValue));
202
203 env->ReleaseStringUTFChars(jKey, cKey);
204 env->ReleaseStringUTFChars(jValue, cValue);
205
206 bHasNext = env->CallBooleanMethod(jParamsIterator, jcIteratorHasNext);
207 }
208
209 return params;
210}
211
212
Ivan Yeodb0052d2015-02-08 17:27:04 -0800213JNIEXPORT void JNICALL
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700214Java_net_named_1data_nfd_service_NfdService_startNfd(JNIEnv* env, jclass, jobject jParams)
Ivan Yeodb0052d2015-02-08 17:27:04 -0800215{
Alexander Afanasyev216df012015-02-10 17:35:46 -0800216 if (nfd::g_runner.get() == nullptr) {
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700217 nfd::g_params = getParams(env, jParams);
218
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800219 // set/update HOME environment variable
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700220 ::setenv("HOME", nfd::g_params["homePath"].c_str(), true);
221 NFD_LOG_INFO("Use [" << nfd::g_params["homePath"] << "] as a security storage");
Ivan Yeodb0052d2015-02-08 17:27:04 -0800222
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800223 nfd::g_thread = boost::thread([] {
Alexander Afanasyevf7b62362015-09-10 23:29:47 -0700224 nfd::scheduler::resetGlobalScheduler();
225 nfd::resetGlobalIoService();
226
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800227 NFD_LOG_INFO("Starting NFD...");
228 try {
229 nfd::g_runner.reset(new nfd::Runner());
230 nfd::g_runner->start();
231 }
232 catch (const std::exception& e) {
233 NFD_LOG_FATAL(e.what());
234 }
235 catch (const nfd::PrivilegeHelper::Error& e) {
236 NFD_LOG_FATAL("PrivilegeHelper: " << e.what());
237 }
238 catch (...) {
239 NFD_LOG_FATAL("Unknown fatal error");
240 }
Alexander Afanasyev216df012015-02-10 17:35:46 -0800241
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800242 nfd::g_runner.reset();
243 nfd::scheduler::resetGlobalScheduler();
244 nfd::resetGlobalIoService();
245 NFD_LOG_INFO("NFD stopped");
246 });
247 }
Ivan Yeodb0052d2015-02-08 17:27:04 -0800248}
249
250JNIEXPORT void JNICALL
Ivan Yeo6296dce2015-02-10 23:29:43 -0800251Java_net_named_1data_nfd_service_NfdService_stopNfd(JNIEnv*, jclass)
Ivan Yeodb0052d2015-02-08 17:27:04 -0800252{
Alexander Afanasyevc134b6f2015-02-12 17:01:44 -0800253 if (nfd::g_runner.get() != nullptr) {
254 NFD_LOG_INFO("Stopping NFD...");
255 nfd::g_runner->stop();
256 // do not block anything
Alexander Afanasyev216df012015-02-10 17:35:46 -0800257 }
Ivan Yeodb0052d2015-02-08 17:27:04 -0800258}
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700259
Haitao Zhang0c79a9c2016-11-21 00:38:51 -0800260JNIEXPORT jboolean JNICALL
261Java_net_named_1data_nfd_service_NfdService_isNfdRunning(JNIEnv*, jclass)
262{
263 return nfd::g_runner.get() != nullptr;
264}
265
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700266JNIEXPORT jobject JNICALL
267Java_net_named_1data_nfd_service_NfdService_getNfdLogModules(JNIEnv* env, jclass)
268{
269 jclass jcLinkedList = env->FindClass("java/util/LinkedList");
270 jmethodID jcLinkedListConstructor = env->GetMethodID(jcLinkedList, "<init>", "()V");
271 jmethodID jcLinkedListAdd = env->GetMethodID(jcLinkedList, "add", "(Ljava/lang/Object;)Z");
272
273 jobject jModules = env->NewObject(jcLinkedList, jcLinkedListConstructor);
274
Alexander Afanasyev14fd3e22018-07-04 14:20:53 -0400275 for (const auto& module : ndn::util::Logging::getLoggerNames()) {
Alexander Afanasyev45b7ad62015-04-19 22:00:45 -0700276 jstring jModule = env->NewStringUTF(module.c_str());
277 env->CallBooleanMethod(jModules, jcLinkedListAdd, jModule);
278 }
279
280 return jModules;
281}