blob: 9cfff3a3c9e22d1a98976de0a8747543ab721b78 [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 Zhao0e043e52016-12-05 18:27:09 -080031#include <ndn-cxx/mgmt/nfd/controller.hpp>
32#include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
33
taylorchuc27dd482014-05-17 20:06:49 -070034#include <boost/thread.hpp>
35
36namespace ndn {
37
38class Ncc
39{
40public:
41 Ncc()
42 : m_isActive(true)
Qi Zhaodec77d92017-02-15 15:49:04 -080043 , m_localhopFibEntry(Name("/localhop/nfd"))
Qi Zhao0e043e52016-12-05 18:27:09 -080044 , m_face(nullptr, m_keyChain)
45 , m_controller(m_face, m_keyChain)
Alexander Afanasyev4086d512014-07-11 15:56:33 -070046 , m_scheduler(m_face.getIoService())
Qi Zhao7e524492017-03-02 15:05:45 -080047 , m_tray(m_engine.rootContext(), m_face, m_keyChain)
taylorchuc27dd482014-05-17 20:06:49 -070048 {
49 QQmlContext* context = m_engine.rootContext();
50
taylorchuc27dd482014-05-17 20:06:49 -070051 context->setContextProperty("trayModel", &m_tray);
52
53 m_engine.load((QUrl("qrc:/main.qml")));
54
55 m_nfdThread = boost::thread(&Ncc::nfdConnectionLoop, this);
56 }
57
58 void
59 nfdConnectionLoop()
60 {
61 try {
62#ifdef BOOST_THREAD_USES_CHRONO
63 time::seconds retryTimeout = time::seconds(5);
64#else
65 boost::posix_time::time_duration retryTimeout = boost::posix_time::seconds(1);
66#endif
67
68 while (m_isActive) {
69 try {
70 while (m_isActive) {
Qi Zhaodec77d92017-02-15 15:49:04 -080071 requestNfdStatus();
taylorchuc27dd482014-05-17 20:06:49 -070072 m_face.processEvents(time::milliseconds::zero(), true);
73 }
74 }
Qi Zhaod3de12b2017-02-21 20:11:58 -080075 catch (const std::exception& e) {
taylorchuc27dd482014-05-17 20:06:49 -070076 emit m_tray.nfdActivityUpdate(false);
Alexander Afanasyev8e986f82016-03-21 14:19:15 -070077 m_face.shutdown();
taylorchuc27dd482014-05-17 20:06:49 -070078#ifdef BOOST_THREAD_USES_CHRONO
79 boost::this_thread::sleep_for(retryTimeout);
80#else
81 boost::this_thread::sleep(retryTimeout);
82#endif
83 }
84 }
85 }
86 catch (const boost::thread_interrupted&) {
87 // done
88 }
89 }
90
91 void
Qi Zhao0e043e52016-12-05 18:27:09 -080092 onStatusRetrieved(const nfd::ForwarderStatus& status)
taylorchuc27dd482014-05-17 20:06:49 -070093 {
94 emit m_tray.nfdActivityUpdate(true);
Alexander Afanasyev4086d512014-07-11 15:56:33 -070095
Qi Zhaodec77d92017-02-15 15:49:04 -080096 m_controller.fetch<ndn::nfd::FibDataset>(bind(&Ncc::onFibStatusRetrieved, this, _1),
97 bind(&Ncc::onStatusTimeout, this));
98
Alexander Afanasyev4086d512014-07-11 15:56:33 -070099 m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
taylorchuc27dd482014-05-17 20:06:49 -0700100 }
101
102 void
Qi Zhaodec77d92017-02-15 15:49:04 -0800103 onFibStatusRetrieved(const std::vector<nfd::FibEntry>& status)
104 {
105 bool isConnectedToHub = false;
106 for (auto const& fibEntry : status) {
107 if (fibEntry.getPrefix() == m_localhopFibEntry) {
108 isConnectedToHub = true;
109 }
110 }
111 emit m_tray.connectivityUpdate(isConnectedToHub);
Qi Zhaoec8ddd22017-02-24 05:16:15 -0800112 }
113
114 void
taylorchuc27dd482014-05-17 20:06:49 -0700115 onStatusTimeout()
116 {
117 emit m_tray.nfdActivityUpdate(false);
118
119 std::cerr << "Should not really happen, most likely a serious problem" << std::endl;
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700120 m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
taylorchuc27dd482014-05-17 20:06:49 -0700121 }
122
123 void
124 stop()
125 {
126 m_isActive = false;
127 m_face.shutdown();
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700128 m_scheduler.cancelAllEvents();
taylorchuc27dd482014-05-17 20:06:49 -0700129 m_nfdThread.interrupt();
130 m_nfdThread.join();
131 }
132
133private:
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700134 void
135 requestNfdStatus()
136 {
Qi Zhao0e043e52016-12-05 18:27:09 -0800137 m_controller.fetch<ndn::nfd::ForwarderGeneralStatusDataset>(bind(&Ncc::onStatusRetrieved, this, _1),
Qi Zhaodec77d92017-02-15 15:49:04 -0800138 bind(&Ncc::onStatusTimeout, this));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700139 }
140
141private:
taylorchuc27dd482014-05-17 20:06:49 -0700142 volatile bool m_isActive;
143 boost::thread m_nfdThread;
Qi Zhaodec77d92017-02-15 15:49:04 -0800144 const Name m_localhopFibEntry;
taylorchuc27dd482014-05-17 20:06:49 -0700145
Qi Zhao0e043e52016-12-05 18:27:09 -0800146 KeyChain m_keyChain;
taylorchuc27dd482014-05-17 20:06:49 -0700147 Face m_face;
Qi Zhao0e043e52016-12-05 18:27:09 -0800148 nfd::Controller m_controller;
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700149 Scheduler m_scheduler;
taylorchuc27dd482014-05-17 20:06:49 -0700150
151 QQmlApplicationEngine m_engine;
152
Alexander Afanasyevfda42a82017-02-01 18:03:39 -0800153 ncc::TrayMenu m_tray;
taylorchuc27dd482014-05-17 20:06:49 -0700154};
155
156} // namespace ndn
157
158Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>)
Qi Zhao0e043e52016-12-05 18:27:09 -0800159Q_DECLARE_METATYPE(ndn::nfd::ForwarderStatus)
Qi Zhaod3de12b2017-02-21 20:11:58 -0800160Q_DECLARE_METATYPE(std::vector<ndn::nfd::FibEntry>)
Qi Zhaoec8ddd22017-02-24 05:16:15 -0800161Q_DECLARE_METATYPE(std::vector<ndn::nfd::RibEntry>)
Qi Zhao078ac692017-03-02 16:28:51 -0800162Q_DECLARE_METATYPE(std::vector<ndn::nfd::FaceStatus>)
Qi Zhaobd19e072017-03-05 15:12:46 -0800163Q_DECLARE_METATYPE(std::vector<ndn::nfd::ChannelStatus>)
164Q_DECLARE_METATYPE(std::vector<ndn::nfd::StrategyChoice>)
taylorchuc27dd482014-05-17 20:06:49 -0700165
166int
167main(int argc, char *argv[])
168{
Qi Zhao0e043e52016-12-05 18:27:09 -0800169 qRegisterMetaType<ndn::shared_ptr<const ndn::Data>>();
170 qRegisterMetaType<ndn::nfd::ForwarderStatus>();
Qi Zhaod3de12b2017-02-21 20:11:58 -0800171 qRegisterMetaType<std::vector<ndn::nfd::FibEntry>>();
Qi Zhaoec8ddd22017-02-24 05:16:15 -0800172 qRegisterMetaType<std::vector<ndn::nfd::RibEntry>>();
Qi Zhao078ac692017-03-02 16:28:51 -0800173 qRegisterMetaType<std::vector<ndn::nfd::FaceStatus>>();
Qi Zhaobd19e072017-03-05 15:12:46 -0800174 qRegisterMetaType<std::vector<ndn::nfd::ChannelStatus>>();
175 qRegisterMetaType<std::vector<ndn::nfd::StrategyChoice>>();
taylorchuc27dd482014-05-17 20:06:49 -0700176
Alexander Afanasyev4c37bfb2016-03-20 12:06:30 -0700177 QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
taylorchuc27dd482014-05-17 20:06:49 -0700178 QApplication app(argc, argv);
179
180 ndn::Ncc controlCenterGui;
181
182 QApplication::setQuitOnLastWindowClosed(false);
183 int retval = app.exec();
184
185 controlCenterGui.stop();
186
187 return retval;
188}