300 lines
11 KiB
C++
300 lines
11 KiB
C++
#pragma once
|
|
|
|
#include "CommandManager.hpp"
|
|
#include "ConfigLoader.hpp"
|
|
#include "LyricsService.hpp"
|
|
#include "MusicManager.hpp"
|
|
#include "NetworkClient.hpp"
|
|
#include "RpcServer.hpp"
|
|
#ifdef ECHOION_WITH_WEB
|
|
#include "echoion/web/HttpServer.hpp"
|
|
#endif
|
|
#include <ConfigManager.hpp>
|
|
#include <ConfigTypes.hpp>
|
|
#include <CoreInterface.hpp>
|
|
#include <Database.hpp>
|
|
#include <RpcTypes.hpp>
|
|
#include <StateBlob.hpp>
|
|
#include <TrackTypes.hpp>
|
|
#include <Types.hpp>
|
|
#include <Void/Async/EventBus.hpp>
|
|
#include <Void/Service/Registry.hpp>
|
|
#include <atomic>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <thread>
|
|
#include <unordered_set>
|
|
#include <utility>
|
|
|
|
namespace Void::Logging
|
|
{
|
|
class MultiLogger;
|
|
}
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
class PluginHost;
|
|
class TeamSpeakDirectoryService;
|
|
|
|
// Main Core implementation of ICoreInterface
|
|
class Core : public Api::ICoreInterface
|
|
{
|
|
public:
|
|
|
|
Core();
|
|
~Core() override;
|
|
|
|
// ICoreInterface - Lifecycle
|
|
Api::Result<void>
|
|
initialize(const std::filesystem::path& configPath) override;
|
|
Api::Result<void> shutdown() override;
|
|
[[nodiscard]] bool isInitialized() const noexcept override;
|
|
|
|
// ICoreInterface - Playback control (decoupled)
|
|
Api::Result<void> play(const std::string& trackId) override;
|
|
Api::Result<void> pause() override;
|
|
Api::Result<void> resume() override;
|
|
Api::Result<void> stop() override;
|
|
Api::Result<void> skip(uint32_t count) override;
|
|
Api::Result<void> seek(std::chrono::milliseconds position) override;
|
|
Api::Result<void> setVolume(float volume) override;
|
|
|
|
// ICoreInterface - Queue management (decoupled)
|
|
Api::Result<uint64_t> queueTrack(const std::string& trackId,
|
|
uint64_t clientId) override;
|
|
Api::Result<void> removeFromQueue(uint64_t queueId) override;
|
|
Api::Result<void> clearQueue() override;
|
|
Api::Result<void> moveInQueue(uint64_t queueId,
|
|
uint32_t newPosition) override;
|
|
|
|
// ICoreInterface - Track info (decoupled)
|
|
[[nodiscard]] Api::Result<std::optional<Api::TrackSummary>>
|
|
getCurrentTrackSummary() const override;
|
|
[[nodiscard]] Api::Result<Api::PlaybackSummary>
|
|
getPlaybackSummary() const override;
|
|
|
|
// ICoreInterface - Playlist (decoupled)
|
|
Api::Result<void> loadPlaylist(const std::string& playlistId,
|
|
uint64_t clientId) override;
|
|
[[nodiscard]] Api::Result<std::vector<std::string>>
|
|
getPlaylistIds() const override;
|
|
|
|
// ICoreInterface - Configuration (decoupled)
|
|
Api::Result<void>
|
|
updateConfigJson(const std::string& configJson) override;
|
|
[[nodiscard]] Api::Result<std::string>
|
|
getConfigJson() const override;
|
|
|
|
// ICoreInterface - Hot reload (decoupled)
|
|
[[nodiscard]] Api::Result<std::string>
|
|
prepareForHotReload() override;
|
|
Api::Result<void>
|
|
restoreAfterHotReload(const std::string& state) override;
|
|
|
|
// ICoreInterface - Decoupled state management
|
|
[[nodiscard]] Api::StateBlob saveState() override;
|
|
Api::Result<void>
|
|
restoreState(const Api::StateBlob& state) override;
|
|
[[nodiscard]] uint32_t getStateFormatVersion() const override;
|
|
[[nodiscard]] bool
|
|
canRestoreStateVersion(uint32_t version) const override;
|
|
|
|
// ICoreInterface - Event handling
|
|
void registerEventCallback(
|
|
const Api::EventCallback& callback) override;
|
|
void unregisterEventCallback(
|
|
const Api::EventCallback& callback) override;
|
|
|
|
// ICoreInterface - Plugin event processing
|
|
Api::Result<void>
|
|
onPluginEvent(const Api::PluginEvent& event) override;
|
|
|
|
// ICoreInterface - Status
|
|
[[nodiscard]] std::string getVersion() const override;
|
|
[[nodiscard]] std::chrono::system_clock::time_point
|
|
getUptime() const override;
|
|
|
|
// ICoreInterface - Plugin interface
|
|
void setPluginInterface(
|
|
Api::PluginInterfacePtr pluginInterface) override;
|
|
void triggerAutoConnect() override;
|
|
|
|
// ICoreInterface - Advanced (for internal Core-to-Core use)
|
|
[[nodiscard]] Api::Result<Api::OpaqueHandle<Api::TrackInfo>>
|
|
getCurrentTrackHandle() const override;
|
|
[[nodiscard]] Api::Result<
|
|
std::vector<Api::OpaqueHandle<Api::QueueEntry>>>
|
|
getQueueHandles() const override;
|
|
[[nodiscard]] Api::Result<Api::AppConfigHandle>
|
|
getConfigHandle() const override;
|
|
Api::Result<void>
|
|
updateConfigFromHandle(Api::AppConfigHandle config) override;
|
|
|
|
// Additional methods for internal use (not in interface)
|
|
void setAudioDataCallback(
|
|
const MusicManager::AudioDataCallback& callback);
|
|
Api::Result<void> reloadConfig() override;
|
|
[[nodiscard]] Api::Result<std::string>
|
|
managePluginsJson(const std::string& requestJson) override;
|
|
|
|
// Internal implementations using full types
|
|
Api::Result<void> playTrack(const Api::TrackInfo& track);
|
|
Api::Result<uint64_t> queueTrackFull(const Api::TrackInfo& track,
|
|
uint64_t clientId);
|
|
[[nodiscard]] Api::Result<std::vector<Api::QueueEntry>>
|
|
getQueue() const;
|
|
[[nodiscard]] Api::Result<std::optional<Api::QueueEntry>>
|
|
getCurrentTrack() const;
|
|
[[nodiscard]] Api::Result<Api::PlaybackState>
|
|
getPlaybackState() const;
|
|
[[nodiscard]] Api::Result<std::vector<Api::PlaylistInfo>>
|
|
getPlaylists() const;
|
|
Api::Result<void> updateConfig(const Api::AppConfig& config);
|
|
[[nodiscard]] Api::Result<Api::AppConfig> getConfig() const;
|
|
[[nodiscard]] Api::Result<Api::HotReloadState>
|
|
prepareForHotReloadFull();
|
|
Api::Result<void>
|
|
restoreAfterHotReloadFull(const Api::HotReloadState& state);
|
|
|
|
// Generate RPC tokens for existing subscribers
|
|
void generateRpcTokensForSubscribers();
|
|
|
|
private:
|
|
|
|
void onMusicEvent(const Api::PluginEvent& event);
|
|
void broadcastEvent(const Api::PluginEvent& event);
|
|
[[nodiscard]] bool isAdmin(uint64_t clientId) const;
|
|
[[nodiscard]] bool isDj(uint64_t clientId) const;
|
|
void updateWhisperList();
|
|
void setupRpcHandlers();
|
|
void setupEventBusSubscriptions() noexcept;
|
|
void applyUrlShorteningConfig();
|
|
|
|
// Fetch an online client's avatar bytes (TS local cache, then the
|
|
// myTeamSpeak URL, then a legacy file-transfer download) and
|
|
// persist them — and the myTS URL — to the database keyed by uid.
|
|
// Returns {contentType, bytes} on success. Web-agnostic; used by
|
|
// both the avatar HTTP provider and the background warmer.
|
|
[[nodiscard]] std::optional<std::pair<std::string, std::string>>
|
|
fetchClientAvatarBytes(const std::string& uid,
|
|
uint64_t clientId);
|
|
// Periodically cache avatars for every online client so they remain
|
|
// available (from the DB) once the user goes offline. Runs on
|
|
// avatarWarmer_.
|
|
void warmAvatarCache();
|
|
|
|
// Build the console + split-file logging stack and install it as
|
|
// the global Void::Log sink (logSink_ must outlive logging).
|
|
void installLogging(const std::filesystem::path& logDirectory,
|
|
const Api::LoggingConfig& config);
|
|
|
|
// Guard helper: ok if initialized, else a ServiceUnavailable error.
|
|
[[nodiscard]] Api::Result<void> requireInitialized() const noexcept;
|
|
|
|
// Components
|
|
std::shared_ptr<NetworkClient> networkClient_;
|
|
std::shared_ptr<LyricsService> lyricsService_;
|
|
std::shared_ptr<MusicManager> musicManager_;
|
|
std::unique_ptr<CommandManager> commandManager_;
|
|
std::shared_ptr<PluginHost> extensionPlugins_;
|
|
std::shared_ptr<TeamSpeakDirectoryService> directoryService_;
|
|
Api::PluginInterfacePtr pluginInterface_;
|
|
|
|
// State
|
|
Api::AppConfig config_;
|
|
std::filesystem::path configPath_;
|
|
std::chrono::system_clock::time_point startTime_;
|
|
std::atomic<bool> initialized_{false};
|
|
// Guards triggerAutoConnect() against duplicate connection
|
|
// attempts (reset on a failed initiation so a retry can run).
|
|
std::atomic<bool> autoConnectInitiated_{false};
|
|
|
|
// Event callbacks
|
|
std::vector<Api::EventCallback> eventCallbacks_;
|
|
mutable std::mutex eventMutex_;
|
|
|
|
// Authenticated RPC client -> stable TS uid + nickname, resolved
|
|
// at token auth. Keys per-user features (favorites, vote-skip).
|
|
struct RpcIdentity {
|
|
std::string uid;
|
|
std::string nickname;
|
|
};
|
|
std::map<uint64_t, RpcIdentity> rpcIdentities_;
|
|
mutable std::mutex rpcIdentityMutex_;
|
|
[[nodiscard]] std::optional<RpcIdentity>
|
|
rpcIdentity(uint64_t clientId) const;
|
|
[[nodiscard]] bool hasLiveTeamSpeakRole(
|
|
uint64_t clientId,
|
|
const std::vector<uint64_t>& configuredGroups) const;
|
|
|
|
// Full identity behind an RPC token: uid, display nickname and
|
|
// admin standing. Shared by token auth and IP autologin.
|
|
struct TokenIdentity {
|
|
std::string uid;
|
|
std::string nickname;
|
|
bool isAdmin = false;
|
|
bool isDj = false;
|
|
};
|
|
[[nodiscard]] std::optional<TokenIdentity>
|
|
identityForToken(const std::string& token);
|
|
// Mark an RPC connection authenticated as `identity` and retain its
|
|
// stable TeamSpeak identity. Roles are checked live for each
|
|
// request.
|
|
void registerRpcAuth(uint64_t clientId,
|
|
const TokenIdentity& identity);
|
|
// Guards RestartWebInterface against overlapping restarts.
|
|
std::atomic<bool> webRestartPending_{false};
|
|
|
|
// Database for persistence
|
|
std::shared_ptr<Api::Database> database_;
|
|
|
|
// Background avatar cache warmer + the uids already cached this
|
|
// session (so we don't re-download an unchanged avatar every
|
|
// cycle).
|
|
std::jthread avatarWarmer_;
|
|
std::unordered_set<std::string> warmedUids_;
|
|
|
|
// RPC server for Desktop/API clients
|
|
std::shared_ptr<RpcServer> rpcServer_;
|
|
|
|
#ifdef ECHOION_WITH_WEB
|
|
// Browser web interface: serves the static dashboard and
|
|
// same-origin RPC bridge. Bot control still terminates at the RPC
|
|
// server.
|
|
std::shared_ptr<Web::HttpServer> webServer_;
|
|
#endif
|
|
|
|
// Composition root: a type-keyed registry of all Core services
|
|
// (logger, config, event bus, network, database, music, rpc).
|
|
// initialize() provides each instance; resolveServices() and other
|
|
// code can require<I>() them by interface instead of reaching for
|
|
// global singletons. The former singletons stay as thin macro
|
|
// shims over the same instances.
|
|
Void::Service::ServiceRegistry registry_;
|
|
|
|
// Application event bus (async): playback/queue/subscription events
|
|
// published by MusicManager and consumed here for RPC broadcast +
|
|
// DB persistence. Owned by Core, provided as a service.
|
|
Void::EventBus::EventBus eventBus_{Void::EventBus::Mode::Async};
|
|
|
|
// Config holder kept in sync with config_ and provided as a service
|
|
// (replaces the former ConfigManager singleton).
|
|
Api::ConfigManager configManager_;
|
|
|
|
// Owns the logging stack installed as the global Void::Log sink;
|
|
// must outlive any logging (i.e. the whole process).
|
|
std::shared_ptr<Void::Logging::MultiLogger> logSink_;
|
|
|
|
public:
|
|
|
|
// Access the service registry (composition root).
|
|
[[nodiscard]] Void::Service::ServiceRegistry& services() noexcept
|
|
{
|
|
return registry_;
|
|
}
|
|
};
|
|
|
|
} // namespace Echoion::Core
|