blob: 81a7ba6e69c589df742428537a5e67cc9ac5b12d [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>
22#include <QtQml/QQmlContext>
23
24#include "forwarder-status.hpp"
25#include "fib-status.hpp"
26#include "tray-menu.hpp"
27
28#include <ndn-cxx/face.hpp>
29#include <boost/thread.hpp>
30
31namespace ndn {
32
33class Ncc
34{
35public:
36 Ncc()
37 : m_isActive(true)
38 , m_forwarderModel(m_face)
39 , m_fibModel(m_face)
40 {
41 QQmlContext* context = m_engine.rootContext();
42
43 context->setContextProperty("forwarderModel", &m_forwarderModel);
44 context->setContextProperty("fibModel", &m_fibModel);
45 context->setContextProperty("trayModel", &m_tray);
46
47 m_engine.load((QUrl("qrc:/main.qml")));
48
49 m_nfdThread = boost::thread(&Ncc::nfdConnectionLoop, this);
50 }
51
52 void
53 nfdConnectionLoop()
54 {
55 try {
56#ifdef BOOST_THREAD_USES_CHRONO
57 time::seconds retryTimeout = time::seconds(5);
58#else
59 boost::posix_time::time_duration retryTimeout = boost::posix_time::seconds(1);
60#endif
61
62 while (m_isActive) {
63 try {
64 while (m_isActive) {
65 m_face.expressInterest(Interest("/localhost/nfd/status"),
66 bind(&Ncc::onStatusRetrieved, this),
67 bind(&Ncc::onStatusTimeout, this));
68 m_face.processEvents(time::milliseconds::zero(), true);
69 }
70 }
71 catch (const std::exception&) {
72 emit m_tray.nfdActivityUpdate(false);
73#ifdef BOOST_THREAD_USES_CHRONO
74 boost::this_thread::sleep_for(retryTimeout);
75#else
76 boost::this_thread::sleep(retryTimeout);
77#endif
78 }
79 }
80 }
81 catch (const boost::thread_interrupted&) {
82 // done
83 }
84 }
85
86 void
87 onStatusRetrieved()
88 {
89 emit m_tray.nfdActivityUpdate(true);
90 }
91
92 void
93 onStatusTimeout()
94 {
95 emit m_tray.nfdActivityUpdate(false);
96
97 std::cerr << "Should not really happen, most likely a serious problem" << std::endl;
98 m_face.expressInterest(Interest("/localhost/nfd/status"),
99 bind(&Ncc::onStatusRetrieved, this),
100 bind(&Ncc::onStatusTimeout, this));
101 }
102
103 void
104 stop()
105 {
106 m_isActive = false;
107 m_face.shutdown();
108 m_nfdThread.interrupt();
109 m_nfdThread.join();
110 }
111
112private:
113 volatile bool m_isActive;
114 boost::thread m_nfdThread;
115
116 Face m_face;
117
118 QQmlApplicationEngine m_engine;
119
120 ForwarderStatusModel m_forwarderModel;
121 FibStatusModel m_fibModel;
122 TrayMenu m_tray;
123};
124
125} // namespace ndn
126
127Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>)
128
129int
130main(int argc, char *argv[])
131{
132 qRegisterMetaType<ndn::shared_ptr<const ndn::Data> >();
133
134 QApplication app(argc, argv);
135
136 ndn::Ncc controlCenterGui;
137
138 QApplication::setQuitOnLastWindowClosed(false);
139 int retval = app.exec();
140
141 controlCenterGui.stop();
142
143 return retval;
144}