blob: eda7197d04460e87c67e0076d21f24217dbfb6cf [file] [log] [blame]
Yingdi Yuf3401182015-02-02 20:21:07 -08001/* -*- 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
16namespace chronochat {
17
18static const int CONNECTION_RETRY_TIMER = 2;
19
20NfdConnectionChecker::NfdConnectionChecker(QObject* parent)
21 :QThread(parent)
22{
23}
24
25void
26NfdConnectionChecker::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 Patil3d850902020-11-23 12:19:14 +053045 NULL,
Yingdi Yuf3401182015-02-02 20:21:07 -080046 [] (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
64void
65NfdConnectionChecker::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