Skip to content

Commit

Permalink
RSDK-7642 - add simple module client for module integration testing (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
stuqdog authored Jun 25, 2024
1 parent ef1016b commit 5461780
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/viam/examples/modules/simple/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ install(
TARGETS simple_module
COMPONENT examples
)

add_executable(simple_module_client)
target_sources(simple_module_client
PRIVATE
client.cpp
)

target_link_libraries(simple_module_client
viam-cpp-sdk::viamsdk
)

install(
TARGETS simple_module_client
COMPONENT examples
)
63 changes: 63 additions & 0 deletions src/viam/examples/modules/simple/client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <memory>
#include <string>

#include <viam/sdk/common/proto_type.hpp>
#include <viam/sdk/robot/client.hpp>
#include <viam/sdk/rpc/dial.hpp>
#include <viam/sdk/services/generic.hpp>

using namespace viam::sdk;

int main() {
const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely
DialOptions dial_options;
dial_options.set_allow_insecure_downgrade(true); // set to false if connecting securely

// Uncomment and fill out your credentials details if connecting securely
// std::string type = "<your authentication type>";
// std::string payload = "<your authentication payload>";
// Credentials credentials(type, payload);
// dial_options.set_credentials(credentials);

boost::optional<DialOptions> opts(dial_options);
std::string address(uri);
Options options(1, opts);

std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);

// Print resources
std::cout << "Resources\n";
std::vector<Name> resource_names = robot->resource_names();
for (const Name& resource : resource_names) {
std::cout << "\t" << resource << "\n";
}

// Exercise printer methods
auto printer = robot->resource_by_name<GenericService>("printer1");
if (!printer) {
std::cerr << "could not get 'printer1' resource from robot\n";
return EXIT_FAILURE;
}

auto proto_ptr = std::make_shared<ProtoType>(std::string("world"));
AttributeMap command =
std::make_shared<std::unordered_map<std::string, std::shared_ptr<ProtoType>>>();
command->insert({{std::string("hello"), proto_ptr}});

auto resp = printer->do_command(command);

if (!resp) {
std::cerr << "Failed to get a response from 'printer1'\n";
return EXIT_FAILURE;
}

std::shared_ptr<ProtoType> expected = command->at(std::string("hello"));
std::shared_ptr<ProtoType> result = resp->at(std::string("hello"));

if (!(*expected == *result)) {
std::cerr << "Got unexpected result from 'printer1'\n";
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

0 comments on commit 5461780

Please sign in to comment.