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>
19class ServiceDescriptor;
39 std::shared_ptr<ResourceManager> manager,
Server& server)
const = 0;
45 const ::google::protobuf::ServiceDescriptor* service_descriptor_;
61 std::string name, std::shared_ptr<GrpcChannel> channel)
const = 0;
73 std::function<std::shared_ptr<Resource>(Dependencies,
ResourceConfig)> constructor);
78 std::function<std::shared_ptr<Resource>(Dependencies,
ResourceConfig)> constructor,
79 std::function<std::vector<std::string>(
ResourceConfig)> validator);
81 const API& api()
const;
82 const Model& model()
const;
97 static std::vector<std::string> default_validator(
ResourceConfig cfg) {
117 std::shared_ptr<const ModelRegistration>
lookup_model(
const std::string& name)
const;
126 template <
typename ResourceClientT>
130 using ResourceClientRegistration::ResourceClientRegistration;
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));
138 Registry::register_resource_client_(API::get<typename ResourceClientT::interface_type>(),
139 std::make_shared<ResourceClientRegistration2>());
143 template <
typename ResourceServerT>
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());
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));
163 template <
typename ResourceClientT,
typename ResourceServerT>
165 register_resource_client<ResourceClientT>();
166 register_resource_server<ResourceServerT>();
181 const std::unordered_map<std::string, std::shared_ptr<const ModelRegistration>>&
186 const std::unordered_map<API, std::shared_ptr<const ResourceServerRegistration>>&
196 mutable std::mutex lock_;
198 std::unordered_map<std::string, std::shared_ptr<const ModelRegistration>> resources_;
200 std::unordered_map<API, std::shared_ptr<const ResourceClientRegistration>> client_apis_;
201 std::unordered_map<API, std::shared_ptr<const ResourceServerRegistration>> server_apis_;
203 void register_resources();
205 void register_resource_server_(
206 API api, std::shared_ptr<ResourceServerRegistration> resource_registration);
208 void register_resource_client_(
209 API api, std::shared_ptr<ResourceClientRegistration> resource_registration);
211 static const google::protobuf::ServiceDescriptor* get_service_descriptor_(
212 const char* service_full_name);
214 std::shared_ptr<const ModelRegistration> lookup_model_inlock_(
215 const std::string& name,
const std::lock_guard<std::mutex>&)
const;
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.