mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-11-27 15:49:52 -05:00
Everything that drives OrcaSlicer by command (the dispatch of the command catalog, jobs, the journal, replays, Python scripts, recordings) lived inside the MCP server's service class. So Action Replay, the Run Script panel, a plugin's tool call, a person's recording and a run from the command line each had to start "the MCP service", with all of its GUI hooks, although none of them needs a server or an agent. The automation runtime is now a subsystem of its own. It starts the first time a front end asks for it and knows none of them. The MCP endpoint (HTTP, the protocol, the token, the discovery file, the takeover of a port another window holds) is one front end beside the others, opened and closed without stopping what the others run. The widget inspector, menu capture and ImGui item recording are turned on only by a front end that drives the GUI, so a person's recording or long command does not carry them. The part of the runtime that needs no GUI is the library libslic3r_automation, built without wxWidgets and tested by its own suite, automation_tests. A wx dependency in it fails to build, and its tests no longer link the GUI library or the embedded Python. The command line has one option set for this: --run FILE runs a replay or a Python script once the GUI is ready, with --outputdir for its files, and --unrestricted-scripts and --cad turn their settings on for one run without saving them. --mcp-run, --mcp-run-out and --mcp-token are gone; the scripts setting is python_scripts_unrestricted and a session's output folder is <datadir>/automation/out. get_app_state reports ready and version at the top level, and its mcp block describes the endpoint only. No change to slicing output.
109 lines
3.0 KiB
C++
109 lines
3.0 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include "slic3r/GUI/Mcp/McpToken.hpp"
|
|
#include "test_utils.hpp"
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
#include <boost/filesystem/path.hpp>
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <string>
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#include <tlhelp32.h>
|
|
#else
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
using namespace Slic3r::GUI::Mcp;
|
|
namespace fs = boost::filesystem;
|
|
|
|
namespace {
|
|
|
|
// Above every platform's pid range, so no process has it.
|
|
constexpr int kNoSuchPid = 0x7FFFFFF0;
|
|
|
|
int this_pid()
|
|
{
|
|
#ifdef _WIN32
|
|
return int(GetCurrentProcessId());
|
|
#else
|
|
return int(getpid());
|
|
#endif
|
|
}
|
|
|
|
// A running process other than this one: the one that started the test run.
|
|
int other_running_pid()
|
|
{
|
|
#ifdef _WIN32
|
|
const DWORD self = GetCurrentProcessId();
|
|
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
PROCESSENTRY32W entry{};
|
|
entry.dwSize = sizeof(entry);
|
|
DWORD parent = 0;
|
|
for (BOOL more = Process32FirstW(snapshot, &entry); more; more = Process32NextW(snapshot, &entry))
|
|
if (entry.th32ProcessID == self) {
|
|
parent = entry.th32ParentProcessID;
|
|
break;
|
|
}
|
|
CloseHandle(snapshot);
|
|
return int(parent);
|
|
#else
|
|
return int(getppid());
|
|
#endif
|
|
}
|
|
|
|
void write_server(const fs::path& datadir, int port, int pid)
|
|
{
|
|
std::string err;
|
|
REQUIRE(write_discovery(datadir, {{"port", port}, {"pid", pid}}, err));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("the discovery file is removed only by the process that wrote it", "[Mcp]")
|
|
{
|
|
ScopedTemporaryDir datadir("orca-mcp");
|
|
|
|
SECTION("another window's file stays")
|
|
{
|
|
write_server(datadir.path(), 13619, other_running_pid());
|
|
remove_discovery(datadir.path());
|
|
CHECK(fs::exists(discovery_path(datadir.path())));
|
|
}
|
|
SECTION("this window's file goes")
|
|
{
|
|
write_server(datadir.path(), 13619, this_pid());
|
|
remove_discovery(datadir.path());
|
|
CHECK_FALSE(fs::exists(discovery_path(datadir.path())));
|
|
}
|
|
SECTION("a file that names no owner goes")
|
|
{
|
|
std::string err;
|
|
mcp_dir(datadir.path());
|
|
REQUIRE(write_private_file(discovery_path(datadir.path()), "not json", err));
|
|
remove_discovery(datadir.path());
|
|
CHECK_FALSE(fs::exists(discovery_path(datadir.path())));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("a port is held by another window only when the discovery file names it and a running process", "[Mcp]")
|
|
{
|
|
ScopedTemporaryDir datadir("orca-mcp");
|
|
const int port = 13619;
|
|
|
|
CHECK_FALSE(discovery_held_by_other(datadir.path(), port));
|
|
|
|
write_server(datadir.path(), port, other_running_pid());
|
|
CHECK(discovery_held_by_other(datadir.path(), port));
|
|
CHECK_FALSE(discovery_held_by_other(datadir.path(), port + 1));
|
|
|
|
write_server(datadir.path(), port, this_pid());
|
|
CHECK_FALSE(discovery_held_by_other(datadir.path(), port));
|
|
|
|
// A window that crashed leaves its file behind.
|
|
write_server(datadir.path(), port, kNoSuchPid);
|
|
CHECK_FALSE(discovery_held_by_other(datadir.path(), port));
|
|
}
|