taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 22 | #include <QtWidgets/QPushButton> |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 23 | #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 Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/util/scheduler.hpp> |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 31 | #include <boost/thread.hpp> |
| 32 | |
| 33 | namespace ndn { |
| 34 | |
| 35 | class Ncc |
| 36 | { |
| 37 | public: |
| 38 | Ncc() |
| 39 | : m_isActive(true) |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 40 | , m_scheduler(m_face.getIoService()) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 41 | , m_fibModel(m_face) |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 42 | , m_tray(m_engine.rootContext()) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 43 | { |
| 44 | QQmlContext* context = m_engine.rootContext(); |
| 45 | |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 46 | context->setContextProperty("forwarderModel", &m_forwarderStatusModel); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 47 | 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 Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 69 | bind(&Ncc::onStatusRetrieved, this, _2), |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 70 | bind(&Ncc::onStatusTimeout, this)); |
| 71 | m_face.processEvents(time::milliseconds::zero(), true); |
| 72 | } |
| 73 | } |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 74 | catch (const std::exception&e) { |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 75 | emit m_tray.nfdActivityUpdate(false); |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame^] | 76 | m_face.shutdown(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 77 | #ifdef BOOST_THREAD_USES_CHRONO |
| 78 | boost::this_thread::sleep_for(retryTimeout); |
| 79 | #else |
| 80 | boost::this_thread::sleep(retryTimeout); |
| 81 | #endif |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | catch (const boost::thread_interrupted&) { |
| 86 | // done |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 91 | onStatusRetrieved(const Data& data) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 92 | { |
| 93 | emit m_tray.nfdActivityUpdate(true); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 94 | emit m_forwarderStatusModel.onDataReceived(data.shared_from_this()); |
| 95 | |
| 96 | m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this)); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
| 100 | onStatusTimeout() |
| 101 | { |
| 102 | emit m_tray.nfdActivityUpdate(false); |
| 103 | |
| 104 | std::cerr << "Should not really happen, most likely a serious problem" << std::endl; |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 105 | m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this)); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
| 109 | stop() |
| 110 | { |
| 111 | m_isActive = false; |
| 112 | m_face.shutdown(); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 113 | m_scheduler.cancelAllEvents(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 114 | m_nfdThread.interrupt(); |
| 115 | m_nfdThread.join(); |
| 116 | } |
| 117 | |
| 118 | private: |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 119 | void |
| 120 | requestNfdStatus() |
| 121 | { |
| 122 | Interest interest("/localhost/nfd/status"); |
| 123 | interest.setMustBeFresh(true); |
| 124 | m_face.expressInterest(interest, |
| 125 | bind(&Ncc::onStatusRetrieved, this, _2), |
| 126 | bind(&Ncc::onStatusTimeout, this)); |
| 127 | } |
| 128 | |
| 129 | private: |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 130 | volatile bool m_isActive; |
| 131 | boost::thread m_nfdThread; |
| 132 | |
| 133 | Face m_face; |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 134 | Scheduler m_scheduler; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 135 | |
| 136 | QQmlApplicationEngine m_engine; |
| 137 | |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 138 | ForwarderStatusModel m_forwarderStatusModel; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 139 | FibStatusModel m_fibModel; |
| 140 | TrayMenu m_tray; |
| 141 | }; |
| 142 | |
| 143 | } // namespace ndn |
| 144 | |
| 145 | Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>) |
| 146 | |
| 147 | int |
| 148 | main(int argc, char *argv[]) |
| 149 | { |
| 150 | qRegisterMetaType<ndn::shared_ptr<const ndn::Data> >(); |
| 151 | |
Alexander Afanasyev | 4c37bfb | 2016-03-20 12:06:30 -0700 | [diff] [blame] | 152 | QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 153 | QApplication app(argc, argv); |
| 154 | |
| 155 | ndn::Ncc controlCenterGui; |
| 156 | |
| 157 | QApplication::setQuitOnLastWindowClosed(false); |
| 158 | int retval = app.exec(); |
| 159 | |
| 160 | controlCenterGui.stop(); |
| 161 | |
| 162 | return retval; |
| 163 | } |