hopefully fixes the thing that bothers me a lot since the change of using ccncat's code for local prefix
diff --git a/chatdialog.cpp b/chatdialog.cpp
index f677b43..464ac91 100644
--- a/chatdialog.cpp
+++ b/chatdialog.cpp
@@ -19,7 +19,7 @@
static const int HELLO_INTERVAL = FRESHNESS * 3 / 4; // seconds
ChatDialog::ChatDialog(QWidget *parent)
- : QDialog(parent), m_sock(NULL), m_lastMsgTime(0), m_historyInitialized(false)
+ : QDialog(parent), m_sock(NULL), m_lastMsgTime(0), m_historyInitialized(false), m_joined(false)
{
// have to register this, otherwise
// the signal-slot system won't recognize this type
@@ -55,7 +55,7 @@
connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
connect(treeButton, SIGNAL(pressed()), this, SLOT(treeButtonPressed()));
- connect(refreshButton, SIGNAL(pressed()), this, SLOT(getLocalPrefix()));
+ connect(refreshButton, SIGNAL(pressed()), this, SLOT(updateLocalPrefix()));
connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool, bool)), this, SLOT(processData(QString, const char *, size_t, bool, bool)));
connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
connect(m_timer, SIGNAL(timeout()), this, SLOT(replot()));
@@ -76,12 +76,24 @@
bind(&ChatDialog::processRemoveWrapper, this, _1));
Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create();
handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1));
- QTimer::singleShot(100, this, SLOT(getLocalPrefix()));
+ //QTimer::singleShot(100, this, SLOT(getLocalPrefix()));
+ if (!getLocalPrefix())
+ {
+ // if getLocalPrefix indicates no prefix change
+ // this sock is going to be used
+ QTimer::singleShot(600, this, SLOT(sendJoin()));
+ m_timer->start(FRESHNESS * 1000);
+ disableTreeDisplay();
+ QTimer::singleShot(2200, this, SLOT(enableTreeDisplay()));
+ }
+ else
+ {
+ // this socket is going to be destroyed anyway
+ // why bother doing the following steps
- QTimer::singleShot(600, this, SLOT(sendJoin()));
- m_timer->start(FRESHNESS * 1000);
- disableTreeDisplay();
- QTimer::singleShot(2200, this, SLOT(enableTreeDisplay()));
+ // the same steps would be performed for another socket
+ // in settingUpdated
+ }
}
catch (Sync::CcnxOperationException ex)
{
@@ -111,6 +123,7 @@
usleep(500000);
m_sock->remove(m_user.getPrefix().toStdString());
usleep(5000);
+ m_joined = false;
#ifdef __DEBUG
std::cout << "Sync REMOVE signal sent" << std::endl;
#endif
@@ -547,7 +560,7 @@
return randStr.c_str();
}
-void
+bool
ChatDialog::getLocalPrefix()
{
// /*
@@ -584,23 +597,26 @@
std::cerr << "trying to get local prefix" << std::endl;
if (m_sock != NULL)
- {
- std::cerr << "trying to get local prefix2" << std::endl;
- QString originPrefix = QString::fromStdString (m_sock->getLocalPrefix()).trimmed ();
- std::cerr << "got: " << originPrefix.toStdString () << std::endl;
+ {
+ QString originPrefix = QString::fromStdString (m_sock->getLocalPrefix()).trimmed ();
+ std::cerr << "got: " << originPrefix.toStdString () << std::endl;
- if (originPrefix != "" && m_user.getOriginPrefix () != originPrefix)
- {
- // m_user.setOriginPrefix(originPrefix);
- emit settingUpdated(m_user.getNick (), m_user.getChatroom (), originPrefix);
- // connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT());
- }
- }
- else
+ if (originPrefix != "" && m_user.getOriginPrefix () != originPrefix)
{
- std::cerr << "socket is not availble" << std::endl;
- // QTimer::singleShot(1000, this, SLOT(getLocalPrefix())); // try again
+ emit settingUpdated(m_user.getNick (), m_user.getChatroom (), originPrefix);
+ // prefix updated
+ return true;
}
+ }
+
+ // prefix not changed
+ return false;
+}
+
+void
+ChatDialog::updateLocalPrefix()
+{
+ getLocalPrefix();
}
bool
@@ -716,6 +732,7 @@
void
ChatDialog::sendJoin()
{
+ m_joined = true;
SyncDemo::ChatMessage msg;
formControlMessage(msg, SyncDemo::ChatMessage::JOIN);
sendMsg(msg);
@@ -834,14 +851,17 @@
textEdit->clear();
- // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
if (m_sock != NULL)
{
// keep the new prefix
QString newPrefix = m_user.getPrefix();
// send leave for the old
m_user.setPrefix(oldPrefix);
- sendLeave();
+ // there is no point to send leave if we haven't joined yet
+ if (m_joined)
+ {
+ sendLeave();
+ }
// resume new prefix
m_user.setPrefix(newPrefix);
Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create();
@@ -859,7 +879,7 @@
m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemoveWrapper, this, _1));
Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create();
handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1));
- QTimer::singleShot(1000, this, SLOT(sendJoin()));
+ QTimer::singleShot(600, this, SLOT(sendJoin()));
m_timer->start(FRESHNESS * 1000);
disableTreeDisplay();
QTimer::singleShot(2200, this, SLOT(enableTreeDisplay()));