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/actor/daemon_actor.hpp>
18 : : #include <hpactor/mem/std_allocator.hpp>
19 : : #include <hpactor/ref/actor_address.hpp>
20 : : #include <hpactor/ref/actor_ref.hpp>
21 : : #include <hpactor/types/types.hpp>
22 : :
23 : : #include <functional>
24 : : #include <string>
25 : : #include <unordered_map>
26 : :
27 : : namespace hpactor {
28 : :
29 : : class ExternalMsgGatewayActor : public DaemonActor {
30 : : public:
31 : 1 : ExternalMsgGatewayActor(ActorContext* ctx, ActorSystem& sys)
32 : 1 : : DaemonActor(ctx, sys)
33 : 1 : , routes_(mem::MemStdAllocator<std::pair<const std::string, ActorAddr>>(
34 : : id_ptr(), mem::RegionType::kActor))
35 : 1 : , transforms_(mem::MemStdAllocator<std::pair<const TypeTag, PayloadTransform>>(
36 : 1 : id_ptr(), mem::RegionType::kActor)) {}
37 : :
38 : : void route(const std::string& path_pattern, ActorAddr target) {
39 : : routes_[path_pattern] = target;
40 : : }
41 : :
42 : : void route(const std::string& path_pattern, ActorRef target) {
43 : : routes_[path_pattern] = target.address();
44 : : }
45 : :
46 : : using PayloadTransform =
47 : : std::function<TypedMessage(StreamBuffer)>;
48 : :
49 : : void set_transform(TypeTag tag, PayloadTransform tx) {
50 : : transforms_[tag] = std::move(tx);
51 : : }
52 : :
53 : : protected:
54 : : ActorAddr resolve_route(const std::string& path) const {
55 : : auto it = routes_.find(path);
56 : : if (it != routes_.end()) return it->second;
57 : :
58 : : for (const auto& [pattern, target] : routes_) {
59 : : if (path.find(pattern) == 0) return target;
60 : : }
61 : : return invalid_actor_addr;
62 : : }
63 : :
64 : : TypedMessage transform(TypeTag tag, StreamBuffer payload) const {
65 : : auto it = transforms_.find(tag);
66 : : if (it != transforms_.end()) {
67 : : return it->second(std::move(payload));
68 : : }
69 : : return TypedMessage(tag, std::move(payload));
70 : : }
71 : :
72 : : using RouteMap =
73 : : std::unordered_map<std::string, ActorAddr, std::hash<std::string>,
74 : : std::equal_to<>,
75 : : mem::MemStdAllocator<std::pair<const std::string,
76 : : ActorAddr>>>;
77 : : using TransformMap =
78 : : std::unordered_map<TypeTag, PayloadTransform, std::hash<TypeTag>,
79 : : std::equal_to<>,
80 : : mem::MemStdAllocator<std::pair<const TypeTag,
81 : : PayloadTransform>>>;
82 : : RouteMap routes_;
83 : : TransformMap transforms_;
84 : : };
85 : :
86 : : } // namespace hpactor
|