Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/viam/sdk/module/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service {
ModuleService& parent;

// TODO(RSDK-6528) - to the extent possible, switch to using `server_helper`
::grpc::Status AddResource(::grpc::ServerContext*,
::grpc::Status AddResource(::grpc::ServerContext* ctx,
const ::viam::module::v1::AddResourceRequest* request,
::viam::module::v1::AddResourceResponse*) override {
const viam::app::v1::ComponentConfig& proto = request->config();
Expand All @@ -74,7 +74,8 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service {
}
};
try {
parent.server_->add_resource(res);
auto deadline = ctx->deadline();
parent.server_->add_resource(res, &deadline);
} catch (const std::exception& exc) {
return grpc::Status(::grpc::INTERNAL, exc.what());
}
Expand Down
14 changes: 14 additions & 0 deletions src/viam/sdk/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,21 @@ void Server::register_service(grpc::Service* service) {
builder_->RegisterService(service);
}

namespace {
bool deadline_exceeded(const std::chrono::system_clock::time_point& deadline) {
return deadline < std::chrono::system_clock::now(); // deadline is before now
}
} // namespace

void Server::add_resource(std::shared_ptr<Resource> resource) {
add_resource(std::move(resource), nullptr);
}

void Server::add_resource(std::shared_ptr<Resource> resource,
std::chrono::system_clock::time_point* deadline) {
if (deadline && deadline_exceeded(*deadline)) {
throw Exception(ErrorCondition::k_general, "Deadline expired");
}
auto api = resource->api();
if (managed_servers_.find(api) == managed_servers_.end()) {
std::ostringstream buffer;
Expand Down
8 changes: 8 additions & 0 deletions src/viam/sdk/rpc/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class Server {
/// @throws `Exception` if a matching `ResourceServer` doesn't exist in the server.
void add_resource(std::shared_ptr<Resource> resource);

/// @brief Adds a specific managed resource to the associated resource server
/// @param resource The resource to add
/// @param deadline Deadline after which to not add the resource
/// @throws `Exception` if a matching `ResourceServer` doesn't exist in the server.
/// @throws `Exception` if the deadline is not nil and has passed
void add_resource(std::shared_ptr<Resource> resource,
std::chrono::system_clock::time_point* deadline);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor but i would favor making this a boost::optional


/// @brief Adds a listening port to the server.
/// @param address The address to listen at.
/// @param creds The server credentials; defaults to a insecure server credentials.
Expand Down