feat(sync): client sync engine with links, settings conflicts, transfers and background agent
Per-sequence state between the local index and a server from listings and manifest diffs; version-counter settings sync with explicit conflicts; upload, original and proxy download jobs; WebSocket-driven background agent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
13 files changed
+1858
-2
No files matched your search
@@ -0,0 +1,109 @@
|
||||
using Titano.Protocol;
|
||||
using Titano.Protocol.Client;
|
||||
|
||||
namespace Titano.Data.Sync;
|
||||
|
||||
/// <summary>
|
||||
/// The server calls the sync engine makes (BRIEF §14.3). <see cref="TitanoSyncRemote"/> sends them with
|
||||
/// <see cref="TitanoClient"/>; screenshots and tests substitute fakes. Failures are <see cref="TitanoApiException"/>s
|
||||
/// (404 gone, 409 settings conflict) or network exceptions. Dispose a remote handed out by an <see cref="ISyncConnector"/>.
|
||||
/// </summary>
|
||||
public interface ISyncRemote : IDisposable
|
||||
{
|
||||
/// <summary>Normalised server address the links are stored under (<see cref="SyncService.ServerKey"/>).</summary>
|
||||
string Server { get; }
|
||||
|
||||
/// <summary>Every sequence on the server.</summary>
|
||||
Task<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken);
|
||||
|
||||
Task<SequenceDto> GetSequenceAsync(Guid id, CancellationToken cancellationToken);
|
||||
|
||||
Task<SequenceManifest> GetManifestAsync(Guid id, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Every frame row of a sequence, in index order.</summary>
|
||||
Task<IReadOnlyList<FrameDto>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken);
|
||||
|
||||
Task<SequenceDto> CreateSequenceAsync(CreateSequenceRequest request, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>A 409 <see cref="TitanoApiException"/> means the settings version moved on the server.</summary>
|
||||
Task<SequenceDto> UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Chunked, resumable upload of one original that the server verifies against <paramref name="sha256"/> before
|
||||
/// committing it (BRIEF §14.4); see <see cref="TitanoClient.UploadFileAsync"/>.
|
||||
/// </summary>
|
||||
Task<CompleteUploadResponse?> UploadFileAsync(string path, Guid sequenceId, string sha256, DateTime? capturedAt, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Streams a frame's original, proxy or thumb, resuming with Range from the destination's length.</summary>
|
||||
Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Server events until cancelled or the socket closes.</summary>
|
||||
IAsyncEnumerable<ServerEvent> EventsAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>Where sync jobs find their server connection.</summary>
|
||||
public interface ISyncConnector
|
||||
{
|
||||
/// <summary>A signed-in remote of <paramref name="server"/> owned by the caller; null when this PC is not connected to that server.</summary>
|
||||
ValueTask<ISyncRemote?> ConnectAsync(string server, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary><see cref="ISyncRemote"/> over a signed-in <see cref="TitanoClient"/>, which it disposes when it owns it.</summary>
|
||||
public sealed class TitanoSyncRemote(TitanoClient client, bool ownsClient) : ISyncRemote
|
||||
{
|
||||
private const int SequencePage = 500;
|
||||
private const int FramePage = 1000;
|
||||
|
||||
public string Server { get; } = SyncService.ServerKey(client.BaseAddress);
|
||||
|
||||
public async Task<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var all = new List<SequenceDto>();
|
||||
string? cursor = null;
|
||||
do
|
||||
{
|
||||
var page = await client.ListSequencesAsync(null, cursor, SequencePage, cancellationToken).ConfigureAwait(false);
|
||||
all.AddRange(page.Items);
|
||||
cursor = page.NextCursor;
|
||||
}
|
||||
while (cursor is not null);
|
||||
return all;
|
||||
}
|
||||
|
||||
public Task<SequenceDto> GetSequenceAsync(Guid id, CancellationToken cancellationToken) => client.GetSequenceAsync(id, cancellationToken);
|
||||
|
||||
public Task<SequenceManifest> GetManifestAsync(Guid id, CancellationToken cancellationToken) => client.GetManifestAsync(id, cancellationToken);
|
||||
|
||||
public async Task<IReadOnlyList<FrameDto>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken)
|
||||
{
|
||||
var all = new List<FrameDto>();
|
||||
string? cursor = null;
|
||||
do
|
||||
{
|
||||
var page = await client.ListFramesAsync(sequenceId, cursor, FramePage, cancellationToken).ConfigureAwait(false);
|
||||
all.AddRange(page.Items);
|
||||
cursor = page.NextCursor;
|
||||
}
|
||||
while (cursor is not null);
|
||||
return all;
|
||||
}
|
||||
|
||||
public Task<SequenceDto> CreateSequenceAsync(CreateSequenceRequest request, CancellationToken cancellationToken) =>
|
||||
client.CreateSequenceAsync(request, cancellationToken);
|
||||
|
||||
public Task<SequenceDto> UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken) =>
|
||||
client.UpdateSequenceAsync(id, request, cancellationToken);
|
||||
|
||||
public Task<CompleteUploadResponse?> UploadFileAsync(string path, Guid sequenceId, string sha256, DateTime? capturedAt, CancellationToken cancellationToken) =>
|
||||
client.UploadFileAsync(path, sequenceId, capturedAt, null, sha256, null, cancellationToken);
|
||||
|
||||
public Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken) =>
|
||||
client.DownloadFrameAsync(frameId, kind, destination, null, cancellationToken);
|
||||
|
||||
public IAsyncEnumerable<ServerEvent> EventsAsync(CancellationToken cancellationToken) => client.EventsAsync(cancellationToken);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (ownsClient) client.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user