blob: 8ea5931000b85d317b569aee3fb7a7e14d001fa1 [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"
Qi Zhaoec8ddd22017-02-24 05:16:15 -080027#include "rib-status.hpp"
taylorchuc27dd482014-05-17 20:06:49 -070028#include "tray-menu.hpp"
29
30#include <ndn-cxx/face.hpp>
Qi Zhaodec77d92017-02-15 15:49:04 -080031#include <ndn-cxx/name.hpp>
Alexander Afanasyev4086d512014-07-11 15:56:33 -070032#include <ndn-cxx/util/scheduler.hpp>
Qi Zhaodec77d92017-02-15 15:49:04 -080033#include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
Qi Zhaoec8ddd22017-02-24 05:16:15 -080034#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Qi Zhao0e043e52016-12-05 18:27:09 -080035#include <ndn-cxx/mgmt/nfd/controller.hpp>
36#include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
37
taylorchuc27dd482014-05-17 20:06:49 -070038#include <boost/thread.hpp>
39
40namespace ndn {
41
42class Ncc
43{
44public:
45 Ncc()
46 : m_isActive(true)
Qi Zhaodec77d92017-02-15 15:49:04 -080047 , m_localhopFibEntry(Name("/localhop/nfd"))
Qi Zhao0e043e52016-12-05 18:27:09 -080048 , m_face(nullptr, m_keyChain)
49 , m_controller(m_face, m_keyChain)
Alexander Afanasyev4086d512014-07-11 15:56:33 -070050 , m_scheduler(m_face.getIoService())
Alexander Afanasyev81509f32016-03-21 17:02:38 -070051 , m_tray(m_engine.rootContext(), m_face)
taylorchuc27dd482014-05-17 20:06:49 -070052 {
53 QQmlContext* context = m_engine.rootContext();
54
Alexander Afanasyev4086d512014-07-11 15:56:33 -070055 context->setContextProperty("forwarderModel", &m_forwarderStatusModel);
taylorchuc27dd482014-05-17 20:06:49 -070056 context->setContextProperty("fibModel", &m_fibModel);
Qi Zhaoec8ddd22017-02-24 05:16:15 -080057 context->setContextProperty("ribModel", &m_ribModel);
taylorchuc27dd482014-05-17 20:06:49 -070058 context->setContextProperty("trayModel", &m_tray);
59
60 m_engine.load((QUrl("qrc:/main.qml")));
61
62 m_nfdThread = boost::thread(&Ncc::nfdConnectionLoop, this);
63 }
64
65 void
66 nfdConnectionLoop()
67 {
68 try {
69#ifdef BOOST_THREAD_USES_CHRONO
70 time::seconds retryTimeout = time::seconds(5);
71#else
72 boost::posix_time::time_duration retryTimeout = boost::posix_time::seconds(1);
73#endif
74
75 while (m_isActive) {
76 try {
77 while (m_isActive) {
Qi Zhaodec77d92017-02-15 15:49:04 -080078 requestNfdStatus();
taylorchuc27dd482014-05-17 20:06:49 -070079 m_face.processEvents(time::milliseconds::zero(), true);
80 }
81 }
Qi Zhaod3de12b2017-02-21 20:11:58 -080082 catch (const std::exception& e) {
taylorchuc27dd482014-05-17 20:06:49 -070083 emit m_tray.nfdActivityUpdate(false);
Alexander Afanasyev8e986f82016-03-21 14:19:15 -070084 m_face.shutdown();
taylorchuc27dd482014-05-17 20:06:49 -070085#ifdef BOOST_THREAD_USES_CHRONO
86 boost::this_thread::sleep_for(retryTimeout);
87#else
88 boost::this_thread::sleep(retryTimeout);
89#endif
90 }
91 }
92 }
93 catch (const boost::thread_interrupted&) {
94 // done
95 }
96 }
97
98 void
Qi Zhao0e043e52016-12-05 18:27:09 -080099 onStatusRetrieved(const nfd::ForwarderStatus& status)
taylorchuc27dd482014-05-17 20:06:49 -0700100 {
101 emit m_tray.nfdActivityUpdate(true);
Qi Zhao0e043e52016-12-05 18:27:09 -0800102 emit m_forwarderStatusModel.onDataReceived(status);
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700103
Qi Zhaodec77d92017-02-15 15:49:04 -0800104 m_controller.fetch<ndn::nfd::FibDataset>(bind(&Ncc::onFibStatusRetrieved, this, _1),
105 bind(&Ncc::onStatusTimeout, this));
106
Qi Zhaoec8ddd22017-02-24 05:16:15 -0800107 m_controller.fetch<ndn::nfd::RibDataset>(bind(&Ncc::onRibStatusRetrieved, this, _1),
108 bind(&Ncc::onStatusTimeout, this));
109
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700110 m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
taylorchuc27dd482014-05-17 20:06:49 -0700111 }
112
113 void
Qi Zhaodec77d92017-02-15 15:49:04 -0800114 onFibStatusRetrieved(const std::vector<nfd::FibEntry>& status)
115 {
116 bool isConnectedToHub = false;
117 for (auto const& fibEntry : status) {
118 if (fibEntry.getPrefix() == m_localhopFibEntry) {
119 isConnectedToHub = true;
120 }
121 }
122 emit m_tray.connectivityUpdate(isConnectedToHub);
Qi Zhaod3de12b2017-02-21 20:11:58 -0800123 emit m_fibModel.onDataReceived(status);
Qi Zhaodec77d92017-02-15 15:49:04 -0800124 }
125
126 void
Qi Zhaoec8ddd22017-02-24 05:16:15 -0800127 onRibStatusRetrieved(const std::vector<nfd::RibEntry>& status)
128 {
129 emit m_ribModel.onDataReceived(status);
130 }
131
132 void
taylorchuc27dd482014-05-17 20:06:49 -0700133 onStatusTimeout()
134 {
135 emit m_tray.nfdActivityUpdate(false);
136
137 std::cerr << "Should not really happen, most likely a serious problem" << std::endl;
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700138 m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
taylorchuc27dd482014-05-17 20:06:49 -0700139 }
140
141 void
142 stop()
143 {
144 m_isActive = false;
145 m_face.shutdown();
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700146 m_scheduler.cancelAllEvents();
taylorchuc27dd482014-05-17 20:06:49 -0700147 m_nfdThread.interrupt();
148 m_nfdThread.join();
149 }
150
151private:
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700152 void
153 requestNfdStatus()
154 {
Qi Zhao0e043e52016-12-05 18:27:09 -0800155 m_controller.fetch<ndn::nfd::ForwarderGeneralStatusDataset>(bind(&Ncc::onStatusRetrieved, this, _1),
Qi Zhaodec77d92017-02-15 15:49:04 -0800156 bind(&Ncc::onStatusTimeout, this));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700157 }
158
159private:
taylorchuc27dd482014-05-17 20:06:49 -0700160 volatile bool m_isActive;
161 boost::thread m_nfdThread;
Qi Zhaodec77d92017-02-15 15:49:04 -0800162 const Name m_localhopFibEntry;
taylorchuc27dd482014-05-17 20:06:49 -0700163
Qi Zhao0e043e52016-12-05 18:27:09 -0800164 KeyChain m_keyChain;
taylorchuc27dd482014-05-17 20:06:49 -0700165 Face m_face;
Qi Zhao0e043e52016-12-05 18:27:09 -0800166 nfd::Controller m_controller;
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700167 Scheduler m_scheduler;
taylorchuc27dd482014-05-17 20:06:49 -0700168
169 QQmlApplicationEngine m_engine;
170
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700171 ForwarderStatusModel m_forwarderStatusModel;
taylorchuc27dd482014-05-17 20:06:49 -0700172 FibStatusModel m_fibModel;
Qi Zhaoec8ddd22017-02-24 05:16:15 -0800173 RibStatusModel m_ribModel;
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800174 ncc::TrayMenu m_tray;
taylorchuc27dd482014-05-17 20:06:49 -0700175};
176
177} // namespace ndn
178
179Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>)
Qi Zhao0e043e52016-12-05 18:27:09 -0800180Q_DECLARE_METATYPE(ndn::nfd::ForwarderStatus)
Qi Zhaod3de12b2017-02-21 20:11:58 -0800181Q_DECLARE_METATYPE(std::vector<ndn::nfd::FibEntry>)
Qi Zhaoec8ddd22017-02-24 05:16:15 -0800182Q_DECLARE_METATYPE(std::vector<ndn::nfd::RibEntry>)
taylorchuc27dd482014-05-17 20:06:49 -0700183
184int
185main(int argc, char *argv[])
186{
Qi Zhao0e043e52016-12-05 18:27:09 -0800187 qRegisterMetaType<ndn::shared_ptr<const ndn::Data>>();
188 qRegisterMetaType<ndn::nfd::ForwarderStatus>();
Qi Zhaod3de12b2017-02-21 20:11:58 -0800189 qRegisterMetaType<std::vector<ndn::nfd::FibEntry>>();
Qi Zhaoec8ddd22017-02-24 05:16:15 -0800190 qRegisterMetaType<std::vector<ndn::nfd::RibEntry>>();
taylorchuc27dd482014-05-17 20:06:49 -0700191
Alexander Afanasyev4c37bfb2016-03-20 12:06:30 -0700192 QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
taylorchuc27dd482014-05-17 20:06:49 -0700193 QApplication app(argc, argv);
194
195 ndn::Ncc controlCenterGui;
196
197 QApplication::setQuitOnLastWindowClosed(false);
198 int retval = app.exec();
199
200 controlCenterGui.stop();
201
202 return retval;
203}