blob: e349b927da817bf3c9febb60af55faf14684621e [file] [log] [blame]
Yingdi Yuf3401182015-02-02 20:21:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Yingdi Yuf3401182015-02-02 20:21:07 -08004 *
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
Yingdi Yuf3401182015-02-02 20:21:07 -080012namespace chronochat {
13
14static const int CONNECTION_RETRY_TIMER = 2;
15
16NfdConnectionChecker::NfdConnectionChecker(QObject* parent)
17 :QThread(parent)
18{
19}
20
21void
22NfdConnectionChecker::run()
23{
24 m_face = unique_ptr<ndn::Face>(new ndn::Face);
25#ifdef BOOST_THREAD_USES_CHRONO
26 time::seconds reconnetTimer = time::seconds(CONNECTION_RETRY_TIMER);
27#else
28 boost::posix_time::time_duration reconnectTimer;
29 reconnectTimer = boost::posix_time::seconds(CONNECTION_RETRY_TIMER);
30#endif
31 do {
32 {
33 std::lock_guard<std::mutex>lock(m_nfdMutex);
34 m_nfdConnected = true;
35 }
36 try {
Varun Patila24bd3e2020-11-24 10:08:33 +053037 Interest interest("/localhost/nfd/status");
38 interest.setCanBePrefix(true);
39 m_face->expressInterest(interest,
Yingdi Yuf3401182015-02-02 20:21:07 -080040 [&] (const Interest& interest, const Data& data) {
41 m_face->shutdown();
42 },
Varun Patil3d850902020-11-23 12:19:14 +053043 NULL,
Yingdi Yuf3401182015-02-02 20:21:07 -080044 [] (const Interest& interest) {});
45 m_face->processEvents(time::milliseconds::zero(), true);
46 }
Varun Patila24bd3e2020-11-24 10:08:33 +053047 catch (const std::runtime_error& e) {
Yingdi Yuf3401182015-02-02 20:21:07 -080048 {
49 std::lock_guard<std::mutex>lock(m_nfdMutex);
50 m_nfdConnected = false;
51 }
52#ifdef BOOST_THREAD_USES_CHRONO
53 boost::this_thread::sleep_for(reconnetTimer);
54#else
55 boost::this_thread::sleep(reconnectTimer);
56#endif
57 }
58 } while (!m_nfdConnected);
59 emit nfdConnected();
60}
61
62void
63NfdConnectionChecker::shutdown()
64{
65 // In this case, we just stop checking the nfd connection and exit
66 std::lock_guard<std::mutex>lock(m_nfdMutex);
67 m_nfdConnected = true;
68}
69
70} // namespace chronochat
71
72#if WAF
73#include "nfd-connection-checker.moc"
Yingdi Yuf3401182015-02-02 20:21:07 -080074#endif