-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathQueueTap.cs
More file actions
153 lines (141 loc) · 5.36 KB
/
Copy pathQueueTap.cs
File metadata and controls
153 lines (141 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
namespace CMDownloaderUI
{
// Minimal queue mirror for the web UI (thread-safe)
internal static class QueueTap
{
private static readonly object _lock = new();
// Very small data model for the UI
public sealed class QItem
{
public string id { get; init; } = Guid.NewGuid().ToString("n"); // keep init-only
public string kind { get; set; } = ""; // was init; → now set;
public string name { get; set; } = ""; // was init; → now set;
public string host { get; set; } = ""; // was init; → now set;
public string state { get; set; } = "Queued";
public int pct { get; set; } // 0..100
public double bps { get; set; } // bytes/sec
public bool ok { get; set; }
}
public static class QueueTapBridge
{
/// <summary>
/// Mirror per-file progress to the web queue.
/// Call from your existing per-file progress UI update.
/// </summary>
public static void MirrorProgress(string idOrPath, int value, int max, double bytesPerSecond = 0)
{
try
{
if (max <= 0) return;
var pct = (int)Math.Clamp((value * 100.0) / max, 0, 100);
var id = idOrPath;
if (string.IsNullOrEmpty(id)) id = Guid.NewGuid().ToString("n");
CMDownloaderUI.QueueTap.UpdateWorking(id, pct, bytesPerSecond);
}
catch { /* never break worker */ }
}
}
// Internal storage
private static readonly Dictionary<string, QItem> _items = new(); // by id
private static readonly LinkedList<string> _qQueued = new();
private static readonly LinkedList<string> _qWorking = new();
private static readonly LinkedList<string> _qDone = new();
// ----- Public helpers you can call from your code (optional, add gradually) -----
// Add or update a queued item
public static void UpsertQueued(string id, string kind, string name, string host)
{
lock (_lock)
{
if (!_items.TryGetValue(id, out var it))
{
it = new QItem { id = id, kind = kind, name = name, host = host, state = "Queued" };
_items[id] = it;
_qQueued.AddLast(id);
}
else
{
it.kind = kind; it.name = name; it.host = host; it.state = "Queued"; it.pct = 0; it.bps = 0;
MoveRef(id, _qWorking, _qQueued); // ensure in correct list
MoveRef(id, _qDone, _qQueued);
}
}
}
// Mark as working (pct/bps optional)
public static void MoveToWorking(string id, int pct = 0, double bps = 0)
{
lock (_lock)
{
if (_items.TryGetValue(id, out var it))
{
it.state = "Working"; it.pct = pct; it.bps = bps;
MoveRef(id, _qQueued, _qWorking);
MoveRef(id, _qDone, _qWorking);
}
}
}
// Update progress
public static void UpdateWorking(string id, int pct, double bps)
{
lock (_lock)
{
if (_items.TryGetValue(id, out var it))
{
it.pct = Math.Clamp(pct, 0, 100);
it.bps = bps;
}
}
}
// Mark as done
public static void MoveToDone(string id, bool ok = true)
{
lock (_lock)
{
if (_items.TryGetValue(id, out var it))
{
it.state = "Done"; it.ok = ok; it.pct = 100; it.bps = 0;
MoveRef(id, _qQueued, _qDone);
MoveRef(id, _qWorking, _qDone);
}
}
}
// Clear everything (optional)
public static void Reset()
{
lock (_lock) { _items.Clear(); _qQueued.Clear(); _qWorking.Clear(); _qDone.Clear(); }
}
// Snapshot for /api/queue
public static object Snapshot()
{
lock (_lock)
{
QItem[] Map(LinkedList<string> ll)
{
var list = new List<QItem>(ll.Count);
foreach (var id in ll)
if (_items.TryGetValue(id, out var it)) list.Add(it);
return list.ToArray();
}
return new
{
queued = Map(_qQueued),
working = Map(_qWorking),
done = Map(_qDone)
};
}
}
// helper: move id between lists
private static void MoveRef(string id, LinkedList<string> from, LinkedList<string> to)
{
for (var n = from.First; n != null; n = n.Next)
{
if (n.Value == id) { from.Remove(n); break; }
}
// ensure not already in 'to'
for (var n = to.First; n != null; n = n.Next)
if (n.Value == id) return;
to.AddLast(id);
}
}
}