Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #pragma once
16 : :
17 : : #include <hpactor/core/actor_system_ids.hpp>
18 : : #include <hpactor/ref/actor_address.hpp>
19 : : #include <hpactor/ref/actor_ref.hpp>
20 : : #include <hpactor/types/types.hpp>
21 : :
22 : : #include <condition_variable>
23 : : #include <memory>
24 : : #include <mutex>
25 : : #include <variant>
26 : :
27 : : namespace hpactor {
28 : :
29 : : // -----------------------------------------------------------------------------
30 : : // Spawn-specific error codes (separate from general errors namespace)
31 : : // -----------------------------------------------------------------------------
32 : : namespace spawn_errors {
33 : : constexpr uint32_t success = 0;
34 : : constexpr uint32_t unknown_type = 1;
35 : : constexpr uint32_t deserialization_failed = 2;
36 : : constexpr uint32_t node_unreachable = 3;
37 : : constexpr uint32_t timeout = 4;
38 : : constexpr uint32_t spawn_receiver_not_running = 5;
39 : : } // namespace spawn_errors
40 : :
41 : : // -----------------------------------------------------------------------------
42 : : // SpawnRequest - sent from caller to spawn receiver on remote node
43 : : // Serialized via protobuf messages in messages.proto
44 : : // -----------------------------------------------------------------------------
45 : : struct SpawnRequest {
46 : : std::string actor_type_name; // e.g., "calculator"
47 : : TypeTag args_type; // type tag for deserializing args
48 : : StreamBuffer serialized_args; // type-erased constructor arguments
49 : : ActorAddress supervisor_addr; // supervisor's address for link establishment
50 : : };
51 : :
52 : : // -----------------------------------------------------------------------------
53 : : // SpawnResponse - sent back from spawn receiver to caller
54 : : // Serialized via protobuf messages in messages.proto
55 : : // -----------------------------------------------------------------------------
56 : : struct SpawnResponse {
57 : : ActorAddress actor_addr; // new actor's address (node_id, type, id,
58 : : // incarnation)
59 : : uint32_t error_code; // spawn_errors::code
60 : : };
61 : :
62 : : // -----------------------------------------------------------------------------
63 : : // -----------------------------------------------------------------------------
64 : : // AsyncActor - handle for asynchronous remote spawn
65 : : // -----------------------------------------------------------------------------
66 : : // Allows non-blocking spawn with result retrieval via get().
67 : : // WARNING: get() blocks the calling thread.
68 : : class AsyncActor {
69 : : public:
70 : : AsyncActor();
71 : : AsyncActor(AsyncActor&& other) noexcept;
72 : : AsyncActor& operator=(AsyncActor&& other) noexcept;
73 : :
74 : : // Construct with node_id and timeout
75 : : AsyncActor(EndPoint endpoint, std::chrono::milliseconds timeout);
76 : :
77 : : // Wait for response and return result (blocks until response or timeout)
78 : : result<ActorRef> get();
79 : :
80 : : // Check if response received (non-blocking)
81 : : bool ready() const;
82 : :
83 : : // Cancel pending spawn
84 : : void cancel();
85 : :
86 : : // Get associated node ID
87 : 2 : EndPoint endpoint() const {
88 : 2 : return endpoint_;
89 : : }
90 : :
91 : : // Set response (called by transport layer when response received)
92 : : void set_response(SpawnResponse response);
93 : :
94 : : // Message ID for correlation with response
95 : 0 : void set_message_id(uint64_t id) {
96 : 0 : message_id_ = id;
97 : 0 : }
98 : : uint64_t message_id() const {
99 : : return message_id_;
100 : : }
101 : :
102 : : private:
103 : : EndPoint endpoint_ = LocalEndpoint;
104 : 1 : std::chrono::milliseconds timeout_{5000};
105 : : mutable std::unique_ptr<std::mutex> mutex_;
106 : : std::unique_ptr<std::condition_variable> cv_;
107 : : bool ready_ = false;
108 : : bool cancelled_ = false;
109 : : SpawnResponse response_{};
110 : : uint64_t message_id_ = 0; // For correlation with response
111 : : };
112 : :
113 : : } // namespace hpactor
|