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) {
114 static std::shared_ptr<const ModelRegistration>
lookup_model(
const std::string& name);
124 template <
typename ResourceClientT>
128 using ResourceClientRegistration::ResourceClientRegistration;
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));
136 Registry::register_resource_client_(API::get<typename ResourceClientT::interface_type>(),
137 std::make_shared<ResourceClientRegistration2>());
141 template <
typename ResourceServerT>
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());
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));
161 template <
typename ResourceClientT,
typename ResourceServerT>
163 register_resource_client<ResourceClientT>();
164 register_resource_server<ResourceServerT>();
179 static const std::unordered_map<std::string, std::shared_ptr<const ModelRegistration>>&
184 static const std::unordered_map<API, std::shared_ptr<const ResourceServerRegistration>>&
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_;
197 static void register_resource_server_(
198 API api, std::shared_ptr<ResourceServerRegistration> resource_registration);
200 static void register_resource_client_(
201 API api, std::shared_ptr<ResourceClientRegistration> resource_registration);
203 static const google::protobuf::ServiceDescriptor* get_service_descriptor_(
204 const char* service_full_name);
206 static std::shared_ptr<const ModelRegistration> lookup_model_inlock_(
207 const std::string& name,
const std::lock_guard<std::mutex>&);
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.