-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditorCommandServiceHost.cpp
More file actions
421 lines (384 loc) · 21.6 KB
/
Copy pathEditorCommandServiceHost.cpp
File metadata and controls
421 lines (384 loc) · 21.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include "EditorCommandServiceHost.h"
#include "ConsoleCommandSystem.h"
#include "CommandCore/CommandRegistry.h"
#include "CommandCore/CommandResult.h"
#include "../../Engine/CommandService/CommandGateway.h"
#include "../../Engine/CommandService/CommandService.h"
#include "../../Engine/CommandService/JsonValue.h"
#include "CommandResultJson.h" // LC9: 변환·봉투의 단일 정본
#include <atomic>
#include <algorithm>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <sstream>
#include <cmath>
namespace EditorCommandService
{
namespace
{
// ★ LC9: `ToJson` 이 여기 있었다 — 지금은 `CommandResultJson` 하나뿐이다.
//
// 배치 JSONL 이 생기면서 같은 변환이 두 벌이 될 뻔했고, 두 벌이 되는
// 순간 §18 의 "배치 JSONL 과 서비스 JSON 이 같은 schema v1 을 공유한다"
// 는 작성 시점에만 참인 문장이 된다. 변환도 봉투도 한 곳에서 나온다.
using EditorCommandJson::ToJson;
/// §5.3 의 상태 사상.
///
/// ★ 논리 실패를 500 으로 내지 않는다. selftest 가 정직하게 "실패"를
/// 판정한 것과 서버가 망가진 것은 다른 사건이고, 섞으면 클라이언트가
/// 재시도해서는 안 될 것을 재시도한다. 200 + `status:"failed"` 다.
int HttpStatusFor(CommandCore::CommandStatus status, const std::string& code)
{
// §5.3 은 "없는 명령"을 404 로 둔다. 그것은 문법 오류(400)와 다른
// 사건이다 — 400 은 "요청이 잘못됐다"이고 404 는 "그런 것이 없다"라,
// 클라이언트가 재시도할지 이름을 고칠지가 갈린다.
if ("command.unknown" == code) return 404;
switch (status)
{
case CommandCore::CommandStatus::Succeeded: return 200;
case CommandCore::CommandStatus::Failed: return 200;
case CommandCore::CommandStatus::InvalidArguments: return 400;
case CommandCore::CommandStatus::PreconditionsFailed: return 409;
case CommandCore::CommandStatus::Cancelled: return 200;
case CommandCore::CommandStatus::TimedOut: return 200;
case CommandCore::CommandStatus::InternalError: return 500;
}
return 500;
}
CommandService::JsonValue DescriptorToJson(const CommandCore::CommandDescriptor& d)
{
using JV = CommandService::JsonValue;
JV object = JV::Object();
object.Set("name", JV::String(d.canonical));
JV aliases = JV::Array();
for (const std::string& alias : d.aliases) aliases.Append(JV::String(alias));
object.Set("aliases", std::move(aliases));
object.Set("summary", JV::String(d.summary));
object.Set("usage", JV::String(d.usage));
object.Set("cost", JV::String(std::string(CommandCore::ToString(d.cost))));
object.Set("roles", JV::String(std::string(CommandCore::ToString(d.roles))));
object.Set("class", JV::String(std::string(CommandCore::ToString(d.cls))));
object.Set("liveness", JV::String(std::string(CommandCore::ToString(d.liveness))));
object.Set("userCode", JV::Bool(d.executesUserCode));
object.Set("resultBearing", JV::Bool(d.resultBearing));
object.Set("undoable", JV::Bool(d.undoable));
if (d.hasNamedInput)
{
JV schema = JV::Object(), properties = JV::Object(), required = JV::Array();
schema.Set("type", JV::String("object"));
for (const auto& parameter : d.namedParameters)
{
JV field = JV::Object();
if (parameter.kind == "vec3")
{
field.Set("type", JV::String("array")); field.Set("minItems", JV::Int(3)); field.Set("maxItems", JV::Int(3));
JV item = JV::Object(); item.Set("type", JV::String("number")); field.Set("items", std::move(item));
}
else if (parameter.kind == "value")
{
JV kinds = JV::Array();
for (auto kind : {"string", "number", "boolean", "array"}) kinds.Append(JV::String(kind));
field.Set("type", std::move(kinds));
JV item = JV::Object(); item.Set("type", JV::String("number")); field.Set("items", std::move(item));
field.Set("minItems", JV::Int(2)); field.Set("maxItems", JV::Int(4));
}
else
{
field.Set("type", JV::String(parameter.kind));
if (parameter.kind == "string" && !parameter.optional) field.Set("minLength", JV::Int(1));
}
if (parameter.optional && parameter.kind == "string") field.Set("default", JV::String(parameter.defaultValue));
if (parameter.optional && parameter.kind == "integer") field.Set("default", JV::Int(std::stoll(parameter.defaultValue)));
properties.Set(parameter.name, std::move(field));
if (!parameter.optional) required.Append(JV::String(parameter.name));
}
schema.Set("properties", std::move(properties));
schema.Set("required", std::move(required));
schema.Set("additionalProperties", JV::Bool(false));
object.Set("inputSchema", std::move(schema));
}
return object;
}
class EditorGateway final : public CommandService::ICommandGateway
{
public:
bool BuildNamedArguments(const std::string& command, const CommandService::JsonValue& parameters,
std::vector<std::string>& arguments, std::string& error) override
{
using JV = CommandService::JsonValue;
const auto* descriptor = CommandCore::CommandRegistry::Get().Find(command);
if (!descriptor || !descriptor->hasNamedInput || parameters.GetKind() != JV::Kind::Object)
{
error = "This command requires a supported named parameter object";
return false;
}
const auto& parametersSpec = descriptor->namedParameters;
for (const auto& field : parameters.Fields())
if (std::none_of(parametersSpec.begin(), parametersSpec.end(), [&](const auto& p) { return p.name == field.first; }))
{ error = "Unknown parameter: " + field.first; return false; }
const auto isNumber = [](const JV& value) { return value.GetKind() == JV::Kind::Int || value.GetKind() == JV::Kind::Double; };
for (const auto& parameter : parametersSpec)
{
const auto* value = parameters.Find(parameter.name);
if (!value)
{
if (!parameter.optional) { error = "Missing parameter: " + parameter.name; return false; }
if (parameter.kind == "vec3")
{
std::istringstream defaults(parameter.defaultValue); std::string number;
while (defaults >> number) arguments.push_back(number);
}
else arguments.push_back(parameter.defaultValue);
continue;
}
bool valid = true;
if (parameter.kind == "string")
{
valid = value->GetKind() == JV::Kind::String && value->AsString().find('\0') == std::string::npos
&& (parameter.optional || !value->AsString().empty());
if (valid) arguments.push_back(value->AsString());
}
else if (parameter.kind == "integer")
{
if (value->GetKind() == JV::Kind::Int) arguments.push_back(value->Serialize());
else if (value->GetKind() == JV::Kind::Double)
{
const double number = value->AsDouble();
valid = std::isfinite(number) && std::trunc(number) == number && number >= -9223372036854775808.0 && number < 9223372036854775808.0;
if (valid) arguments.push_back(std::to_string(static_cast<int64_t>(number)));
}
else valid = false;
}
else if (parameter.kind == "number")
{ valid = isNumber(*value); if (valid) arguments.push_back(value->Serialize()); }
else if (parameter.kind == "vec3")
{
valid = value->GetKind() == JV::Kind::Array && value->Items().size() == 3;
if (valid) for (const auto& number : value->Items())
{ if (!isNumber(number)) { valid = false; break; } arguments.push_back(number.Serialize()); }
}
else if (parameter.kind == "value")
{
if (value->GetKind() == JV::Kind::String)
{ valid = value->AsString().find('\0') == std::string::npos; if (valid) arguments.push_back(value->AsString()); }
else if (isNumber(*value) || value->GetKind() == JV::Kind::Bool) arguments.push_back(value->Serialize());
else if (value->GetKind() == JV::Kind::Array && value->Items().size() >= 2 && value->Items().size() <= 4)
{
std::string joined;
for (const auto& number : value->Items())
{ if (!isNumber(number)) { valid = false; break; } if (!joined.empty()) joined += ' '; joined += number.Serialize(); }
if (valid) arguments.push_back(std::move(joined));
}
else valid = false;
}
else valid = false;
if (!valid) { error = "Invalid " + parameter.kind + " parameter: " + parameter.name; return false; }
}
return true;
}
CommandService::CommandOutcome Execute(const std::vector<std::string>& arguments,
int timeoutMs) override
{
// 결과를 스레드 사이로 넘긴다. 수신 스레드가 여기서 기다리고,
// GT 가 completion 에서 채워 깨운다.
struct Slot
{
std::mutex mutex;
std::condition_variable ready;
bool done{ false };
CommandCore::CommandResult result;
ConsoleCommandSystem::CommandTiming timing;
};
auto slot = std::make_shared<Slot>();
const bool accepted = ConsoleCommandSystem::Get().EnqueueStructured(arguments,
[slot](const CommandCore::CommandResult& result,
const ConsoleCommandSystem::CommandTiming& timing)
{
// GT 에서 불린다. 값만 옮기고 곧 반환한다.
{
std::lock_guard<std::mutex> guard(slot->mutex);
slot->result = result;
slot->timing = timing;
slot->done = true;
}
slot->ready.notify_one();
},
m_queueCapacity.load(std::memory_order_relaxed));
CommandService::CommandOutcome outcome;
if (!accepted)
{
// 상한에서 거절됐다. 넣지 않았으므로 기다릴 것도 없다 —
// 여기서 곧장 429 를 돌려준다.
outcome.httpStatus = 429;
outcome.status = "error";
outcome.code = "service.queue_full";
outcome.message = "서비스 큐가 상한에 찼다";
outcome.dataJson = "{}";
return outcome;
}
std::unique_lock<std::mutex> lock(slot->mutex);
const bool finished = slot->ready.wait_for(
lock, std::chrono::milliseconds(timeoutMs), [&slot] { return slot->done; });
if (!finished)
{
// ★ 명령을 죽이지 않는다(§5.2).
//
// 이미 시작한 GT 작업을 중간에 끊는 것이 더 위험하다. 지금은
// 응답만 먼저 돌려주고, operationId 로 승격하는 것은 LC5 다.
// `slot` 이 shared_ptr 인 이유가 이것이다 — 나중에 도착한
// completion 이 죽은 스택을 만지면 안 된다.
outcome.httpStatus = 200;
outcome.status = "timed_out";
outcome.code = "command.timeout";
outcome.message = "명령이 " + std::to_string(timeoutMs)
+ "ms 안에 끝나지 않았다(실행은 계속된다)";
outcome.dataJson = "{}";
outcome.timedOut = true;
return outcome;
}
outcome.httpStatus = HttpStatusFor(slot->result.status, slot->result.code);
outcome.status = std::string(CommandCore::ToString(slot->result.status));
outcome.code = slot->result.code;
outcome.message = slot->result.message;
// `data` 는 항상 객체다(§5.2). 값이 없으면 `null` 이 아니라 `{}` 다 —
// 소비자가 `data.frames` 를 읽기 전에 형을 확인하지 않아도 되게 한다.
outcome.dataJson = (CommandCore::CommandData::Kind::Null == slot->result.data.GetKind())
? std::string("{}")
: ToJson(slot->result.data).Serialize();
outcome.queuedMs = slot->timing.queuedMs;
outcome.executedMs = slot->timing.executedMs;
outcome.waitedFrames = slot->timing.waitedFrames;
return outcome;
}
bool IsLongRunning(const std::string& command, bool& outFound) override
{
const CommandCore::CommandDescriptor* descriptor =
CommandCore::CommandRegistry::Get().Find(command);
outFound = (nullptr != descriptor);
if (!outFound) return false;
return CommandCore::CommandCost::Long == descriptor->cost;
}
bool ExecutesUserCode(const std::string& command) override
{
const CommandCore::CommandDescriptor* descriptor =
CommandCore::CommandRegistry::Get().Find(command);
// ★ 없는 명령은 **참으로 답하지 않는다.**
//
// 없는 이름은 404 로 끝나야 하고(§5.3), 여기서 참을 내면 오타
// 하나가 403 이 된다 — 호출자는 이름이 틀린 것을 권한 문제로
// 읽고 플래그를 켜 가며 재시도한다.
return (nullptr != descriptor) && descriptor->executesUserCode;
}
bool ExecuteAsync(const std::vector<std::string>& arguments,
AsyncCompletion onDone) override
{
return ConsoleCommandSystem::Get().EnqueueStructured(arguments,
[onDone](const CommandCore::CommandResult& result,
const ConsoleCommandSystem::CommandTiming& timing)
{
CommandService::CommandOutcome outcome;
outcome.httpStatus = HttpStatusFor(result.status, result.code);
outcome.status = std::string(CommandCore::ToString(result.status));
outcome.code = result.code;
outcome.message = result.message;
outcome.dataJson =
(CommandCore::CommandData::Kind::Null == result.data.GetKind())
? std::string("{}") : ToJson(result.data).Serialize();
outcome.queuedMs = timing.queuedMs;
outcome.executedMs = timing.executedMs;
outcome.waitedFrames = timing.waitedFrames;
onDone(outcome);
},
m_queueCapacity.load(std::memory_order_relaxed));
}
std::size_t QueueDepth() override
{
return ConsoleCommandSystem::Get().ServiceQueueDepth();
}
std::size_t BatchQueueDepth() override
{
return ConsoleCommandSystem::Get().BatchQueueDepth();
}
void SetQueueCapacity(std::size_t capacity) override
{
m_queueCapacity.store(capacity, std::memory_order_relaxed);
}
std::string CommandsJson() override
{
// LC3 의 snapshot 을 그대로 낸다. 정렬도 그쪽이 보장한다 —
// 소비자가 diff 로 비교할 수 있어야 한다.
const CommandCore::CommandRegistry& registry = CommandCore::CommandRegistry::Get();
CommandService::JsonValue root = CommandService::JsonValue::Object();
root.Set("schemaVersion", CommandService::JsonValue::Int(1));
root.Set("count", CommandService::JsonValue::Int(
static_cast<int64_t>(registry.CommandCount())));
CommandService::JsonValue list = CommandService::JsonValue::Array();
for (const CommandCore::CommandDescriptor& d : registry.Sorted())
{
list.Append(DescriptorToJson(d));
}
root.Set("commands", std::move(list));
return root.Serialize();
}
std::string CommandJson(const std::string& name) override
{
const CommandCore::CommandDescriptor* d =
CommandCore::CommandRegistry::Get().Find(name);
if (nullptr == d) return {};
return DescriptorToJson(*d).Serialize();
}
HealthSnapshot Health() override
{
const ConsoleCommandSystem::ServiceStatus status =
ConsoleCommandSystem::Get().SnapshotStatus();
HealthSnapshot health;
health.role = "editor";
health.frame = status.frame;
health.queueDepth = status.serviceQueueDepth;
health.batchQueueDepth = status.batchQueueDepth;
health.oldestQueuedMs = status.oldestQueuedMs;
health.currentCommand = status.currentCommand;
health.state = status.executing ? "busy" : "idle";
// 막혀 있음을 "지연"이 아니라 "상태"로 낸다(§7.3).
//
// ★ 실행 중이 아닌 정지도 정지다. `wait N` 과 씬 로딩은 `Pump()`
// 를 조기 반환시켜 서비스 큐를 통째로 세운다 — 그 동안
// `executing` 은 거짓이라, 이 세 갈래가 없으면 "idle"만 나간다.
if (status.executing && !status.currentCommand.empty())
{
health.blockedReason = "command.running:" + status.currentCommand;
}
else if (status.sceneLoading)
{
health.state = "blocked";
health.blockedReason = "scene.loading";
}
else if (status.waitFramesRemaining > 0)
{
health.state = "blocked";
health.blockedReason = "batch.wait:"
+ std::to_string(status.waitFramesRemaining);
}
return health;
}
private:
/// 서비스가 `Start` 에서 알려 주는 큐 상한. 0 이면 무제한이다.
/// 수신 스레드 여럿이 읽으므로 원자적이어야 한다.
std::atomic<std::size_t> m_queueCapacity{ 0 };
};
EditorGateway& Gateway() { static EditorGateway gateway; return gateway; }
CommandService::Service& Instance() { static CommandService::Service service; return service; }
}
bool Start(const std::string& projectRoot, bool allowUserCode, std::string& outError)
{
CommandService::ServiceConfig config;
config.role = "editor";
config.projectRoot = projectRoot;
config.allowUserCode = allowUserCode;
return Instance().Start(config, Gateway(), outError);
}
void Stop() noexcept { Instance().Stop(); }
bool IsRunning() noexcept { return Instance().IsRunning(); }
uint16_t Port() noexcept { return Instance().Port(); }
}