-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try to reconnect to nfd when connection is down
Change-Id: I37ac7d6894cf058dc0453dd25425d8e1a6dc4e18 Refs: #2470
- Loading branch information
1 parent
7ff31f0
commit f340118
Showing
12 changed files
with
402 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
* BSD license, See the LICENSE file for more information | ||
* | ||
* Author: Yingdi Yu <[email protected]> | ||
* Qiuhan Ding <[email protected]> | ||
*/ | ||
|
||
#include "chat-dialog-backend.hpp" | ||
|
@@ -28,6 +29,7 @@ static const time::seconds HELLO_INTERVAL(60); | |
static const ndn::Name::Component ROUTING_HINT_SEPARATOR = | ||
ndn::name::Component::fromEscapedString("%F0%2E"); | ||
static const int IDENTITY_OFFSET = -3; | ||
static const int CONNECTION_RETRY_TIMER = 3; | ||
|
||
ChatDialogBackend::ChatDialogBackend(const Name& chatroomPrefix, | ||
const Name& userChatPrefix, | ||
|
@@ -37,6 +39,7 @@ ChatDialogBackend::ChatDialogBackend(const Name& chatroomPrefix, | |
const Name& signingId, | ||
QObject* parent) | ||
: QThread(parent) | ||
, m_shouldResume(false) | ||
, m_localRoutingPrefix(routingPrefix) | ||
, m_chatroomPrefix(chatroomPrefix) | ||
, m_userChatPrefix(userChatPrefix) | ||
|
@@ -63,13 +66,40 @@ ChatDialogBackend::run() | |
if (m_face == nullptr) | ||
break; | ||
|
||
m_face->getIoService().run(); | ||
|
||
try { | ||
m_face->getIoService().run(); | ||
} | ||
catch (std::runtime_error& e) { | ||
{ | ||
std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); | ||
m_isNfdConnected = false; | ||
} | ||
emit nfdError(); | ||
{ | ||
std::lock_guard<std::mutex>lock(m_resumeMutex); | ||
m_shouldResume = true; | ||
} | ||
#ifdef BOOST_THREAD_USES_CHRONO | ||
time::seconds reconnectTimer = time::seconds(CONNECTION_RETRY_TIMER); | ||
#else | ||
boost::posix_time::time_duration reconnectTimer; | ||
reconnectTimer = boost::posix_time::seconds(CONNECTION_RETRY_TIMER); | ||
#endif | ||
while (!m_isNfdConnected) { | ||
#ifdef BOOST_THREAD_USES_CHRONO | ||
boost::this_thread::sleep_for(reconnectTimer); | ||
#else | ||
boost::this_thread::sleep(reconnectTimer); | ||
#endif | ||
} | ||
emit refreshChatDialog(m_routableUserChatPrefix); | ||
} | ||
{ | ||
std::lock_guard<std::mutex>lock(m_mutex); | ||
std::lock_guard<std::mutex>lock(m_resumeMutex); | ||
shouldResume = m_shouldResume; | ||
m_shouldResume = false; | ||
} | ||
close(); | ||
|
||
} while (shouldResume); | ||
|
||
|
@@ -161,13 +191,16 @@ ChatDialogBackend::loadTrustAnchor() | |
} | ||
|
||
void | ||
ChatDialogBackend::close() | ||
{ | ||
ChatDialogBackend::exitChatroom() { | ||
if (m_joined) | ||
sendLeave(); | ||
|
||
usleep(100000); | ||
} | ||
|
||
void | ||
ChatDialogBackend::close() | ||
{ | ||
m_scheduler->cancelAllEvents(); | ||
m_helloEventId.reset(); | ||
m_roster.clear(); | ||
|
@@ -386,6 +419,7 @@ ChatDialogBackend::sendJoin() | |
|
||
m_helloEventId = m_scheduler->scheduleEvent(HELLO_INTERVAL, | ||
bind(&ChatDialogBackend::sendHello, this)); | ||
emit newChatroomForDiscovery(Name::Component(m_chatroomName)); | ||
} | ||
|
||
void | ||
|
@@ -487,11 +521,11 @@ ChatDialogBackend::updateRoutingPrefix(const QString& localRoutingPrefix) | |
m_localRoutingPrefix = newLocalRoutingPrefix; | ||
|
||
{ | ||
std::lock_guard<std::mutex>lock(m_mutex); | ||
std::lock_guard<std::mutex>lock(m_resumeMutex); | ||
m_shouldResume = true; | ||
} | ||
|
||
close(); | ||
exitChatroom(); | ||
|
||
updatePrefixes(); | ||
|
||
|
@@ -503,15 +537,28 @@ void | |
ChatDialogBackend::shutdown() | ||
{ | ||
{ | ||
std::lock_guard<std::mutex>lock(m_mutex); | ||
std::lock_guard<std::mutex>lock(m_resumeMutex); | ||
m_shouldResume = false; | ||
} | ||
|
||
close(); | ||
{ | ||
// In this case, we just stop checking the nfd connection and exit | ||
std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); | ||
m_isNfdConnected = true; | ||
} | ||
|
||
exitChatroom(); | ||
|
||
m_face->getIoService().stop(); | ||
} | ||
|
||
void | ||
ChatDialogBackend::onNfdReconnect() | ||
{ | ||
std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); | ||
m_isNfdConnected = true; | ||
} | ||
|
||
} // namespace chronochat | ||
|
||
#if WAF | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
* BSD license, See the LICENSE file for more information | ||
* | ||
* Author: Yingdi Yu <[email protected]> | ||
* Qiuhan Ding <[email protected]> | ||
*/ | ||
|
||
#ifndef CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP | ||
|
@@ -18,6 +19,7 @@ | |
#include "chatbuf.pb.h" | ||
#include <mutex> | ||
#include <socket.hpp> | ||
#include <boost/thread.hpp> | ||
#endif | ||
|
||
namespace chronochat { | ||
|
@@ -47,7 +49,7 @@ class ChatDialogBackend : public QThread | |
const std::string& chatroomName, | ||
const std::string& nick, | ||
const Name& signingId = Name(), | ||
QObject* parent = 0); | ||
QObject* parent = nullptr); | ||
|
||
~ChatDialogBackend(); | ||
|
||
|
@@ -62,6 +64,9 @@ class ChatDialogBackend : public QThread | |
shared_ptr<ndn::IdentityCertificate> | ||
loadTrustAnchor(); | ||
|
||
void | ||
exitChatroom(); | ||
|
||
void | ||
close(); | ||
|
||
|
@@ -120,12 +125,21 @@ class ChatDialogBackend : public QThread | |
void | ||
chatPrefixChanged(ndn::Name newChatPrefix); | ||
|
||
void | ||
refreshChatDialog(ndn::Name chatPrefix); | ||
|
||
void | ||
eraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName); | ||
|
||
void | ||
addInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName); | ||
|
||
void | ||
newChatroomForDiscovery(ndn::Name::Component chatroomName); | ||
|
||
void | ||
nfdError(); | ||
|
||
public slots: | ||
void | ||
sendChatMessage(QString text, time_t timestamp); | ||
|
@@ -136,9 +150,14 @@ public slots: | |
void | ||
shutdown(); | ||
|
||
void | ||
onNfdReconnect(); | ||
|
||
private: | ||
typedef std::map<ndn::Name, UserInfo> BackendRoster; | ||
|
||
bool m_shouldResume; | ||
bool m_isNfdConnected; | ||
shared_ptr<ndn::Face> m_face; | ||
|
||
Name m_localRoutingPrefix; // routable local prefix | ||
|
@@ -160,8 +179,8 @@ public slots: | |
|
||
BackendRoster m_roster; // User roster | ||
|
||
std::mutex m_mutex; | ||
bool m_shouldResume; | ||
std::mutex m_resumeMutex; | ||
std::mutex m_nfdConnectionMutex; | ||
}; | ||
|
||
} // namespace chronochat | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.