1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(core): pass bootstrap list interface to Core

This commit is contained in:
Anthony Bilinski 2020-05-01 22:05:43 -07:00
parent 5d56a3c039
commit 5a877d742b
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
10 changed files with 72 additions and 15 deletions

View File

@ -379,6 +379,7 @@ set(${PROJECT_NAME}_SOURCES
src/model/chathistory.h
src/model/chathistory.cpp
src/model/toxclientstandards.h
src/model/ibootstraplistgenerator.h
src/net/bootstrapnodeupdater.cpp
src/net/bootstrapnodeupdater.h
src/net/avatarbroadcaster.cpp

View File

@ -28,7 +28,7 @@
#include "src/core/toxstring.h"
#include "src/model/groupinvite.h"
#include "src/model/status.h"
#include "src/net/bootstrapnodeupdater.h"
#include "src/model/ibootstraplistgenerator.h"
#include "src/nexus.h"
#include "src/persistence/profile.h"
#include "src/util/strongtype.h"
@ -473,10 +473,11 @@ bool parseErr(Tox_Err_Conference_Delete error, int line)
} // namespace
Core::Core(QThread* coreThread)
Core::Core(QThread* coreThread, IBootstrapListGenerator& _bootstrapNodes)
: tox(nullptr)
, toxTimer{new QTimer{this}}
, coreThread(coreThread)
, bootstrapNodes(_bootstrapNodes)
{
assert(toxTimer);
toxTimer->setSingleShot(true);
@ -525,7 +526,7 @@ void Core::registerCallbacks(Tox* tox)
* @return nullptr or a Core object ready to start
*/
ToxCorePtr Core::makeToxCore(const QByteArray& savedata, const ICoreSettings* const settings,
ToxCoreErrors* err)
IBootstrapListGenerator& bootstrapNodes, ToxCoreErrors* err)
{
QThread* thread = new QThread();
if (thread == nullptr) {
@ -543,7 +544,7 @@ ToxCorePtr Core::makeToxCore(const QByteArray& savedata, const ICoreSettings* co
return {};
}
ToxCorePtr core(new Core(thread));
ToxCorePtr core(new Core(thread, bootstrapNodes));
if (core == nullptr) {
if (err) {
*err = ToxCoreErrors::ERROR_ALLOC;
@ -785,9 +786,9 @@ void Core::bootstrapDht()
{
ASSERT_CORE_THREAD;
QList<DhtServer> bootstrapNodes = BootstrapNodeUpdater::loadDefaultBootstrapNodes();
QList<DhtServer> bootstrapNodesList = bootstrapNodes.getBootstrapnodes();
int listSize = bootstrapNodes.size();
int listSize = bootstrapNodesList.size();
if (!listSize) {
qWarning() << "No bootstrap node list";
return;
@ -801,7 +802,7 @@ void Core::bootstrapDht()
#endif
// i think the more we bootstrap, the more we jitter because the more we overwrite nodes
while (i < 2) {
const DhtServer& dhtServer = bootstrapNodes[j % listSize];
const DhtServer& dhtServer = bootstrapNodesList[j % listSize];
QString dhtServerAddress = dhtServer.address.toLatin1();
QString port = QString::number(dhtServer.port);
QString name = dhtServer.name;

View File

@ -49,6 +49,7 @@ class ICoreSettings;
class GroupInvite;
class Profile;
class Core;
class IBootstrapListGenerator;
using ToxCorePtr = std::unique_ptr<Core>;
@ -69,7 +70,7 @@ public:
};
static ToxCorePtr makeToxCore(const QByteArray& savedata, const ICoreSettings* const settings,
ToxCoreErrors* err = nullptr);
IBootstrapListGenerator& bootstrapNodes, ToxCoreErrors* err = nullptr);
static Core* getInstance();
const CoreAV* getAv() const;
CoreAV* getAv();
@ -188,7 +189,7 @@ signals:
void failedToRemoveFriend(uint32_t friendId);
private:
Core(QThread* coreThread);
Core(QThread* coreThread, IBootstrapListGenerator& _bootstrapNodes);
static void onFriendRequest(Tox* tox, const uint8_t* cUserId, const uint8_t* cMessage,
size_t cMessageSize, void* core);
@ -251,4 +252,5 @@ private:
mutable QMutex coreLoopLock{QMutex::Recursive};
std::unique_ptr<QThread> coreThread;
IBootstrapListGenerator& bootstrapNodes;
};

View File

@ -0,0 +1,30 @@
/*
Copyright © 2020 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QList>
class DhtServer;
class IBootstrapListGenerator
{
public:
virtual ~IBootstrapListGenerator() = default;
virtual QList<DhtServer> getBootstrapnodes() = 0;
};

View File

@ -58,6 +58,11 @@ BootstrapNodeUpdater::BootstrapNodeUpdater(const QNetworkProxy& proxy, QObject*
, proxy{proxy}
{}
QList<DhtServer> BootstrapNodeUpdater::getBootstrapnodes()
{
return loadDefaultBootstrapNodes();
}
void BootstrapNodeUpdater::requestBootstrapNodes()
{
nam.setProxy(proxy);
@ -71,7 +76,7 @@ void BootstrapNodeUpdater::requestBootstrapNodes()
/**
* @brief Loads the list of built in boostrap nodes
* @return List of bootstrapnodes on success, empty list on error
* @return List of bootstrap nodes on success, empty list on error
*/
QList<DhtServer> BootstrapNodeUpdater::loadDefaultBootstrapNodes()
{

View File

@ -25,14 +25,16 @@
#include <QObject>
#include "src/core/dhtserver.h"
#include "src/model/ibootstraplistgenerator.h"
class QNetworkReply;
class BootstrapNodeUpdater : public QObject
class BootstrapNodeUpdater : public QObject, public IBootstrapListGenerator
{
Q_OBJECT
public:
explicit BootstrapNodeUpdater(const QNetworkProxy& proxy, QObject* parent = nullptr);
QList<DhtServer> getBootstrapnodes() override;
void requestBootstrapNodes();
static QList<DhtServer> loadDefaultBootstrapNodes();

View File

@ -35,6 +35,7 @@
#include "src/core/coreav.h"
#include "src/core/corefile.h"
#include "src/net/avatarbroadcaster.h"
#include "src/net/bootstrapnodeupdater.h"
#include "src/nexus.h"
#include "src/widget/gui.h"
#include "src/widget/tool/identicon.h"
@ -244,7 +245,7 @@ void Profile::initCore(const QByteArray& toxsave, const ICoreSettings& s, bool i
}
Core::ToxCoreErrors err;
core = Core::makeToxCore(toxsave, &s, &err);
core = Core::makeToxCore(toxsave, &s, bootstrapNodes, &err);
if (!core) {
switch (err) {
case Core::ToxCoreErrors::BAD_PROXY:
@ -281,6 +282,7 @@ Profile::Profile(const QString& name, const QString& password, std::unique_ptr<T
, passkey{std::move(passkey)}
, isRemoved{false}
, encrypted{this->passkey != nullptr}
, bootstrapNodes(Settings::getInstance().getProxy())
{}
/**

View File

@ -25,6 +25,7 @@
#include "src/core/toxid.h"
#include "src/persistence/history.h"
#include "src/net/bootstrapnodeupdater.h"
#include <QByteArray>
#include <QObject>
@ -116,4 +117,5 @@ private:
bool isRemoved;
bool encrypted = false;
static QStringList profiles;
BootstrapNodeUpdater bootstrapNodes;
};

View File

@ -21,6 +21,7 @@
#include "src/core/toxoptions.h"
#include "src/core/icoresettings.h"
#include "src/net/bootstrapnodeupdater.h"
#include "src/model/ibootstraplistgenerator.h"
#include <QtTest/QtTest>
#include <QtGlobal>
@ -74,6 +75,12 @@ private:
quint16 port;
};
class MockNodeListGenerator : public IBootstrapListGenerator
{
QList<DhtServer> getBootstrapnodes() {
return BootstrapNodeUpdater::loadDefaultBootstrapNodes();
}
};
class TestCore : public QObject
{
@ -104,7 +111,9 @@ void TestCore::startup_without_proxy()
settings->setProxyPort(0);
settings->setProxyType(MockSettings::ProxyType::ptNone);
test_core = Core::makeToxCore(savedata, settings, err);
MockNodeListGenerator nodesGenerator{};
test_core = Core::makeToxCore(savedata, settings, nodesGenerator, err);
if (test_core == nullptr) {
QFAIL("ToxCore initialisation failed");
@ -130,7 +139,9 @@ void TestCore::startup_with_invalid_proxy()
settings->setProxyPort(9985);
settings->setProxyType(MockSettings::ProxyType::ptSOCKS5);
test_core = Core::makeToxCore(savedata, settings, err);
MockNodeListGenerator nodesGenerator{};
test_core = Core::makeToxCore(savedata, settings, nodesGenerator, err);
if (test_core != nullptr) {
QFAIL("ToxCore initialisation passed with invalid SOCKS5 proxy address");
@ -142,7 +153,7 @@ void TestCore::startup_with_invalid_proxy()
settings->setProxyPort(9985);
settings->setProxyType(MockSettings::ProxyType::ptHTTP);
test_core = Core::makeToxCore(savedata, settings, err);
test_core = Core::makeToxCore(savedata, settings, nodesGenerator, err);
if (test_core != nullptr) {
QFAIL("ToxCore initialisation passed with invalid HTTP proxy address");

View File

@ -18,6 +18,7 @@
*/
#include "src/net/bootstrapnodeupdater.h"
#include "src/persistence/paths.h"
#include <QNetworkProxy>
#include <QSignalSpy>