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:
109 static void register_model(std::shared_ptr<const ModelRegistration> resource);
110
114 static std::shared_ptr<const ModelRegistration> lookup_model(const std::string& name);
115
120 static std::shared_ptr<const ModelRegistration> lookup_model(const API& api,
121 const Model& model);
122
124 template <typename ResourceClientT>
126 class ResourceClientRegistration2 final : public ResourceClientRegistration {
127 public:
128 using ResourceClientRegistration::ResourceClientRegistration;
129
130 std::shared_ptr<Resource> create_rpc_client(
131 std::string name, std::shared_ptr<GrpcChannel> chan) const override {
132 return std::make_shared<ResourceClientT>(std::move(name), std::move(chan));
133 }
134 };
135
136 Registry::register_resource_client_(API::get<typename ResourceClientT::interface_type>(),
137 std::make_shared<ResourceClientRegistration2>());
138 }
139
141 template <typename ResourceServerT>
143 class ResourceServerRegistration2 final : public ResourceServerRegistration {
144 public:
145 using ResourceServerRegistration::ResourceServerRegistration;
146 std::shared_ptr<ResourceServer> create_resource_server(
147 std::shared_ptr<ResourceManager> manager, Server& server) const override {
148 auto rs = std::make_shared<ResourceServerT>(manager);
149 server.register_service(rs.get());
150 return rs;
151 }
152 };
153
154 const google::protobuf::ServiceDescriptor* sd =
155 get_service_descriptor_(ResourceServerT::service_type::service_full_name());
156 Registry::register_resource_server_(API::get<typename ResourceServerT::interface_type>(),
157 std::make_shared<ResourceServerRegistration2>(sd));
158 }
159
161 template <typename ResourceClientT, typename ResourceServerT>
162 static void register_resource() {
163 register_resource_client<ResourceClientT>();
164 register_resource_server<ResourceServerT>();
165 }
166
170 static std::shared_ptr<const ResourceServerRegistration> lookup_resource_server(const API& api);
171
175 static std::shared_ptr<const ResourceClientRegistration> lookup_resource_client(const API& api);
176
179 static const std::unordered_map<std::string, std::shared_ptr<const ModelRegistration>>&
181
184 static const std::unordered_map<API, std::shared_ptr<const ResourceServerRegistration>>&
186
188 static void initialize();
189
190 private:
191 static std::mutex lock_;
192 static bool initialized_;
193 static std::unordered_map<std::string, std::shared_ptr<const ModelRegistration>> resources_;
194 static std::unordered_map<API, std::shared_ptr<const ResourceClientRegistration>> client_apis_;
195 static std::unordered_map<API, std::shared_ptr<const ResourceServerRegistration>> server_apis_;
196
197 static void register_resource_server_(
198 API api, std::shared_ptr<ResourceServerRegistration> resource_registration);
199
200 static void register_resource_client_(
201 API api, std::shared_ptr<ResourceClientRegistration> resource_registration);
202
203 static const google::protobuf::ServiceDescriptor* get_service_descriptor_(
204 const char* service_full_name);
205
206 static std::shared_ptr<const ModelRegistration> lookup_model_inlock_(
207 const std::string& name, const std::lock_guard<std::mutex>&);
208};
209
210} // namespace sdk
211} // namespace viam
Definition resource_api.hpp:21
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
static std::shared_ptr< const ResourceClientRegistration > lookup_resource_client(const API &api)
Lookup a registered client api.
static void initialize()
Initialized the Viam registry. No-op if it has already been called.
static const std::unordered_map< API, std::shared_ptr< const ResourceServerRegistration > > & registered_resource_servers()
Provide access to registered resources.
static std::shared_ptr< const ModelRegistration > lookup_model(const std::string &name)
Lookup a given registered resource.
static std::shared_ptr< const ModelRegistration > lookup_model(const API &api, const Model &model)
Lookup a given registered resource.
static void register_resource_server()
Register a resource server constructor.
Definition registry.hpp:142
static std::shared_ptr< const ResourceServerRegistration > lookup_resource_server(const API &api)
Lookup a registered server api.
static void register_resource()
Register resource client and server constructors.
Definition registry.hpp:162
static const std::unordered_map< std::string, std::shared_ptr< const ModelRegistration > > & registered_models()
Provide information on registered resource models.
static void register_model(std::shared_ptr< const ModelRegistration > resource)
Registers a resource with the Registry.
static void register_resource_client()
Register a resource client constructor.
Definition registry.hpp:125
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:31
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.