still trying

This commit is contained in:
Blue 2019-04-26 15:52:34 +03:00
parent 1f450ad3e0
commit 88ea4cf3ac
3 changed files with 82 additions and 48 deletions

View File

@ -38,6 +38,7 @@ public:
void close(); void close();
bool addElement(const Shared::Message& message); bool addElement(const Shared::Message& message);
unsigned int addElements(const std::list<Shared::Message>& messages);
Shared::Message getElement(const QString& id); Shared::Message getElement(const QString& id);
Shared::Message oldest(); Shared::Message oldest();
QString oldestId(); QString oldestId();

View File

@ -28,11 +28,11 @@ Core::Contact::Contact(const QString& pJid, const QString& account, QObject* par
subscriptionState(Shared::unknown), subscriptionState(Shared::unknown),
syncronizing(false), syncronizing(false),
requestedCount(0), requestedCount(0),
receivedCount(0), requestedBefore(),
hisoryCache(), hisoryCache(),
appendCache(), appendCache(),
requestCache(), responseCache(),
responseCache() requestCache()
{ {
archive->open(account); archive->open(account);
if (archive->isFromTheBeginning()) { if (archive->isFromTheBeginning()) {
@ -108,7 +108,7 @@ unsigned int Core::Contact::groupsCount() const
void Core::Contact::addMessageToArchive(const Shared::Message& msg) void Core::Contact::addMessageToArchive(const Shared::Message& msg)
{ {
hisoryCache.emplace_back(msg);
} }
void Core::Contact::requestHistory(int count, const QString& before) void Core::Contact::requestHistory(int count, const QString& before)
@ -122,6 +122,11 @@ void Core::Contact::requestHistory(int count, const QString& before)
void Core::Contact::nextRequest() void Core::Contact::nextRequest()
{ {
if (syncronizing) {
if (requestedCount != -1) {
emit historyResponse(responseCache);
}
}
if (requestCache.size() > 0) { if (requestCache.size() > 0) {
std::pair<int, QString> request = requestCache.front(); std::pair<int, QString> request = requestCache.front();
requestCache.pop_front(); requestCache.pop_front();
@ -129,7 +134,7 @@ void Core::Contact::nextRequest()
} else { } else {
syncronizing = false; syncronizing = false;
requestedCount = 0; requestedCount = 0;
receivedCount = 0; requestedBefore = "";
hisoryCache.clear(); hisoryCache.clear();
responseCache.clear(); responseCache.clear();
} }
@ -139,7 +144,7 @@ void Core::Contact::performRequest(int count, const QString& before)
{ {
syncronizing = true; syncronizing = true;
requestedCount = count; requestedCount = count;
receivedCount = 0; requestedBefore = before;
hisoryCache.clear(); hisoryCache.clear();
responseCache.clear(); responseCache.clear();
@ -147,6 +152,7 @@ void Core::Contact::performRequest(int count, const QString& before)
case empty: case empty:
emit needEarlierHistory(before, "", QDateTime(), QDateTime()); emit needEarlierHistory(before, "", QDateTime(), QDateTime());
break; break;
case chunk:
case beginning: { case beginning: {
bool found = false; bool found = false;
if (appendCache.size() != 0) { if (appendCache.size() != 0) {
@ -154,12 +160,10 @@ void Core::Contact::performRequest(int count, const QString& before)
const Shared::Message& msg = *itr; const Shared::Message& msg = *itr;
if (found) { if (found) {
responseCache.emplace_front(msg); responseCache.emplace_front(msg);
++receivedCount;
} else { } else {
if (msg.getId() == before) { if (msg.getId() == before) {
found = true; found = true;
responseCache.emplace_front(*itr); responseCache.emplace_front(*itr);
++receivedCount;
} }
} }
if (responseCache.size() == count) { if (responseCache.size() == count) {
@ -167,54 +171,48 @@ void Core::Contact::performRequest(int count, const QString& before)
} }
} }
if (responseCache.size() == count) { if (responseCache.size() == count) {
emit historyResponse(responseCache);
nextRequest(); nextRequest();
break; break;
} }
} }
if (found) { if (found) {
emit needEarlierHistory(responseCache.front().getId(), "", QDateTime(), QDateTime()); requestedBefore = responseCache.front().getId();
emit needEarlierHistory(requestedBefore, "", QDateTime(), QDateTime());
} else { } else {
if (requiestFromArchive(before)) { if (requestFromArchive(before)) {
nextRequest(); nextRequest();
} }
} }
} }
break; break;
case end: { case end: {
std::list<Shared::Message> arc; bool found = requestFromArchive(before);
if (count != -1) { if (found) {
try { int rSize = responseCache.size();
arc = archive->getBefore(requestedCount - receivedCount, before); if (rSize < count) {
responseCache.insert(responseCache.begin(), arc.begin(), arc.end()); if (rSize != 0) {
emit historyResponse(responseCache); requestedBefore = responseCache.front().getId();
emit needEarlierHistory(responseCache.front().getId(), "", QDateTime(), QDateTime());
} else {
requestedBefore = before;
emit needEarlierHistory(before, "", QDateTime(), QDateTime());
}
} else {
nextRequest(); nextRequest();
} catch (Archive::NotFound e) {
requestCache.emplace_back(count, before);
requestedCount = -1;
emit needEarlierHistory(archive->oldestId(), "", QDateTime(), QDateTime());
}
} else {
try {
arc = archive->getBefore(1, before);
//just do nothing since response is not required
nextRequest(); //may be even it's a signal that the history is now complete?
} catch (Archive::NotFound e) {
emit needEarlierHistory(archive->oldestId(), "", QDateTime(), QDateTime());
} }
} }
} }
break; break;
case chunk:
//from last
break;
case complete: case complete:
//just give if (!requestFromArchive(before)) {
qDebug("requesting id hasn't been found in archive, skipping");
}
nextRequest();
break; break;
} }
} }
bool Core::Contact::requiestFromArchive(const QString& before) bool Core::Contact::requestFromArchive(const QString& before)
{ {
std::list<Shared::Message> arc; std::list<Shared::Message> arc;
QString lBefore; QString lBefore;
@ -225,21 +223,13 @@ bool Core::Contact::requiestFromArchive(const QString& before)
} }
if (requestedCount != -1) { if (requestedCount != -1) {
try { try {
arc = archive->getBefore(requestedCount - receivedCount, lBefore); arc = archive->getBefore(requestedCount - responseCache.size(), lBefore);
responseCache.insert(responseCache.begin(), arc.begin(), arc.end()); responseCache.insert(responseCache.begin(), arc.begin(), arc.end());
emit historyResponse(responseCache);
return true; return true;
} catch (Archive::NotFound e) { } catch (Archive::NotFound e) {
requestCache.emplace_back(requestedCount, before); requestCache.emplace_back(requestedCount, before);
requestedCount = -1; requestedCount = -1;
switch (archiveState) { requestEarlierToSync();
case empty:
case beginning:
case end:
case chunk:
case complete:
}
emit needEarlierHistory("", archive->newestId(), QDateTime(), QDateTime());
return false; return false;
} }
} else { } else {
@ -249,12 +239,28 @@ bool Core::Contact::requiestFromArchive(const QString& before)
//may be even it's a signal that the history is now complete? //may be even it's a signal that the history is now complete?
return true; return true;
} catch (Archive::NotFound e) { } catch (Archive::NotFound e) {
emit needEarlierHistory("", archive->newestId(), QDateTime(), QDateTime()); requestEarlierToSync();
return false; return false;
} }
} }
} }
void Core::Contact::requestEarlierToSync()
{
switch (archiveState) {
case empty:
break;
case beginning: //need to reach complete
case chunk: //need to reach end
emit needEarlierHistory("", archive->newestId(), QDateTime(), QDateTime());
break;
case end: //need to reach complete
emit needEarlierHistory(archive->oldestId(), "", QDateTime(), QDateTime());
break;
case complete: //nothing to sync
break;
}
}
void Core::Contact::appendMessageToArchive(const Shared::Message& msg) void Core::Contact::appendMessageToArchive(const Shared::Message& msg)
{ {
@ -284,3 +290,28 @@ void Core::Contact::appendMessageToArchive(const Shared::Message& msg)
} }
} }
void Core::Contact::flushMessagesToArchive(bool finished, const QString& lastId)
{
unsigned int amount(0);
if (hisoryCache.size() > 0) {
amount = archive->addElements(hisoryCache);
}
if (requestedCount == -1) {
if (amount >= requestedCount - responseCache.size()) {
if (requestFromArchive(requestedBefore)){
nextRequest();
return;
}
}
if (!finished) {
if (lastId.size() != 0) {
}
}
} else {
}
}

View File

@ -21,6 +21,7 @@
#include <QObject> #include <QObject>
#include <QSet> #include <QSet>
#include <QString>
#include "archive.h" #include "archive.h"
#include "../global.h" #include "../global.h"
#include <list> #include <list>
@ -51,7 +52,7 @@ public:
unsigned int groupsCount() const; unsigned int groupsCount() const;
void addMessageToArchive(const Shared::Message& msg); void addMessageToArchive(const Shared::Message& msg);
void appendMessageToArchive(const Shared::Message& msg); void appendMessageToArchive(const Shared::Message& msg);
void flushMessagesToArchive(); void flushMessagesToArchive(bool finished, const QString& lastId);
void requestHistory(int count, const QString& before); void requestHistory(int count, const QString& before);
signals: signals:
@ -74,7 +75,7 @@ private:
bool syncronizing; bool syncronizing;
int requestedCount; int requestedCount;
int receivedCount; QString requestedBefore;
std::list<Shared::Message> hisoryCache; std::list<Shared::Message> hisoryCache;
std::list<Shared::Message> appendCache; std::list<Shared::Message> appendCache;
std::list<Shared::Message> responseCache; std::list<Shared::Message> responseCache;
@ -83,7 +84,8 @@ private:
private: private:
void nextRequest(); void nextRequest();
void performRequest(int count, const QString& before); void performRequest(int count, const QString& before);
bool requiestFromArchive(const QString& before); bool requestFromArchive(const QString& before);
void requestEarlierToSync();
}; };
} }