blob: ad4dcf089ae24f7b1b389f10edb45a9d934060c7 [file] [log] [blame]
taylorchuc27dd482014-05-17 20:06:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014, Regents of the University of California,
4 *
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>
21#include <QtWidgets/QApplication>
Alexander Afanasyev4086d512014-07-11 15:56:33 -070022#include <QtWidgets/QPushButton>
taylorchuc27dd482014-05-17 20:06:49 -070023#include <QtQml/QQmlContext>
24
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>
taylorchuc27dd482014-05-17 20:06:49 -070031#include <boost/thread.hpp>
32
33namespace ndn {
34
35class Ncc
36{
37public:
38 Ncc()
39 : m_isActive(true)
Alexander Afanasyev4086d512014-07-11 15:56:33 -070040 , m_scheduler(m_face.getIoService())
taylorchuc27dd482014-05-17 20:06:49 -070041 , m_fibModel(m_face)
Alexander Afanasyev4086d512014-07-11 15:56:33 -070042 , m_tray(m_engine.rootContext())
taylorchuc27dd482014-05-17 20:06:49 -070043 {
44 QQmlContext* context = m_engine.rootContext();
45
Alexander Afanasyev4086d512014-07-11 15:56:33 -070046 context->setContextProperty("forwarderModel", &m_forwarderStatusModel);
taylorchuc27dd482014-05-17 20:06:49 -070047 context->setContextProperty("fibModel", &m_fibModel);
48 context->setContextProperty("trayModel", &m_tray);
49
50 m_engine.load((QUrl("qrc:/main.qml")));
51
52 m_nfdThread = boost::thread(&Ncc::nfdConnectionLoop, this);
53 }
54
55 void
56 nfdConnectionLoop()
57 {
58 try {
59#ifdef BOOST_THREAD_USES_CHRONO
60 time::seconds retryTimeout = time::seconds(5);
61#else
62 boost::posix_time::time_duration retryTimeout = boost::posix_time::seconds(1);
63#endif
64
65 while (m_isActive) {
66 try {
67 while (m_isActive) {
68 m_face.expressInterest(Interest("/localhost/nfd/status"),
Alexander Afanasyev4086d512014-07-11 15:56:33 -070069 bind(&Ncc::onStatusRetrieved, this, _2),
taylorchuc27dd482014-05-17 20:06:49 -070070 bind(&Ncc::onStatusTimeout, this));
71 m_face.processEvents(time::milliseconds::zero(), true);
72 }
73 }
Alexander Afanasyev4086d512014-07-11 15:56:33 -070074 catch (const std::exception&e) {
taylorchuc27dd482014-05-17 20:06:49 -070075 emit m_tray.nfdActivityUpdate(false);
76#ifdef BOOST_THREAD_USES_CHRONO
77 boost::this_thread::sleep_for(retryTimeout);
78#else
79 boost::this_thread::sleep(retryTimeout);
80#endif
81 }
82 }
83 }
84 catch (const boost::thread_interrupted&) {
85 // done
86 }
87 }
88
89 void
Alexander Afanasyev4086d512014-07-11 15:56:33 -070090 onStatusRetrieved(const Data& data)
taylorchuc27dd482014-05-17 20:06:49 -070091 {
92 emit m_tray.nfdActivityUpdate(true);
Alexander Afanasyev4086d512014-07-11 15:56:33 -070093 emit m_forwarderStatusModel.onDataReceived(data.shared_from_this());
94
95 m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
taylorchuc27dd482014-05-17 20:06:49 -070096 }
97
98 void
99 onStatusTimeout()
100 {
101 emit m_tray.nfdActivityUpdate(false);
102
103 std::cerr << "Should not really happen, most likely a serious problem" << std::endl;
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700104 m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
taylorchuc27dd482014-05-17 20:06:49 -0700105 }
106
107 void
108 stop()
109 {
110 m_isActive = false;
111 m_face.shutdown();
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700112 m_scheduler.cancelAllEvents();
taylorchuc27dd482014-05-17 20:06:49 -0700113 m_nfdThread.interrupt();
114 m_nfdThread.join();
115 }
116
117private:
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700118 void
119 requestNfdStatus()
120 {
121 Interest interest("/localhost/nfd/status");
122 interest.setMustBeFresh(true);
123 m_face.expressInterest(interest,
124 bind(&Ncc::onStatusRetrieved, this, _2),
125 bind(&Ncc::onStatusTimeout, this));
126 }
127
128private:
taylorchuc27dd482014-05-17 20:06:49 -0700129 volatile bool m_isActive;
130 boost::thread m_nfdThread;
131
132 Face m_face;
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700133 Scheduler m_scheduler;
taylorchuc27dd482014-05-17 20:06:49 -0700134
135 QQmlApplicationEngine m_engine;
136
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700137 ForwarderStatusModel m_forwarderStatusModel;
taylorchuc27dd482014-05-17 20:06:49 -0700138 FibStatusModel m_fibModel;
139 TrayMenu m_tray;
140};
141
142} // namespace ndn
143
144Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>)
145
146int
147main(int argc, char *argv[])
148{
149 qRegisterMetaType<ndn::shared_ptr<const ndn::Data> >();
150
Alexander Afanasyev4c37bfb2016-03-20 12:06:30 -0700151 QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
taylorchuc27dd482014-05-17 20:06:49 -0700152 QApplication app(argc, argv);
153
154 ndn::Ncc controlCenterGui;
155
156 QApplication::setQuitOnLastWindowClosed(false);
157 int retval = app.exec();
158
159 controlCenterGui.stop();
160
161 return retval;
162}