blob: 3f8d238e9f5e3e53c71a236b1186b0abad823276 [file] [log] [blame]
taylorchuc27dd482014-05-17 20:06:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Qi Zhao86f2b212016-12-06 12:44:16 -08003 * Copyright (c) 2013-2017, Regents of the University of California,
taylorchuc27dd482014-05-17 20:06:49 -07004 *
5 * This file is part of NFD Control Center. See AUTHORS.md for complete list of NFD
6 * authors and contributors.
7 *
8 * NFD Control Center is free software: you can redistribute it and/or modify it under the
9 * terms 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 Control Center is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14 * PARTICULAR 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 NFD
17 * Control Center, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <QtQml/QQmlApplicationEngine>
Qi Zhao86f2b212016-12-06 12:44:16 -080021#include <QtQml/QQmlContext>
taylorchuc27dd482014-05-17 20:06:49 -070022#include <QtWidgets/QApplication>
Alexander Afanasyev4086d512014-07-11 15:56:33 -070023#include <QtWidgets/QPushButton>
taylorchuc27dd482014-05-17 20:06:49 -070024
25#include "forwarder-status.hpp"
26#include "fib-status.hpp"
27#include "tray-menu.hpp"
28
29#include <ndn-cxx/face.hpp>
Qi Zhaodec77d92017-02-15 15:49:04 -080030#include <ndn-cxx/name.hpp>
Alexander Afanasyev4086d512014-07-11 15:56:33 -070031#include <ndn-cxx/util/scheduler.hpp>
Qi Zhaodec77d92017-02-15 15:49:04 -080032#include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
Qi Zhao0e043e52016-12-05 18:27:09 -080033#include <ndn-cxx/mgmt/nfd/controller.hpp>
34#include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
35
taylorchuc27dd482014-05-17 20:06:49 -070036#include <boost/thread.hpp>
37
38namespace ndn {
39
40class Ncc
41{
42public:
43 Ncc()
44 : m_isActive(true)
Qi Zhaodec77d92017-02-15 15:49:04 -080045 , m_localhopFibEntry(Name("/localhop/nfd"))
Qi Zhao0e043e52016-12-05 18:27:09 -080046 , m_face(nullptr, m_keyChain)
47 , m_controller(m_face, m_keyChain)
Alexander Afanasyev4086d512014-07-11 15:56:33 -070048 , m_scheduler(m_face.getIoService())
taylorchuc27dd482014-05-17 20:06:49 -070049 , m_fibModel(m_face)
Alexander Afanasyev81509f32016-03-21 17:02:38 -070050 , m_tray(m_engine.rootContext(), m_face)
taylorchuc27dd482014-05-17 20:06:49 -070051 {
52 QQmlContext* context = m_engine.rootContext();
53
Alexander Afanasyev4086d512014-07-11 15:56:33 -070054 context->setContextProperty("forwarderModel", &m_forwarderStatusModel);
taylorchuc27dd482014-05-17 20:06:49 -070055 context->setContextProperty("fibModel", &m_fibModel);
56 context->setContextProperty("trayModel", &m_tray);
57
58 m_engine.load((QUrl("qrc:/main.qml")));
59
60 m_nfdThread = boost::thread(&Ncc::nfdConnectionLoop, this);
61 }
62
63 void
64 nfdConnectionLoop()
65 {
66 try {
67#ifdef BOOST_THREAD_USES_CHRONO
68 time::seconds retryTimeout = time::seconds(5);
69#else
70 boost::posix_time::time_duration retryTimeout = boost::posix_time::seconds(1);
71#endif
72
73 while (m_isActive) {
74 try {
75 while (m_isActive) {
Qi Zhaodec77d92017-02-15 15:49:04 -080076 requestNfdStatus();
taylorchuc27dd482014-05-17 20:06:49 -070077 m_face.processEvents(time::milliseconds::zero(), true);
78 }
79 }
Alexander Afanasyev4086d512014-07-11 15:56:33 -070080 catch (const std::exception&e) {
taylorchuc27dd482014-05-17 20:06:49 -070081 emit m_tray.nfdActivityUpdate(false);
Alexander Afanasyev8e986f82016-03-21 14:19:15 -070082 m_face.shutdown();
taylorchuc27dd482014-05-17 20:06:49 -070083#ifdef BOOST_THREAD_USES_CHRONO
84 boost::this_thread::sleep_for(retryTimeout);
85#else
86 boost::this_thread::sleep(retryTimeout);
87#endif
88 }
89 }
90 }
91 catch (const boost::thread_interrupted&) {
92 // done
93 }
94 }
95
96 void
Qi Zhao0e043e52016-12-05 18:27:09 -080097 onStatusRetrieved(const nfd::ForwarderStatus& status)
taylorchuc27dd482014-05-17 20:06:49 -070098 {
99 emit m_tray.nfdActivityUpdate(true);
Qi Zhao0e043e52016-12-05 18:27:09 -0800100 emit m_forwarderStatusModel.onDataReceived(status);
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700101
Qi Zhaodec77d92017-02-15 15:49:04 -0800102 m_controller.fetch<ndn::nfd::FibDataset>(bind(&Ncc::onFibStatusRetrieved, this, _1),
103 bind(&Ncc::onStatusTimeout, this));
104
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700105 m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
taylorchuc27dd482014-05-17 20:06:49 -0700106 }
107
108 void
Qi Zhaodec77d92017-02-15 15:49:04 -0800109 onFibStatusRetrieved(const std::vector<nfd::FibEntry>& status)
110 {
111 bool isConnectedToHub = false;
112 for (auto const& fibEntry : status) {
113 if (fibEntry.getPrefix() == m_localhopFibEntry) {
114 isConnectedToHub = true;
115 }
116 }
117 emit m_tray.connectivityUpdate(isConnectedToHub);
118 }
119
120 void
taylorchuc27dd482014-05-17 20:06:49 -0700121 onStatusTimeout()
122 {
123 emit m_tray.nfdActivityUpdate(false);
124
125 std::cerr << "Should not really happen, most likely a serious problem" << std::endl;
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700126 m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
taylorchuc27dd482014-05-17 20:06:49 -0700127 }
128
129 void
130 stop()
131 {
132 m_isActive = false;
133 m_face.shutdown();
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700134 m_scheduler.cancelAllEvents();
taylorchuc27dd482014-05-17 20:06:49 -0700135 m_nfdThread.interrupt();
136 m_nfdThread.join();
137 }
138
139private:
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700140 void
141 requestNfdStatus()
142 {
Qi Zhao0e043e52016-12-05 18:27:09 -0800143 m_controller.fetch<ndn::nfd::ForwarderGeneralStatusDataset>(bind(&Ncc::onStatusRetrieved, this, _1),
Qi Zhaodec77d92017-02-15 15:49:04 -0800144 bind(&Ncc::onStatusTimeout, this));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700145 }
146
147private:
taylorchuc27dd482014-05-17 20:06:49 -0700148 volatile bool m_isActive;
149 boost::thread m_nfdThread;
Qi Zhaodec77d92017-02-15 15:49:04 -0800150 const Name m_localhopFibEntry;
taylorchuc27dd482014-05-17 20:06:49 -0700151
Qi Zhao0e043e52016-12-05 18:27:09 -0800152 KeyChain m_keyChain;
taylorchuc27dd482014-05-17 20:06:49 -0700153 Face m_face;
Qi Zhao0e043e52016-12-05 18:27:09 -0800154 nfd::Controller m_controller;
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700155 Scheduler m_scheduler;
taylorchuc27dd482014-05-17 20:06:49 -0700156
157 QQmlApplicationEngine m_engine;
158
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700159 ForwarderStatusModel m_forwarderStatusModel;
taylorchuc27dd482014-05-17 20:06:49 -0700160 FibStatusModel m_fibModel;
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800161 ncc::TrayMenu m_tray;
taylorchuc27dd482014-05-17 20:06:49 -0700162};
163
164} // namespace ndn
165
166Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>)
Qi Zhao0e043e52016-12-05 18:27:09 -0800167Q_DECLARE_METATYPE(ndn::nfd::ForwarderStatus)
taylorchuc27dd482014-05-17 20:06:49 -0700168
169int
170main(int argc, char *argv[])
171{
Qi Zhao0e043e52016-12-05 18:27:09 -0800172 qRegisterMetaType<ndn::shared_ptr<const ndn::Data>>();
173 qRegisterMetaType<ndn::nfd::ForwarderStatus>();
taylorchuc27dd482014-05-17 20:06:49 -0700174
Alexander Afanasyev4c37bfb2016-03-20 12:06:30 -0700175 QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
taylorchuc27dd482014-05-17 20:06:49 -0700176 QApplication app(argc, argv);
177
178 ndn::Ncc controlCenterGui;
179
180 QApplication::setQuitOnLastWindowClosed(false);
181 int retval = app.exec();
182
183 controlCenterGui.stop();
184
185 return retval;
186}