1212 ********************************************************************************/
1313#include " score/hm/health_monitor.h"
1414
15+ namespace
16+ {
1517extern " C" {
1618using namespace score ::hm;
19+ using namespace score ::hm::internal;
20+ using namespace score ::hm::deadline;
21+
22+ // Functions below must match functions defined in `crate::ffi`.
23+
24+ FFICode health_monitor_builder_create (FFIHandle* health_monitor_builder_handle_out);
25+ FFICode health_monitor_builder_destroy (FFIHandle health_monitor_builder_handle);
26+ FFICode health_monitor_builder_build (FFIHandle health_monitor_builder_handle,
27+ uint32_t supervisor_cycle_ms,
28+ uint32_t internal_cycle_ms,
29+ FFIHandle* health_monitor_handle_out);
30+ FFICode health_monitor_builder_add_deadline_monitor (FFIHandle health_monitor_builder_handle,
31+ const IdentTag* monitor_tag,
32+ FFIHandle deadline_monitor_builder_handle);
33+ FFICode health_monitor_get_deadline_monitor (FFIHandle health_monitor_handle,
34+ const IdentTag* monitor_tag,
35+ FFIHandle* deadline_monitor_handle_out);
36+ FFICode health_monitor_start (FFIHandle health_monitor_handle);
37+ FFICode health_monitor_destroy (FFIHandle health_monitor_handle);
38+ }
1739
18- // Health Monitor Foreign Function Interface Declarations that are exported by Rust implementation library
19- internal::FFIHandle health_monitor_builder_create ();
20- void health_monitor_builder_destroy (internal::FFIHandle handler);
21-
22- internal::FFIHandle health_monitor_builder_build (internal::FFIHandle health_monitor_builder_handle,
23- uint32_t supervisor_cycle_ms,
24- uint32_t internal_cycle_ms);
25- void health_monitor_builder_add_deadline_monitor (internal::FFIHandle handle,
26- const IdentTag* tag,
27- internal::FFIHandle monitor_handle);
28-
29- internal::FFIHandle health_monitor_get_deadline_monitor (internal::FFIHandle health_monitor_handle, const IdentTag* tag);
30- void health_monitor_start (internal::FFIHandle health_monitor_handle);
31- void health_monitor_destroy (internal::FFIHandle handler);
40+ FFIHandle health_monitor_builder_create_wrapper ()
41+ {
42+ FFIHandle handle{nullptr };
43+ auto result{health_monitor_builder_create (&handle)};
44+ abort_on_error (result);
45+ return handle;
3246}
3347
48+ } // namespace
49+
3450// C++ wrapper for Rust library - the API implementation obeys the Rust API semantics and it's invariants
3551
3652namespace score ::hm
3753{
3854
3955HealthMonitorBuilder::HealthMonitorBuilder ()
40- : health_monitor_builder_handle_{health_monitor_builder_create (), &health_monitor_builder_destroy}
56+ : health_monitor_builder_handle_{health_monitor_builder_create_wrapper (), &health_monitor_builder_destroy}
4157{
4258}
4359
4460HealthMonitorBuilder HealthMonitorBuilder::add_deadline_monitor (const IdentTag& tag,
45- deadline:: DeadlineMonitorBuilder&& monitor) &&
61+ DeadlineMonitorBuilder&& monitor) &&
4662{
4763 auto monitor_handle = monitor.drop_by_rust ();
4864 SCORE_LANGUAGE_FUTURECPP_PRECONDITION (monitor_handle.has_value ());
4965 SCORE_LANGUAGE_FUTURECPP_PRECONDITION (health_monitor_builder_handle_.as_rust_handle ().has_value ());
5066
51- health_monitor_builder_add_deadline_monitor (
52- health_monitor_builder_handle_.as_rust_handle ().value (), &tag, monitor_handle.value ());
67+ auto result{health_monitor_builder_add_deadline_monitor (
68+ health_monitor_builder_handle_.as_rust_handle ().value (), &tag, monitor_handle.value ())};
69+ abort_on_error (result);
70+
5371 return std::move (*this );
5472}
5573
@@ -67,16 +85,21 @@ HealthMonitorBuilder HealthMonitorBuilder::with_supervisor_api_cycle(std::chrono
6785
6886HealthMonitor HealthMonitorBuilder::build () &&
6987{
70- auto handle = health_monitor_builder_handle_.drop_by_rust ();
71- SCORE_LANGUAGE_FUTURECPP_PRECONDITION (handle .has_value ());
88+ auto health_monitor_builder_handle = health_monitor_builder_handle_.drop_by_rust ();
89+ SCORE_LANGUAGE_FUTURECPP_PRECONDITION (health_monitor_builder_handle .has_value ());
7290
7391 uint32_t supervisor_duration_ms = static_cast <uint32_t >(supervisor_api_cycle_duration_.count ());
7492 uint32_t internal_duration_ms = static_cast <uint32_t >(internal_processing_cycle_duration_.count ());
7593
76- return HealthMonitor (health_monitor_builder_build (handle.value (), supervisor_duration_ms, internal_duration_ms));
94+ FFIHandle health_monitor_handle{nullptr };
95+ auto result{health_monitor_builder_build (
96+ health_monitor_builder_handle.value (), supervisor_duration_ms, internal_duration_ms, &health_monitor_handle)};
97+ abort_on_error (result);
98+
99+ return HealthMonitor{health_monitor_handle};
77100}
78101
79- HealthMonitor::HealthMonitor (internal:: FFIHandle handle) : health_monitor_(handle)
102+ HealthMonitor::HealthMonitor (FFIHandle handle) : health_monitor_(handle)
80103{
81104 // Initialize health monitor
82105}
@@ -87,21 +110,22 @@ HealthMonitor::HealthMonitor(HealthMonitor&& other)
87110 other.health_monitor_ = nullptr ;
88111}
89112
90- score::cpp::expected<deadline:: DeadlineMonitor, Error> HealthMonitor::get_deadline_monitor (const IdentTag& tag)
113+ score::cpp::expected<DeadlineMonitor, Error> HealthMonitor::get_deadline_monitor (const IdentTag& tag)
91114{
92- auto maybe_monitor = health_monitor_get_deadline_monitor (health_monitor_, &tag) ;
93-
94- if (maybe_monitor != nullptr )
115+ FFIHandle handle{ nullptr } ;
116+ auto result{ health_monitor_get_deadline_monitor (health_monitor_, &tag, &handle)};
117+ if (result != kSuccess )
95118 {
96-
97- return score::cpp::expected<deadline::DeadlineMonitor, Error>(deadline::DeadlineMonitor{maybe_monitor});
119+ return score::cpp::unexpected (static_cast <Error>(result));
98120 }
99121
100- return score::cpp::unexpected ( Error::NotFound );
122+ return score::cpp::expected<DeadlineMonitor, Error>(DeadlineMonitor{handle} );
101123}
124+
102125void HealthMonitor::start ()
103126{
104- health_monitor_start (health_monitor_);
127+ auto result{health_monitor_start (health_monitor_)};
128+ abort_on_error (result);
105129}
106130
107131HealthMonitor::~HealthMonitor ()
0 commit comments