Skip to content

Commit

Permalink
RSDK-5881 Add Stoppable class
Browse files Browse the repository at this point in the history
  • Loading branch information
benjirewis committed Nov 29, 2023
1 parent e209c57 commit afe1d68
Show file tree
Hide file tree
Showing 29 changed files with 42 additions and 48 deletions.
2 changes: 0 additions & 2 deletions src/viam/examples/modules/complex/gizmo/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,6 @@ std::vector<bool> GizmoClient::do_one_bidi_stream(std::vector<std::string> arg1)
return rets;
}

void GizmoClient::stop(const AttributeMap& extra) {}

std::string GizmoClient::do_two(bool arg1) {
DoTwoRequest request;
DoTwoResponse response;
Expand Down
1 change: 0 additions & 1 deletion src/viam/examples/modules/complex/gizmo/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class GizmoClient : public Gizmo {
std::vector<bool> do_one_server_stream(std::string arg1) override;
std::vector<bool> do_one_bidi_stream(std::vector<std::string> arg1) override;
std::string do_two(bool arg1) override;
void stop(const AttributeMap& extra) override;

private:
std::unique_ptr<GizmoService::StubInterface> stub_;
Expand Down
2 changes: 0 additions & 2 deletions src/viam/examples/modules/complex/summation/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,3 @@ double SummationClient::sum(std::vector<double> numbers) {

return response.sum();
}

void SummationClient::stop(const AttributeMap& extra) {}
1 change: 0 additions & 1 deletion src/viam/examples/modules/complex/summation/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class SummationClient : public Summation {
SummationClient(std::string name, std::shared_ptr<grpc::Channel> channel);

double sum(std::vector<double> numbers) override;
void stop(const AttributeMap& extra) override;

private:
std::unique_ptr<SummationService::StubInterface> stub_;
Expand Down
2 changes: 1 addition & 1 deletion src/viam/examples/modules/tflite/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class MLModelServiceTFLite : public vsdk::MLModelService {
// drain.
}

void stop(const vsdk::AttributeMap& extra) noexcept final {
void stop(const vsdk::AttributeMap& extra) noexcept final override {
return stop();
}

Expand Down
2 changes: 1 addition & 1 deletion src/viam/sdk/components/base/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class BaseRegistration : public ResourceRegistration {
///
/// This acts as an abstract parent class to be inherited from by any drivers representing
/// specific base implementations. This class cannot be used on its own.
class Base : public Component {
class Base : public Component, public Stoppable {
public:
/// @struct properties
/// @brief Information about the physical base
Expand Down
2 changes: 0 additions & 2 deletions src/viam/sdk/components/board/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ void BoardClient::set_gpio(const std::string& pin, bool high, const AttributeMap
}
}

void BoardClient::stop(const AttributeMap& extra) {}

bool BoardClient::get_gpio(const std::string& pin, const AttributeMap& extra) {
viam::component::board::v1::GetGPIORequest request;
viam::component::board::v1::GetGPIOResponse response;
Expand Down
1 change: 0 additions & 1 deletion src/viam/sdk/components/board/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace sdk {
class BoardClient : public Board {
public:
BoardClient(std::string name, std::shared_ptr<grpc::Channel> channel);
void stop(const AttributeMap& extra) override;
AttributeMap do_command(const AttributeMap& command) override;
status get_status(const AttributeMap& extra) override;
void set_gpio(const std::string& pin, bool high, const AttributeMap& extra) override;
Expand Down
2 changes: 0 additions & 2 deletions src/viam/sdk/components/camera/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,5 @@ Camera::properties CameraClient::get_properties() {
return from_proto(resp);
};

void CameraClient::stop(const AttributeMap& extra) {}

} // namespace sdk
} // namespace viam
1 change: 0 additions & 1 deletion src/viam/sdk/components/camera/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace sdk {
/// @ingroup Camera
class CameraClient : public Camera {
public:
void stop(const AttributeMap& extra) override;
CameraClient(std::string name, std::shared_ptr<grpc::Channel> channel);
AttributeMap do_command(AttributeMap command) override;
raw_image get_image(std::string mime_type, const AttributeMap& extra) override;
Expand Down
2 changes: 0 additions & 2 deletions src/viam/sdk/components/encoder/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,5 @@ AttributeMap EncoderClient::do_command(AttributeMap command) {
return struct_to_map(response.result());
}

void EncoderClient::stop(const AttributeMap& extra) {}

} // namespace sdk
} // namespace viam
1 change: 0 additions & 1 deletion src/viam/sdk/components/encoder/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace sdk {
class EncoderClient : public Encoder {
public:
EncoderClient(std::string name, std::shared_ptr<grpc::Channel> channel);
void stop(const AttributeMap& extra) override;
position get_position(const AttributeMap& extra, position_type position_type) override;
void reset_position(const AttributeMap& extra) override;
properties get_properties(const AttributeMap& extra) override;
Expand Down
2 changes: 0 additions & 2 deletions src/viam/sdk/components/generic/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ AttributeMap GenericClient::do_command(AttributeMap command) {
return struct_to_map(resp.result());
}

void GenericClient::stop(const AttributeMap& extra) {}

std::vector<GeometryConfig> GenericClient::get_geometries() {
viam::common::v1::GetGeometriesRequest req;
viam::common::v1::GetGeometriesResponse resp;
Expand Down
1 change: 0 additions & 1 deletion src/viam/sdk/components/generic/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace sdk {
/// @ingroup Generic
class GenericClient : public Generic {
public:
void stop(const AttributeMap& extra) override;
GenericClient(std::string name, std::shared_ptr<grpc::Channel> channel);
AttributeMap do_command(AttributeMap command) override;
std::vector<GeometryConfig> get_geometries() override;
Expand Down
2 changes: 1 addition & 1 deletion src/viam/sdk/components/motor/motor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MotorRegistration : public ResourceRegistration {
///
/// This acts as an abstract base class to be inherited from by any drivers representing
/// specific motor implementations. This class cannot be used on its own.
class Motor : public Component {
class Motor : public Component, public Stoppable {
public:
/// @struct position
/// @brief Current position of the motor relative to its home
Expand Down
2 changes: 0 additions & 2 deletions src/viam/sdk/components/movement_sensor/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,5 @@ std::vector<GeometryConfig> MovementSensorClient::get_geometries(const Attribute
.invoke([](auto& response) { return GeometryConfig::from_proto(response); });
}

void MovementSensorClient::stop(const AttributeMap& extra) {}

} // namespace sdk
} // namespace viam
1 change: 0 additions & 1 deletion src/viam/sdk/components/movement_sensor/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace sdk {
class MovementSensorClient : public MovementSensor {
public:
MovementSensorClient(std::string name, std::shared_ptr<grpc::Channel> channel);
void stop(const AttributeMap& extra) override;
Vector3 get_linear_velocity(const AttributeMap& extra) override;
Vector3 get_angular_velocity(const AttributeMap& extra) override;
compassheading get_compass_heading(const AttributeMap& extra) override;
Expand Down
2 changes: 0 additions & 2 deletions src/viam/sdk/components/power_sensor/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,5 @@ AttributeMap PowerSensorClient::do_command(const AttributeMap& command) {
.invoke([](auto& response) { return struct_to_map(response.result()); });
}

void PowerSensorClient::stop(const AttributeMap& extra) {}

} // namespace sdk
} // namespace viam
1 change: 0 additions & 1 deletion src/viam/sdk/components/power_sensor/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace sdk {
/// @ingroup PowerSensor
class PowerSensorClient : public PowerSensor {
public:
void stop(const AttributeMap& extra) override;
PowerSensorClient(std::string name, std::shared_ptr<grpc::Channel> channel);
voltage get_voltage(const AttributeMap& extra) override;
current get_current(const AttributeMap& extra) override;
Expand Down
2 changes: 0 additions & 2 deletions src/viam/sdk/components/sensor/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,5 @@ std::vector<GeometryConfig> SensorClient::get_geometries(const AttributeMap& ext
.invoke([](auto& response) { return GeometryConfig::from_proto(response); });
}

void SensorClient::stop(const AttributeMap& extra) {}

} // namespace sdk
} // namespace viam
1 change: 0 additions & 1 deletion src/viam/sdk/components/sensor/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace sdk {
/// @ingroup Sensor
class SensorClient : public Sensor {
public:
void stop(const AttributeMap& extra) override;
SensorClient(std::string name, std::shared_ptr<grpc::Channel> channel);
AttributeMap get_readings(const AttributeMap& extra) override;
AttributeMap do_command(const AttributeMap& command) override;
Expand Down
4 changes: 2 additions & 2 deletions src/viam/sdk/module/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ ::grpc::Status ModuleService_::ReconfigureResource(

// if the type isn't reconfigurable by default, replace it
try {
res->stop();
stop_resource_if_stoppable(res);
} catch (const std::exception& err) {
BOOST_LOG_TRIVIAL(error) << "unable to stop resource: " << err.what();
}
Expand Down Expand Up @@ -194,7 +194,7 @@ ::grpc::Status ModuleService_::RemoveResource(
}

try {
res->stop();
stop_resource_if_stoppable(res);
} catch (const std::exception& err) {
BOOST_LOG_TRIVIAL(error) << "unable to stop resource: " << err.what();
}
Expand Down
7 changes: 7 additions & 0 deletions src/viam/sdk/resource/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,12 @@ ResourceName Resource::get_resource_name(std::string name) {
return r;
}

void stop_resource_if_stoppable(std::shared_ptr<Resource> resource) {
auto stoppable_res = std::dynamic_pointer_cast<Stoppable>(resource);
if (stoppable_res != NULL) {
stoppable_res->stop();
}
}

} // namespace sdk
} // namespace viam
25 changes: 16 additions & 9 deletions src/viam/sdk/resource/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ class Resource {
/// @brief Returns a `ResourceName` for a particular resource name.
virtual viam::common::v1::ResourceName get_resource_name(std::string name);

/// @brief Stops a resource from running.
/// @param extra Extra arguments to pass to the resource's `stop` method.
virtual void stop(const AttributeMap& extra) = 0;

/// @brief Stops a resource from running.
inline void stop() {
return stop({});
}

/// @brief Reconfigures a resource.
/// @param deps Dependencies of the resource.
/// @param cfg The resource's config.
Expand All @@ -51,5 +42,21 @@ class Resource {
std::string name_;
};

class Stoppable {
public:
/// @brief Stops a resource from running.
/// @param extra Extra arguments to pass to the resource's `stop` method.
virtual void stop(const AttributeMap& extra) = 0;

/// @brief Stops a resource from running.
inline void stop() {
return stop({});
}
};

/// @brief Stops a Resource if it is Stoppable.
/// @param resource The Resource to stop.
void stop_resource_if_stoppable(std::shared_ptr<Resource> resource);

} // namespace sdk
} // namespace viam
6 changes: 3 additions & 3 deletions src/viam/sdk/robot/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ ::grpc::Status RobotService_::StopAll(::grpc::ServerContext* context,
const std::string rn_ = rn.SerializeAsString();
if (extra.find(rn_) != extra.end()) {
try {
resource->stop(extra.at(rn_));
stop_resource_if_stoppable(resource);
} catch (const std::runtime_error& err) {
try {
status_message = err.what();
resource->stop();
stop_resource_if_stoppable(resource);
} catch (std::runtime_error& err) {
status_message = err.what();
status = grpc::UNKNOWN;
Expand All @@ -179,7 +179,7 @@ ::grpc::Status RobotService_::StopAll(::grpc::ServerContext* context,
}
} else {
try {
resource->stop();
stop_resource_if_stoppable(resource);
} catch (std::runtime_error& err) {
status_message = err.what();
status = grpc::UNKNOWN;
Expand Down
11 changes: 10 additions & 1 deletion src/viam/sdk/services/mlmodel/mlmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MLModelServiceRegistration : public ResourceRegistration {
/// This class acts as an abstract base class to be used by any driver
/// implementing an MLModel. It is also used as the base class for the
/// gRPC client for communicating with a remote MLModel instance.
class MLModelService : public Service {
class MLModelService : public Service, public Stoppable {
private:
template <typename T>
struct make_tensor_view_ {
Expand Down Expand Up @@ -180,6 +180,15 @@ class MLModelService : public Service {
/// @param `extra`: Any additional arguments to the method.
virtual struct metadata metadata(const AttributeMap& extra) = 0;

/// @brief Stops a resource from running.
inline void stop() {
return stop({});
}

/// @brief Stops a resource from running.
/// @param extra Extra arguments to pass to the resource's `stop` method.
void stop(const AttributeMap& extra) override = 0;

protected:
explicit MLModelService(std::string name);
};
Expand Down
2 changes: 0 additions & 2 deletions src/viam/sdk/services/motion/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,5 @@ AttributeMap MotionClient::do_command(const AttributeMap& command) {
return struct_to_map(response.result());
}

void MotionClient::stop(const AttributeMap& extra) {}

} // namespace sdk
} // namespace viam
1 change: 0 additions & 1 deletion src/viam/sdk/services/motion/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace sdk {
class MotionClient : public Motion {
public:
MotionClient(std::string name, std::shared_ptr<grpc::Channel> channel);
void stop(const AttributeMap& extra) override;
bool move(const pose_in_frame& destination,
const Name& component_name,
std::shared_ptr<WorldState> world_state,
Expand Down
1 change: 1 addition & 0 deletions src/viam/sdk/tests/mocks/mlmodel_mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class MockMLModelService : public sdk::MLModelService {
MockMLModelService& set_infer_handler(infer_handler handler);
std::shared_ptr<named_tensor_views> infer(const named_tensor_views& inputs,
const sdk::AttributeMap& extra) override;
void stop(const sdk::AttributeMap& extra) override{};

MockMLModelService& set_metadata(struct metadata metadata);
struct metadata metadata(const sdk::AttributeMap& extra) override;
Expand Down

0 comments on commit afe1d68

Please sign in to comment.