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); |
| 76 | #ifdef BOOST_THREAD_USES_CHRONO |
| 77 | boost::this_thread::sleep_for(retryTimeout); |
| 78 | #else |
| 79 | boost::this_thread::sleep(retryTimeout); |
| 80 | #endif |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | catch (const boost::thread_interrupted&) { |
| 85 | // done |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 90 | onStatusRetrieved(const Data& data) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 91 | { |
| 92 | emit m_tray.nfdActivityUpdate(true); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 93 | emit m_forwarderStatusModel.onDataReceived(data.shared_from_this()); |
| 94 | |
| 95 | m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this)); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
| 99 | onStatusTimeout() |
| 100 | { |
| 101 | emit m_tray.nfdActivityUpdate(false); |
| 102 | |
| 103 | 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] | 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 |
| 108 | stop() |
| 109 | { |
| 110 | m_isActive = false; |
| 111 | m_face.shutdown(); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 112 | m_scheduler.cancelAllEvents(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 113 | m_nfdThread.interrupt(); |
| 114 | m_nfdThread.join(); |
| 115 | } |
| 116 | |
| 117 | private: |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 118 | void |
| 119 | requestNfdStatus() |
| 120 | { |
| 121 | Interest interest("/localhost/nfd/status"); |
| 122 | interest.setMustBeFresh(true); |
| 123 | m_face.expressInterest(interest, |
| 124 | bind(&Ncc::onStatusRetrieved, this, _2), |
| 125 | bind(&Ncc::onStatusTimeout, this)); |
| 126 | } |
| 127 | |
| 128 | private: |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 129 | volatile bool m_isActive; |
| 130 | boost::thread m_nfdThread; |
| 131 | |
| 132 | Face m_face; |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 133 | Scheduler m_scheduler; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 134 | |
| 135 | QQmlApplicationEngine m_engine; |
| 136 | |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 137 | ForwarderStatusModel m_forwarderStatusModel; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 138 | FibStatusModel m_fibModel; |
| 139 | TrayMenu m_tray; |
| 140 | }; |
| 141 | |
| 142 | } // namespace ndn |
| 143 | |
| 144 | Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>) |
| 145 | |
| 146 | int |
| 147 | main(int argc, char *argv[]) |
| 148 | { |
| 149 | qRegisterMetaType<ndn::shared_ptr<const ndn::Data> >(); |
| 150 | |
| 151 | QApplication app(argc, argv); |
| 152 | |
| 153 | ndn::Ncc controlCenterGui; |
| 154 | |
| 155 | QApplication::setQuitOnLastWindowClosed(false); |
| 156 | int retval = app.exec(); |
| 157 | |
| 158 | controlCenterGui.stop(); |
| 159 | |
| 160 | return retval; |
| 161 | } |