Viam C++ SDK current
Loading...
Searching...
No Matches
registry.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <string>
7
8#include <viam/sdk/common/grpc_fwd.hpp>
9#include <viam/sdk/config/resource.hpp>
10#include <viam/sdk/resource/resource.hpp>
11#include <viam/sdk/resource/resource_api.hpp>
13#include <viam/sdk/resource/resource_server_base.hpp>
15
16namespace google {
17namespace protobuf {
18
19class ServiceDescriptor;
20
21}
22} // namespace google
23
24namespace viam {
25namespace sdk {
26
27// TODO(RSDK-6617): one class per header
29 public:
30 ResourceServerRegistration(const ::google::protobuf::ServiceDescriptor* service_descriptor);
31
33
38 virtual std::shared_ptr<ResourceServer> create_resource_server(
39 std::shared_ptr<ResourceManager> manager, Server& server) const = 0;
40
42 const ::google::protobuf::ServiceDescriptor* service_descriptor() const;
43
44 private:
45 const ::google::protobuf::ServiceDescriptor* service_descriptor_;
46};
47
51 public:
53
55
60 virtual std::shared_ptr<Resource> create_rpc_client(
61 std::string name, std::shared_ptr<GrpcChannel> channel) const = 0;
62};
63
64// TODO(RSDK-6616): instead of std::functions, consider making these functions
65// virtual
69 public:
71 API api,
72 Model model,
73 std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor);
74
76 API api,
77 Model model,
78 std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor,
79 std::function<std::vector<std::string>(ResourceConfig)> validator);
80
81 const API& api() const;
82 const Model& model() const;
83
85 std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> construct_resource;
86
90 std::function<std::vector<std::string>(ResourceConfig)> validate;
91
92 private:
93 // default_validator is the default validator for all models if no validator
94 // is provided in construction. No dependencies are returned.
95 Model model_;
96 API api_;
97 static std::vector<std::string> default_validator(ResourceConfig cfg) {
98 return {};
99 };
100};
101
104class Registry {
105 public:
107 static Registry& get();
108
112 void register_model(std::shared_ptr<const ModelRegistration> resource);
113
117 std::shared_ptr<const ModelRegistration> lookup_model(const std::string& name) const;
118
123 std::shared_ptr<const ModelRegistration> lookup_model(const API& api, const Model& model) const;
124
126 template <typename ResourceClientT>
128 class ResourceClientRegistration2 final : public ResourceClientRegistration {
129 public:
130 using ResourceClientRegistration::ResourceClientRegistration;
131
132 std::shared_ptr<Resource> create_rpc_client(
133 std::string name, std::shared_ptr<GrpcChannel> chan) const override {
134 return std::make_shared<ResourceClientT>(std::move(name), std::move(chan));
135 }
136 };
137
138 Registry::register_resource_client_(API::get<typename ResourceClientT::interface_type>(),
139 std::make_shared<ResourceClientRegistration2>());
140 }
141
143 template <typename ResourceServerT>
145 class ResourceServerRegistration2 final : public ResourceServerRegistration {
146 public:
147 using ResourceServerRegistration::ResourceServerRegistration;
148 std::shared_ptr<ResourceServer> create_resource_server(
149 std::shared_ptr<ResourceManager> manager, Server& server) const override {
150 auto rs = std::make_shared<ResourceServerT>(manager);
151 server.register_service(rs.get());
152 return rs;
153 }
154 };
155
156 const google::protobuf::ServiceDescriptor* sd =
157 get_service_descriptor_(ResourceServerT::service_type::service_full_name());
158 Registry::register_resource_server_(API::get<typename ResourceServerT::interface_type>(),
159 std::make_shared<ResourceServerRegistration2>(sd));
160 }
161
163 template <typename ResourceClientT, typename ResourceServerT>
165 register_resource_client<ResourceClientT>();
166 register_resource_server<ResourceServerT>();
167 }
168
172 std::shared_ptr<const ResourceServerRegistration> lookup_resource_server(const API& api) const;
173
177 std::shared_ptr<const ResourceClientRegistration> lookup_resource_client(const API& api) const;
178
181 const std::unordered_map<std::string, std::shared_ptr<const ModelRegistration>>&
183
186 const std::unordered_map<API, std::shared_ptr<const ResourceServerRegistration>>&
188
189 private:
190 friend class Instance;
191 Registry() = default;
192
194 void initialize();
195
196 mutable std::mutex lock_;
197
198 std::unordered_map<std::string, std::shared_ptr<const ModelRegistration>> resources_;
199
200 std::unordered_map<API, std::shared_ptr<const ResourceClientRegistration>> client_apis_;
201 std::unordered_map<API, std::shared_ptr<const ResourceServerRegistration>> server_apis_;
202
203 void register_resources();
204
205 void register_resource_server_(
206 API api, std::shared_ptr<ResourceServerRegistration> resource_registration);
207
208 void register_resource_client_(
209 API api, std::shared_ptr<ResourceClientRegistration> resource_registration);
210
211 static const google::protobuf::ServiceDescriptor* get_service_descriptor_(
212 const char* service_full_name);
213
214 std::shared_ptr<const ModelRegistration> lookup_model_inlock_(
215 const std::string& name, const std::lock_guard<std::mutex>&) const;
216};
217
218} // namespace sdk
219} // namespace viam
Definition resource_api.hpp:21
Instance management for Viam C++ SDK applications. This is a single instance class which is responsib...
Definition instance.hpp:13
Information about a registered model, including a constructor and config validator.
Definition registry.hpp:68
std::function< std::shared_ptr< Resource >(Dependencies, ResourceConfig)> construct_resource
Constructs a resource from a map of dependencies and a resource config.
Definition registry.hpp:85
std::function< std::vector< std::string >(ResourceConfig)> validate
Validates a resource config.
Definition registry.hpp:90
Defines the namespace_, family, and name for a particular resource model.
Definition resource_api.hpp:125
A registry of known resources.
Definition registry.hpp:104
std::shared_ptr< const ResourceClientRegistration > lookup_resource_client(const API &api) const
Lookup a registered client api.
void register_resource_server()
Register a resource server constructor.
Definition registry.hpp:144
static Registry & get()
Get the application-wide instance of Registry.
std::shared_ptr< const ModelRegistration > lookup_model(const API &api, const Model &model) const
Lookup a given registered resource.
void register_model(std::shared_ptr< const ModelRegistration > resource)
Registers a resource with the Registry.
std::shared_ptr< const ResourceServerRegistration > lookup_resource_server(const API &api) const
Lookup a registered server api.
const std::unordered_map< std::string, std::shared_ptr< const ModelRegistration > > & registered_models() const
Provide information on registered resource models.
void register_resource()
Register resource client and server constructors.
Definition registry.hpp:164
const std::unordered_map< API, std::shared_ptr< const ResourceServerRegistration > > & registered_resource_servers() const
Provide access to registered resources.
void register_resource_client()
Register a resource client constructor.
Definition registry.hpp:127
std::shared_ptr< const ModelRegistration > lookup_model(const std::string &name) const
Lookup a given registered resource.
Defines registered Resource client creation functionality.
Definition registry.hpp:50
virtual std::shared_ptr< Resource > create_rpc_client(std::string name, std::shared_ptr< GrpcChannel > channel) const =0
Create gRPC client to a resource.
Definition resource.hpp:40
Definition registry.hpp:28
const ::google::protobuf::ServiceDescriptor * service_descriptor() const
Returns a reference to the ResourceServerRegistration's service descriptor.
virtual std::shared_ptr< ResourceServer > create_resource_server(std::shared_ptr< ResourceManager > manager, Server &server) const =0
Create a resource's gRPC server.
Defines gRPC Server functionality.
Definition server.hpp:28
Defines a general-purpose resource manager.
Defines the Server class.