-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRpcModel.cs
More file actions
205 lines (182 loc) · 5.6 KB
/
Copy pathRpcModel.cs
File metadata and controls
205 lines (182 loc) · 5.6 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Coder.Desktop.App.Converters;
using Coder.Desktop.Vpn.Proto;
namespace Coder.Desktop.App.Models;
public enum RpcLifecycle
{
Disconnected,
Connecting,
Connected,
}
public enum VpnLifecycle
{
Unknown,
Stopped,
Starting,
Started,
Stopping,
}
public enum VpnStartupStage
{
Unknown,
Initializing,
Downloading,
Finalizing,
}
public class VpnDownloadProgress
{
public ulong BytesWritten { get; set; } = 0;
public ulong? BytesTotal { get; set; } = null; // null means unknown total size
public double Progress
{
get
{
if (BytesTotal is > 0)
{
return (double)BytesWritten / BytesTotal.Value;
}
return 0.0;
}
}
public override string ToString()
{
// TODO: it would be nice if the two suffixes could match
var s = FriendlyByteConverter.FriendlyBytes(BytesWritten);
if (BytesTotal != null)
s += $" of {FriendlyByteConverter.FriendlyBytes(BytesTotal.Value)}";
else
s += " of unknown";
if (BytesTotal != null)
s += $" ({Progress:0%})";
return s;
}
public VpnDownloadProgress Clone()
{
return new VpnDownloadProgress
{
BytesWritten = BytesWritten,
BytesTotal = BytesTotal,
};
}
public static VpnDownloadProgress FromProto(StartProgressDownloadProgress proto)
{
return new VpnDownloadProgress
{
BytesWritten = proto.BytesWritten,
BytesTotal = proto.HasBytesTotal ? proto.BytesTotal : null,
};
}
}
public class VpnStartupProgress
{
public const string DefaultStartProgressMessage = "Starting Coder Connect...";
// Scale the download progress to an overall progress value between these
// numbers.
private const double DownloadProgressMin = 0.05;
private const double DownloadProgressMax = 0.80;
public VpnStartupStage Stage { get; init; } = VpnStartupStage.Unknown;
public VpnDownloadProgress? DownloadProgress { get; init; } = null;
// 0.0 to 1.0
public double Progress
{
get
{
switch (Stage)
{
case VpnStartupStage.Unknown:
case VpnStartupStage.Initializing:
return 0.0;
case VpnStartupStage.Downloading:
var progress = DownloadProgress?.Progress ?? 0.0;
return DownloadProgressMin + (DownloadProgressMax - DownloadProgressMin) * progress;
case VpnStartupStage.Finalizing:
return DownloadProgressMax;
default:
throw new ArgumentOutOfRangeException();
}
}
}
public override string ToString()
{
switch (Stage)
{
case VpnStartupStage.Unknown:
case VpnStartupStage.Initializing:
return DefaultStartProgressMessage;
case VpnStartupStage.Downloading:
var s = "Downloading Coder Connect binary...";
if (DownloadProgress is not null)
{
s += "\n" + DownloadProgress;
}
return s;
case VpnStartupStage.Finalizing:
return "Finalizing Coder Connect startup...";
default:
throw new ArgumentOutOfRangeException();
}
}
public VpnStartupProgress Clone()
{
return new VpnStartupProgress
{
Stage = Stage,
DownloadProgress = DownloadProgress?.Clone(),
};
}
public static VpnStartupProgress FromProto(StartProgress proto)
{
return new VpnStartupProgress
{
Stage = proto.Stage switch
{
StartProgressStage.Initializing => VpnStartupStage.Initializing,
StartProgressStage.Downloading => VpnStartupStage.Downloading,
StartProgressStage.Finalizing => VpnStartupStage.Finalizing,
_ => VpnStartupStage.Unknown,
},
DownloadProgress = proto.Stage is StartProgressStage.Downloading ?
VpnDownloadProgress.FromProto(proto.DownloadProgress) :
null,
};
}
}
public class RpcModel
{
public RpcLifecycle RpcLifecycle { get; set; } = RpcLifecycle.Disconnected;
private VpnLifecycle _vpnLifecycle;
public VpnLifecycle VpnLifecycle
{
get => _vpnLifecycle;
set
{
if (_vpnLifecycle != value && value == VpnLifecycle.Starting)
// Reset the startup progress when the VPN lifecycle changes to
// Starting.
_vpnStartupProgress = null;
_vpnLifecycle = value;
}
}
// Nullable because it is only set when the VpnLifecycle is Starting
private VpnStartupProgress? _vpnStartupProgress;
public VpnStartupProgress? VpnStartupProgress
{
get => VpnLifecycle is VpnLifecycle.Starting ? _vpnStartupProgress ?? new VpnStartupProgress() : null;
set => _vpnStartupProgress = value;
}
public IReadOnlyList<Workspace> Workspaces { get; set; } = [];
public IReadOnlyList<Agent> Agents { get; set; } = [];
public RpcModel Clone()
{
return new RpcModel
{
RpcLifecycle = RpcLifecycle,
VpnLifecycle = VpnLifecycle,
VpnStartupProgress = VpnStartupProgress?.Clone(),
Workspaces = Workspaces,
Agents = Agents,
};
}
}