Skip to content

Commit 8ba5efa

Browse files
committed
overhaul options and refresh interval
1 parent afa6c70 commit 8ba5efa

File tree

13 files changed

+47
-63
lines changed

13 files changed

+47
-63
lines changed

src/viam/examples/camera/example_camera.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ int main() try {
3131
dial_options.allow_insecure_downgrade =
3232
(credentials.type().empty() && credentials.payload().empty());
3333

34-
// Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial
35-
// options
36-
vs::Options options = vs::Options(1, dial_options);
34+
// Pass the scheduled refresh interval of the robot (0 seconds = only on config update) and the
35+
// dial options
36+
std::shared_ptr<vs::RobotClient> robot =
37+
vs::RobotClient::at_address(robot_address, std::chrono::seconds{1}, dial_options);
3738

38-
std::shared_ptr<vs::RobotClient> robot = vs::RobotClient::at_address(robot_address, options);
3939
VIAM_SDK_LOG(info) << "Successfully connected to the robot";
4040

4141
std::vector<vs::Name> resource_names = robot->resource_names();

src/viam/examples/dial/example_dial.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ int main() {
2727
std::string payload = "<your authentication payload>";
2828
dial_options.credentials = Credentials(type, payload);
2929
std::string address(uri);
30-
Options options(1, dial_options);
3130

3231
// connect to robot, ensure we can refresh it
33-
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
32+
std::shared_ptr<RobotClient> robot =
33+
RobotClient::at_address(address, std::chrono::seconds{1}, dial_options);
3434

3535
// ensure we can query resources
3636
std::vector<Name> resource_names = robot->resource_names();

src/viam/examples/dial_api_key/example_dial_api_key.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int main(int argc, char* argv[]) {
4646

4747
// connect to robot, ensure we can refresh it
4848
std::shared_ptr<RobotClient> robot =
49-
RobotClient::at_address(vm["uri"].as<std::string>(), Options(1, opts));
49+
RobotClient::at_address(vm["uri"].as<std::string>(), std::chrono::seconds{1}, opts);
5050

5151
// ensure we can query resources
5252
std::vector<Name> resource_names = robot->resource_names();

src/viam/examples/mlmodel/example_audio_classification_client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ int main(int argc, char* argv[]) try {
246246
dial_options.auth_entity = opt_api_key_id.get();
247247
dial_options.credentials = viam::sdk::Credentials("api-key", opt_api_key.get());
248248

249-
auto robot =
250-
vsdk::RobotClient::at_address(opt_robot_host.get(), {0, {std::move(dial_options)}});
249+
auto robot = vsdk::RobotClient::at_address(
250+
opt_robot_host.get(), std::chrono::seconds{0}, dial_options);
251251

252252
// Obtain a handle to the MLModelService module on the robot. Note that the string
253253
// `yamnet_classification_tflite` is arbitrary. It just matches what was used to name the

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main() {
2626
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
2727
Instance inst;
2828

29-
const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely
29+
std::string address = "http://localhost:8080/"; // replace with your URI if connecting securely
3030
DialOptions dial_options;
3131
dial_options.allow_insecure_downgrade = true; // set to false if connecting securely
3232

@@ -36,17 +36,15 @@ int main() {
3636
// Credentials credentials(type, payload);
3737
// dial_options.set_credentials(credentials);
3838

39-
boost::optional<DialOptions> opts(dial_options);
40-
std::string address(uri);
41-
Options options(1, opts);
42-
4339
// Register custom gizmo and summation clients so robot client can access resources
4440
// of that type from the server.
4541
Registry::get().register_resource_client<GizmoClient>();
4642
Registry::get().register_resource_client<SummationClient>();
4743

4844
// Connect to robot.
49-
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
45+
std::shared_ptr<RobotClient> robot =
46+
RobotClient::at_address(address, std::chrono::seconds{1}, dial_options);
47+
5048
// Print resources.
5149
VIAM_SDK_LOG(info) << "Resources:";
5250
std::vector<Name> resource_names = robot->resource_names();

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ int main() {
1616
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
1717
Instance inst;
1818

19-
const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely
19+
// replace with your URI if connecting securely
20+
std::string address = "http://localhost:8080/";
21+
2022
DialOptions dial_options;
2123
dial_options.allow_insecure_downgrade = true; // set to false if connecting securely
2224

@@ -26,11 +28,8 @@ int main() {
2628
// Credentials credentials(type, payload);
2729
// dial_options.set_credentials(credentials);
2830

29-
boost::optional<DialOptions> opts(dial_options);
30-
std::string address(uri);
31-
Options options(1, opts);
32-
33-
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
31+
std::shared_ptr<RobotClient> robot =
32+
RobotClient::at_address(address, std::chrono::seconds{1}, dial_options);
3433

3534
// Print resources
3635
VIAM_SDK_LOG(info) << "Resources";

src/viam/examples/motor/example_motor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ int main() try {
4444
dial_options.allow_insecure_downgrade =
4545
(credentials.type().empty() && credentials.payload().empty());
4646

47-
// Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial
48-
// options
49-
vs::Options options = vs::Options(1, dial_options);
47+
// Pass the scheduled refresh interval of the robot (0 seconds = only on config update) and the
48+
// dial options
49+
std::shared_ptr<vs::RobotClient> robot =
50+
vs::RobotClient::at_address(robot_address, std::chrono::seconds{1}, dial_options);
5051

51-
std::shared_ptr<vs::RobotClient> robot = vs::RobotClient::at_address(robot_address, options);
5252
VIAM_SDK_LOG(info) << "Successfully connected to the robot";
5353

5454
std::vector<vs::Name> resource_names = robot->resource_names();

src/viam/sdk/module/service.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service {
194194
auto new_parent_addr = request->parent_address();
195195
if (parent.parent_addr_ != new_parent_addr) {
196196
parent.parent_addr_ = std::move(new_parent_addr);
197-
parent.parent_ = RobotClient::at_local_socket(parent.parent_addr_, {0, boost::none});
197+
parent.parent_ =
198+
RobotClient::at_local_socket(parent.parent_addr_, std::chrono::seconds{0});
198199
parent.parent_->connect_logging();
199200
}
200201
response->set_ready(parent.module_->ready());
@@ -223,7 +224,7 @@ Dependencies ModuleService::get_dependencies_(
223224
std::shared_ptr<Resource> ModuleService::get_parent_resource_(const Name& name) {
224225
if (!parent_) {
225226
// LS: I think maybe this is never hit
226-
parent_ = RobotClient::at_local_socket(parent_addr_, {0, boost::none});
227+
parent_ = RobotClient::at_local_socket(parent_addr_, std::chrono::seconds{0});
227228
parent_->connect_logging();
228229
}
229230

src/viam/sdk/robot/client.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,9 @@ void RobotClient::log(const std::string& name,
271271
}
272272

273273
std::unique_ptr<RobotClient> RobotClient::with_channel(ViamChannel channel,
274-
const Options& options) {
274+
std::chrono::seconds refresh_interval) {
275275
auto robot = std::make_unique<RobotClient>(std::move(channel));
276-
robot->refresh_interval_ = std::chrono::seconds{options.refresh_interval()};
276+
robot->refresh_interval_ = refresh_interval;
277277
robot->should_refresh_ = (robot->refresh_interval_ > std::chrono::seconds{0});
278278
if (robot->should_refresh_) {
279279
robot->refresh_thread_ = std::thread{&RobotClient::refresh_every, robot.get()};
@@ -284,20 +284,21 @@ std::unique_ptr<RobotClient> RobotClient::with_channel(ViamChannel channel,
284284
};
285285

286286
std::unique_ptr<RobotClient> RobotClient::at_address(const std::string& address,
287-
const Options& options) {
287+
std::chrono::seconds refresh_interval,
288+
const DialOptions& options) {
288289
const char* uri = address.c_str();
289290
auto robot =
290-
RobotClient::with_channel(ViamChannel::dial_initial(uri, options.dial_options()), options);
291+
RobotClient::with_channel(ViamChannel::dial_initial(uri, options), refresh_interval);
291292

292293
return robot;
293294
};
294295

295296
std::unique_ptr<RobotClient> RobotClient::at_local_socket(const std::string& address,
296-
const Options& options) {
297+
std::chrono::seconds refresh_interval) {
297298
const std::string addr = "unix://" + address;
298299
auto robot = RobotClient::with_channel(
299300
ViamChannel(sdk::impl::create_viam_grpc_channel(addr, grpc::InsecureChannelCredentials())),
300-
options);
301+
refresh_interval);
301302

302303
return robot;
303304
};

src/viam/sdk/robot/client.hpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ class RobotClient {
7171
friend bool operator==(const operation& lhs, const operation& rhs);
7272
};
7373

74+
struct options {
75+
std::chrono::seconds refresh_interval;
76+
boost::optional<DialOptions> dial_options;
77+
};
78+
7479
explicit RobotClient(ViamChannel channel);
7580

7681
~RobotClient();
@@ -90,22 +95,25 @@ class RobotClient {
9095

9196
/// @brief Create a robot client connected to the robot at the provided address.
9297
/// @param address The address of the robot (IP address, URI, URL, etc.)
93-
/// @param options Options for connecting and refreshing.
98+
/// @param refresh_interval How often to call refresh.
99+
/// @param options Options for dialing to the address.
94100
static std::unique_ptr<RobotClient> at_address(const std::string& address,
95-
const Options& options);
101+
std::chrono::seconds refresh_interval,
102+
const DialOptions& options);
96103

97104
/// @brief Creates a robot client connected to the robot at the provided local socket.
98105
/// @param address The local socket of the robot (a .sock file, etc.).
99-
/// @param options Options for connecting and refreshing.
106+
/// @param refresh_interval How often to call refresh.
100107
/// Creates a direct connection to the robot using the `unix://` scheme.
101108
/// Only useful for connecting to robots across Unix sockets.
102109
static std::unique_ptr<RobotClient> at_local_socket(const std::string& address,
103-
const Options& options);
110+
std::chrono::seconds refresh_interval);
104111

105112
/// @brief Creates a robot client connected to the provided channel.
106113
/// @param channel The channel to connect with.
107-
/// @param options Options for connecting and refreshing.
108-
static std::unique_ptr<RobotClient> with_channel(ViamChannel channel, const Options& options);
114+
/// @param refresh_interval How often to call refresh.
115+
static std::unique_ptr<RobotClient> with_channel(ViamChannel channel,
116+
std::chrono::seconds refresh_interval);
109117

110118
std::vector<Name> resource_names() const;
111119

src/viam/sdk/rpc/dial.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,6 @@ void ViamChannel::close() {
187187
pimpl_.reset();
188188
}
189189

190-
unsigned int Options::refresh_interval() const {
191-
return refresh_interval_;
192-
}
193-
194-
const boost::optional<DialOptions>& Options::dial_options() const {
195-
return dial_options_;
196-
}
197-
198190
Credentials::Credentials(std::string payload)
199191
: type_("robot-location-secret"), payload_(std::move(payload)) {}
200192

src/viam/sdk/rpc/dial.hpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,5 @@ struct DialOptions {
100100
std::chrono::duration<float> initial_connection_attempt_timeout{20};
101101
};
102102

103-
class Options {
104-
public:
105-
Options(unsigned int refresh_interval, boost::optional<DialOptions> dial_options)
106-
: refresh_interval_(std::move(refresh_interval)), dial_options_(std::move(dial_options)) {}
107-
108-
unsigned int refresh_interval() const;
109-
const boost::optional<DialOptions>& dial_options() const;
110-
111-
private:
112-
/// @brief How often to refresh the status/parts of the robot, in seconds. If set to 0, the
113-
/// robot will not automatically refresh.
114-
unsigned int refresh_interval_;
115-
boost::optional<DialOptions> dial_options_;
116-
};
117-
118103
} // namespace sdk
119104
} // namespace viam

src/viam/sdk/tests/test_robot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void robot_client_to_mocks_pipeline(F&& test_case) {
5050
// in-process gRPC channel.
5151
auto test_server = TestServer(server);
5252
auto grpc_channel = test_server.grpc_in_process_channel();
53-
auto client = RobotClient::with_channel(ViamChannel(grpc_channel), Options(0, boost::none));
53+
auto client = RobotClient::with_channel(ViamChannel(grpc_channel), {});
5454

5555
// Run the passed-in test case on the created stack and give access to the
5656
// created RobotClient and MockRobotService.

0 commit comments

Comments
 (0)