Files
OrcaSlicer/src/slic3r/Utils/NetworkAgentFactory.hpp
T
HanifKoh 84657ff11e Add Missing Includes Across the Remaining Sources and Tests (#16071)
* Ignore Clipper, libpng, mcut and Boost.Polygon Internals in clang-tidy

Each only works through a wrapper or umbrella header: libslic3r/clipper.hpp or clipper_z.hpp configure Clipper before including it, png.h pulls in libpng's config headers, and Boost.Polygon's headers only compile through polygon.hpp or voronoi.hpp.

* Ignore minilzo's Config Headers in clang-tidy

lzoconf.h and lzodefs.h are internal to minilzo.h, which is what the code includes.

* Add Missing Includes Across the Remaining Sources and Tests

Covers src/slic3r/Utils, src/slic3r/plugin, src/slic3r/Config, src/libvgcode, src/dev-utils, src/OrcaSlicer.cpp and tests/, the directories left after src/slic3r/GUI and src/libslic3r. Generated with clang-tidy misc-include-cleaner. libvgcode's own headers are included by relative path as in the rest of that library, and Catch2 and pybind11 with angle brackets as elsewhere in the repo.

* Make the GUI and Test Headers Compile on Their Own

Each now includes, or forward-declares, what it uses instead of relying on what its includers happened to include first. Headers that only compile on one platform, or that nothing built includes, are left alone.

* Keep Windows and nanosvg Setup Ahead of the Added Includes

OrcaSlicer.cpp and several tests set _WIN32_WINNT, WIN32_LEAN_AND_MEAN or NOMINMAX before including Windows.h, and the profile validator defines NANOSVG_IMPLEMENTATION before any libslic3r header. The added includes had landed above those blocks, which broke the Windows build.

* Add the GUI Includes the First Pass Missed

Covers headers that only became editable once they compiled on their own, and wx symbols whose suggested header changed as the clang-tidy ignore list grew after the src/slic3r/GUI pass.

* Keep the Added Test Includes Below the NOMINMAX Guard

test_marchingsquares.cpp and test_texture_displacement.cpp had includes inside #ifndef NOMINMAX, which the tests inherit as defined on Windows from libslic3r, so those were skipped there. .clang-tidy also ignores the MSVC STL and UCRT internals, Boost.Multiprecision's fwd.hpp and CPython's Windows include directory, as in #16068.
2026-10-03 13:45:21 +08:00

198 lines
7.9 KiB
C++

#ifndef __NETWORK_AGENT_FACTORY_HPP__
#define __NETWORK_AGENT_FACTORY_HPP__
#include "CloudProvider.hpp"
#include "ICloudServiceAgent.hpp"
#include "IPrinterAgent.hpp"
#include "NetworkAgent.hpp"
#include "OrcaCloudServiceAgent.hpp"
#include "BBLCloudServiceAgent.hpp"
#include "BBLNetworkPlugin.hpp"
#include "libslic3r/AppConfig.hpp"
#include <memory>
#include <string>
#include <functional>
#include <utility>
#include <vector>
namespace Slic3r {
static constexpr char ORCA_PRINTER_AGENT_ID[] = "orca";
static constexpr char BBL_PRINTER_AGENT_ID[] = "bbl";
// Factory function type for creating printer agents
using PrinterAgentFactory =
std::function<std::shared_ptr<IPrinterAgent>(std::shared_ptr<ICloudServiceAgent> cloud_agent, const std::string& log_dir)>;
// Information about a registered printer agent
struct PrinterAgentInfo
{
std::string id; // Registry/config key, e.g. "orca" or a plugin AgentInfo::id
std::string display_name; // e.g., "Orca Native", "Bambu Lab"
std::string plugin_identifier; // Empty for built-ins, otherwise <plugin_key>;<uuid>;<capability_name>
PrinterAgentFactory factory; // Function to create the agent
bool is_plugin() const { return !plugin_identifier.empty(); }
PrinterAgentInfo(const std::string& id_, const std::string& display_name_, PrinterAgentFactory factory_)
: id(id_), display_name(display_name_), factory(std::move(factory_))
{}
PrinterAgentInfo(const std::string& id_, const std::string& display_name_, const std::string& plugin_identifier, PrinterAgentFactory factory_)
: id(id_), display_name(display_name_), plugin_identifier(plugin_identifier), factory(std::move(factory_))
{}
};
/**
* NetworkAgentFactory - Factory for creating network agent instances
*
* This factory creates cloud agents and printer agents for the networking subsystem.
* The architecture separates cloud services (authentication, project sync) from
* printer communication (device discovery, print jobs).
*
* Startup flow:
* 1. Call register_all_agents() during app initialization
* 2. Cloud agent created at startup via create_agent_from_config()
* 3. Printer agent created on-demand when a printer is selected
*
* Usage:
* // At app startup (before any agent creation)
* NetworkAgentFactory::register_all_agents();
*
* // Create NetworkAgent with cloud agent only
* auto agent = create_agent_from_config(log_dir, app_config);
*
* // When printer is selected - create printer agent from registry
* auto printer = NetworkAgentFactory::create_printer_agent_by_id("orca", cloud, log_dir);
*/
class NetworkAgentFactory
{
public:
// ========================================================================
// Printer Agent Registry
// ========================================================================
/**
* Register all built-in printer agents.
* Must be called once during application initialization, before any
* calls to get_registered_printer_agents() or create_printer_agent_by_id().
*/
static void register_all_agents();
/**
* Register a printer agent type
*
* @param id Unique identifier for the agent (e.g., "orca", "bbl")
* @param display_name Human-readable name for UI
* @param factory Factory function to create the agent
* @return true if registration succeeded, false if already registered
*/
static bool register_printer_agent(const std::string& id, const std::string& display_name, PrinterAgentFactory factory);
/**
* Check if an agent registry/config key is registered
*/
static bool is_printer_agent_registered(const std::string& id);
/**
* Get info about a registered agent by registry/config key
*/
static const PrinterAgentInfo* get_printer_agent_info(const std::string& id);
/**
* Return the full plugin reference for a plugin-backed printer agent ID, or empty for built-ins.
*/
static std::string get_printer_agent_plugin_identifier(const std::string& id);
/**
* Get all registered printer agents (for UI population)
*/
static std::vector<PrinterAgentInfo> get_registered_printer_agents();
/**
* Create a printer agent by registry/config key
*
* Returns a cached instance if one exists for the given key, otherwise
* creates a new agent via the registered factory and caches it.
*
* @param id Agent registry/config key to create
* @param cloud_agent Cloud agent for token access
* @param log_dir Directory for log files
* @return Shared pointer to IPrinterAgent, or nullptr if ID not found
*/
static std::shared_ptr<IPrinterAgent> create_printer_agent_by_id(const std::string& id,
std::shared_ptr<ICloudServiceAgent> cloud_agent,
const std::string& log_dir);
/**
* Clear the printer agent cache.
* Calls disconnect_printer() on each cached agent and releases all shared_ptrs.
* Should be called during application shutdown before destroying the NetworkAgent.
*/
static void clear_printer_agent_cache();
// ========================================================================
// Cloud Agent Factory
// ========================================================================
/**
* Create a cloud service agent based on provider type.
* Handles authentication, project sync, and other cloud services.
*
* @param provider Which implementation to use (Orca or BBL)
* @param log_dir Directory for log files
* @return Shared pointer to ICloudServiceAgent implementation
*/
static std::shared_ptr<ICloudServiceAgent> create_cloud_agent(const std::string& provider, const std::string& log_dir)
{
if (provider == ORCA_CLOUD_PROVIDER) {
return std::make_shared<OrcaCloudServiceAgent>(log_dir);
} else if (provider == BBL_CLOUD_PROVIDER) {
auto& plugin = BBLNetworkPlugin::instance();
if (!plugin.is_loaded()) {
return nullptr;
}
if (!plugin.has_agent()) {
plugin.create_agent(log_dir);
}
if (!plugin.has_agent()) {
return nullptr;
}
return std::make_shared<BBLCloudServiceAgent>();
}
return nullptr;
}
// Plugin printer agents
static void register_python_plugin(const std::string& plugin_key);
static void deregister_python_plugin(const std::string& plugin_key);
static void register_python_printer_agent(const std::string& plugin_key, const std::string& capability_name);
static void deregister_python_printer_agent(const std::string& plugin_key, const std::string& capability_name);
private:
// Factory is not instantiable
NetworkAgentFactory() = delete;
~NetworkAgentFactory() = delete;
NetworkAgentFactory(const NetworkAgentFactory&) = delete;
NetworkAgentFactory& operator=(const NetworkAgentFactory&) = delete;
};
/**
* Create a NetworkAgent from AppConfig settings (main entry point)
*
* Creates a NetworkAgent with cloud agent only. The printer agent is created
* separately when a printer is selected, via create_printer_agent_by_id().
*
* Cloud provider: Always creates OrcaCloudServiceAgent as the primary provider.
* Third-party cloud agents (e.g., Bambu) are created from the cloud_providers
* AppConfig setting and added via NetworkAgent::add_cloud_agent().
*
* @param log_dir Directory for log files
* @param app_config Application configuration object
* @return NetworkAgent with cloud agent, or nullptr on failure
*/
std::unique_ptr<NetworkAgent> create_agent_from_config(const std::string& log_dir, AppConfig* app_config);
} // namespace Slic3r
#endif // __NETWORK_AGENT_FACTORY_HPP__