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>
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>
34 std::shared_ptr<ResourceManager> manager,
Server& server)
const = 0;
43 const google::protobuf::ServiceDescriptor* service_descriptor_;
57 std::string name, std::shared_ptr<grpc::Channel> channel)
const = 0;
71 std::function<std::shared_ptr<Resource>(Dependencies,
ResourceConfig)> constructor)
74 model_(std::move(model)),
75 api_(std::move(api)){};
80 std::function<std::shared_ptr<Resource>(Dependencies,
ResourceConfig)> constructor,
84 model_(std::move(model)),
85 api_(std::move(api)){};
87 const API& api()
const;
88 const Model& model()
const;
99 viam::robot::v1::Status
create_status(
const std::shared_ptr<Resource>& resource)
const;
106 static std::vector<std::string> default_validator(
ResourceConfig cfg) {
123 static std::shared_ptr<const ModelRegistration>
lookup_model(
const std::string& name);
133 template <
typename ResourceClientT>
137 using ResourceClientRegistration::ResourceClientRegistration;
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));
145 Registry::register_resource_client_(API::get<typename ResourceClientT::interface_type>(),
146 std::make_shared<ResourceClientRegistration2>());
150 template <
typename ResourceServerT>
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());
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));
170 template <
typename ResourceClientT,
typename ResourceServerT>
172 register_resource_client<ResourceClientT>();
173 register_resource_server<ResourceServerT>();
188 static const std::unordered_map<std::string, std::shared_ptr<const ModelRegistration>>&
193 static const std::unordered_map<API, std::shared_ptr<const ResourceServerRegistration>>&
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_;
206 static void register_resource_server_(
207 API api, std::shared_ptr<ResourceServerRegistration> resource_registration);
209 static void register_resource_client_(
210 API api, std::shared_ptr<ResourceClientRegistration> resource_registration);
212 static const google::protobuf::ServiceDescriptor* get_service_descriptor_(
213 const char* service_full_name);
215 static std::shared_ptr<const ModelRegistration> lookup_model_inlock_(
216 const std::string& name,
const std::lock_guard<std::mutex>&);
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.