taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Qi Zhao | 86f2b21 | 2016-12-06 12:44:16 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California, |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 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> |
Qi Zhao | 86f2b21 | 2016-12-06 12:44:16 -0800 | [diff] [blame] | 21 | #include <QtQml/QQmlContext> |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 22 | #include <QtWidgets/QApplication> |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 23 | #include <QtWidgets/QPushButton> |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 24 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 25 | #include "tray-menu.hpp" |
| 26 | |
| 27 | #include <ndn-cxx/face.hpp> |
Qi Zhao | dec77d9 | 2017-02-15 15:49:04 -0800 | [diff] [blame] | 28 | #include <ndn-cxx/name.hpp> |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/util/scheduler.hpp> |
Qi Zhao | dec77d9 | 2017-02-15 15:49:04 -0800 | [diff] [blame] | 30 | #include <ndn-cxx/mgmt/nfd/fib-entry.hpp> |
Qi Zhao | ec8ddd2 | 2017-02-24 05:16:15 -0800 | [diff] [blame] | 31 | #include <ndn-cxx/mgmt/nfd/rib-entry.hpp> |
Qi Zhao | 0e043e5 | 2016-12-05 18:27:09 -0800 | [diff] [blame] | 32 | #include <ndn-cxx/mgmt/nfd/controller.hpp> |
| 33 | #include <ndn-cxx/mgmt/nfd/status-dataset.hpp> |
| 34 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 35 | #include <boost/thread.hpp> |
| 36 | |
| 37 | namespace ndn { |
| 38 | |
| 39 | class Ncc |
| 40 | { |
| 41 | public: |
| 42 | Ncc() |
| 43 | : m_isActive(true) |
Qi Zhao | dec77d9 | 2017-02-15 15:49:04 -0800 | [diff] [blame] | 44 | , m_localhopFibEntry(Name("/localhop/nfd")) |
Qi Zhao | 0e043e5 | 2016-12-05 18:27:09 -0800 | [diff] [blame] | 45 | , m_face(nullptr, m_keyChain) |
| 46 | , m_controller(m_face, m_keyChain) |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 47 | , m_scheduler(m_face.getIoService()) |
Qi Zhao | 7e52449 | 2017-03-02 15:05:45 -0800 | [diff] [blame^] | 48 | , m_tray(m_engine.rootContext(), m_face, m_keyChain) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 49 | { |
| 50 | QQmlContext* context = m_engine.rootContext(); |
| 51 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 52 | context->setContextProperty("trayModel", &m_tray); |
| 53 | |
| 54 | m_engine.load((QUrl("qrc:/main.qml"))); |
| 55 | |
| 56 | m_nfdThread = boost::thread(&Ncc::nfdConnectionLoop, this); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | nfdConnectionLoop() |
| 61 | { |
| 62 | try { |
| 63 | #ifdef BOOST_THREAD_USES_CHRONO |
| 64 | time::seconds retryTimeout = time::seconds(5); |
| 65 | #else |
| 66 | boost::posix_time::time_duration retryTimeout = boost::posix_time::seconds(1); |
| 67 | #endif |
| 68 | |
| 69 | while (m_isActive) { |
| 70 | try { |
| 71 | while (m_isActive) { |
Qi Zhao | dec77d9 | 2017-02-15 15:49:04 -0800 | [diff] [blame] | 72 | requestNfdStatus(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 73 | m_face.processEvents(time::milliseconds::zero(), true); |
| 74 | } |
| 75 | } |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 76 | catch (const std::exception& e) { |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 77 | emit m_tray.nfdActivityUpdate(false); |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 78 | m_face.shutdown(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 79 | #ifdef BOOST_THREAD_USES_CHRONO |
| 80 | boost::this_thread::sleep_for(retryTimeout); |
| 81 | #else |
| 82 | boost::this_thread::sleep(retryTimeout); |
| 83 | #endif |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | catch (const boost::thread_interrupted&) { |
| 88 | // done |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void |
Qi Zhao | 0e043e5 | 2016-12-05 18:27:09 -0800 | [diff] [blame] | 93 | onStatusRetrieved(const nfd::ForwarderStatus& status) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 94 | { |
| 95 | emit m_tray.nfdActivityUpdate(true); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 96 | |
Qi Zhao | dec77d9 | 2017-02-15 15:49:04 -0800 | [diff] [blame] | 97 | m_controller.fetch<ndn::nfd::FibDataset>(bind(&Ncc::onFibStatusRetrieved, this, _1), |
| 98 | bind(&Ncc::onStatusTimeout, this)); |
| 99 | |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 100 | m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this)); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
Qi Zhao | dec77d9 | 2017-02-15 15:49:04 -0800 | [diff] [blame] | 104 | onFibStatusRetrieved(const std::vector<nfd::FibEntry>& status) |
| 105 | { |
| 106 | bool isConnectedToHub = false; |
| 107 | for (auto const& fibEntry : status) { |
| 108 | if (fibEntry.getPrefix() == m_localhopFibEntry) { |
| 109 | isConnectedToHub = true; |
| 110 | } |
| 111 | } |
| 112 | emit m_tray.connectivityUpdate(isConnectedToHub); |
Qi Zhao | ec8ddd2 | 2017-02-24 05:16:15 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 116 | onStatusTimeout() |
| 117 | { |
| 118 | emit m_tray.nfdActivityUpdate(false); |
| 119 | |
| 120 | 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] | 121 | m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this)); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void |
| 125 | stop() |
| 126 | { |
| 127 | m_isActive = false; |
| 128 | m_face.shutdown(); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 129 | m_scheduler.cancelAllEvents(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 130 | m_nfdThread.interrupt(); |
| 131 | m_nfdThread.join(); |
| 132 | } |
| 133 | |
| 134 | private: |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 135 | void |
| 136 | requestNfdStatus() |
| 137 | { |
Qi Zhao | 0e043e5 | 2016-12-05 18:27:09 -0800 | [diff] [blame] | 138 | m_controller.fetch<ndn::nfd::ForwarderGeneralStatusDataset>(bind(&Ncc::onStatusRetrieved, this, _1), |
Qi Zhao | dec77d9 | 2017-02-15 15:49:04 -0800 | [diff] [blame] | 139 | bind(&Ncc::onStatusTimeout, this)); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | private: |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 143 | volatile bool m_isActive; |
| 144 | boost::thread m_nfdThread; |
Qi Zhao | dec77d9 | 2017-02-15 15:49:04 -0800 | [diff] [blame] | 145 | const Name m_localhopFibEntry; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 146 | |
Qi Zhao | 0e043e5 | 2016-12-05 18:27:09 -0800 | [diff] [blame] | 147 | KeyChain m_keyChain; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 148 | Face m_face; |
Qi Zhao | 0e043e5 | 2016-12-05 18:27:09 -0800 | [diff] [blame] | 149 | nfd::Controller m_controller; |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 150 | Scheduler m_scheduler; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 151 | |
| 152 | QQmlApplicationEngine m_engine; |
| 153 | |
Alexander Afanasyev | fda42a8 | 2017-02-01 18:03:39 -0800 | [diff] [blame] | 154 | ncc::TrayMenu m_tray; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | } // namespace ndn |
| 158 | |
| 159 | Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>) |
Qi Zhao | 0e043e5 | 2016-12-05 18:27:09 -0800 | [diff] [blame] | 160 | Q_DECLARE_METATYPE(ndn::nfd::ForwarderStatus) |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 161 | Q_DECLARE_METATYPE(std::vector<ndn::nfd::FibEntry>) |
Qi Zhao | ec8ddd2 | 2017-02-24 05:16:15 -0800 | [diff] [blame] | 162 | Q_DECLARE_METATYPE(std::vector<ndn::nfd::RibEntry>) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 163 | |
| 164 | int |
| 165 | main(int argc, char *argv[]) |
| 166 | { |
Qi Zhao | 0e043e5 | 2016-12-05 18:27:09 -0800 | [diff] [blame] | 167 | qRegisterMetaType<ndn::shared_ptr<const ndn::Data>>(); |
| 168 | qRegisterMetaType<ndn::nfd::ForwarderStatus>(); |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 169 | qRegisterMetaType<std::vector<ndn::nfd::FibEntry>>(); |
Qi Zhao | ec8ddd2 | 2017-02-24 05:16:15 -0800 | [diff] [blame] | 170 | qRegisterMetaType<std::vector<ndn::nfd::RibEntry>>(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 171 | |
Alexander Afanasyev | 4c37bfb | 2016-03-20 12:06:30 -0700 | [diff] [blame] | 172 | QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 173 | QApplication app(argc, argv); |
| 174 | |
| 175 | ndn::Ncc controlCenterGui; |
| 176 | |
| 177 | QApplication::setQuitOnLastWindowClosed(false); |
| 178 | int retval = app.exec(); |
| 179 | |
| 180 | controlCenterGui.stop(); |
| 181 | |
| 182 | return retval; |
| 183 | } |