Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3

- name: install Doxygen
uses: ssciwr/doxygen-install@v1

- name: create path
run: mkdir -p etc/docs/api/current

Expand Down
6 changes: 3 additions & 3 deletions src/viam/examples/modules/complex/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ int main() {
std::string address(uri);
Options options(1, opts);

// Register custom gizmo and summation API so robot client can access resources
// Register custom gizmo and summation clients so robot client can access resources
// of that type from the server.
Registry::register_resource(API::get<Gizmo>(), Gizmo::resource_registration());
Registry::register_resource(API::get<Summation>(), Summation::resource_registration());
Registry::register_resource_client<GizmoClient>();
Copy link
Member

Choose a reason for hiding this comment

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

👍

Registry::register_resource_client<SummationClient>();

// Connect to robot.
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
Expand Down
31 changes: 1 addition & 30 deletions src/viam/examples/modules/complex/gizmo/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,6 @@
using namespace viam::sdk;
using namespace viam::component::gizmo::v1;

/* GizmoRegistration methods */

GizmoRegistration::GizmoRegistration(const google::protobuf::ServiceDescriptor* service_descriptor)
: ResourceRegistration(service_descriptor){};

std::shared_ptr<ResourceServer> GizmoRegistration::create_resource_server(
std::shared_ptr<ResourceManager> manager, Server& server) {
auto gs = std::make_shared<GizmoServer>(std::move(manager));
server.register_service(gs.get());
return gs;
};

std::shared_ptr<Resource> GizmoRegistration::create_rpc_client(
std::string name, std::shared_ptr<grpc::Channel> chan) {
return std::make_shared<GizmoClient>(std::move(name), std::move(chan));
};

/* Gizmo methods */

std::shared_ptr<ResourceRegistration> Gizmo::resource_registration() {
const google::protobuf::DescriptorPool* p = google::protobuf::DescriptorPool::generated_pool();
const google::protobuf::ServiceDescriptor* sd =
p->FindServiceByName(GizmoService::service_full_name());
if (!sd) {
throw std::runtime_error("Unable to get service descriptor for the gizmo service");
}
return std::make_shared<GizmoRegistration>(sd);
}

API Gizmo::api() const {
return API::get<Gizmo>();
}
Expand All @@ -68,7 +39,7 @@ grpc::Status GizmoServer::DoOne(grpc::ServerContext* context,
"Called [Gizmo::DoOne] without a request");
};

auto rg = ResourceServer::resource_manager()->resource(request->name());
auto rg = resource_manager()->resource(request->name());
if (!rg) {
return grpc::Status(grpc::UNKNOWN, "resource not found: " + request->name());
}
Expand Down
16 changes: 3 additions & 13 deletions src/viam/examples/modules/complex/gizmo/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,9 @@
using namespace viam::sdk;
using namespace viam::component::gizmo::v1;

// `GizmoRegistration` Defines a `ResourceRegistration` for the `Gizmo`
// component.
class GizmoRegistration : public ResourceRegistration {
public:
explicit GizmoRegistration(const google::protobuf::ServiceDescriptor* service_descriptor);
std::shared_ptr<ResourceServer> create_resource_server(std::shared_ptr<ResourceManager> manager,
Server& server) override;
std::shared_ptr<Resource> create_rpc_client(std::string name,
std::shared_ptr<grpc::Channel> chan) override;
};

// `Gizmo` is a custom modular component.
class Gizmo : public Component {
public:
// methods shared across all components
static std::shared_ptr<ResourceRegistration> resource_registration();
API api() const override;

virtual bool do_one(std::string arg1) = 0;
Expand All @@ -53,6 +40,7 @@ struct API::traits<Gizmo> {
// `GizmoClient` is the gRPC client implementation of a `Gizmo` component.
class GizmoClient : public Gizmo {
public:
using interface_type = Gizmo;
GizmoClient(std::string name, std::shared_ptr<grpc::Channel> channel);

bool do_one(std::string arg1) override;
Expand All @@ -70,6 +58,8 @@ class GizmoClient : public Gizmo {
// `GizmoServer` is the gRPC server implementation of a `Gizmo` component.
class GizmoServer : public ResourceServer, public GizmoService::Service {
public:
using interface_type = Gizmo;
using service_type = GizmoService;
explicit GizmoServer(std::shared_ptr<ResourceManager> manager);

grpc::Status DoOne(grpc::ServerContext* context,
Expand Down
21 changes: 7 additions & 14 deletions src/viam/examples/modules/complex/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,29 @@
using namespace viam::sdk;

int main(int argc, char** argv) {
API base_api = API::get<Base>();
Model mybase_model("viam", "base", "mybase");

// Make sure to explicity register resources with custom APIs.
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed the "must be done in main and not in resource implementation" bit of this comment because the idea of doing registration pre-main doesn't really make sense anymore.

Registry::register_resource_server<GizmoServer>();
Registry::register_resource_server<SummationServer>();

std::shared_ptr<ModelRegistration> mybase_mr = std::make_shared<ModelRegistration>(
base_api,
API::get<Base>(),
mybase_model,
[](Dependencies deps, ResourceConfig cfg) { return std::make_unique<MyBase>(deps, cfg); },
MyBase::validate);

API gizmo_api = API::get<Gizmo>();
Model mygizmo_model("viam", "gizmo", "mygizmo");
// Make sure to explicity register resources with custom APIs. Note that
// this must be done in `main` and not in resource implementation files due
// to order of static initialization.
Registry::register_resource(gizmo_api, Gizmo::resource_registration());
std::shared_ptr<ModelRegistration> mygizmo_mr = std::make_shared<ModelRegistration>(
gizmo_api,
API::get<Gizmo>(),
mygizmo_model,
[](Dependencies deps, ResourceConfig cfg) { return std::make_unique<MyGizmo>(deps, cfg); },
MyGizmo::validate);

API summation_api = API::get<Summation>();
Model mysummation_model("viam", "summation", "mysummation");
// Make sure to explicity register resources with custom APIs. Note that
// this must be done in `main` and not in resource implementation files due
// to order of static initialization.
Registry::register_resource(summation_api, Summation::resource_registration());

std::shared_ptr<ModelRegistration> mysummation_mr = std::make_shared<ModelRegistration>(
summation_api, mysummation_model, [](Dependencies deps, ResourceConfig cfg) {
API::get<Summation>(), mysummation_model, [](Dependencies deps, ResourceConfig cfg) {
return std::make_unique<MySummation>(deps, cfg);
});

Expand Down
4 changes: 2 additions & 2 deletions src/viam/examples/modules/complex/proto/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: a86849a25cc04f4dbe9b15ddddfbc488
digest: shake256:e19143328f8cbfe13fc226aeee5e63773ca494693a72740a7560664270039a380d94a1344234b88c7691311460df9a9b1c2982190d0a2612eae80368718e1943
commit: 7e6f6e774e29406da95bd61cdcdbc8bc
digest: shake256:fe43dd2265ea0c07d76bd925eeba612667cf4c948d2ce53d6e367e1b4b3cb5fa69a51e6acb1a6a50d32f894f054a35e6c0406f6808a483f2752e10c866ffbf73
30 changes: 0 additions & 30 deletions src/viam/examples/modules/complex/summation/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,6 @@
using namespace viam::sdk;
using namespace viam::service::summation::v1;

/* SummationRegistration methods */

SummationRegistration::SummationRegistration(
const google::protobuf::ServiceDescriptor* service_descriptor)
: ResourceRegistration(service_descriptor){};

std::shared_ptr<ResourceServer> SummationRegistration::create_resource_server(
std::shared_ptr<ResourceManager> manager, Server& server) {
auto ss = std::make_shared<SummationServer>(std::move(manager));
server.register_service(ss.get());
return ss;
};

std::shared_ptr<Resource> SummationRegistration::create_rpc_client(
std::string name, std::shared_ptr<grpc::Channel> chan) {
return std::make_shared<SummationClient>(std::move(name), std::move(chan));
};

/* Summation methods */

std::shared_ptr<ResourceRegistration> Summation::resource_registration() {
const google::protobuf::DescriptorPool* p = google::protobuf::DescriptorPool::generated_pool();
const google::protobuf::ServiceDescriptor* sd =
p->FindServiceByName(SummationService::service_full_name());
if (!sd) {
throw std::runtime_error("Unable to get service descriptor for the summation service");
}
return std::make_shared<SummationRegistration>(sd);
}

API Summation::api() const {
return API::get<Summation>();
}
Expand Down
15 changes: 3 additions & 12 deletions src/viam/examples/modules/complex/summation/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,10 @@
using namespace viam::sdk;
using namespace viam::service::summation::v1;

// `SummationRegistration` defines a `ResourceRegistration` for the `Summation`
// service.
class SummationRegistration : public ResourceRegistration {
public:
explicit SummationRegistration(const google::protobuf::ServiceDescriptor* service_descriptor);
std::shared_ptr<ResourceServer> create_resource_server(std::shared_ptr<ResourceManager> manager,
Server& server) override;
std::shared_ptr<Resource> create_rpc_client(std::string name,
std::shared_ptr<grpc::Channel> chan) override;
};

// A `Summation` is a custom modular service.
class Summation : public Service {
public:
// methods shared across all services
static std::shared_ptr<ResourceRegistration> resource_registration();
API api() const override;

virtual double sum(std::vector<double> numbers) = 0;
Expand All @@ -52,6 +40,7 @@ struct API::traits<Summation> {
// service.
class SummationClient : public Summation {
public:
using interface_type = Summation;
SummationClient(std::string name, std::shared_ptr<grpc::Channel> channel);

double sum(std::vector<double> numbers) override;
Expand All @@ -66,6 +55,8 @@ class SummationClient : public Summation {
// service.
class SummationServer : public ResourceServer, public SummationService::Service {
public:
using interface_type = Summation;
using service_type = SummationService;
explicit SummationServer(std::shared_ptr<ResourceManager> manager);

grpc::Status Sum(grpc::ServerContext* context,
Expand Down
4 changes: 2 additions & 2 deletions src/viam/examples/modules/complex/test_complex_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ using namespace viam::sdktests;

struct RegisterGizmoAndSummationFixture {
RegisterGizmoAndSummationFixture() {
Registry::register_resource(API::get<Gizmo>(), Gizmo::resource_registration());
Registry::register_resource(API::get<Summation>(), Summation::resource_registration());
Registry::register_resource<GizmoClient, GizmoServer>();
Registry::register_resource<SummationClient, SummationServer>();
}

// Test teardown is a noop;
Expand Down
8 changes: 1 addition & 7 deletions src/viam/sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ endif()

target_sources(viamsdk
PRIVATE
# TODO(RSDK-1742): we have to put `registry` up top here because we need the
Copy link
Member

Choose a reason for hiding this comment

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

👍

# registry types to be defined first, before anything tries to init them.
# this obviously isn't great. it breaks up stylistic ordering and is brittle
# when we need to add updates. we should refactor to make this unnecessary.
# consider making all necessary runtime values a single `context` that has to
# be initialized within main before anything else happens?
registry/registry.cpp
common/client_helper.cpp
common/linear_algebra.cpp
common/pose.cpp
Expand Down Expand Up @@ -87,6 +80,7 @@ target_sources(viamsdk
module/service.cpp
module/signal_manager.cpp
referenceframe/frame.cpp
registry/registry.cpp
resource/resource.cpp
resource/resource_api.cpp
resource/resource_manager.cpp
Expand Down
4 changes: 2 additions & 2 deletions src/viam/sdk/common/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ std::vector<ResourceName> resource_names_for_resource(const std::shared_ptr<Reso
std::string resource_type;
std::string resource_subtype;
std::vector<ResourceName> resource_names;
for (auto& kv : Registry::registered_models()) {
const std::shared_ptr<ModelRegistration> reg = kv.second;
for (const auto& kv : Registry::registered_models()) {
const std::shared_ptr<const ModelRegistration> reg = kv.second;
if (reg->api().to_string() == resource->api().to_string()) {
resource_type = reg->api().resource_type();
resource_subtype = reg->api().resource_subtype();
Expand Down
38 changes: 0 additions & 38 deletions src/viam/sdk/components/base/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,11 @@
#include <viam/api/component/base/v1/base.pb.h>

#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/components/base/client.hpp>
#include <viam/sdk/components/base/server.hpp>
#include <viam/sdk/registry/registry.hpp>
#include <viam/sdk/resource/resource.hpp>

namespace viam {
namespace sdk {

BaseRegistration::BaseRegistration(const google::protobuf::ServiceDescriptor* service_descriptor)
: ResourceRegistration(service_descriptor){};

std::shared_ptr<ResourceServer> BaseRegistration::create_resource_server(
std::shared_ptr<ResourceManager> manager, Server& server) {
auto bs = std::make_shared<BaseServer>(manager);
server.register_service(bs.get());
return bs;
};

std::shared_ptr<Resource> BaseRegistration::create_rpc_client(std::string name,
std::shared_ptr<grpc::Channel> chan) {
return std::make_shared<BaseClient>(std::move(name), std::move(chan));
};

std::shared_ptr<ResourceRegistration> Base::resource_registration() {
const google::protobuf::DescriptorPool* p = google::protobuf::DescriptorPool::generated_pool();
const google::protobuf::ServiceDescriptor* sd =
p->FindServiceByName(viam::component::base::v1::BaseService::service_full_name());
if (!sd) {
throw std::runtime_error("Unable to get service descriptor for the base service");
}
return std::make_shared<BaseRegistration>(sd);
}

API Base::api() const {
return API::get<Base>();
}
Expand Down Expand Up @@ -68,15 +40,5 @@ bool operator==(const Base::properties& lhs, const Base::properties& rhs) {

Base::Base(std::string name) : Component(std::move(name)){};

namespace {
bool init() {
Registry::register_resource(API::get<Base>(), Base::resource_registration());
return true;
};

// NOLINTNEXTLINE
const bool inited = init();
} // namespace

} // namespace sdk
} // namespace viam
17 changes: 0 additions & 17 deletions src/viam/sdk/components/base/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,13 @@
#include <viam/sdk/common/proto_type.hpp>
#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/config/resource.hpp>
#include <viam/sdk/registry/registry.hpp>
#include <viam/sdk/resource/resource_manager.hpp>
#include <viam/sdk/resource/stoppable.hpp>

namespace viam {
namespace sdk {

/// @defgroup Base Classes related to the Base component.

/// @class BaseRegistration
/// @brief Defines a `ResourceRegistration` for the `Base` component.
/// @ingroup Base
class BaseRegistration : public ResourceRegistration {
public:
explicit BaseRegistration(const google::protobuf::ServiceDescriptor* service_descriptor);
std::shared_ptr<ResourceServer> create_resource_server(std::shared_ptr<ResourceManager> manager,
Server& server) override;
std::shared_ptr<Resource> create_rpc_client(std::string name,
std::shared_ptr<grpc::Channel> chan) override;
};

/// @class Base base.hpp "components/base/base.hpp"
/// @brief A `Base` is the platform that the other parts of a mobile robot attach to.
/// @ingroup Base
Expand All @@ -52,9 +38,6 @@ class Base : public Component, public Stoppable {
friend std::ostream& operator<<(std::ostream& os, const properties& v);
friend bool operator==(const properties& lhs, const properties& rhs);

// functions shared across all components
static std::shared_ptr<ResourceRegistration> resource_registration();
Copy link
Member

Choose a reason for hiding this comment

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

BTW, I should add that I am happy to see the registration entry points removed from the base classes. It was a little weird that the base classes were responsible for registering the client subclasses and associated servers types.


/// @brief Move a robot's base in a straight line by a given distance. This method blocks
/// until completed or cancelled
/// @param distance_mm Desired travel distance in millimeters
Expand Down
1 change: 1 addition & 0 deletions src/viam/sdk/components/base/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace sdk {
/// @ingroup Base
class BaseClient : public Base {
public:
using interface_type = Base;
BaseClient(std::string name, std::shared_ptr<grpc::Channel> channel);
void move_straight(int64_t distance_mm, double mm_per_sec, const AttributeMap& extra) override;
void spin(double angle_deg, double degs_per_sec, const AttributeMap& extra) override;
Expand Down
Loading