basic sync loop!

This commit is contained in:
Sorunome 2019-10-17 12:54:49 +02:00
parent 6e604860e6
commit 65037cc3ea
No known key found for this signature in database
GPG key ID: 63E31F7B5993A9C4
2 changed files with 35 additions and 7 deletions

View file

@ -18,10 +18,11 @@ private:
public: public:
std::string hsUrl; std::string hsUrl;
std::string token; std::string token;
int requestId;
bool stopSyncing;
Store* store; Store* store;
void startSync(); std::string userIdCache = "";
int requestId = 0;
bool stopSyncing = false;
Thread syncThread;
void processSync(json_t* sync); void processSync(json_t* sync);
json_t* doSync(std::string token); json_t* doSync(std::string token);
json_t* doRequest(const char* method, std::string path, json_t* body = NULL); json_t* doRequest(const char* method, std::string path, json_t* body = NULL);
@ -31,6 +32,9 @@ public:
std::string sendTextMessage(std::string roomId, std::string text); std::string sendTextMessage(std::string roomId, std::string text);
std::string sendMessage(std::string roomId, json_t* content); std::string sendMessage(std::string roomId, json_t* content);
std::string sendEvent(std::string roomId, std::string eventType, json_t* content); std::string sendEvent(std::string roomId, std::string eventType, json_t* content);
void startSyncLoop();
void stopSyncLoop();
void startSync();
}; };
}; // namespace Matrix }; // namespace Matrix

View file

@ -22,8 +22,6 @@ static u32 *SOC_buffer = NULL;
Client::Client(std::string homeserverUrl, std::string matrixToken, Store* clientStore) { Client::Client(std::string homeserverUrl, std::string matrixToken, Store* clientStore) {
hsUrl = homeserverUrl; hsUrl = homeserverUrl;
token = matrixToken; token = matrixToken;
requestId = 0;
stopSyncing = false;
if (!clientStore) { if (!clientStore) {
clientStore = new MemoryStore(); clientStore = new MemoryStore();
} }
@ -31,6 +29,9 @@ Client::Client(std::string homeserverUrl, std::string matrixToken, Store* client
} }
std::string Client::userId() { std::string Client::userId() {
if (userIdCache != "") {
return userIdCache;
}
json_t* ret = doRequest("GET", "/_matrix/client/r0/account/whoami"); json_t* ret = doRequest("GET", "/_matrix/client/r0/account/whoami");
if (!ret) { if (!ret) {
return ""; return "";
@ -42,7 +43,8 @@ std::string Client::userId() {
} }
const char* userIdStr = json_string_value(userId); const char* userIdStr = json_string_value(userId);
json_decref(ret); json_decref(ret);
return userIdStr; userIdCache = std::string(userIdStr);
return userIdCache;
} }
std::string Client::sendTextMessage(std::string roomId, std::string text) { std::string Client::sendTextMessage(std::string roomId, std::string text) {
@ -66,7 +68,6 @@ std::string Client::sendEvent(std::string roomId, std::string eventType, json_t*
return ""; return "";
} }
json_t* eventId = json_object_get(ret, "event_id"); json_t* eventId = json_object_get(ret, "event_id");
free(ret);
if (!eventId) { if (!eventId) {
json_decref(ret); json_decref(ret);
return ""; return "";
@ -76,6 +77,27 @@ std::string Client::sendEvent(std::string roomId, std::string eventType, json_t*
return eventIdStr; return eventIdStr;
} }
void startSyncLoopWithoutClass(void* arg) {
printf("%lld\n", (u64)arg);
((Client*)arg)->startSync();
}
void Client::startSyncLoop() {
stopSyncLoop(); // first we stop an already running sync loop
stopSyncing = false;
s32 prio = 0;
// startSyncLoopWithoutClass(this);
printf("%lld\n", (u64)this);
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
syncThread = threadCreate(startSyncLoopWithoutClass, this, 8*1024, prio-1, -2, true);
}
void Client::stopSyncLoop() {
stopSyncing = true;
//threadJoin(syncThread, U64_MAX);
//threadFree(syncThread);
}
void Client::processSync(json_t* sync) { void Client::processSync(json_t* sync) {
json_t* rooms = json_object_get(sync, "rooms"); json_t* rooms = json_object_get(sync, "rooms");
if (!rooms) { if (!rooms) {
@ -145,6 +167,8 @@ void Client::startSync() {
processSync(ret); processSync(ret);
json_decref(ret); json_decref(ret);
} }
svcSleepThread((u64)1000000ULL * (u64)200);
startSync();
} }
json_t* Client::doSync(std::string token) { json_t* Client::doSync(std::string token) {