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 <google/protobuf/descriptor.h>
9#include <google/protobuf/message.h>
10#include <grpcpp/channel.h>
11#include <grpcpp/impl/service_type.h>
12#include <grpcpp/server.h>
13
14#include <viam/sdk/config/resource.hpp>
15#include <viam/sdk/resource/resource.hpp>
16#include <viam/sdk/resource/resource_api.hpp>
18#include <viam/sdk/resource/resource_server_base.hpp>
20
21namespace viam {
22namespace sdk {
23
24// TODO(RSDK-6617): one class per header
26 public:
28
33 virtual std::shared_ptr<ResourceServer> create_resource_server(
34 std::shared_ptr<ResourceManager> manager, Server& server) const = 0;
35
37 const google::protobuf::ServiceDescriptor* service_descriptor() const;
38
39 ResourceServerRegistration(const google::protobuf::ServiceDescriptor* service_descriptor)
40 : service_descriptor_(service_descriptor){};
41
42 private:
43 const google::protobuf::ServiceDescriptor* service_descriptor_;
44};
45
49 public:
51
56 virtual std::shared_ptr<Resource> create_rpc_client(
57 std::string name, std::shared_ptr<grpc::Channel> channel) const = 0;
58
60};
61
62// TODO(RSDK-6616): instead of std::functions, consider making these functions
63// virtual
67 public:
69 API api,
70 Model model,
71 std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor)
72 : construct_resource(std::move(constructor)),
73 validate(default_validator),
74 model_(std::move(model)),
75 api_(std::move(api)){};
76
78 API api,
79 Model model,
80 std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> constructor,
81 std::function<std::vector<std::string>(ResourceConfig)> validator)
82 : construct_resource(std::move(constructor)),
83 validate(std::move(validator)),
84 model_(std::move(model)),
85 api_(std::move(api)){};
86
87 const API& api() const;
88 const Model& model() const;
89
91 std::function<std::shared_ptr<Resource>(Dependencies, ResourceConfig)> construct_resource;
92
96 std::function<std::vector<std::string>(ResourceConfig)> validate;
97
99 viam::robot::v1::Status create_status(const std::shared_ptr<Resource>& resource) const;
100
101 private:
102 // default_validator is the default validator for all models if no validator
103 // is provided in construction. No dependencies are returned.
104 Model model_;
105 API api_;
106 static std::vector<std::string> default_validator(ResourceConfig cfg) {
107 return {};
108 };
109};
110
113class Registry {
114 public:
118 static void register_model(std::shared_ptr<const ModelRegistration> resource);
119
123 static std::shared_ptr<const ModelRegistration> lookup_model(const std::string& name);
124
129 static std::shared_ptr<const ModelRegistration> lookup_model(const API& api,
130 const Model& model);
131
133 template <typename ResourceClientT>
135 class ResourceClientRegistration2 final : public ResourceClientRegistration {
136 public:
137 using ResourceClientRegistration::ResourceClientRegistration;
138
139 std::shared_ptr<Resource> create_rpc_client(
140 std::string name, std::shared_ptr<grpc::Channel> chan) const override {
141 return std::make_shared<ResourceClientT>(std::move(name), std::move(chan));
142 }
143 };
144
145 Registry::register_resource_client_(API::get<typename ResourceClientT::interface_type>(),
146 std::make_shared<ResourceClientRegistration2>());
147 }
148
150 template <typename ResourceServerT>
152 class ResourceServerRegistration2 final : public ResourceServerRegistration {
153 public:
154 using ResourceServerRegistration::ResourceServerRegistration;
155 std::shared_ptr<ResourceServer> create_resource_server(
156 std::shared_ptr<ResourceManager> manager, Server& server) const override {
157 auto rs = std::make_shared<ResourceServerT>(manager);
158 server.register_service(rs.get());
159 return rs;
160 }
161 };
162
163 const google::protobuf::ServiceDescriptor* sd =
164 get_service_descriptor_(ResourceServerT::service_type::service_full_name());
165 Registry::register_resource_server_(API::get<typename ResourceServerT::interface_type>(),
166 std::make_shared<ResourceServerRegistration2>(sd));
167 }
168
170 template <typename ResourceClientT, typename ResourceServerT>
171 static void register_resource() {
172 register_resource_client<ResourceClientT>();
173 register_resource_server<ResourceServerT>();
174 }
175
179 static std::shared_ptr<const ResourceServerRegistration> lookup_resource_server(const API& api);
180
184 static std::shared_ptr<const ResourceClientRegistration> lookup_resource_client(const API& api);
185
188 static const std::unordered_map<std::string, std::shared_ptr<const ModelRegistration>>&
190
193 static const std::unordered_map<API, std::shared_ptr<const ResourceServerRegistration>>&
195
197 static void initialize();
198
199 private:
200 static std::mutex lock_;
201 static bool initialized_;
202 static std::unordered_map<std::string, std::shared_ptr<const ModelRegistration>> resources_;
203 static std::unordered_map<API, std::shared_ptr<const ResourceClientRegistration>> client_apis_;
204 static std::unordered_map<API, std::shared_ptr<const ResourceServerRegistration>> server_apis_;
205
206 static void register_resource_server_(
207 API api, std::shared_ptr<ResourceServerRegistration> resource_registration);
208
209 static void register_resource_client_(
210 API api, std::shared_ptr<ResourceClientRegistration> resource_registration);
211
212 static const google::protobuf::ServiceDescriptor* get_service_descriptor_(
213 const char* service_full_name);
214
215 static std::shared_ptr<const ModelRegistration> lookup_model_inlock_(
216 const std::string& name, const std::lock_guard<std::mutex>&);
217};
218
219} // namespace sdk
220} // namespace viam
Extends APIType to additionally define a resource's subtype (e.g., camera).
Definition resource_api.hpp:33
Information about a registered model, including a constructor and config validator.
Definition registry.hpp:66
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:91
viam::robot::v1::Status create_status(const std::shared_ptr< Resource > &resource) const
Creates a Status object for a given resource.
std::function< std::vector< std::string >(ResourceConfig)> validate
Validates a resource config.
Definition registry.hpp:96
Defines the namespace_, family, and name for a particular resource model.
Definition resource_api.hpp:117
A registry of known resources.
Definition registry.hpp:113
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:151
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:171
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:134
Defines registered Resource client creation functionality.
Definition registry.hpp:48
virtual std::shared_ptr< Resource > create_rpc_client(std::string name, std::shared_ptr< grpc::Channel > channel) const =0
Create gRPC client to a resource.
Definition resource.hpp:23
Definition registry.hpp:25
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:25
Defines a general-purpose resource manager.
Defines the Server class.