basic sync loop!
This commit is contained in:
parent
6e604860e6
commit
65037cc3ea
2 changed files with 35 additions and 7 deletions
|
@ -18,10 +18,11 @@ private:
|
|||
public:
|
||||
std::string hsUrl;
|
||||
std::string token;
|
||||
int requestId;
|
||||
bool stopSyncing;
|
||||
Store* store;
|
||||
void startSync();
|
||||
std::string userIdCache = "";
|
||||
int requestId = 0;
|
||||
bool stopSyncing = false;
|
||||
Thread syncThread;
|
||||
void processSync(json_t* sync);
|
||||
json_t* doSync(std::string token);
|
||||
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 sendMessage(std::string roomId, json_t* content);
|
||||
std::string sendEvent(std::string roomId, std::string eventType, json_t* content);
|
||||
void startSyncLoop();
|
||||
void stopSyncLoop();
|
||||
void startSync();
|
||||
};
|
||||
|
||||
}; // namespace Matrix
|
||||
|
|
|
@ -22,8 +22,6 @@ static u32 *SOC_buffer = NULL;
|
|||
Client::Client(std::string homeserverUrl, std::string matrixToken, Store* clientStore) {
|
||||
hsUrl = homeserverUrl;
|
||||
token = matrixToken;
|
||||
requestId = 0;
|
||||
stopSyncing = false;
|
||||
if (!clientStore) {
|
||||
clientStore = new MemoryStore();
|
||||
}
|
||||
|
@ -31,6 +29,9 @@ Client::Client(std::string homeserverUrl, std::string matrixToken, Store* client
|
|||
}
|
||||
|
||||
std::string Client::userId() {
|
||||
if (userIdCache != "") {
|
||||
return userIdCache;
|
||||
}
|
||||
json_t* ret = doRequest("GET", "/_matrix/client/r0/account/whoami");
|
||||
if (!ret) {
|
||||
return "";
|
||||
|
@ -42,7 +43,8 @@ std::string Client::userId() {
|
|||
}
|
||||
const char* userIdStr = json_string_value(userId);
|
||||
json_decref(ret);
|
||||
return userIdStr;
|
||||
userIdCache = std::string(userIdStr);
|
||||
return userIdCache;
|
||||
}
|
||||
|
||||
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 "";
|
||||
}
|
||||
json_t* eventId = json_object_get(ret, "event_id");
|
||||
free(ret);
|
||||
if (!eventId) {
|
||||
json_decref(ret);
|
||||
return "";
|
||||
|
@ -76,6 +77,27 @@ std::string Client::sendEvent(std::string roomId, std::string eventType, json_t*
|
|||
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) {
|
||||
json_t* rooms = json_object_get(sync, "rooms");
|
||||
if (!rooms) {
|
||||
|
@ -145,6 +167,8 @@ void Client::startSync() {
|
|||
processSync(ret);
|
||||
json_decref(ret);
|
||||
}
|
||||
svcSleepThread((u64)1000000ULL * (u64)200);
|
||||
startSync();
|
||||
}
|
||||
|
||||
json_t* Client::doSync(std::string token) {
|
||||
|
|
Loading…
Reference in a new issue