Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * |
| 5 | * BSD license, See the LICENSE file for more information |
| 6 | * |
| 7 | * Author: Qiuhan Ding <qiuhanding@cs.ucla.edu> |
| 8 | */ |
| 9 | |
| 10 | #include "nfd-connection-checker.hpp" |
| 11 | |
| 12 | #ifndef Q_MOC_RUN |
| 13 | |
| 14 | #endif |
| 15 | |
| 16 | namespace chronochat { |
| 17 | |
| 18 | static const int CONNECTION_RETRY_TIMER = 2; |
| 19 | |
| 20 | NfdConnectionChecker::NfdConnectionChecker(QObject* parent) |
| 21 | :QThread(parent) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | void |
| 26 | NfdConnectionChecker::run() |
| 27 | { |
| 28 | m_face = unique_ptr<ndn::Face>(new ndn::Face); |
| 29 | #ifdef BOOST_THREAD_USES_CHRONO |
| 30 | time::seconds reconnetTimer = time::seconds(CONNECTION_RETRY_TIMER); |
| 31 | #else |
| 32 | boost::posix_time::time_duration reconnectTimer; |
| 33 | reconnectTimer = boost::posix_time::seconds(CONNECTION_RETRY_TIMER); |
| 34 | #endif |
| 35 | do { |
| 36 | { |
| 37 | std::lock_guard<std::mutex>lock(m_nfdMutex); |
| 38 | m_nfdConnected = true; |
| 39 | } |
| 40 | try { |
| 41 | m_face->expressInterest(Interest("/localhost/nfd/status"), |
| 42 | [&] (const Interest& interest, const Data& data) { |
| 43 | m_face->shutdown(); |
| 44 | }, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 45 | NULL, |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 46 | [] (const Interest& interest) {}); |
| 47 | m_face->processEvents(time::milliseconds::zero(), true); |
| 48 | } |
| 49 | catch (std::runtime_error& e) { |
| 50 | { |
| 51 | std::lock_guard<std::mutex>lock(m_nfdMutex); |
| 52 | m_nfdConnected = false; |
| 53 | } |
| 54 | #ifdef BOOST_THREAD_USES_CHRONO |
| 55 | boost::this_thread::sleep_for(reconnetTimer); |
| 56 | #else |
| 57 | boost::this_thread::sleep(reconnectTimer); |
| 58 | #endif |
| 59 | } |
| 60 | } while (!m_nfdConnected); |
| 61 | emit nfdConnected(); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | NfdConnectionChecker::shutdown() |
| 66 | { |
| 67 | // In this case, we just stop checking the nfd connection and exit |
| 68 | std::lock_guard<std::mutex>lock(m_nfdMutex); |
| 69 | m_nfdConnected = true; |
| 70 | } |
| 71 | |
| 72 | } // namespace chronochat |
| 73 | |
| 74 | #if WAF |
| 75 | #include "nfd-connection-checker.moc" |
| 76 | // #include "nfd-connection-checker.cpp.moc" |
| 77 | #endif |