send periodic hello msg if no data msg
diff --git a/chatdialog.cpp b/chatdialog.cpp
index 26226d9..19ea99a 100644
--- a/chatdialog.cpp
+++ b/chatdialog.cpp
@@ -11,6 +11,9 @@
#define BROADCAST_PREFIX_FOR_SYNC_DEMO "/ndn/broadcast/sync-demo"
+static const int FRESHNESS = 60;
+static const int HELLO_INTERVAL = 59;
+
void
ChatDialog::testDraw()
{
@@ -34,7 +37,7 @@
}
ChatDialog::ChatDialog(QWidget *parent)
- : QDialog(parent), m_sock(NULL)
+ : QDialog(parent), m_sock(NULL), m_lastMsgTime(0)
{
// have to register this, otherwise
// the signal-slot system won't recognize this type
@@ -392,10 +395,12 @@
std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
abort();
}
- m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, 60);
+ m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
delete buf;
+ m_lastMsgTime = time(NULL);
+
int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
std::vector<Sync::MissingDataInfo> v;
@@ -410,9 +415,19 @@
void
ChatDialog::sendHello()
{
- SyncDemo::ChatMessage msg;
- formHelloMessage(msg);
- sendMsg(msg);
+ time_t now = time(NULL);
+ int elapsed = now - m_lastMsgTime;
+ if (elapsed >= HELLO_INTERVAL)
+ {
+ SyncDemo::ChatMessage msg;
+ formHelloMessage(msg);
+ sendMsg(msg);
+ QTimer::singleShot(HELLO_INTERVAL * 1000, this, SLOT(sendHello()));
+ }
+ else
+ {
+ QTimer::singleShot((HELLO_INTERVAL - elapsed) * 1000, this, SLOT(sendHello()));
+ }
}
void
diff --git a/chatdialog.h b/chatdialog.h
index 3f75247..a825657 100644
--- a/chatdialog.h
+++ b/chatdialog.h
@@ -44,7 +44,6 @@
void formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
void formHelloMessage(SyncDemo::ChatMessage &msg);
void sendMsg(SyncDemo::ChatMessage &msg);
- void sendHello();
bool readSettings();
void writeSettings();
void updateLabels();
@@ -60,6 +59,7 @@
void buttonPressed();
void checkSetting();
void settingUpdated(QString, QString, QString);
+ void sendHello();
// icon related
void iconActivated(QSystemTrayIcon::ActivationReason reason);
@@ -78,6 +78,7 @@
DigestTreeScene *m_scene;
boost::mutex m_msgMutex;
boost::mutex m_sceneMutex;
+ time_t m_lastMsgTime;
// icon related
QAction *minimizeAction;