Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : // Licensed under the Apache License, Version 2.0
3 : : #pragma once
4 : :
5 : : #include <hpactor/actor/daemon_actor.hpp>
6 : : #include <hpactor/cli/cli_config.hpp>
7 : : #include <hpactor/cli/cli_types.hpp>
8 : : #include <hpactor/cli/command_node.hpp>
9 : : #include <hpactor/cli/line_editor.hpp>
10 : : #include <hpactor/cli/output_formatter.hpp>
11 : : #include <hpactor/cli/pager.hpp>
12 : : #include <hpactor/cli/token.hpp>
13 : : #include <hpactor/types/types.hpp>
14 : :
15 : : #include <chrono>
16 : : #include <memory>
17 : : #include <optional>
18 : : #include <string>
19 : : #include <vector>
20 : :
21 : : namespace hpactor {
22 : :
23 : : class ActorSystem;
24 : :
25 : : namespace cli {
26 : :
27 : : // Forward-declare protobuf types (defined in cli_messages.pb.h)
28 : : class InspectStateReply;
29 : : class KillReply;
30 : : class ListActorsReply;
31 : : class SystemStatsReply;
32 : : class MemoryStatsReply;
33 : :
34 : : class CliActor : public DaemonActor {
35 : : public:
36 : : CliActor(ActorContext* ctx, ActorSystem& system, const CliConfig& config);
37 : :
38 : : // DaemonActor interface
39 : : bool run_once() override;
40 : : void on_daemon_start() override;
41 : : void on_daemon_stop() override;
42 : :
43 : 0 : bool is_system_actor() const override {
44 : 0 : return true;
45 : : }
46 : :
47 : : // Accessors for commands
48 : 0 : ActorSystem& system() {
49 : 0 : return system_;
50 : : }
51 : : const CliConfig& config() const {
52 : : return config_;
53 : : }
54 : : OutputFormatter* formatter() {
55 : : return formatter_.get();
56 : : }
57 : : Pager* pager() {
58 : : return pager_.get();
59 : : }
60 : :
61 : : // Whether the CLI input loop is still running.
62 : : // Set to false by /quit or EOF on stdin.
63 : : bool is_running() const {
64 : : return running_;
65 : : }
66 : :
67 : : // --- Request-Response Helpers ---
68 : : //
69 : : // Send an InspectStateRequest to target and block on the reply.
70 : : // Polls this actor's mailbox on the dedicated thread — safe, no
71 : : // scheduler contention since CliActor uses DispatchPolicy::DedicatedThread.
72 : : std::optional<InspectStateReply> send_and_wait_inspect(
73 : : ActorId target, const class InspectStateRequest& req,
74 : 0 : std::chrono::milliseconds timeout = std::chrono::milliseconds(2000));
75 : :
76 : : std::optional<KillReply> send_and_wait_kill(
77 : : ActorId target, const class KillRequest& req,
78 : 0 : std::chrono::milliseconds timeout = std::chrono::milliseconds(2000));
79 : :
80 : : // Enumerate all known actors. Returns metadata for each.
81 : : std::vector<ActorMeta> enumerate_actors(const std::string& filter = "");
82 : :
83 : : // Resolve the CLI history file path from config.
84 : : // If config.history_path is non-empty, returns it directly.
85 : : // Otherwise returns $HOME/.hpactor_history, falling back to
86 : : // /tmp/.hpactor_history.
87 : : static std::string get_history_path(const CliConfig& config);
88 : :
89 : : private:
90 : : void build_command_tree();
91 : : void execute_tokens(const std::vector<Token>& tokens);
92 : : void print_greeting();
93 : :
94 : : // Poll mailbox for a message with the given TypeTag, ignoring all others.
95 : : // Returns the raw StreamBuffer payload if found before timeout.
96 : : std::optional<StreamBuffer>
97 : : poll_for_response(TypeTag expected_tag, std::chrono::milliseconds timeout);
98 : :
99 : : ActorSystem& system_;
100 : : CliConfig config_;
101 : : LineEditor line_editor_;
102 : : std::unique_ptr<CommandNode> command_tree_;
103 : : std::unique_ptr<OutputFormatter> formatter_;
104 : : std::unique_ptr<Pager> pager_;
105 : : bool running_ = true;
106 : : };
107 : :
108 : : } // namespace cli
109 : : } // namespace hpactor
|