Skip to content
Snippets Groups Projects
Commit c656d1eb authored by aviau's avatar aviau Committed by gerrit2
Browse files

read default bootstrap servers from file

Default bootstrap servers are now read from the following files:
 - share/<APPNAME>/bootstrap/servers.json
 - share/<APPNAME>/bootstrap/servers.json.d/*.json

Should those files be absent in all QStandardPaths::Datalocation directories,
empty, or contain invalid JSON, they will be ignored. In the event where no
bootstrap servers can be loaded from file, it will default to
bootstrap.ring.cx.

Tuleap: #611
Change-Id: I5e81b5338097c46a5d68135ed3dc38cd9912edf1
parent 9fca958e
No related branches found
No related tags found
No related merge requests found
[
{"host": "bootstrap.ring.cx", "port": 4222}
]
......@@ -181,7 +181,18 @@ Account* AccountPrivate::buildNewAccountFromAlias(Account::Protocol proto, const
iter.next();
a->d_ptr->m_hAccountDetails[iter.key()] = iter.value();
}
if (proto == Account::Protocol::RING)
{
/* Set LRC-provided bootstrap servers */
a->bootstrapModel() << BootstrapModel::EditAction::RESET;
a->bootstrapModel() << BootstrapModel::EditAction::SAVE;
}
else
{
a->setHostname(a->d_ptr->m_hAccountDetails[DRing::Account::ConfProperties::HOSTNAME]);
}
a->d_ptr->setAccountProperty(DRing::Account::ConfProperties::ALIAS,alias);
a->d_ptr->m_RemoteEnabledState = a->isEnabled();
//a->setObjectName(a->id());
......
......@@ -17,6 +17,15 @@
***************************************************************************/
#include "bootstrapmodel.h"
//Qt
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QDirIterator>
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonArray>
#include <QtCore/QJsonObject>
#include <QtCore/QStandardPaths>
//Ring daemon
#include <account_const.h>
......@@ -47,6 +56,7 @@ public:
//Helper
void clearLines();
inline void performAction(const BootstrapModel::EditAction action);
QVector<Lines*> loadDefaultBootstrapServers();
//Attributes
Account* m_pAccount ;
......@@ -69,6 +79,7 @@ Matrix2D<BootstrapModel::EditState, BootstrapModel::EditAction, BootstrapModelPr
/* MODIFIED */ {{ BMP::save , BMP::nothing, BMP::reload , BMP::clear, BMP::reset }},
/* OUTDATED */ {{ BMP::save , BMP::nothing, BMP::reload , BMP::clear, BMP::reset }},
/* RELOADING */ {{ BMP::nothing, BMP::nothing, BMP::nothing, BMP::nothing, BMP::nothing }},
/* RESETING */ {{ BMP::nothing, BMP::modify, BMP::nothing, BMP::nothing, BMP::nothing }},
}};
#undef BMP
......@@ -155,6 +166,80 @@ void BootstrapModelPrivate::clearLines()
}
}
QVector<BootstrapModelPrivate::Lines*> BootstrapModelPrivate::loadDefaultBootstrapServers()
{
auto servers = QVector<BootstrapModelPrivate::Lines*>();
/* get the bootstrap directory */
QString bootstrapDirPath = QStandardPaths::locate(
QStandardPaths::DataLocation,
"bootstrap",
QStandardPaths::LocateDirectory
);
QDir bootstrapDir = QDir(bootstrapDirPath);
auto bootstrapFiles = QVector<QFileInfo>();
// Main bootstrap servers file
auto mainBootstrapFile = QFileInfo(bootstrapDir.path() + "/servers.json");
if (mainBootstrapFile.exists() && mainBootstrapFile.isFile())
{
bootstrapFiles << mainBootstrapFile;
}
// Secondary bootstrap servers files
auto secondaryBootstrapServersDir = QFileInfo(bootstrapDir.path() + "/servers.json.d/");
if (secondaryBootstrapServersDir.exists() && secondaryBootstrapServersDir.isDir())
{
QDirIterator dirIter(
secondaryBootstrapServersDir.path(),
QDirIterator::Subdirectories
);
while (dirIter.hasNext()) {
dirIter.next();
auto secBootstrapFileInfo = QFileInfo(dirIter.filePath());
if (secBootstrapFileInfo.isFile() && secBootstrapFileInfo.suffix() == "json")
{
bootstrapFiles << secBootstrapFileInfo;
}
}
}
//Parse JSON files
foreach(const auto fileInfo, bootstrapFiles)
{
QFile bootstrapFile(fileInfo.filePath());
if (bootstrapFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
auto jsonDoc = QJsonDocument::fromJson(bootstrapFile.readAll());
bootstrapFile.close();
if (jsonDoc.isNull() == false && jsonDoc.isArray() == true)
{
QJsonArray jsonArray = jsonDoc.array();
foreach(const auto jsonValue, jsonArray)
{
auto hostObject = jsonValue.toObject();
auto hostValue = hostObject.value("host");
auto portValue = hostObject.value("port");
if (hostValue.isUndefined() == false &&
hostValue.isString() &&
portValue.isUndefined() == false &&
portValue.isDouble())
{
BootstrapModelPrivate::Lines* server = new BootstrapModelPrivate::Lines();
server->hostname = hostValue.toString();
server->port = portValue.toInt(-1);
servers << server;
}
}
}
}
}
return servers;
}
BootstrapModel::BootstrapModel(Account* a) : QAbstractTableModel(a), d_ptr(new BootstrapModelPrivate(this,a))
{
d_ptr->m_EditState = BootstrapModel::EditState::LOADING;
......@@ -311,8 +396,24 @@ bool BootstrapModel::isCustom() const
void BootstrapModelPrivate::reset()
{
m_EditState = BootstrapModel::EditState::RESETING;
clearLines();
auto defaultBootStrapServers = loadDefaultBootstrapServers();
if (defaultBootStrapServers.size() >= 1)
{
q_ptr->beginInsertRows(
QModelIndex(),
m_lines.size(),
m_lines.size() + defaultBootStrapServers.size() - 1
);
m_lines << defaultBootStrapServers;
q_ptr->endInsertRows();
}
else
{
/* If we can't load anything from file, default to bootstrap.ring.cx */
BootstrapModelPrivate::Lines* l = new BootstrapModelPrivate::Lines();
l->hostname = "bootstrap.ring.cx";
l->port = -1;
......@@ -320,6 +421,7 @@ void BootstrapModelPrivate::reset()
q_ptr->beginInsertRows(QModelIndex(), m_lines.size(), m_lines.size());
m_lines << l;
q_ptr->endInsertRows();
}
q_ptr << BootstrapModel::EditAction::MODIFY;
}
......
......@@ -53,6 +53,7 @@ public:
MODIFIED = 2, /*!< Our version differ from the remote one */
OUTDATED = 3, /*!< The remote version differ from ours */
RELOADING = 4, /*!< During a reload */
RESETING = 5, /*!< During a reset */
COUNT__
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment