diff --git a/core.cpp b/core.cpp index 795752161..94f76c318 100644 --- a/core.cpp +++ b/core.cpp @@ -49,15 +49,9 @@ Core::Core(Camera* cam, QThread *coreThread) : toxTimer = new QTimer(this); toxTimer->setSingleShot(true); - //saveTimer = new QTimer(this); - //saveTimer->start(TOX_SAVE_INTERVAL); - bootstrapTimer = new QTimer(this); - bootstrapTimer->start(TOX_BOOTSTRAP_INTERVAL); connect(toxTimer, &QTimer::timeout, this, &Core::process); - //connect(saveTimer, &QTimer::timeout, this, &Core::saveConfiguration); //Disable save timer in favor of saving on events //connect(fileTimer, &QTimer::timeout, this, &Core::fileHeartbeat); - connect(bootstrapTimer, &QTimer::timeout, this, &Core::onBootstrapTimer); - connect(&Settings::getInstance(), &Settings::dhtServerListChanged, this, &Core::bootstrapDht); + connect(&Settings::getInstance(), &Settings::dhtServerListChanged, this, &Core::process); connect(this, SIGNAL(fileTransferFinished(ToxFile)), this, SLOT(onFileTransferFinished(ToxFile))); for (int i=0; istart(tox_do_interval(tox)); } -void Core::onBootstrapTimer() +bool Core::checkConnection() { - if (!tox) - return; - if(!tox_isconnected(tox)) - bootstrapDht(); + static bool isConnected = false; + bool toxConnected = tox_isconnected(tox); + + if (toxConnected && !isConnected) { + qDebug() << "Core: Connected to DHT"; + emit connected(); + isConnected = true; + } else if (!toxConnected && isConnected) { + qDebug() << "Core: Disconnected to DHT"; + emit disconnected(); + isConnected = false; + } + return isConnected; +} + +void Core::bootstrapDht() +{ + const Settings& s = Settings::getInstance(); + QList dhtServerList = s.getDhtServerList(); + + int listSize = dhtServerList.size(); + static int j = qrand() % listSize; + + qDebug() << "Core: Bootstraping to the DHT ..."; + + int i=0; + while (i < 2) + { + const Settings::DhtServer& dhtServer = dhtServerList[j % listSize]; + if (tox_bootstrap_from_address(tox, dhtServer.address.toLatin1().data(), + dhtServer.port, CUserId(dhtServer.userId).data()) == 1) + qDebug() << QString("Core: Bootstraping from ")+dhtServer.name+QString(", addr ")+dhtServer.address.toLatin1().data() + +QString(", port ")+QString().setNum(dhtServer.port); + else + qDebug() << "Core: Error bootstraping from "+dhtServer.name; + + j++; + i++; + } } void Core::onFriendRequest(Tox*/* tox*/, const uint8_t* cUserId, const uint8_t* cMessage, uint16_t cMessageSize, void* core) @@ -512,20 +563,13 @@ void Core::onAvatarInfoCallback(Tox*, int32_t friendnumber, uint8_t format, Core* core = static_cast(_core); if (format == TOX_AVATAR_FORMAT_NONE) - { - qDebug() << "Core: Got null avatar info from" << friendnumber; emit core->friendAvatarRemoved(friendnumber); - } else { QByteArray oldHash = Settings::getInstance().getAvatarHash(core->getFriendAddress(friendnumber)); - if (QByteArray((char*)hash, TOX_HASH_LENGTH) != oldHash) // comparison failed miserably if I didn't convert hash to QByteArray - { - qDebug() << "Core: got different avatar hash from" << friendnumber; + if (QByteArray((char*)hash, TOX_HASH_LENGTH) != oldHash) + // comparison failed miserably if I didn't convert hash to QByteArray tox_request_avatar_data(core->tox, friendnumber); - } - else - qDebug() << "Core: Got old avatar info from" << friendnumber; } } @@ -534,11 +578,8 @@ void Core::onAvatarDataCallback(Tox*, int32_t friendnumber, uint8_t, { QPixmap pic; pic.loadFromData((uchar*)data, datalen); - if (pic.isNull()) - qDebug() << "Core: Got null avatar from "<(core)->getFriendAddress(friendnumber)); Settings::getInstance().saveAvatarHash(QByteArray((char*)hash, TOX_HASH_LENGTH), static_cast(core)->getFriendAddress(friendnumber)); emit static_cast(core)->friendAvatarChanged(friendnumber, pic); @@ -927,69 +968,6 @@ void Core::onFileTransferFinished(ToxFile file) emit fileDownloadFinished(file.filePath); } -void Core::bootstrapDht() -{ - const Settings& s = Settings::getInstance(); - QList dhtServerList = s.getDhtServerList(); - - int listSize = dhtServerList.size(); - static int j = qrand() % listSize, n=0; - - // We couldn't connect after trying 6 different nodes, let's try something else - if (n>3) - { - qDebug() << "Core: We're having trouble connecting to the DHT, slowing down"; - bootstrapTimer->setInterval(TOX_BOOTSTRAP_INTERVAL*(n-1)); - } - else - qDebug() << "Core: Connecting to the DHT ..."; - - int i=0; - while (i < (2 - (n>3))) - { - const Settings::DhtServer& dhtServer = dhtServerList[j % listSize]; - if (tox_bootstrap_from_address(tox, dhtServer.address.toLatin1().data(), - dhtServer.port, CUserId(dhtServer.userId).data()) == 1) - qDebug() << QString("Core: Bootstraping from ")+dhtServer.name+QString(", addr ")+dhtServer.address.toLatin1().data() - +QString(", port ")+QString().setNum(dhtServer.port); - else - qDebug() << "Core: Error bootstraping from "+dhtServer.name; - - tox_do(tox); - j++; - i++; - n++; - } -} - -void Core::process() -{ - tox_do(tox); -#ifdef DEBUG - //we want to see the debug messages immediately - fflush(stdout); -#endif - checkConnection(); - //int toxInterval = tox_do_interval(tox); - //qDebug() << QString("Tox interval %1").arg(toxInterval); - toxTimer->start(50); -} - -void Core::checkConnection() -{ - static bool isConnected = false; - - if (tox_isconnected(tox) && !isConnected) { - qDebug() << "Core: Connected to DHT"; - emit connected(); - isConnected = true; - } else if (!tox_isconnected(tox) && isConnected) { - qDebug() << "Core: Disconnected to DHT"; - emit disconnected(); - isConnected = false; - } -} - void Core::loadConfiguration() { QString path = QDir(Settings::getSettingsDirPath()).filePath(CONFIG_FILE_NAME); diff --git a/core.h b/core.h index fbaca60da..4b5aa7a86 100644 --- a/core.h +++ b/core.h @@ -207,8 +207,7 @@ private: static void playCallVideo(ToxAv* toxav, int32_t callId, vpx_image_t* img, void *user_data); void sendCallVideo(int callId); - void checkConnection(); - void onBootstrapTimer(); + bool checkConnection(); void loadConfiguration(); void loadFriends(); diff --git a/coredefines.h b/coredefines.h index cb6fcffed..e2c71b8d4 100644 --- a/coredefines.h +++ b/coredefines.h @@ -3,9 +3,7 @@ #define TOXAV_MAX_CALLS 16 #define GROUPCHAT_MAX_SIZE 32 -#define TOX_SAVE_INTERVAL 30*1000 #define TOX_FILE_INTERVAL 0 -#define TOX_BOOTSTRAP_INTERVAL 5*1000 #define TOXAV_RINGING_TIME 15 // TODO: Put that in the settings