blob: a3e125f5cf52fb66217265bec5db4d010662f613 [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
taylorchuc27dd482014-05-17 20:06:49 -070025#include "tray-menu.hpp"
26
27#include <ndn-cxx/face.hpp>
Qi Zhaodec77d92017-02-15 15:49:04 -080028#include <ndn-cxx/name.hpp>
Alexander Afanasyev4086d512014-07-11 15:56:33 -070029#include <ndn-cxx/util/scheduler.hpp>
Qi Zhaodec77d92017-02-15 15:49:04 -080030#include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
Qi Zhaoec8ddd22017-02-24 05:16:15 -080031#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Qi Zhao0e043e52016-12-05 18:27:09 -080032#include <ndn-cxx/mgmt/nfd/controller.hpp>
33#include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
34
taylorchuc27dd482014-05-17 20:06:49 -070035#include <boost/thread.hpp>
36
37namespace ndn {
38
39class Ncc
40{
41public:
42 Ncc()
43 : m_isActive(true)
Qi Zhaodec77d92017-02-15 15:49:04 -080044 , m_localhopFibEntry(Name("/localhop/nfd"))
Qi Zhao0e043e52016-12-05 18:27:09 -080045 , m_face(nullptr, m_keyChain)
46 , m_controller(m_face, m_keyChain)
Alexander Afanasyev4086d512014-07-11 15:56:33 -070047 , m_scheduler(m_face.getIoService())
Qi Zhao7e524492017-03-02 15:05:45 -080048 , m_tray(m_engine.rootContext(), m_face, m_keyChain)
taylorchuc27dd482014-05-17 20:06:49 -070049 {
50 QQmlContext* context = m_engine.rootContext();
51
taylorchuc27dd482014-05-17 20:06:49 -070052 context->setContextProperty("trayModel", &m_tray);
53
54 m_engine.load((QUrl("qrc:/main.qml")));
55
56 m_nfdThread = boost::thread(&Ncc::nfdConnectionLoop, this);
57 }
58
59 void
60 nfdConnectionLoop()
61 {
62 try {
63#ifdef BOOST_THREAD_USES_CHRONO
64 time::seconds retryTimeout = time::seconds(5);
65#else
66 boost::posix_time::time_duration retryTimeout = boost::posix_time::seconds(1);
67#endif
68
69 while (m_isActive) {
70 try {
71 while (m_isActive) {
Qi Zhaodec77d92017-02-15 15:49:04 -080072 requestNfdStatus();
taylorchuc27dd482014-05-17 20:06:49 -070073 m_face.processEvents(time::milliseconds::zero(), true);
74 }
75 }
Qi Zhaod3de12b2017-02-21 20:11:58 -080076 catch (const std::exception& e) {
taylorchuc27dd482014-05-17 20:06:49 -070077 emit m_tray.nfdActivityUpdate(false);
Alexander Afanasyev8e986f82016-03-21 14:19:15 -070078 m_face.shutdown();
taylorchuc27dd482014-05-17 20:06:49 -070079#ifdef BOOST_THREAD_USES_CHRONO
80 boost::this_thread::sleep_for(retryTimeout);
81#else
82 boost::this_thread::sleep(retryTimeout);
83#endif
84 }
85 }
86 }
87 catch (const boost::thread_interrupted&) {
88 // done
89 }
90 }
91
92 void
Qi Zhao0e043e52016-12-05 18:27:09 -080093 onStatusRetrieved(const nfd::ForwarderStatus& status)
taylorchuc27dd482014-05-17 20:06:49 -070094 {
95 emit m_tray.nfdActivityUpdate(true);
Alexander Afanasyev4086d512014-07-11 15:56:33 -070096
Qi Zhaodec77d92017-02-15 15:49:04 -080097 m_controller.fetch<ndn::nfd::FibDataset>(bind(&Ncc::onFibStatusRetrieved, this, _1),
98 bind(&Ncc::onStatusTimeout, this));
99
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700100 m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
taylorchuc27dd482014-05-17 20:06:49 -0700101 }
102
103 void
Qi Zhaodec77d92017-02-15 15:49:04 -0800104 onFibStatusRetrieved(const std::vector<nfd::FibEntry>& status)
105 {
106 bool isConnectedToHub = false;
107 for (auto const& fibEntry : status) {
108 if (fibEntry.getPrefix() == m_localhopFibEntry) {
109 isConnectedToHub = true;
110 }
111 }
112 emit m_tray.connectivityUpdate(isConnectedToHub);
Qi Zhaoec8ddd22017-02-24 05:16:15 -0800113 }
114
115 void
taylorchuc27dd482014-05-17 20:06:49 -0700116 onStatusTimeout()
117 {
118 emit m_tray.nfdActivityUpdate(false);
119
120 std::cerr << "Should not really happen, most likely a serious problem" << std::endl;
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700121 m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
taylorchuc27dd482014-05-17 20:06:49 -0700122 }
123
124 void
125 stop()
126 {
127 m_isActive = false;
128 m_face.shutdown();
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700129 m_scheduler.cancelAllEvents();
taylorchuc27dd482014-05-17 20:06:49 -0700130 m_nfdThread.interrupt();
131 m_nfdThread.join();
132 }
133
134private:
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700135 void
136 requestNfdStatus()
137 {
Qi Zhao0e043e52016-12-05 18:27:09 -0800138 m_controller.fetch<ndn::nfd::ForwarderGeneralStatusDataset>(bind(&Ncc::onStatusRetrieved, this, _1),
Qi Zhaodec77d92017-02-15 15:49:04 -0800139 bind(&Ncc::onStatusTimeout, this));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700140 }
141
142private:
taylorchuc27dd482014-05-17 20:06:49 -0700143 volatile bool m_isActive;
144 boost::thread m_nfdThread;
Qi Zhaodec77d92017-02-15 15:49:04 -0800145 const Name m_localhopFibEntry;
taylorchuc27dd482014-05-17 20:06:49 -0700146
Qi Zhao0e043e52016-12-05 18:27:09 -0800147 KeyChain m_keyChain;
taylorchuc27dd482014-05-17 20:06:49 -0700148 Face m_face;
Qi Zhao0e043e52016-12-05 18:27:09 -0800149 nfd::Controller m_controller;
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700150 Scheduler m_scheduler;
taylorchuc27dd482014-05-17 20:06:49 -0700151
152 QQmlApplicationEngine m_engine;
153
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800154 ncc::TrayMenu m_tray;
taylorchuc27dd482014-05-17 20:06:49 -0700155};
156
157} // namespace ndn
158
159Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>)
Qi Zhao0e043e52016-12-05 18:27:09 -0800160Q_DECLARE_METATYPE(ndn::nfd::ForwarderStatus)
Qi Zhaod3de12b2017-02-21 20:11:58 -0800161Q_DECLARE_METATYPE(std::vector<ndn::nfd::FibEntry>)
Qi Zhaoec8ddd22017-02-24 05:16:15 -0800162Q_DECLARE_METATYPE(std::vector<ndn::nfd::RibEntry>)
taylorchuc27dd482014-05-17 20:06:49 -0700163
164int
165main(int argc, char *argv[])
166{
Qi Zhao0e043e52016-12-05 18:27:09 -0800167 qRegisterMetaType<ndn::shared_ptr<const ndn::Data>>();
168 qRegisterMetaType<ndn::nfd::ForwarderStatus>();
Qi Zhaod3de12b2017-02-21 20:11:58 -0800169 qRegisterMetaType<std::vector<ndn::nfd::FibEntry>>();
Qi Zhaoec8ddd22017-02-24 05:16:15 -0800170 qRegisterMetaType<std::vector<ndn::nfd::RibEntry>>();
taylorchuc27dd482014-05-17 20:06:49 -0700171
Alexander Afanasyev4c37bfb2016-03-20 12:06:30 -0700172 QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
taylorchuc27dd482014-05-17 20:06:49 -0700173 QApplication app(argc, argv);
174
175 ndn::Ncc controlCenterGui;
176
177 QApplication::setQuitOnLastWindowClosed(false);
178 int retval = app.exec();
179
180 controlCenterGui.stop();
181
182 return retval;
183}