This repository has been archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also unified version info data to just be put in one place
- Loading branch information
1 parent
2ca5ebf
commit d965c2e
Showing
12 changed files
with
248 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include "UpdateChecker.h" | ||
|
||
#define GH_REPO "WorkingRobot/EGL2" | ||
#define GH_RELEASE_URL "https://api.github.com/repos/" GH_REPO "/releases/latest" | ||
|
||
#ifndef LOG_SECTION | ||
#define LOG_SECTION "UpdateChecker" | ||
#endif | ||
|
||
#include "../Logger.h" | ||
#include "../web/http.h" | ||
#include "versioninfo.h" | ||
|
||
#include <rapidjson/document.h> | ||
|
||
UpdateChecker::UpdateChecker(UpdateCallback callback, std::chrono::milliseconds checkInterval) : | ||
Callback(callback), | ||
CheckInterval(checkInterval), | ||
LatestVersion(VERSION_STRING) | ||
{ | ||
LOG_DEBUG("Creating update thread"); | ||
UpdateThread = std::thread(&UpdateChecker::Thread, this); | ||
UpdateThread.detach(); // causes some exception when the deconstructer is trying to join it otherwise | ||
} | ||
|
||
UpdateChecker::~UpdateChecker() { | ||
UpdateFlag.cancel(); | ||
} | ||
|
||
void UpdateChecker::SetInterval(std::chrono::milliseconds newInterval) | ||
{ | ||
CheckInterval.store(newInterval); | ||
UpdateWakeup = std::chrono::steady_clock::time_point::min(); // force update | ||
} | ||
|
||
void UpdateChecker::StopUpdateThread() | ||
{ | ||
UpdateFlag.cancel(); | ||
} | ||
|
||
bool UpdateChecker::ForceUpdate() { | ||
auto conn = Client::CreateConnection(); | ||
conn->SetUrl(GH_RELEASE_URL); | ||
conn->SetUserAgent(GH_REPO); | ||
|
||
if (!Client::Execute(conn, cancel_flag(), true)) { | ||
LOG_WARN("Could not check for EGL2 update"); | ||
return false; | ||
} | ||
|
||
if (conn->GetResponseCode() != 200) { | ||
return false; | ||
} | ||
|
||
rapidjson::Document releaseInfo; | ||
releaseInfo.Parse(conn->GetResponseBody().c_str()); | ||
|
||
if (releaseInfo.HasParseError()) { | ||
LOG_ERROR("Getting release info: JSON Parse Error %d @ %zu", releaseInfo.GetParseError(), releaseInfo.GetErrorOffset()); | ||
return false; | ||
} | ||
|
||
auto version = releaseInfo["tag_name"].GetString(); | ||
if (LatestVersion == version) { | ||
LOG_DEBUG("NO EGL2 UPDATE"); | ||
return false; | ||
} | ||
|
||
LatestInfo.Version = version; | ||
LatestInfo.Url = releaseInfo["html_url"].GetString(); | ||
|
||
auto& exeAsset = releaseInfo["assets"][0]; | ||
LatestInfo.DownloadUrl = exeAsset["browser_download_url"].GetString(); | ||
LatestInfo.DownloadCount = exeAsset["download_count"].GetInt(); | ||
LatestInfo.DownloadSize = exeAsset["size"].GetInt(); | ||
return true; | ||
} | ||
|
||
UpdateInfo& UpdateChecker::GetLatestInfo() | ||
{ | ||
return LatestInfo; | ||
} | ||
|
||
void UpdateChecker::Thread() { | ||
while (!UpdateFlag.cancelled()) { | ||
do { | ||
std::this_thread::sleep_for(std::chrono::milliseconds(5000)); | ||
} while (std::chrono::steady_clock::now() < UpdateWakeup); | ||
if (ForceUpdate()) { | ||
Callback(LatestInfo); | ||
} | ||
UpdateWakeup = std::chrono::steady_clock::now() + CheckInterval.load(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#pragma once | ||
#include "../containers/cancel_flag.h" | ||
|
||
#include <functional> | ||
#include <thread> | ||
|
||
struct UpdateInfo { | ||
std::string Version; | ||
std::string Url; | ||
|
||
std::string DownloadUrl; | ||
int DownloadCount; | ||
int DownloadSize; | ||
}; | ||
|
||
typedef std::function<void(const UpdateInfo& Info)> UpdateCallback; | ||
|
||
class UpdateChecker { | ||
public: | ||
UpdateChecker(UpdateCallback callback, std::chrono::milliseconds checkInterval); | ||
~UpdateChecker(); | ||
|
||
void SetInterval(std::chrono::milliseconds newInterval); | ||
|
||
void StopUpdateThread(); | ||
|
||
bool ForceUpdate(); | ||
|
||
UpdateInfo& GetLatestInfo(); | ||
|
||
private: | ||
void Thread(); | ||
|
||
std::atomic<std::chrono::milliseconds> CheckInterval; | ||
|
||
std::string LatestVersion; | ||
UpdateInfo LatestInfo; | ||
|
||
UpdateCallback Callback; | ||
std::thread UpdateThread; | ||
std::chrono::steady_clock::time_point UpdateWakeup; | ||
cancel_flag UpdateFlag; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.