Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit aad1e93

Browse files
committed
check server uuid when getting online
1 parent c3ec9ea commit aad1e93

File tree

8 files changed

+107
-34
lines changed

8 files changed

+107
-34
lines changed

src/ramdatainterface/dbinterface.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void DBInterface::setOffline()
4646
pm->finish();
4747
}
4848

49-
void DBInterface::setOnline()
49+
void DBInterface::setOnline(QString serverUuid)
5050
{
5151
if (m_ldi->dataFile() == "")
5252
{
@@ -60,7 +60,7 @@ void DBInterface::setOnline()
6060
}
6161

6262
// Connects to the Ramses Server and change connection status
63-
m_rsi->setOnline();
63+
m_rsi->setOnline(serverUuid);
6464
}
6565

6666
void DBInterface::setRamsesPath(QString p)
@@ -146,13 +146,16 @@ void DBInterface::setDataFile(const QString &file, bool ignoreUser)
146146
m_rsi->setServerPort(config.port);
147147
m_updateFrequency = config.updateDelay;
148148

149+
// Get the serverUuid we should be connecting to
150+
QString serverUuid = m_ldi->serverUuid();
151+
149152
// Check the user
150153
QString userUuid = m_ldi->currentUserUuid();
151154

152155
qDebug() << "Selected previous user: " << userUuid;
153156

154157
emit userChanged( userUuid );
155-
setOnline();
158+
setOnline(serverUuid);
156159

157160
pm->setText(tr("Ready!"));
158161
pm->finish();
@@ -356,6 +359,7 @@ void DBInterface::connectEvents()
356359
connect(m_rsi, &RamServerInterface::connectionStatusChanged, this, &DBInterface::serverConnectionStatusChanged);
357360
connect(m_rsi, &RamServerInterface::syncReady, m_ldi, &LocalDataInterface::sync);
358361
connect(m_rsi, &RamServerInterface::userChanged, this, &DBInterface::serverUserChanged);
362+
connect(m_rsi, &RamServerInterface::pong, m_ldi, &LocalDataInterface::setServerUuid);
359363
connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(sync()));
360364

361365
connect(qApp, &QApplication::aboutToQuit, this, &DBInterface::quit);

src/ramdatainterface/dbinterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public slots:
9797
/**
9898
* @brief Changes to online mode.
9999
*/
100-
void setOnline();
100+
void setOnline(QString serverUuid = "");
101101
// MAINTENANCE //
102102
QString cleanDabaBase(int deleteDataOlderThan = -1);
103103
bool undoClean();

src/ramdatainterface/localdatainterface.cpp

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,23 @@ ServerConfig LocalDataInterface::serverConfig()
382382
return config;
383383
}
384384

385+
void LocalDataInterface::setServerUuid(QString serverUuid)
386+
{
387+
QString q = "UPDATE _Sync SET uuid = '%1';";
388+
query( q.arg(serverUuid) );
389+
}
390+
391+
QString LocalDataInterface::serverUuid()
392+
{
393+
QString q = "SELECT uuid FROM _Sync;";
394+
QSqlQuery qry = query( q );
395+
QString uuid = "";
396+
if (qry.first()) {
397+
uuid = qry.value(0).toString();
398+
}
399+
return uuid;
400+
}
401+
385402
QString LocalDataInterface::ramsesPath()
386403
{
387404
QString q = "SELECT path FROM _Paths WHERE name = 'Ramses';";
@@ -740,7 +757,7 @@ void LocalDataInterface::setCurrentUserUuid(QString uuid)
740757
query( q.arg(uuid) );
741758
}
742759

743-
void LocalDataInterface::sync(QJsonObject data)
760+
void LocalDataInterface::sync(QJsonObject data, QString serverUuid)
744761
{
745762
ProgressManager *pm = ProgressManager::instance();
746763
pm->reInit();
@@ -760,8 +777,8 @@ void LocalDataInterface::sync(QJsonObject data)
760777

761778
QString q = "DELETE FROM _Sync;";
762779
query( q );
763-
q = "INSERT INTO _Sync ( lastSync) VALUES ( '%1' );";
764-
query( q.arg( QDateTime::currentDateTimeUtc().toString("yyyy-MM-dd hh:mm:ss") ) );
780+
q = "INSERT INTO _Sync ( lastSync, uuid ) VALUES ( '%1', '%2' );";
781+
query( q.arg( QDateTime::currentDateTimeUtc().toString("yyyy-MM-dd hh:mm:ss"), serverUuid ) );
765782

766783
pm->finish();
767784
}
@@ -953,7 +970,7 @@ void LocalDataInterface::quit()
953970
qDebug() << "LocalDataInterface: Everything's clean.";
954971
}
955972

956-
LocalDataInterface::LocalDataInterface() :
973+
LocalDataInterface::LocalDataInterface():
957974
DuQFLoggerObject("Local Data Interface")
958975
{
959976
//Load local database
@@ -1006,7 +1023,6 @@ bool LocalDataInterface::openDB(QSqlDatabase db, const QString &dbFile)
10061023
currentVersion = QVersionNumber::fromString( qry.value(0).toString() );
10071024
}
10081025

1009-
10101026
if (currentVersion < newVersion)
10111027
{
10121028
pm->setText(tr("Updating database scheme"));
@@ -1038,6 +1054,20 @@ bool LocalDataInterface::openDB(QSqlDatabase db, const QString &dbFile)
10381054
qry.exec("UPDATE _Server SET port = 80 WHERE useSsl = 0;");
10391055
}
10401056

1057+
if (currentVersion < QVersionNumber(0, 6, 0))
1058+
{
1059+
// Add the uuid entry to the _sync table
1060+
ok = qry.exec("ALTER TABLE _Sync ADD COLUMN \"uuid\" TEXT;");
1061+
if (!ok)
1062+
{
1063+
QString errorMessage = "Something went wrong when updating the database scheme to the new version.\nHere's some information:";
1064+
errorMessage += "\n> " + tr("Query:") + "\n" + qry.lastQuery();
1065+
errorMessage += "\n> " + tr("Database Error:") + "\n" + qry.lastError().databaseText();
1066+
errorMessage += "\n> " + tr("Driver Error:") + "\n" + qry.lastError().driverText();
1067+
LocalDataInterface::instance()->log(errorMessage, DuQFLog::Critical);
1068+
}
1069+
}
1070+
10411071
if (ok)
10421072
{
10431073
// Remove previous version and update with ours

src/ramdatainterface/localdatainterface.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class LocalDataInterface : public DuQFLoggerObject
5858
void updateUser(QString uuid, QString username, QString data, QString modified);
5959

6060
ServerConfig serverConfig();
61+
void setServerUuid(QString serverUuid);
62+
QString serverUuid();
6163

6264
QString ramsesPath();
6365
void setRamsesPath(QString path);
@@ -82,7 +84,7 @@ class LocalDataInterface : public DuQFLoggerObject
8284
const QHash<QString, QSet<QString> > &deletedUuids() const;
8385

8486
public slots:
85-
void sync(QJsonObject data);
87+
void sync(QJsonObject data, QString serverUuid = "");
8688

8789
signals:
8890
void dataReset();

src/ramdatainterface/ramserverinterface.cpp

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ QJsonArray RamServerInterface::downloadData()
333333

334334
// PUBLIC SLOTS //
335335

336-
void RamServerInterface::setOnline()
336+
void RamServerInterface::setOnline(QString serverUuid)
337337
{
338338
ProgressManager *pm = ProgressManager::instance();
339339

@@ -371,16 +371,9 @@ void RamServerInterface::setOnline()
371371

372372
QJsonObject repObj = parseData(reply);
373373

374-
bool repSuccess = repObj.value("success").toBool();
374+
if (!checkPing(repObj, serverUuid)) return;
375375

376-
// If ping is not successful, set offline.
377-
if (!repSuccess)
378-
{
379-
QString reason = tr("The server is unavailable.\nPlease check your network.\n\n(server ping failed)");
380-
log(reason, DuQFLog::Warning);
381-
setConnectionStatus(NetworkUtils::Offline, reason);
382-
return;
383-
}
376+
m_localServerUuid = serverUuid;
384377

385378
pm->setText(tr("Pong!"));
386379
pm->increment();
@@ -584,6 +577,10 @@ void RamServerInterface::dataReceived(QNetworkReply *reply)
584577
bool repSuccess = repObj.value("success").toBool();
585578
QString repMessage = repObj.value("message").toString();
586579
QJsonArray repLog = repObj.value("debug").toArray();
580+
QString serverUuid = repObj.value("serverUuid").toString();
581+
582+
// Check server UUID
583+
if (!checkServerUuid(repObj)) return;
587584

588585
// Log
589586
for (int i = 0; i < repLog.count(); i++)
@@ -603,19 +600,11 @@ void RamServerInterface::dataReceived(QNetworkReply *reply)
603600
// Parse specific data
604601
if (repQuery == "ping")
605602
{
606-
// If ping is not successful, set offline.
607-
if (!repSuccess)
603+
if (checkPing(repObj))
608604
{
609-
QString reason = tr("The server is unavailable.\nPlease check your network.\n\n(server ping failed)");
610-
log(reason, DuQFLog::Warning);
611-
setConnectionStatus(NetworkUtils::Offline, reason);
612-
return;
605+
// If we're connecting, login!
606+
if (m_status == NetworkUtils::Connecting) login();
613607
}
614-
615-
// get the server version
616-
m_serverVersion = repObj.value("content").toObject().value("version").toString();
617-
// If we're connecting, login!
618-
if (m_status == NetworkUtils::Connecting) login();
619608
}
620609
else if (repQuery == "loggedout")
621610
{
@@ -634,7 +623,7 @@ void RamServerInterface::dataReceived(QNetworkReply *reply)
634623
return;
635624
}
636625

637-
emit syncReady(repObj.value("content").toObject());
626+
emit syncReady(repObj.value("content").toObject(), serverUuid);
638627
}
639628
else if (repQuery == "setPassword")
640629
{
@@ -672,6 +661,48 @@ void RamServerInterface::nextRequest()
672661
postRequest( m_requestQueue.takeFirst() );
673662
}
674663

664+
bool RamServerInterface::checkPing(QJsonObject repObj, QString serverUuid)
665+
{
666+
bool repSuccess = repObj.value("success").toBool();
667+
668+
// If ping is not successful, set offline.
669+
if (!repSuccess)
670+
{
671+
QString reason = tr("The server is unavailable.\nPlease check your network.\n\n(server ping failed)");
672+
log(reason, DuQFLog::Warning);
673+
setConnectionStatus(NetworkUtils::Offline, reason);
674+
return false;
675+
}
676+
677+
// Check the server UUID
678+
if (serverUuid != "") m_localServerUuid = serverUuid;
679+
if (!checkServerUuid(repObj)) return false;
680+
681+
// get the server version
682+
m_serverVersion = repObj.value("content").toObject().value("version").toString();
683+
684+
emit pong(repObj.value("serverUuid").toString());
685+
return true;
686+
}
687+
688+
bool RamServerInterface::checkServerUuid(QJsonObject repObj)
689+
{
690+
QString distantUuid = repObj.value("serverUuid").toString();
691+
if (m_localServerUuid != "" && distantUuid != "" && m_localServerUuid != distantUuid)
692+
{
693+
QString reason = tr("This server is not in sync with this local database.\n\n"
694+
"This can happen if you try to connect to a new server from an existing database,\n"
695+
"Of if the server has been updated, or its configuration changed.\n\n"
696+
"Create a new database to connect to this server ( %1 ).").arg(
697+
serverAddress()
698+
);
699+
log(reason, DuQFLog::Critical);
700+
setConnectionStatus(NetworkUtils::Offline, tr("This server is not in sync with this local database."));
701+
return false;
702+
}
703+
return true;
704+
}
705+
675706
void RamServerInterface::queueRequest(QString query, QJsonObject body)
676707
{
677708
Request r = buildRequest(query, body);

src/ramdatainterface/ramserverinterface.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public slots:
7373
* @brief setOnline posts a ping, and set the status to "Connecting"
7474
* Status will be changed to "Connected" if we get a valid pong
7575
*/
76-
void setOnline();
76+
void setOnline(QString serverUuid = "");
7777
void setOffline();
7878
void login();
7979
QString doLogin(QString username, QString password, bool saveUsername = false, bool savePassword = false);
@@ -82,9 +82,10 @@ public slots:
8282
signals:
8383
void sslChanged(bool);
8484
void connectionStatusChanged(NetworkUtils::NetworkStatus, QString);
85-
void syncReady(QJsonObject);
85+
void syncReady(QJsonObject data, QString serverUuid);
8686
void newData(QJsonObject);
8787
void userChanged(QString uuid, QString username, QString userdata, QString modified);
88+
void pong(QString serverUuid);
8889

8990
protected:
9091
static RamServerInterface *_instance;
@@ -119,6 +120,9 @@ private slots:
119120
*/
120121
void nextRequest();
121122

123+
bool checkPing(QJsonObject repObj, QString serverUuid = "");
124+
bool checkServerUuid(QJsonObject repObj);
125+
122126
private:
123127

124128
// METHODS //
@@ -211,6 +215,8 @@ private slots:
211215
*/
212216
int m_timeout = 3000;
213217

218+
QString m_localServerUuid = "";
219+
214220
/**
215221
* @brief Online / Offline status
216222
*/

src/resources/data/template.ramses

8 KB
Binary file not shown.

src/resources/data/templateDB.sqlite

8 KB
Binary file not shown.

0 commit comments

Comments
 (0)