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 | }, |
| 45 | [] (const Interest& interest) {}); |
| 46 | m_face->processEvents(time::milliseconds::zero(), true); |
| 47 | } |
| 48 | catch (std::runtime_error& e) { |
| 49 | { |
| 50 | std::lock_guard<std::mutex>lock(m_nfdMutex); |
| 51 | m_nfdConnected = false; |
| 52 | } |
| 53 | #ifdef BOOST_THREAD_USES_CHRONO |
| 54 | boost::this_thread::sleep_for(reconnetTimer); |
| 55 | #else |
| 56 | boost::this_thread::sleep(reconnectTimer); |
| 57 | #endif |
| 58 | } |
| 59 | } while (!m_nfdConnected); |
| 60 | emit nfdConnected(); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | NfdConnectionChecker::shutdown() |
| 65 | { |
| 66 | // In this case, we just stop checking the nfd connection and exit |
| 67 | std::lock_guard<std::mutex>lock(m_nfdMutex); |
| 68 | m_nfdConnected = true; |
| 69 | } |
| 70 | |
| 71 | } // namespace chronochat |
| 72 | |
| 73 | #if WAF |
| 74 | #include "nfd-connection-checker.moc" |
| 75 | // #include "nfd-connection-checker.cpp.moc" |
| 76 | #endif |