Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
service_registry.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
21
22#include <cstdint>
23#include <functional>
24#include <memory>
25#include <mutex>
26#include <optional>
27#include <string>
28#include <string_view>
29#include <unordered_map>
30#include <vector>
31
32// Forward declarations for gRPC types
33#if NETWORK_GRPC_OFFICIAL
34namespace grpc {
35class Service;
36class Server;
37class ServerBuilder;
38class ServerContext;
39class ByteBuffer;
40namespace reflection {
41namespace v1alpha {
42class ServerReflectionRequest;
43class ServerReflectionResponse;
44}
45}
46}
47#endif
48
50{
51
52// ============================================================================
53// Method Types and Descriptors
54// ============================================================================
55
67
73{
75 std::string name;
76
78 std::string full_name;
79
82
84 std::string input_type;
85
87 std::string output_type;
88
90 auto is_client_streaming() const -> bool
91 {
94 }
95
97 auto is_server_streaming() const -> bool
98 {
101 }
102};
103
109{
111 std::string name;
112
114 std::string package;
115
117 std::vector<method_descriptor> methods;
118
124 auto find_method(std::string_view method_name) const
125 -> const method_descriptor*
126 {
127 for (const auto& method : methods)
128 {
129 if (method.name == method_name)
130 {
131 return &method;
132 }
133 }
134 return nullptr;
135 }
136
141 auto full_name() const -> std::string
142 {
143 if (package.empty())
144 {
145 return name;
146 }
147 return package + "." + name;
148 }
149};
150
151// ============================================================================
152// Service Interface
153// ============================================================================
154
163{
164public:
165 virtual ~service_base() = default;
166
171 virtual auto descriptor() const -> const service_descriptor& = 0;
172
177 virtual auto is_ready() const -> bool { return true; }
178
179#if NETWORK_GRPC_OFFICIAL
184 virtual auto grpc_service() -> ::grpc::Service* { return nullptr; }
185#endif
186};
187
188// ============================================================================
189// Generic Service (Dynamic Registration)
190// ============================================================================
191
213{
214public:
219 explicit generic_service(std::string service_name);
220
222
223 // Non-copyable
226
227 // Movable
229 generic_service& operator=(generic_service&&) noexcept;
230
235 auto descriptor() const -> const service_descriptor& override;
236
245 auto register_unary_method(
246 const std::string& method_name,
247 unary_handler handler,
248 const std::string& input_type = "",
249 const std::string& output_type = "") -> VoidResult;
250
259 auto register_server_streaming_method(
260 const std::string& method_name,
262 const std::string& input_type = "",
263 const std::string& output_type = "") -> VoidResult;
264
273 auto register_client_streaming_method(
274 const std::string& method_name,
276 const std::string& input_type = "",
277 const std::string& output_type = "") -> VoidResult;
278
287 auto register_bidi_streaming_method(
288 const std::string& method_name,
290 const std::string& input_type = "",
291 const std::string& output_type = "") -> VoidResult;
292
298 auto get_unary_handler(const std::string& method_name) const
299 -> const unary_handler*;
300
306 auto get_server_streaming_handler(const std::string& method_name) const
307 -> const server_streaming_handler*;
308
314 auto get_client_streaming_handler(const std::string& method_name) const
315 -> const client_streaming_handler*;
316
322 auto get_bidi_streaming_handler(const std::string& method_name) const
323 -> const bidi_streaming_handler*;
324
325#if NETWORK_GRPC_OFFICIAL
326 auto grpc_service() -> ::grpc::Service* override;
327#endif
328
329private:
330 class impl;
331 std::unique_ptr<impl> impl_;
332};
333
334// ============================================================================
335// Protoc Service Adapter
336// ============================================================================
337
338#if NETWORK_GRPC_OFFICIAL
339
360class protoc_service_adapter : public service_base
361{
362public:
368 protoc_service_adapter(std::unique_ptr<::grpc::Service> service,
369 std::string service_name);
370
376 protoc_service_adapter(::grpc::Service* service,
377 std::string service_name);
378
379 ~protoc_service_adapter() override;
380
381 // Non-copyable
382 protoc_service_adapter(const protoc_service_adapter&) = delete;
383 protoc_service_adapter& operator=(const protoc_service_adapter&) = delete;
384
385 // Movable
386 protoc_service_adapter(protoc_service_adapter&&) noexcept;
387 protoc_service_adapter& operator=(protoc_service_adapter&&) noexcept;
388
389 auto descriptor() const -> const service_descriptor& override;
390
391 auto grpc_service() -> ::grpc::Service* override;
392
393private:
394 class impl;
395 std::unique_ptr<impl> impl_;
396};
397
398#endif // NETWORK_GRPC_OFFICIAL
399
400// ============================================================================
401// Service Registry
402// ============================================================================
403
409{
411 bool enable_reflection = false;
412
414 bool enable_health_check = false;
415
417 std::string health_service_name = "grpc.health.v1.Health";
418};
419
446{
447public:
452 explicit service_registry(const registry_config& config = {});
453
455
456 // Non-copyable
459
460 // Movable
462 service_registry& operator=(service_registry&&) noexcept;
463
469 auto register_service(service_base* service) -> VoidResult;
470
476 auto unregister_service(const std::string& service_name) -> VoidResult;
477
483 auto find_service(const std::string& service_name) const -> service_base*;
484
489 auto services() const -> std::vector<service_base*>;
490
495 auto service_names() const -> std::vector<std::string>;
496
502 auto find_method(const std::string& full_method_path) const
503 -> std::optional<std::pair<service_base*, const method_descriptor*>>;
504
509 auto is_reflection_enabled() const -> bool;
510
516 auto configure_server(grpc_server& server) -> VoidResult;
517
518#if NETWORK_GRPC_OFFICIAL
524 auto configure_server_builder(::grpc::ServerBuilder& builder) -> VoidResult;
525
531 auto enable_reflection(::grpc::ServerBuilder& builder) -> VoidResult;
532#endif
533
540 auto set_service_health(const std::string& service_name, bool serving)
541 -> VoidResult;
542
548 auto get_service_health(const std::string& service_name) const -> bool;
549
550private:
551 class impl;
552 std::unique_ptr<impl> impl_;
553};
554
555// ============================================================================
556// Health Checking Support
557// ============================================================================
558
564{
565 unknown,
566 serving,
569};
570
579{
580public:
582 ~health_service() override;
583
584 // Non-copyable
587
588 // Movable
590 health_service& operator=(health_service&&) noexcept;
591
592 auto descriptor() const -> const service_descriptor& override;
593
599 auto set_status(const std::string& service_name, health_status status) -> void;
600
606 auto get_status(const std::string& service_name) const -> health_status;
607
611 auto clear() -> void;
612
613#if NETWORK_GRPC_OFFICIAL
614 auto grpc_service() -> ::grpc::Service* override;
615#endif
616
617private:
618 class impl;
619 std::unique_ptr<impl> impl_;
620};
621
622// ============================================================================
623// Utility Functions
624// ============================================================================
625
631auto parse_method_path(std::string_view full_path)
632 -> std::optional<std::pair<std::string, std::string>>;
633
640auto build_method_path(std::string_view service_name,
641 std::string_view method_name) -> std::string;
642
643} // namespace kcenon::network::protocols::grpc
A service that allows dynamic method registration.
generic_service(generic_service &&) noexcept
generic_service(const generic_service &)=delete
generic_service & operator=(const generic_service &)=delete
gRPC server for handling RPC requests
Definition server.h:273
Base class for gRPC service implementations.
Definition server.h:236
Implementation of gRPC health checking protocol.
health_service(const health_service &)=delete
health_service & operator=(const health_service &)=delete
Base class for all gRPC service implementations.
virtual auto descriptor() const -> const service_descriptor &=0
Get the service descriptor.
Central registry for managing gRPC services.
service_registry & operator=(const service_registry &)=delete
service_registry(service_registry &&) noexcept
service_registry(const service_registry &)=delete
tracing_config config
Definition exporters.cpp:29
gRPC protocol implementation
Definition client.h:34
auto parse_method_path(std::string_view full_path) -> std::optional< std::pair< std::string, std::string > >
Parse full method path into service and method names.
health_status
Health status for a service.
@ service_unknown
Service is not registered.
@ server_streaming
Server streaming (single request, multiple responses)
@ client_streaming
Client streaming (multiple requests, single response)
@ bidi_streaming
Bidirectional streaming (multiple requests and responses)
@ unary
Unary RPC (single request, single response)
std::function< std::pair< grpc_status, std::vector< uint8_t > >( server_context &ctx, const std::vector< uint8_t > &request)> unary_handler
Handler function type for unary RPC.
Definition server.h:134
auto build_method_path(std::string_view service_name, std::string_view method_name) -> std::string
Build full method path from service and method names.
std::function< grpc_status( server_context &ctx, const std::vector< uint8_t > &request, server_writer &writer)> server_streaming_handler
Handler function type for server streaming RPC.
Definition server.h:163
std::function< grpc_status( server_context &ctx, server_reader_writer &stream)> bidi_streaming_handler
Handler function type for bidirectional streaming RPC.
Definition server.h:222
std::function< std::pair< grpc_status, std::vector< uint8_t > >( server_context &ctx, server_reader &reader)> client_streaming_handler
Handler function type for client streaming RPC.
Definition server.h:197
Network-specific error and result type definitions.
gRPC server configuration and service hosting.
gRPC status codes and error representation.
Describes a single RPC method within a service.
std::string output_type
Output message type name (for reflection)
std::string input_type
Input message type name (for reflection)
std::string full_name
Full method path (e.g., "/package.Service/Method")
auto is_server_streaming() const -> bool
Whether the method provides server streaming.
std::string name
Method name (without service prefix)
auto is_client_streaming() const -> bool
Whether the method requires client streaming.
Describes a gRPC service and its methods.
auto full_name() const -> std::string
Get full service name including package.
std::vector< method_descriptor > methods
List of methods in this service.
std::string name
Service name (e.g., "helloworld.Greeter")
std::string package
Package name (e.g., "helloworld")
auto find_method(std::string_view method_name) const -> const method_descriptor *
Find a method by name.