Skip to content

Commit 0e9ccd5

Browse files
authored
RSDK-9549: Make RpcSubtype model api ResourceRPCSubtype (#349)
1 parent 7761d2a commit 0e9ccd5

File tree

12 files changed

+134
-151
lines changed

12 files changed

+134
-151
lines changed

src/viam/examples/modules/simple/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <iostream>
12
#include <memory>
23
#include <string>
34

src/viam/examples/motor/example_motor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
#include <unistd.h>
2+
13
#include <chrono>
24
#include <fstream>
5+
#include <iostream>
36
#include <string>
4-
#include <unistd.h>
57
#include <vector>
68

79
#include <viam/sdk/components/motor.hpp>

src/viam/sdk/common/utils.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <chrono>
34
#include <memory>
45

56
#include <boost/optional/optional.hpp>

src/viam/sdk/config/resource.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,19 @@ const ProtoStruct& ResourceConfig::attributes() const {
9595
void ResourceConfig::fix_api() {
9696
if (this->api_.type_namespace().empty() && this->namespace__.empty()) {
9797
this->namespace__ = kRDK;
98-
this->api_.set_namespace(kRDK);
98+
this->api_.namespace_ = kRDK;
9999
} else if (this->api_.type_namespace().empty()) {
100-
this->api_.set_namespace(this->namespace__);
100+
api_.namespace_ = namespace__;
101101
} else {
102102
this->namespace__ = this->api_.type_namespace();
103103
}
104104

105105
if (this->api_.resource_type().empty()) {
106-
this->api_.set_resource_type(kComponent);
106+
api_.resource_type_ = kComponent;
107107
}
108108

109109
if (this->api_.resource_subtype().empty()) {
110-
this->api_.set_resource_subtype(this->type_);
110+
api_.resource_subtype_ = this->type_;
111111
} else if (this->type_.empty()) {
112112
this->type_ = this->api_.resource_subtype();
113113
}

src/viam/sdk/module/handler_map.cpp

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <memory>
44

55
#include <boost/log/trivial.hpp>
6-
#include <google/protobuf/descriptor.h>
76

87
#include <viam/api/common/v1/common.pb.h>
98
#include <viam/api/module/v1/module.pb.h>
@@ -14,70 +13,68 @@
1413
namespace viam {
1514
namespace sdk {
1615

17-
viam::module::v1::HandlerMap HandlerMap_::to_proto() const {
18-
viam::module::v1::HandlerMap proto;
19-
for (const auto& h : this->handles_) {
16+
void HandlerMap_::add_model(Model model, const RPCSubtype& subtype) {
17+
handles_[subtype].push_back(std::move(model));
18+
}
19+
20+
const std::unordered_map<RPCSubtype, std::vector<Model>>& HandlerMap_::handles() const {
21+
return handles_;
22+
}
23+
24+
std::ostream& operator<<(std::ostream& os, const HandlerMap_& hm) {
25+
for (const auto& kv : hm.handles()) {
26+
os << "API: " << kv.first.api().to_string() << '\n';
27+
for (const Model& model : kv.second) {
28+
os << "\tModel: " << model.to_string() << '\n';
29+
}
30+
}
31+
return os;
32+
}
33+
34+
namespace proto_convert_details {
35+
36+
void to_proto<HandlerMap_>::operator()(const HandlerMap_& self,
37+
module::v1::HandlerMap* proto) const {
38+
for (const auto& h : self.handles()) {
2039
viam::module::v1::HandlerDefinition hd;
2140
for (const auto& model : h.second) {
22-
const std::string m = model.to_string();
23-
*hd.mutable_models()->Add() = m;
41+
*hd.mutable_models()->Add() = model.to_string();
2442
}
43+
2544
viam::robot::v1::ResourceRPCSubtype rpc_subtype;
26-
const Name name(h.first.api(), "", "");
27-
const viam::common::v1::ResourceName resource_name = v2::to_proto(name);
28-
*rpc_subtype.mutable_subtype() = resource_name;
45+
46+
*rpc_subtype.mutable_subtype() = v2::to_proto(Name(h.first.api(), "", ""));
2947
*rpc_subtype.mutable_proto_service() = h.first.proto_service_name();
3048
*hd.mutable_subtype() = rpc_subtype;
31-
*proto.add_handlers() = hd;
32-
}
33-
34-
return proto;
35-
};
3649

37-
HandlerMap_::HandlerMap_() {}
50+
*proto->add_handlers() = hd;
51+
}
52+
}
3853

39-
// NOLINTNEXTLINE(readability-const-return-type)
40-
const HandlerMap_ HandlerMap_::from_proto(const viam::module::v1::HandlerMap& proto) {
54+
HandlerMap_ from_proto<module::v1::HandlerMap>::operator()(
55+
const module::v1::HandlerMap* proto) const {
4156
HandlerMap_ hm;
4257

4358
const google::protobuf::RepeatedPtrField<viam::module::v1::HandlerDefinition>& handlers =
44-
proto.handlers();
59+
proto->handlers();
4560

4661
for (const auto& handler : handlers) {
47-
std::vector<Model> models;
4862
const viam::common::v1::ResourceName name = handler.subtype().subtype();
4963
const API api(name.namespace_(), name.type(), name.subtype());
50-
const google::protobuf::DescriptorPool* pool =
51-
google::protobuf::DescriptorPool::generated_pool();
52-
const google::protobuf::ServiceDescriptor* sd = pool->FindServiceByName(name.type());
53-
const RPCSubtype handle(api, *sd);
64+
const RPCSubtype handle(api);
5465
for (const auto& mod : handler.models()) {
5566
try {
56-
const Model model = Model::from_str(mod);
57-
models.push_back(model);
58-
} catch (std::string error) { // NOLINT
59-
BOOST_LOG_TRIVIAL(error) << "Error processing model " + mod;
67+
hm.add_model(Model::from_str(mod), handle);
68+
} catch (const std::exception& ex) {
69+
BOOST_LOG_TRIVIAL(error) << "Error " << ex.what() << " processing model " + mod;
6070
}
6171
}
62-
hm.handles_.emplace(handle, models);
6372
}
6473

6574
return hm;
6675
}
6776

68-
void HandlerMap_::add_model(Model model, const RPCSubtype& subtype) {
69-
handles_[subtype].push_back(std::move(model));
70-
}
71-
72-
std::ostream& operator<<(std::ostream& os, const HandlerMap_& hm) {
73-
for (const auto& kv : hm.handles_) {
74-
os << "API: " << kv.first.api().to_string() << '\n';
75-
for (const Model& model : kv.second) {
76-
os << "\tModel: " << model.to_string() << '\n';
77-
}
78-
}
79-
return os;
80-
}
77+
} // namespace proto_convert_details
8178

8279
} // namespace sdk
8380
} // namespace viam
Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
11
#pragma once
22

3-
#include <viam/api/module/v1/module.pb.h>
4-
3+
#include <viam/sdk/common/proto_convert.hpp>
54
#include <viam/sdk/resource/resource.hpp>
65

6+
namespace viam {
7+
namespace module {
8+
namespace v1 {
9+
10+
class HandlerMap;
11+
12+
} // namespace v1
13+
} // namespace module
14+
} // namespace viam
15+
716
namespace viam {
817
namespace sdk {
918

1019
class HandlerMap_ {
1120
public:
12-
HandlerMap_();
21+
HandlerMap_() = default;
22+
1323
void add_model(Model model, const RPCSubtype& subtype);
1424

15-
viam::module::v1::HandlerMap to_proto() const;
16-
static const HandlerMap_ from_proto(const viam::module::v1::HandlerMap& proto);
17-
friend std::ostream& operator<<(std::ostream& os, const HandlerMap_& hm);
25+
const std::unordered_map<RPCSubtype, std::vector<Model>>& handles() const;
1826

1927
private:
2028
std::unordered_map<RPCSubtype, std::vector<Model>> handles_;
2129
};
2230

31+
std::ostream& operator<<(std::ostream& os, const HandlerMap_& hm);
32+
33+
namespace proto_convert_details {
34+
35+
template <>
36+
struct to_proto<HandlerMap_> {
37+
void operator()(const HandlerMap_&, module::v1::HandlerMap*) const;
38+
};
39+
40+
template <>
41+
struct from_proto<module::v1::HandlerMap> {
42+
HandlerMap_ operator()(const module::v1::HandlerMap*) const;
43+
};
44+
45+
} // namespace proto_convert_details
2346
} // namespace sdk
2447
} // namespace viam

src/viam/sdk/module/service.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service {
5555
const std::lock_guard<std::mutex> lock(parent.lock_);
5656

5757
std::shared_ptr<Resource> res;
58-
const Dependencies deps = parent.get_dependencies_(request->dependencies(), cfg.name());
58+
const Dependencies deps = parent.get_dependencies_(&request->dependencies(), cfg.name());
5959
const std::shared_ptr<const ModelRegistration> reg =
6060
Registry::lookup_model(cfg.api(), cfg.model());
6161
if (reg) {
@@ -81,7 +81,7 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service {
8181
const viam::app::v1::ComponentConfig& proto = request->config();
8282
ResourceConfig cfg = v2::from_proto(proto);
8383

84-
const Dependencies deps = parent.get_dependencies_(request->dependencies(), cfg.name());
84+
const Dependencies deps = parent.get_dependencies_(&request->dependencies(), cfg.name());
8585

8686
auto resource_server = parent.server_->lookup_resource_server(cfg.api());
8787
if (!resource_server) {
@@ -179,7 +179,7 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service {
179179
const ::viam::module::v1::ReadyRequest* request,
180180
::viam::module::v1::ReadyResponse* response) override {
181181
const std::lock_guard<std::mutex> lock(parent.lock_);
182-
const viam::module::v1::HandlerMap hm = parent.module_->handles().to_proto();
182+
const viam::module::v1::HandlerMap hm = v2::to_proto(parent.module_->handles());
183183
*response->mutable_handlermap() = hm;
184184
parent.parent_addr_ = request->parent_address();
185185
response->set_ready(parent.module_->ready());
@@ -188,10 +188,10 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service {
188188
};
189189

190190
Dependencies ModuleService::get_dependencies_(
191-
google::protobuf::RepeatedPtrField<std::string> const& proto,
191+
google::protobuf::RepeatedPtrField<std::string> const* proto,
192192
std::string const& resource_name) {
193193
Dependencies deps;
194-
for (const auto& dep : proto) {
194+
for (const auto& dep : *proto) {
195195
auto dep_name = Name::from_string(dep);
196196
const std::shared_ptr<Resource> dep_resource = get_parent_resource_(dep_name);
197197
if (!dep_resource) {
@@ -276,12 +276,10 @@ void ModuleService::add_model_from_registry_inlock_(API api,
276276
const std::shared_ptr<const ResourceServerRegistration> creator =
277277
Registry::lookup_resource_server(api);
278278
std::string name;
279-
const google::protobuf::ServiceDescriptor* sd = nullptr;
280279
if (creator && creator->service_descriptor()) {
281280
name = creator->service_descriptor()->full_name();
282-
sd = creator->service_descriptor();
283281
}
284-
const RPCSubtype rpc_subtype(std::move(api), name, *sd);
282+
const RPCSubtype rpc_subtype(std::move(api), name);
285283
module_->mutable_handles().add_model(std::move(model), rpc_subtype);
286284
};
287285

src/viam/sdk/module/service.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
#include <viam/sdk/resource/resource.hpp>
99
#include <viam/sdk/rpc/server.hpp>
1010

11+
namespace google {
12+
namespace protobuf {
13+
14+
template <typename>
15+
class RepeatedPtrField;
16+
17+
} // namespace protobuf
18+
} // namespace google
19+
1120
namespace viam {
1221
namespace sdk {
1322

@@ -51,7 +60,7 @@ class ModuleService {
5160
friend ModuleService::ServiceImpl;
5261

5362
void add_model_from_registry_inlock_(API api, Model model, const std::lock_guard<std::mutex>&);
54-
Dependencies get_dependencies_(google::protobuf::RepeatedPtrField<std::string> const& proto,
63+
Dependencies get_dependencies_(google::protobuf::RepeatedPtrField<std::string> const* proto,
5564
std::string const& resource_name);
5665
std::shared_ptr<Resource> get_parent_resource_(const Name& name);
5766

src/viam/sdk/resource/resource.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ std::string Resource::name() const {
1616
}
1717

1818
Name Resource::get_resource_name(const std::string& type) const {
19-
auto api_ = api();
20-
api_.set_resource_type(type);
21-
2219
auto name_parts = long_name_to_remote_and_short(name_);
23-
return {api_, name_parts.first, name_parts.second};
20+
return {API(api().type_namespace(), type, api().resource_subtype()),
21+
name_parts.first,
22+
name_parts.second};
2423
}
2524

2625
Name Resource::get_resource_name() const {

0 commit comments

Comments
 (0)