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