using System.IO;
using Chronos.App.Archiving;
using Chronos.App.Editor;
using Chronos.App.Infrastructure;
using Chronos.Core.Diagnostics;
using Chronos.Core.IO;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data;
using Chronos.Data.Copies;
using Chronos.Data.Homes;
using Chronos.Data.Jobs;
using Chronos.Data.Projects;
using Chronos.Data.Sync;
using Chronos.Protocol;
using Chronos.Protocol.Client;
namespace Chronos.App.Server;
/// Makes the sync connection of a signed-in session; is the key it names the server by ().
public delegate ISyncRemote SyncRemoteFactory(Uri address, string token, IReadOnlyCollection trustedFingerprints, string server);
///
/// Where Download originals puts a server sequence's photos (): the archive's id,
/// and its root when it was made just now (the first download from that server); a null id when there is none to use.
///
public sealed record OriginalsTarget(Guid? ArchiveId, string? Created = null);
///
/// This PC's sync with the signed-in server (BRIEF §4.4, §14.5): the of the index, the
/// connection of upload and download jobs (, also the workspace copies of server projects), a
/// that keeps the table current while a session is open, started and stopped in order as the
/// session changes, the for metered connections, the connections of editors of server sequences
/// (), the server as the home of workspace projects when it keeps projects (owner review 2, R8,
/// phase 5b: , ) and the Export "Sync" of finished renders
/// (). Options come from ,
/// and . A server that announces
/// its stable id (owner review 5, #66) is keyed by it () and recorded in
/// with the address keys it was reached under, which match it all the same
/// (); at a sign-in that tells its id, what this PC keeps under those addresses moves to its id
/// () before the agent starts.
///
public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDisposable
{
/// How long a job resumed at start waits for the saved session to be restored.
private static readonly TimeSpan RestoreWait = TimeSpan.FromSeconds(30);
/// Jobs that write into the workspace or the servers' homes in the cache: the move of the folders to a server's id waits for them.
private static readonly JobKind[] RekeyHeldBy =
[JobKind.WorkspaceCopy, JobKind.WorkspaceRender, JobKind.WorkspaceSend, JobKind.WorkspaceMove, JobKind.Download, JobKind.UploadCommit, JobKind.VideoCopy,
JobKind.Integrate, JobKind.MergeSequences];
private readonly Lock _gate = new();
private Task _switch = Task.CompletedTask;
private Credentials? _credentials;
private Running? _running;
private JobEngine? _renders;
private ICopyEngine? _copies;
/// The alias table of , made again whenever a sign-in records a server.
private ServerIdentity _identity = services.Settings.KnownIdentity();
/// The client project homes of the session share, made at first use for the credentials it holds.
private SharedClient? _projects;
/// Connections over ; screenshots substitute a fake server.
public SyncRemoteFactory RemoteFactory { get; set; } = (address, token, trusted, server) =>
new ChronosSyncRemote(new ChronosClient(address, new CertificatePinning(trusted), $"Chronos ({Environment.MachineName})") { Token = token }, ownsClient: true,
server);
/// Connections of editors of server sequences over ; screenshots substitute a fake server.
public RemoteSequenceApiFactory EditorFactory { get; set; } = (address, token, trusted) =>
new ChronosRemoteSequenceApi(new ChronosClient(address, new CertificatePinning(trusted), $"Chronos ({Environment.MachineName})") { Token = token });
/// The client the session's project homes share (); tests substitute one of their own.
public Func, ChronosClient> ProjectClientFactory { get; set; } = (address, token, trusted) =>
new ChronosClient(address, new CertificatePinning(trusted), $"Chronos ({Environment.MachineName})") { Token = token };
/// The engine; null until the index is open.
public SyncService? Service { get; private set; }
/// The background comparison of the signed-in server; null while signed out.
public SyncAgent? Agent => Volatile.Read(ref _running)?.Agent;
/// The agent's connection, for the table's own calls (conflict resolution); null while signed out.
public ISyncRemote? Remote => Volatile.Read(ref _running)?.Remote;
///
/// Key of the signed-in server: its id key when it announced its id (), else
/// its address key (); null while signed out.
///
public string? Server => Volatile.Read(ref _credentials) is { } c ? c.Key : null;
/// The servers known by id and the address keys each was reached under (any thread).
public ServerIdentity Identity => Volatile.Read(ref _identity);
///
/// A known server id came back with another TLS certificate and another name (owner review 5: a copied server database,
/// two servers sharing one id): the server's name now, and the name it had (UI thread).
///
public event Action? IdentityClash;
/// The key a server reached at goes by: the signed-in one's when it is that server, else its address key mapped through the aliases.
public string KeyOf(Uri address)
{
ArgumentNullException.ThrowIfNull(address);
if (Volatile.Read(ref _credentials) is { } c && c.Address == address) return c.Key;
return Identity.Canonical(SyncService.ServerKey(address));
}
/// The options, and whether the account may change server sequences (without it this PC's edits are not sent).
public SyncPolicy Policy => new(services.Settings.SyncSettingsAutomatically, services.Settings.KeepProxiesOffline, Allows(Permissions.Edit),
services.Settings.CopyLinking);
///
/// The matches engine (owner review 6, #73): found copies here and on the server, compare, integrate, merge; null until
/// the index is open. Setting it raises .
///
public ICopyEngine? Copies
{
get => Volatile.Read(ref _copies);
internal set
{
if (ReferenceEquals(Interlocked.Exchange(ref _copies, value), value)) return;
CopiesChanged?.Invoke();
}
}
/// was set or cleared (any thread).
public event Action? CopiesChanged;
/// The signed-in account may send files (originals, renders) to the server.
public bool CanUpload => Allows(Permissions.Upload);
/// The signed-in account may queue renders on the server.
public bool CanRender => Allows(Permissions.Render);
///
/// The signed-in server keeps its sequences' projects (owner review 2, R8: the projects capability), so Edit opens
/// its sequences as workspace projects; false while signed out and for an older server, whose sequences Edit opens with
/// proxies as before.
///
public bool HasProjects =>
services.Server.IsConnected && services.Server.Flow.Info?.Capabilities is { } capabilities && capabilities.Contains(ServerCapabilities.Projects);
///
/// The highest project settings format the signed-in server reads and renders (owner review 1, phase 4); 1 while signed
/// out and for an older server, which does not announce it. Pushes and renders that need more are held back
/// ().
///
public int SettingsFormat => SettingsFormatOf(services.Server.IsConnected ? services.Server.Flow.Info : null);
/// The settings format a server's server/info announces; 1 when it announces none (an older server).
public static int SettingsFormatOf(ServerInfo? info) => info?.SettingsFormat is > 0 and var format ? format : ProjectSettings.BaseVersion;
/// The home of a workspace project of server sequence ; null while signed out or when the server keeps no projects.
public BundleHome? ServerHome(Guid remoteId) => Server is { } server && HasProjects ? new BundleHome(BundleHome.ServerKind, remoteId, null, server) : null;
///
/// The project home of a workspace project whose home is a server ( over the session's
/// client); null while signed out, signed in to another server, or when the server keeps no projects: the workspace then
/// shows the project offline and its work waits. A home named by an address the signed-in server was reached under before
/// it told its id is that server's all the same (owner review 5).
///
public IProjectHome? ProjectHome(BundleHome home)
{
ArgumentNullException.ThrowIfNull(home);
if (home.IsArchive || !HasProjects || Volatile.Read(ref _credentials) is not { } credentials || !Identity.Same(home.Server, credentials.Key))
return null;
return new ServerProjectHome(SessionClient(credentials), home.SequenceId, services.Server.ServerName, SettingsFormat, credentials.Key);
}
///
/// A project of a sequence of the signed-in server changed there (the server's key, and its projectChanged), or,
/// with null, changes may have been missed while its events were lost, or the session with that server started or
/// stopped (its projects become reachable, or offline) (any thread).
///
public event Action? ProjectChanged;
/// Pauses uploads while the connection is metered and the option is on.
public UploadHold Uploads { get; } = new(services);
/// The agent started, stopped or compared, or the table of the signed-in server changed (any thread).
public event Action? Changed;
///
/// Creates the engine over the open index; the proxy cache lives in the client's cache folder, one home per server
/// (<cache>\servers\<folder>\proxies, owner review 5), read where 1.0 put it until it is moved.
///
public SyncService Open(ChronosDatabase database, Chronos.Data.Archiving.ArchiveService archives, Chronos.Data.Archiving.ArchiveServiceOptions options)
{
var cache = CacheStore.Folder(services.Environment, services.Settings);
Service = new SyncService(database, archives, new SyncOptions
{
Archive = options,
ProxyCacheDirectory = Path.Combine(cache, HomeLayoutMigration.LegacyProxies),
ServersDirectory = CacheStore.ServersFolder(cache),
Identity = () => Identity,
});
Service.Changed += server =>
{
if (Server is { } signedIn && Identity.Same(server, signedIn)) Changed?.Invoke();
};
return Service;
}
/// Follows the server session (UI thread): starts the agent for a new sign-in, stops it on sign-out.
public void Update()
{
var session = services.Server;
Credentials? wanted = session.IsConnected && session.Flow is { Address: { } address, Token: { } token }
? new Credentials(address, token, session.Fingerprints, session.Flow.Info?.ServerId is { } id && id != Guid.Empty ? id : null)
: null;
if (wanted is { ServerId: { } serverId } && session.Flow.Info is { } info) Remember(serverId, wanted.Address, info);
Volatile.Write(ref _credentials, wanted);
_switch = SwitchAsync(_switch, wanted);
if (services.Engine is not { } engine) return;
Uploads.Attach(engine);
if (ReferenceEquals(_renders, engine)) return;
if (_renders is not null) _renders.JobChanged -= OnJobChanged;
_renders = engine;
engine.JobChanged += OnJobChanged;
}
/// The servers known by id were changed elsewhere (Settings › Server › Forget, an import): the alias table follows (UI thread).
public void KnownServersChanged() => Volatile.Write(ref _identity, services.Settings.KnownIdentity());
/// The sign-in uses now (another object once signed in anew); null while signed out.
public object? Session => Volatile.Read(ref _credentials);
/// A connection for the editor of a sequence of the signed-in server; null while signed out.
public IRemoteSequenceApi? ConnectEditor() =>
Volatile.Read(ref _credentials) is { } c ? EditorFactory(c.Address, c.Token, c.Fingerprints) : null;
/// Compares now, once the session switch in progress settled; null while signed out.
public async Task RefreshAsync(CancellationToken ct = default)
{
await _switch.ConfigureAwait(false);
return Agent is { } agent ? await agent.RefreshAsync(ct).ConfigureAwait(false) : null;
}
/// Compares again soon (an option changed, an edit on this PC).
public void RequestRefresh() => Agent?.RequestRefresh();
/// This PC's archive: the one at Settings › Archive's folder, else the only one that is not a server's originals archive.
public async Task CurrentArchiveAsync(CancellationToken ct = default)
{
if (services.Database is not { } db) return null;
var archives = await db.Archives.ListAsync(ct);
if (services.Settings.LastArchivePath is { Length: > 0 } root && CurrentArchive.At(archives, root) is { } current) return current;
var originals = services.Settings.KnownServers.Select(k => k.OriginalsArchiveId).OfType().ToHashSet();
var own = archives.Where(a => !originals.Contains(a.Id)).ToList();
return own.Count == 1 ? own[0] : null;
}
///
/// Where Download originals puts the photos of a server sequence (owner review 5, Q1; UI thread): the archive of its local
/// copy when it has one (a copy keeps receiving where it is), else the signed-in server's own originals archive
/// (), made at the first download beside this PC's archive
/// (: D:\ChronosArchive - Studio NAS, named "Studio NAS (server)").
/// A server that announces no id (an older one) has no home of its own: its originals go to this PC's archive, as in 1.0.
///
/// The originals archive's folder cannot be made.
public async Task OriginalsArchiveAsync(Sequence? local, CancellationToken ct = default)
{
if (local?.ArchiveId is { } held) return new OriginalsTarget(held);
if (services.Database is not { } db || services.Archives is not { } archives) return new OriginalsTarget(null);
if (Volatile.Read(ref _credentials) is not { ServerId: { } id } || services.Settings.FindKnownServer(id) is not { } known)
return new OriginalsTarget((await CurrentArchiveAsync(ct))?.Id);
var listed = await db.Archives.ListAsync(ct);
if (known.OriginalsArchiveId is { } chosen && listed.Any(a => a.Id == chosen)) return new OriginalsTarget(chosen);
var name = known.Name is { Length: > 0 } n ? n : services.Server.ServerName ?? known.LastAddress ?? id.ToString("N");
var mine = await CurrentArchiveAsync(ct);
var root = HomeFolders.OriginalsArchiveRoot(mine?.RootPath ?? services.Settings.LastArchivePath, name, ArchiveLocations.SuggestRoot(),
path => Directory.Exists(path) || File.Exists(path) || CurrentArchive.At(listed, path) is not null);
var made = await archives.CreateAsync(root, Loc.L("Archive.Name.ServerOriginals", name), cancellationToken: ct);
known.OriginalsArchiveId = made.Id;
services.SavePreferences();
Log.Info("sync", $"The originals of {name} ({id}) go to their own archive {made.RootPath}");
return new OriginalsTarget(made.Id, made.RootPath);
}
///
/// The archive Download originals puts a sequence's photos in (; UI thread), with what
/// the user is told: where a new originals archive went, or why there is none (: title, message, is error).
///
public async Task OriginalsForDownloadAsync(Sequence? local, Action notify)
{
ArgumentNullException.ThrowIfNull(notify);
try
{
var target = await OriginalsArchiveAsync(local);
if (target.ArchiveId is null) notify(Loc.L("Errors.Sync.NoArchive"), null, true);
else if (target.Created is { } made) notify(Loc.L("Shell.Homes.Originals"), AppEnvironment.Shown(made), false);
return target.ArchiveId;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidOperationException or ArgumentException or DatabaseException)
{
Log.Warning("sync", "The originals archive of the signed-in server could not be made", ex);
notify(Loc.L("Errors.Sync.NoOriginalsFolder", services.Server.ServerName ?? Server ?? string.Empty), null, true);
return null;
}
}
/// The archives of the known servers' downloaded originals that the index holds (the scan schedule scans them too).
public async Task> OriginalsArchivesAsync(CancellationToken ct = default)
{
if (services.Database is not { } db) return [];
var ids = services.Settings.KnownServers.Select(k => k.OriginalsArchiveId).OfType().ToHashSet();
if (ids.Count == 0) return [];
return [.. (await db.Archives.ListAsync(ct).ConfigureAwait(false)).Where(a => ids.Contains(a.Id))];
}
public async ValueTask ConnectAsync(string server, CancellationToken cancellationToken)
{
// A job resumed at start runs before the saved session is restored: give the restore a moment.
var until = DateTime.UtcNow + RestoreWait;
while (Volatile.Read(ref _credentials) is null && services.Settings.ProtectedToken is not null && DateTime.UtcNow < until)
await Task.Delay(500, cancellationToken);
// A job queued under an address the server was reached under before it told its id reaches it all the same.
if (Volatile.Read(ref _credentials) is not { } c || !Identity.Same(c.Key, server)) return null;
CacheStore.Prepare(CacheStore.Folder(services.Environment, services.Settings));
return RemoteFactory(c.Address, c.Token, c.Fingerprints, c.Key);
}
///
/// Stops the agent once the session switch in progress settled. The app's exit waits for this on the UI thread, so
/// nothing here, in the switch or in the stop continues on it.
///
public async ValueTask DisposeAsync()
{
Volatile.Write(ref _credentials, null);
Uploads.Dispose();
if (_renders is not null) _renders.JobChanged -= OnJobChanged;
await _switch.ConfigureAwait(false);
await StopAsync().ConfigureAwait(false);
lock (_gate)
{
_projects?.Client.Dispose();
_projects = null;
}
}
private bool Allows(Permissions permission) => services.Server.User is { } user && (user.Permissions & (long)permission) != 0;
///
/// The client of ' session, made once for its address and token: another sign-in replaces
/// it (a call still on the old one fails as a server out of reach, and is tried again on the new one).
///
private ChronosClient SessionClient(Credentials credentials)
{
lock (_gate)
{
if (_projects is { } current && current.Address == credentials.Address && current.Token == credentials.Token) return current.Client;
_projects?.Client.Dispose();
var client = ProjectClientFactory(credentials.Address, credentials.Token, credentials.Fingerprints);
_projects = new SharedClient(credentials.Address, credentials.Token, client);
return client;
}
}
///
/// Export "Sync": a render of this PC that asked for it is sent to the server once done (),
/// also when it ends while the session is down: the send is queued for the saved session's server and waits for it.
///
private void OnJobChanged(Job job)
{
if (job is not { Kind: JobKind.Render, State: JobState.Done } || Service is not { } service || services.Database is not { } db || _renders is not { } engine)
return;
var signedIn = Server;
_ = SendRenderAsync(new ExportSync(db, service, engine), job, signedIn ?? SavedServer(), signedIn is null || CanUpload);
}
/// The key of the server this PC is signed in to, else of the saved session's; null without either.
public string? LastServer => Server ?? SavedServer();
/// Key of the server of the saved session (Disconnect keeps it), its id key when it is known by id; null without one.
private string? SavedServer() =>
services.Server.HasSavedSession && ServerAddress.TryParse(services.Settings.ServerUrl, out var address) ? Identity.Canonical(SyncService.ServerKey(address)) : null;
private static async Task SendRenderAsync(ExportSync exports, Job job, string? server, bool canUpload)
{
try
{
switch (await exports.SendAsync(job, server, canUpload))
{
case { Send: { } send }:
Log.Info("sync", $"{job.TargetLabel} will be sent to the server (job {send.Id})");
break;
case { Gate: var gate }:
Log.Warning("sync", $"{job.TargetLabel} asked to be sent to the server but was not ({gate})");
break;
}
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
// Nobody awaits this: the render itself succeeded, and the send can be asked for again on the Server screen.
Log.Warning("sync", $"The render {job.TargetLabel} could not be queued for the server", ex);
}
}
///
/// Records the signed-in server in (UI thread): its id, name, address, certificate,
/// and the address key it was reached by as an alias; the settings are written only when something changed. A known id
/// presented with another certificate and another name is told ().
///
private void Remember(Guid id, Uri address, ServerInfo info)
{
var settings = services.Settings;
var alias = SyncService.ServerKey(address);
var shown = ServerAddress.Display(address);
var known = settings.FindKnownServer(id);
var changed = false;
if (known is null)
{
known = new KnownServer { Id = id, Name = info.Name, LastAddress = shown, Fingerprint = info.TlsFingerprint };
settings.KnownServers.Add(known);
changed = true;
Log.Info("sync", $"Server {info.Name} is known by its id {id} from now on (reached at {shown})");
}
else if (known.Fingerprint is { Length: > 0 } before && info.TlsFingerprint is { Length: > 0 } now
&& !string.Equals(before, now, StringComparison.OrdinalIgnoreCase)
&& !string.Equals(known.Name, info.Name, StringComparison.Ordinal))
{
Log.Warning("sync", $"Server id {id} was {known.Name} with another certificate; it is {info.Name} now: two servers may share one copied database");
IdentityClash?.Invoke(info.Name, known.Name ?? shown);
}
if (!known.Aliases.Contains(alias, StringComparer.OrdinalIgnoreCase))
{
known.Aliases.Add(alias);
changed = true;
}
if (!string.Equals(known.Name, info.Name, StringComparison.Ordinal) || !string.Equals(known.LastAddress, shown, StringComparison.OrdinalIgnoreCase)
|| (info.TlsFingerprint is { Length: > 0 } fingerprint
&& !string.Equals(known.Fingerprint, fingerprint, StringComparison.OrdinalIgnoreCase)))
{
(known.Name, known.LastAddress, known.Fingerprint) = (info.Name, shown, info.TlsFingerprint ?? known.Fingerprint);
changed = true;
}
if (DateTimeOffset.UtcNow - known.LastSeen > TimeSpan.FromHours(1))
{
known.LastSeen = DateTimeOffset.UtcNow;
changed = true;
}
if (!changed) return;
Volatile.Write(ref _identity, settings.KnownIdentity());
services.SavePreferences();
}
///
/// Whatever this PC keeps under the address keys the server of was reached by moves to its
/// id (owner review 5, #66: ), before the agent starts. The links move at
/// once (): the jobs resumed under the id key look them up by it. The
/// folders wait while a job writes into the workspace or the cache, a project is open in the editor, or the workspace
/// is being moved: a later sign-in moves them, and the aliases match meanwhile. A failure is logged: nothing is lost,
/// and the next sign-in goes on.
///
private async Task RekeyAsync(Credentials credentials)
{
if (credentials.ServerId is not { } id || services.Database is not { } db) return;
var aliases = Identity.AliasesOf(id);
if (aliases.Count == 0) return;
try
{
var workspace = services.Workspace;
var migration = new HomeLayoutMigration(new HomeLayoutOptions
{
DataDirectory = services.Environment.DataDirectory,
WorkspaceRoot = workspace.IsEnabled && !workspace.IsMoving ? workspace.Root : null,
CacheRoot = CacheStore.Folder(services.Environment, services.Settings),
Identity = Identity,
Database = db,
});
await migration.RekeyLinksAsync(id, aliases).ConfigureAwait(false);
if (workspace.HasProjectsInEditor || await db.Jobs.HasPendingAsync(RekeyHeldBy).ConfigureAwait(false))
{
Log.Info("sync", $"Server {id}: its folders on this PC move from its addresses to its id at a later sign-in (jobs are under way or a project is open)");
return;
}
await migration.RekeyAsync(id, aliases).ConfigureAwait(false);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or DatabaseException)
{
Log.Warning("sync", $"What this PC keeps under the addresses of server {id} could not all move to its id; the next sign-in goes on", ex);
}
}
///
/// Switches are chained (each waits for the previous one) and continue on the thread pool: a switch still stopping
/// the old agent when the app exits must not need the UI thread its exit blocks.
///
private async Task SwitchAsync(Task previous, Credentials? wanted)
{
await previous.ConfigureAwait(false);
if (Volatile.Read(ref _running) is { } running && wanted is { } same && running.Credentials.Address == same.Address && running.Credentials.Token == same.Token
&& running.Credentials.ServerId == same.ServerId)
return;
var stopped = Volatile.Read(ref _running)?.Remote.Server;
await StopAsync().ConfigureAwait(false);
// The projects of the server left are offline now: their state is read again.
if (stopped is not null) ProjectChanged?.Invoke(stopped, null);
if (wanted is not { } start || Service is not { } service)
{
Changed?.Invoke();
return;
}
await RekeyAsync(start).ConfigureAwait(false);
var remote = RemoteFactory(start.Address, start.Token, start.Fingerprints, start.Key);
var agent = new SyncAgent(service, remote, () => Policy, services.Engine);
agent.Changed += () => Changed?.Invoke();
agent.ProjectChanged += e => ProjectChanged?.Invoke(remote.Server, e);
agent.Start();
Volatile.Write(ref _running, new Running(agent, remote, start));
Log.Info("sync", $"Comparing with {remote.Server}");
Changed?.Invoke();
ProjectChanged?.Invoke(remote.Server, null);
}
private async Task StopAsync()
{
if (Interlocked.Exchange(ref _running, null) is not { } running) return;
await running.Agent.DisposeAsync().ConfigureAwait(false);
running.Remote.Dispose();
Changed?.Invoke();
}
/// The id the server announced (owner review 5); null for an older server.
private sealed record Credentials(Uri Address, string Token, IReadOnlyCollection Fingerprints, Guid? ServerId)
{
/// The key the server goes by ().
public string Key => ServerIdentity.Key(ServerId, Address);
}
/// The client project homes share, and the session it was made for.
private sealed record SharedClient(Uri Address, string Token, ChronosClient Client);
/// The agent of the signed-in session: one reference, so the UI thread never reads half of a switch.
private sealed record Running(SyncAgent Agent, ISyncRemote Remote, Credentials Credentials);
}