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