NAM A2 Sync

This commit is contained in:
Robin E.R. Davies
2026-05-12 12:31:21 -04:00
parent 44db94a955
commit c456e5187b
39 changed files with 5086 additions and 2206 deletions
+153 -34
View File
@@ -8,10 +8,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -21,7 +21,7 @@
* SOFTWARE.
*/
#pragma once
#pragma once
#include <memory>
#include <cstdint>
@@ -31,55 +31,173 @@
#include <condition_variable>
#include <vector>
#include <functional>
#include "ThreadedQueue.hpp"
#include "Tone3000Download.hpp"
namespace pipedal {
namespace pipedal
{
struct CachedPkceParams
{
std::chrono::steady_clock::time_point time;
Tone3000PkceParams pkceParams;
};
class Tone3000AuthResponse
{
private:
std::string access_token_;
int64_t expires_in_ = -1;
std::string refresh_token_;
std::string token_type_;
std::optional<std::string> tone_id_;
std::optional<std::string> model_id_;
bool canceled_ = false;
using clock_t = std::chrono::steady_clock;
clock_t::time_point expires_at_;
class Tone3000DownloaderImpl: public Tone3000Downloader {
public:
virtual ~Tone3000DownloaderImpl();
virtual void SetListener(Listener*listener) override;
Tone3000AuthResponse(const std::string &responseBody);
JSON_GETTER_REF(access_token);
JSON_GETTER(expires_in);
JSON_GETTER_REF(refresh_token);
JSON_GETTER_REF(token_type);
JSON_GETTER_REF(expires_at);
JSON_GETTER_REF(tone_id);
JSON_GETTER_REF(model_id);
JSON_GETTER_SETTER(canceled);
virtual handle_t RequestDownload(
Tone3000DownloadType downloadType,
const std::string &path,
const std::string &url
) override;
DECLARE_JSON_MAP(Tone3000AuthResponse);
};
class Tone3000AccessTokens
{
private:
std::string access_token_;
int64_t expires_in_ = -1;
std::string refresh_token_;
std::string token_type_;
using clock_t = std::chrono::steady_clock;
clock_t::time_point expires_at_;
public:
Tone3000AccessTokens(const Tone3000AuthResponse &authResponse);
JSON_GETTER_REF(access_token);
JSON_GETTER(expires_in);
JSON_GETTER_REF(refresh_token);
JSON_GETTER_REF(token_type);
JSON_GETTER_REF(expires_at);
};
class Tone3000DownloaderImpl : public Tone3000Downloader
{
public:
Tone3000DownloaderImpl();
virtual ~Tone3000DownloaderImpl();
virtual void SetListener(Listener *listener) override;
virtual void CancelDownload(
handle_t handle
) override;
handle_t handle) override;
virtual void Close() override;
virtual Tone3000DownloadProgress GetDownloadStatus() override;
virtual handle_t RequestTone3000Download(
const uri &uri,
const Tone3000PkceParams &pkceParams,
const std::string &downloadPath,
Tone3000DownloadType downloadType) override;
private:
struct DownloadRequest
{
handle_t handle = -1;
std::string requestUri;
Tone3000PkceParams pkceParams;
std::string downloadPath;
Tone3000DownloadType downloadType = Tone3000DownloadType::Nam;
std::atomic<bool> cancelled = false;
};
void DownloadTone3000ToneBg(
std::shared_ptr<DownloadRequest> request);
void OnTone3000DownloadError(int64_t handle, const std::string &error)
{
if (listener)
{
listener->OnTone3000DownloadError(handle, error);
}
}
std::shared_ptr<Tone3000Download> GetTone3000Tone(
int64_t toneId);
std::string DownloadTone3000Files(
const Tone3000FileDownloadRequest &request,
Tone3000DownloadProgress &progress);
void OnTone3000DownloadComplete(int64_t handle, const std::string &path)
{
if (listener)
{
listener->OnTone3000DownloadComplete(handle, path);
}
}
void OnTone3000DownloadCancelled(int64_t handle)
{
if (listener)
{
listener->OnTone3000DownloadComplete(handle, "");
}
}
using clock_t = std::chrono::steady_clock;
std::recursive_mutex pkceCacheMutex;
Listener *listener = nullptr;
void ThreadProc();
// Performs the actual download with cancellation support
std::string PerformDownload(
Tone3000DownloadType downloadType,
const std::string &downloadPath,
const std::string &downloadUrl,
int64_t downloadHandle,
std::function<bool()> isCancelled
);
struct DownloadRequest {
Tone3000DownloadType downloadType;
std::atomic<bool> cancelled = false;
handle_t handle;
const std::string downloadPath;
const std::string downloadUrl;
// std::string PerformFileDownloads(
// const Tone3000FileDownloadRequest &downloadRequets,
// Tone3000DownloadProgress &progress,
// std::function<void(Tone3000DownloadProgress &progress)> updateProgress,
// std::function<bool()> isCancelled);
struct PostResult
{
bool ok = false;
int errorCode = 0;
std::string error;
std::string body;
};
PostResult Post(
const uri &requestedUri,
std::vector<std::string> headers,
const std::string &body);
std::string Tone3000GetText(
const uri &requestedUri
);
struct OAuthCallbackResult
{
std::optional<int64_t> toneId;
std::optional<int64_t> modelId;
};
OAuthCallbackResult handleOAuthCallback(
const uri &callbackUri,
const Tone3000PkceParams &pkce);
std::atomic<bool> closed = false;
handle_t nextHandle = 1;
handle_t NextHandle();
std::vector<std::shared_ptr<DownloadRequest>> requestQueue;
ThreadedQueue<DownloadRequest> requestQueue;
Tone3000DownloadProgress fgDownloadProgress;
void bgUpdateDownloadProgress(const Tone3000DownloadProgress &progress);
@@ -87,9 +205,10 @@ namespace pipedal {
std::shared_ptr<DownloadRequest> activeRequest;
std::mutex mutex;
std::condition_variable thread_cv;
std::unique_ptr<std::jthread> thread;
std::shared_ptr<Tone3000AccessTokens> accessTokens;
std::string GetBearerToken() const;
};
}
}