Skip to content

Commit

Permalink
try to reconnect to nfd when connection is down
Browse files Browse the repository at this point in the history
Change-Id: I37ac7d6894cf058dc0453dd25425d8e1a6dc4e18
Refs: #2470
  • Loading branch information
bruinfish authored and qiuhanding committed Apr 12, 2015
1 parent 7ff31f0 commit f340118
Show file tree
Hide file tree
Showing 12 changed files with 402 additions and 51 deletions.
65 changes: 56 additions & 9 deletions src/chat-dialog-backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -386,6 +419,7 @@ ChatDialogBackend::sendJoin()

m_helloEventId = m_scheduler->scheduleEvent(HELLO_INTERVAL,
bind(&ChatDialogBackend::sendHello, this));
emit newChatroomForDiscovery(Name::Component(m_chatroomName));
}

void
Expand Down Expand Up @@ -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();

Expand All @@ -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
Expand Down
25 changes: 22 additions & 3 deletions src/chat-dialog-backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +19,7 @@
#include "chatbuf.pb.h"
#include <mutex>
#include <socket.hpp>
#include <boost/thread.hpp>
#endif

namespace chronochat {
Expand Down Expand Up @@ -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();

Expand All @@ -62,6 +64,9 @@ class ChatDialogBackend : public QThread
shared_ptr<ndn::IdentityCertificate>
loadTrustAnchor();

void
exitChatroom();

void
close();

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/chat-dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ ChatDialog::ChatDialog(const Name& chatroomPrefix,
connect(&m_backend, SIGNAL(chatPrefixChanged(ndn::Name)),
this, SLOT(updateLabels(ndn::Name)));

connect(&m_backend, SIGNAL(refreshChatDialog(ndn::Name)),
this, SLOT(updateLabels(ndn::Name)));

// When frontend gets a message to send, notify backend.
connect(this, SIGNAL(msgToSent(QString, time_t)),
&m_backend, SLOT(sendChatMessage(QString, time_t)));
Expand Down Expand Up @@ -351,6 +354,7 @@ ChatDialog::receiveMessage(QString sessionPrefix, QString nick, uint64_t seqNo,
appendControlMessage(nick, "enters room", timestamp);
m_rosterModel->setStringList(m_scene->getRosterList());
}
fitView();
}

void
Expand Down
62 changes: 46 additions & 16 deletions src/chatroom-discovery-backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ static const ndn::Name::Component ROUTING_HINT_SEPARATOR =
// a count enforced when a manager himself find another one publish chatroom data
static const int MAXIMUM_COUNT = 3;
static const int IDENTITY_OFFSET = -1;
static const int CONNECTION_RETRY_TIMER = 3;

ChatroomDiscoveryBackend::ChatroomDiscoveryBackend(const Name& routingPrefix,
const Name& identity,
QObject* parent)
: QThread(parent)
, m_shouldResume(false)
, m_routingPrefix(routingPrefix)
, m_identity(identity)
, m_randomGenerator(static_cast<unsigned int>(std::time(0)))
, m_rangeUniformRandom(m_randomGenerator, boost::uniform_int<>(500,2000))
, m_shouldResume(false)
{
m_discoveryPrefix.append("ndn")
.append("broadcast")
Expand All @@ -59,13 +60,38 @@ ChatroomDiscoveryBackend::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 = 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
}
}
{
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);

Expand Down Expand Up @@ -275,12 +301,10 @@ ChatroomDiscoveryBackend::updateRoutingPrefix(const QString& routingPrefix)
updatePrefixes();

{
std::lock_guard<std::mutex>lock(m_mutex);
std::lock_guard<std::mutex>lock(m_resumeMutex);
m_shouldResume = true;
}

close();

m_face->getIoService().stop();
}
}
Expand Down Expand Up @@ -341,14 +365,12 @@ ChatroomDiscoveryBackend::onAddInRoster(ndn::Name sessionPrefix,
sendUpdate(chatroomName);
}
else {
m_chatroomList[chatroomName].chatroomName = chatroomName.toUri();
m_chatroomList[chatroomName].info.setName(chatroomName);
m_chatroomList[chatroomName].info.addParticipant(sessionPrefix);
onNewChatroomForDiscovery(chatroomName);
}
}

void
ChatroomDiscoveryBackend::onNewChatroomForDiscovery(Name::Component chatroomName)
ChatroomDiscoveryBackend::onNewChatroomForDiscovery(ndn::Name::Component chatroomName)
{
Name newPrefix = m_routableUserDiscoveryPrefix;
newPrefix.append(chatroomName);
Expand Down Expand Up @@ -406,12 +428,10 @@ ChatroomDiscoveryBackend::onIdentityUpdated(const QString& identity)
updatePrefixes();

{
std::lock_guard<std::mutex>lock(m_mutex);
std::lock_guard<std::mutex>lock(m_resumeMutex);
m_shouldResume = true;
}

close();

m_face->getIoService().stop();
}

Expand Down Expand Up @@ -443,15 +463,25 @@ void
ChatroomDiscoveryBackend::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;
}

m_face->getIoService().stop();
}

void
ChatroomDiscoveryBackend::onNfdReconnect()
{
std::lock_guard<std::mutex>lock(m_nfdConnectionMutex);
m_isNfdConnected = true;
}

} // namespace chronochat

Expand Down
Loading

0 comments on commit f340118

Please sign in to comment.