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