RynthDreamWeave: launch request state
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5e732cecb7
commit
1eb8bf1e8a
3 changed files with 189 additions and 1 deletions
|
|
@ -0,0 +1,90 @@
|
|||
// The launch request state machine for the website buttons: one "/weblogin" at a time, a 10 s
|
||||
// reply window, and a status line for the card. Pure (no engine calls), so RynthServerPack.Tests
|
||||
// compiles it directly. The code is handed to the caller through OnChatLine and goes nowhere
|
||||
// else: never into Status, never into the message, never into a field.
|
||||
using System;
|
||||
|
||||
namespace RynthCore.Plugin.RynthDreamWeave;
|
||||
|
||||
internal enum LaunchBegin { Started, Busy, TooSoon }
|
||||
internal enum LaunchLine { NotOurs, Code, Refused }
|
||||
|
||||
internal sealed class WebLauncher
|
||||
{
|
||||
/// <summary>Held by the chat pump and the render thread around every call.</summary>
|
||||
public readonly object Gate = new();
|
||||
|
||||
private WebDestination? _pending;
|
||||
private long _deadlineMs;
|
||||
private long _lastBeginMs;
|
||||
private bool _everBegun;
|
||||
|
||||
public WebDestination? Pending => _pending;
|
||||
public string Status { get; private set; } = "";
|
||||
|
||||
/// <summary>Busy while a request is pending (checked first); else TooSoon within MinIntervalMs of the last Begin.</summary>
|
||||
public LaunchBegin Begin(WebDestination d, long nowMs)
|
||||
{
|
||||
if (_pending != null) return LaunchBegin.Busy;
|
||||
if (_everBegun && nowMs - _lastBeginMs < LaunchWire.MinIntervalMs) return LaunchBegin.TooSoon;
|
||||
_pending = d;
|
||||
_everBegun = true;
|
||||
_lastBeginMs = nowMs;
|
||||
_deadlineMs = nowMs + LaunchWire.ReplyTimeoutMs;
|
||||
Status = "Asking DreamWeave for a login code for " + d.Label + "...";
|
||||
return LaunchBegin.Started;
|
||||
}
|
||||
|
||||
/// <summary>The send failed: nothing was asked, so nothing is pending.</summary>
|
||||
public void Cancel(string status)
|
||||
{
|
||||
_pending = null;
|
||||
Status = status;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NotOurs unless a request is pending and the deadline has not passed. Code and Refused clear
|
||||
/// Pending and the caller eats the line. On Code, <paramref name="message"/> is empty and the
|
||||
/// code is only in <paramref name="code"/>.
|
||||
/// </summary>
|
||||
public LaunchLine OnChatLine(string text, int chatType, long nowMs, out WebDestination? dest, out string code, out string message)
|
||||
{
|
||||
dest = null;
|
||||
code = "";
|
||||
message = "";
|
||||
WebDestination? p = _pending;
|
||||
if (p == null || nowMs >= _deadlineMs) return LaunchLine.NotOurs;
|
||||
if (!LaunchWire.TryParse(text, chatType, out string parsedCode, out string refusal)) return LaunchLine.NotOurs;
|
||||
|
||||
_pending = null;
|
||||
dest = p;
|
||||
if (parsedCode.Length > 0)
|
||||
{
|
||||
code = parsedCode;
|
||||
Status = "Opening " + p.Label + "...";
|
||||
return LaunchLine.Code;
|
||||
}
|
||||
message = refusal;
|
||||
Status = p.Label + ": DreamWeave said: " + refusal;
|
||||
return LaunchLine.Refused;
|
||||
}
|
||||
|
||||
/// <summary>The pending destination once, at or after Begin + ReplyTimeoutMs; null otherwise.</summary>
|
||||
public WebDestination? TakeTimedOut(long nowMs)
|
||||
{
|
||||
WebDestination? p = _pending;
|
||||
if (p == null || nowMs < _deadlineMs) return null;
|
||||
_pending = null;
|
||||
Status = "No reply from DreamWeave to " + LaunchWire.RequestCommand + " within "
|
||||
+ (LaunchWire.ReplyTimeoutMs / 1000) + " s, so nothing was opened.";
|
||||
return p;
|
||||
}
|
||||
|
||||
/// <summary>Login/logout: forget the request and the status.</summary>
|
||||
public void Reset()
|
||||
{
|
||||
_pending = null;
|
||||
Status = "";
|
||||
_everBegun = false;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue