blob: d03f0cdeccd7345ed618b026bf29837db9134e11 [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>
Alexander Afanasyev4086d512014-07-11 15:56:33 -070030#include <ndn-cxx/util/scheduler.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 Zhao0e043e52016-12-05 18:27:09 -080043 , m_face(nullptr, m_keyChain)
44 , m_controller(m_face, m_keyChain)
Alexander Afanasyev4086d512014-07-11 15:56:33 -070045 , m_scheduler(m_face.getIoService())
taylorchuc27dd482014-05-17 20:06:49 -070046 , m_fibModel(m_face)
Alexander Afanasyev81509f32016-03-21 17:02:38 -070047 , m_tray(m_engine.rootContext(), m_face)
taylorchuc27dd482014-05-17 20:06:49 -070048 {
49 QQmlContext* context = m_engine.rootContext();
50
Alexander Afanasyev4086d512014-07-11 15:56:33 -070051 context->setContextProperty("forwarderModel", &m_forwarderStatusModel);
taylorchuc27dd482014-05-17 20:06:49 -070052 context->setContextProperty("fibModel", &m_fibModel);
53 context->setContextProperty("trayModel", &m_tray);
54
55 m_engine.load((QUrl("qrc:/main.qml")));
56
57 m_nfdThread = boost::thread(&Ncc::nfdConnectionLoop, this);
58 }
59
60 void
61 nfdConnectionLoop()
62 {
63 try {
64#ifdef BOOST_THREAD_USES_CHRONO
65 time::seconds retryTimeout = time::seconds(5);
66#else
67 boost::posix_time::time_duration retryTimeout = boost::posix_time::seconds(1);
68#endif
69
70 while (m_isActive) {
71 try {
72 while (m_isActive) {
Qi Zhao0e043e52016-12-05 18:27:09 -080073 m_controller.fetch<ndn::nfd::ForwarderGeneralStatusDataset>(bind(&Ncc::onStatusRetrieved, this, _1),
74 bind(&Ncc::onStatusTimeout, this));
taylorchuc27dd482014-05-17 20:06:49 -070075 m_face.processEvents(time::milliseconds::zero(), true);
76 }
77 }
Alexander Afanasyev4086d512014-07-11 15:56:33 -070078 catch (const std::exception&e) {
taylorchuc27dd482014-05-17 20:06:49 -070079 emit m_tray.nfdActivityUpdate(false);
Alexander Afanasyev8e986f82016-03-21 14:19:15 -070080 m_face.shutdown();
taylorchuc27dd482014-05-17 20:06:49 -070081#ifdef BOOST_THREAD_USES_CHRONO
82 boost::this_thread::sleep_for(retryTimeout);
83#else
84 boost::this_thread::sleep(retryTimeout);
85#endif
86 }
87 }
88 }
89 catch (const boost::thread_interrupted&) {
90 // done
91 }
92 }
93
94 void
Qi Zhao0e043e52016-12-05 18:27:09 -080095 onStatusRetrieved(const nfd::ForwarderStatus& status)
taylorchuc27dd482014-05-17 20:06:49 -070096 {
97 emit m_tray.nfdActivityUpdate(true);
Qi Zhao0e043e52016-12-05 18:27:09 -080098 emit m_forwarderStatusModel.onDataReceived(status);
Alexander Afanasyev4086d512014-07-11 15:56:33 -070099
100 m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
taylorchuc27dd482014-05-17 20:06:49 -0700101 }
102
103 void
104 onStatusTimeout()
105 {
106 emit m_tray.nfdActivityUpdate(false);
107
108 std::cerr << "Should not really happen, most likely a serious problem" << std::endl;
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700109 m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
taylorchuc27dd482014-05-17 20:06:49 -0700110 }
111
112 void
113 stop()
114 {
115 m_isActive = false;
116 m_face.shutdown();
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700117 m_scheduler.cancelAllEvents();
taylorchuc27dd482014-05-17 20:06:49 -0700118 m_nfdThread.interrupt();
119 m_nfdThread.join();
120 }
121
122private:
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700123 void
124 requestNfdStatus()
125 {
Qi Zhao0e043e52016-12-05 18:27:09 -0800126 m_controller.fetch<ndn::nfd::ForwarderGeneralStatusDataset>(bind(&Ncc::onStatusRetrieved, this, _1),
127 bind(&Ncc::onStatusTimeout, this));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700128 }
129
130private:
taylorchuc27dd482014-05-17 20:06:49 -0700131 volatile bool m_isActive;
132 boost::thread m_nfdThread;
133
Qi Zhao0e043e52016-12-05 18:27:09 -0800134 KeyChain m_keyChain;
taylorchuc27dd482014-05-17 20:06:49 -0700135 Face m_face;
Qi Zhao0e043e52016-12-05 18:27:09 -0800136 nfd::Controller m_controller;
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700137 Scheduler m_scheduler;
taylorchuc27dd482014-05-17 20:06:49 -0700138
139 QQmlApplicationEngine m_engine;
140
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700141 ForwarderStatusModel m_forwarderStatusModel;
taylorchuc27dd482014-05-17 20:06:49 -0700142 FibStatusModel m_fibModel;
143 TrayMenu m_tray;
144};
145
146} // namespace ndn
147
148Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>)
Qi Zhao0e043e52016-12-05 18:27:09 -0800149Q_DECLARE_METATYPE(ndn::nfd::ForwarderStatus)
taylorchuc27dd482014-05-17 20:06:49 -0700150
151int
152main(int argc, char *argv[])
153{
Qi Zhao0e043e52016-12-05 18:27:09 -0800154 qRegisterMetaType<ndn::shared_ptr<const ndn::Data>>();
155 qRegisterMetaType<ndn::nfd::ForwarderStatus>();
taylorchuc27dd482014-05-17 20:06:49 -0700156
Alexander Afanasyev4c37bfb2016-03-20 12:06:30 -0700157 QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
taylorchuc27dd482014-05-17 20:06:49 -0700158 QApplication app(argc, argv);
159
160 ndn::Ncc controlCenterGui;
161
162 QApplication::setQuitOnLastWindowClosed(false);
163 int retval = app.exec();
164
165 controlCenterGui.stop();
166
167 return retval;
168}