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>
14#include <viam/sdk/rpc/dial.hpp>
16
17namespace google {
18namespace protobuf {
19
20class ServiceDescriptor;
21
22}
23} // namespace google
24
25namespace viam {
26namespace sdk {
27
28// TODO(RSDK-6617): one class per header
30 public:
31 ResourceServerRegistration(const ::google::protobuf::ServiceDescriptor* service_descriptor);
32
34
39 virtual std::shared_ptr<ResourceServer> create_resource_server(
40 std::shared_ptr<ResourceManager> manager, Server& server) const = 0;
41
43 const ::google::protobuf::ServiceDescriptor* service_descriptor() const;
44
45 private:
46 const ::google::protobuf::ServiceDescriptor* service_descriptor_;
47};
48
52 public:
54
56
63 virtual std::shared_ptr<Resource> create_rpc_client(std::string name,
64 const ViamChannel& channel) const = 0;
65};
66
67// TODO(RSDK-6616): instead of std::functions, consider making these functions
68// virtual
72 public:
74 API api,
75 Model model,
76 std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor);
77
79 API api,
80 Model model,
81 std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor,
82 std::function<std::vector<std::string>(ResourceConfig)> validator);
83
84 const API& api() const;
85 const Model& model() const;
86
88 std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> construct_resource;
89
93 std::function<std::vector<std::string>(ResourceConfig)> validate;
94
95 private:
96 // default_validator is the default validator for all models if no validator
97 // is provided in construction. No dependencies are returned.
98 Model model_;
99 API api_;
100 static std::vector<std::string> default_validator(ResourceConfig cfg) {
101 return {};
102 };
103};
104
107class Registry {
108 public:
110 static Registry& get();
111
115 void register_model(std::shared_ptr<const ModelRegistration> resource);
116
120 std::shared_ptr<const ModelRegistration> lookup_model(const std::string& name) const;
121
126 std::shared_ptr<const ModelRegistration> lookup_model(const API& api, const Model& model) const;
127
129 template <typename ResourceClientT>
131 class ResourceClientRegistration2 final : public ResourceClientRegistration {
132 public:
133 using ResourceClientRegistration::ResourceClientRegistration;
134
138 std::shared_ptr<Resource> create_rpc_client(std::string name,
139 const ViamChannel& channel) const override {
140 return std::make_shared<ResourceClientT>(std::move(name), channel);
141 }
142 };
143
144 Registry::register_resource_client_(API::get<typename ResourceClientT::interface_type>(),
145 std::make_shared<ResourceClientRegistration2>());
146 }
147
149 template <typename ResourceServerT>
151 class ResourceServerRegistration2 final : public ResourceServerRegistration {
152 public:
153 using ResourceServerRegistration::ResourceServerRegistration;
154 std::shared_ptr<ResourceServer> create_resource_server(
155 std::shared_ptr<ResourceManager> manager, Server& server) const override {
156 auto rs = std::make_shared<ResourceServerT>(manager);
157 server.register_service(rs.get());
158 return rs;
159 }
160 };
161
162 const google::protobuf::ServiceDescriptor* sd =
163 get_service_descriptor_(ResourceServerT::service_type::service_full_name());
164 Registry::register_resource_server_(API::get<typename ResourceServerT::interface_type>(),
165 std::make_shared<ResourceServerRegistration2>(sd));
166 }
167
169 template <typename ResourceClientT, typename ResourceServerT>
171 register_resource_client<ResourceClientT>();
172 register_resource_server<ResourceServerT>();
173 }
174
178 std::shared_ptr<const ResourceServerRegistration> lookup_resource_server(const API& api) const;
179
183 std::shared_ptr<const ResourceClientRegistration> lookup_resource_client(const API& api) const;
184
187 const std::unordered_map<std::string, std::shared_ptr<const ModelRegistration>>&
189
192 const std::unordered_map<API, std::shared_ptr<const ResourceServerRegistration>>&
194
195 private:
196 friend class Instance;
197 Registry() = default;
198
200 void initialize();
201
202 mutable std::mutex lock_;
203
204 std::unordered_map<std::string, std::shared_ptr<const ModelRegistration>> resources_;
205
206 std::unordered_map<API, std::shared_ptr<const ResourceClientRegistration>> client_apis_;
207 std::unordered_map<API, std::shared_ptr<const ResourceServerRegistration>> server_apis_;
208
209 void register_resources();
210
211 void register_resource_server_(
212 API api, std::shared_ptr<ResourceServerRegistration> resource_registration);
213
214 void register_resource_client_(
215 API api, std::shared_ptr<ResourceClientRegistration> resource_registration);
216
217 static const google::protobuf::ServiceDescriptor* get_service_descriptor_(
218 const char* service_full_name);
219
220 std::shared_ptr<const ModelRegistration> lookup_model_inlock_(
221 const std::string& name, const std::lock_guard<std::mutex>&) const;
222};
223
224} // namespace sdk
225} // 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:71
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:88
std::function< std::vector< std::string >(ResourceConfig)> validate
Validates a resource config.
Definition registry.hpp:93
Defines the namespace_, family, and name for a particular resource model.
Definition resource_api.hpp:129
A registry of known resources.
Definition registry.hpp:107
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:150
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:170
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:130
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:51
virtual std::shared_ptr< Resource > create_rpc_client(std::string name, const ViamChannel &channel) const =0
Create gRPC client to a resource.
Definition resource.hpp:43
Definition registry.hpp:29
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:30
Definition dial.hpp:25
Defines a general-purpose resource manager.
Defines the Server class.