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 : : #include <hpactor/actor/spawn_receiver.hpp>
16 : : #include <hpactor/core/actor_system.hpp>
17 : : #include <hpactor/net/frame.hpp>
18 : :
19 : : #include <hpactor/messages.pb.h>
20 : :
21 : : namespace hpactor {
22 : :
23 : 2 : SpawnReceiver::SpawnReceiver(ActorSystem& sys, ActorTypeRegistry& registry,
24 : 2 : net::Transport* transport)
25 : 2 : : EventBasedActor(nullptr, sys), registry_(registry), transport_(transport) {}
26 : :
27 : 2 : Behavior SpawnReceiver::make_behavior() {
28 : 4 : return Behavior{[this](TypedMessage& msg) {
29 : 0 : if (msg.type_id() == TypeTag::SpawnRequestTag) {
30 : : // Deserialize the protobuf spawn request
31 : 0 : auto pb_req = msg.as<::hpactor::SpawnRequestMessage>();
32 : 0 : if (pb_req) {
33 : : // Convert protobuf to SpawnRequest
34 : 0 : SpawnRequest req;
35 : 0 : req.actor_type_name = pb_req->actor_type_name();
36 : 0 : req.args_type = static_cast<TypeTag>(pb_req->args_type());
37 : 0 : req.serialized_args.assign(pb_req->serialized_args().begin(),
38 : 0 : pb_req->serialized_args().end());
39 : 0 : handle_spawn_request(req, net::WireFrame{});
40 : 0 : }
41 : 0 : }
42 : 2 : }};
43 : : }
44 : :
45 : 0 : void SpawnReceiver::handle_spawn_request(const SpawnRequest& req,
46 : : const net::WireFrame& frame) {
47 : 0 : SpawnResponse response;
48 : :
49 : 0 : auto result = registry_.spawn(system(), req.actor_type_name,
50 : 0 : req.serialized_args, req.args_type);
51 : 0 : if (result.has_value()) {
52 : 0 : response.actor_addr = result.value();
53 : 0 : response.error_code = spawn_errors::success;
54 : : } else {
55 : 0 : response.error_code = result.error().code();
56 : : }
57 : :
58 : 0 : if (transport_ != nullptr) {
59 : : // Serialize response using protobuf
60 : 0 : ::hpactor::SpawnResponseMessage pb_resp;
61 : 0 : net::to_proto(pb_resp.mutable_actor_addr(), response.actor_addr);
62 : 0 : pb_resp.set_error_code(response.error_code);
63 : :
64 : 0 : net::WireFrame response_frame;
65 : 0 : net::to_proto(response_frame.pb_frame.mutable_sender(), address());
66 : 0 : response_frame.pb_frame.mutable_receiver()->CopyFrom(frame.pb_frame.sender());
67 : 0 : response_frame.pb_frame.set_message_id(frame.pb_frame.message_id());
68 : 0 : response_frame.pb_frame.set_flags(net::WireFrame::RpcResponse);
69 : 0 : response_frame.pb_frame.set_type_tag(static_cast<uint32_t>(TypeTag::SpawnResponseTag));
70 : 0 : auto serialized = system().proto_registry().serialize(pb_resp);
71 : 0 : response_frame.pb_frame.set_payload(
72 : 0 : reinterpret_cast<const char*>(serialized.data()),
73 : : serialized.size());
74 : :
75 : 0 : transport_->send(net::from_proto(response_frame.pb_frame.receiver()),
76 : 0 : response_frame.encode());
77 : 0 : }
78 : 0 : }
79 : :
80 : : } // namespace hpactor
|