Skip to content

Commit

Permalink
RSDK-1669 - Module wrapper (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuqdog authored Feb 28, 2023
1 parent 9350132 commit 961544f
Show file tree
Hide file tree
Showing 76 changed files with 89,052 additions and 908 deletions.
7 changes: 7 additions & 0 deletions STYLEGUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Viam CPP-SDK Style Guide
- general introduction, reference to google guide and C++ core guidelines

### Deviation
- exceptions


1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

add_subdirectory(dial)
add_subdirectory(echo)
add_subdirectory(modules)
2 changes: 1 addition & 1 deletion examples/dial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

add_executable(example_dial
example_dial.cc
example_dial.cpp
)

target_link_libraries(example_dial
Expand Down
59 changes: 0 additions & 59 deletions examples/dial/example_dial.cc

This file was deleted.

61 changes: 61 additions & 0 deletions examples/dial/example_dial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <common/v1/common.pb.h>
#include <grpcpp/channel.h>
#include <grpcpp/client_context.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/support/status.h>
#include <robot/v1/robot.grpc.pb.h>
#include <robot/v1/robot.pb.h>
#include <unistd.h>

#include <boost/optional.hpp>
#include <cstddef>
#include <functional>
#include <iostream>
#include <memory>
#include <ostream>
#include <string>
#include <vector>

#include "robot/client.hpp"
#include "robot/service.hpp"
#include "rpc/dial.hpp"

using viam::robot::v1::Status;
using Viam::SDK::Credentials;
using Viam::SDK::Options;

int main() {
const char* uri = "<your robot URI here>";
Viam::SDK::DialOptions dial_options = Viam::SDK::DialOptions();
std::string payload = "<your payload here>";
Credentials credentials(payload);
dial_options.credentials = credentials;
boost::optional<Viam::SDK::DialOptions> opts(dial_options);
std::string address(uri);
Options options = Options(1, opts);
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
robot->refresh();
std::vector<ResourceName>* resource_names = robot->resource_names();
ResourceName the_one_we_care_about = resource_names->at(0);
for (ResourceName resource : *resource_names) {
std::cout << "Resource name: " << resource.name() << resource.type() << resource.subtype()
<< std::endl;
}
std::vector<Status> status_plural = robot->get_status();
std::cout << "Status plural len " << status_plural.size() << std::endl;
for (Status s : status_plural) {
std::cout << " Status! " << s.name().subtype() << std::endl;
}

std::vector<ResourceName> just_one = std::vector<ResourceName>();
just_one.push_back(the_one_we_care_about);
std::vector<Status> status_singular = robot->get_status(just_one);
std::cout << "Status singular len " << status_singular.size() << std::endl;
for (Status s : status_singular) {
std::cout << " Status! " << s.name().subtype() << std::endl;
}

robot->close();
return 0;
}

2 changes: 1 addition & 1 deletion examples/echo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ add_executable(example_echo
${PROTO_GEN_DIR}/gen/google/api/annotations.pb.cc
${PROTO_GEN_DIR}/gen/proto/rpc/examples/echo/v1/echo.pb.cc
${PROTO_GEN_DIR}/gen/proto/rpc/examples/echo/v1/echo.grpc.pb.cc
example_echo.cc
example_echo.cpp
)

if (VIAMCPPSDK_USE_DYNAMIC_PROTOS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include <grpcpp/grpcpp.h>
#include <proto/rpc/examples/echo/v1/echo.grpc.pb.h>
#include <proto/rpc/examples/echo/v1/echo.pb.h>
#include <unistd.h>

#include <cstddef>
#include <iostream>
#include <ostream>
#include <string>

#include "proto/rpc/examples/echo/v1/echo.grpc.pb.h"
#include "proto/rpc/examples/echo/v1/echo.pb.h"

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
Expand Down
26 changes: 26 additions & 0 deletions examples/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2023 Viam Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

add_executable(example_module
example_module.cpp
)

target_link_libraries(example_module
viamcpp
)

install(
TARGETS example_module
COMPONENT examples
)
85 changes: 85 additions & 0 deletions examples/modules/example_module.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <component/arm/v1/arm.grpc.pb.h>
#include <component/generic/v1/generic.grpc.pb.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/server_context.h>
#include <robot/v1/robot.pb.h>
#include <service/slam/v1/slam.grpc.pb.h>
#include <signal.h>

#include <components/component_base.hpp>
#include <iostream>
#include <memory>
#include <module/module.hpp>
#include <module/service.hpp>
#include <rpc/dial.hpp>
#include <rpc/server.hpp>

#include "components/generic.hpp"
#include "config/resource.hpp"
#include "registry/registry.hpp"
#include "resource/resource.hpp"

using viam::component::generic::v1::GenericService;

class MyModule : public GenericService::Service, public ComponentBase {
public:
void signal_handler(int signum);
static int which;
int inner_which;
MyModule() {
inner_which = which;
which += 1;
};

MyModule(const MyModule&) = delete;
MyModule& operator=(const MyModule&) = delete;

::grpc::Status DoCommand(::grpc::ServerContext* context,
const ::viam::component::generic::v1::DoCommandRequest* request,
::viam::component::generic::v1::DoCommandResponse* response) override {
std::cout << "Received DoCommand request for MyModule number " << inner_which << std::endl;
for (auto& req : request->command().fields()) {
std::cout << "request key: " << req.first.c_str()
<< "\trequest value: " << req.second.SerializeAsString();
}
*response->mutable_result() = request->command();

return grpc::Status();
}
};

int MyModule::which = 0;
std::shared_ptr<ModuleService_> my_mod;

void signal_handler(int signum) {
my_mod->close();
}

int main(int argc, char** argv) {
if (argc != 2) {
throw "need socket path as command line argument";
}

// TODO(RSDK-1920) This is still causing non-graceful shutdown. Figure out why, and fix.
struct sigaction sig_handler;
sig_handler.sa_handler = signal_handler;
sigaction(SIGTERM, &sig_handler, nullptr);
sigemptyset(&sig_handler.sa_mask);
sig_handler.sa_flags = 0;

Subtype generic = Generic::subtype();
my_mod = std::make_shared<ModuleService_>(argv[1]);
Model m("acme", "demo", "printer");
std::shared_ptr<ComponentRegistration> cr = std::make_shared<ComponentRegistration>(
ComponentType("MyModule"), generic, m, [](std::string, std::shared_ptr<grpc::Channel>) {
return std::make_unique<MyModule>();
});

Registry::register_component(cr);
my_mod->add_model_from_registry(generic, m);

my_mod->start();
Server::start();
Server::wait();
return 0;
};
15 changes: 11 additions & 4 deletions examples/project/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ find_package(Threads REQUIRED)
find_package(viam-cpp-sdk CONFIG REQUIRED COMPONENTS Lib)


# Here we compile the `examle_dial.cc` source file into an executable,
# but as part of a separate project rather than being built-in to the
# SDK build.
# Here we compile the `examle_dial.cpp` and `example_module.cpp` source
# files into executables, but as parts of separate projects rather than
# being built-in to the SDK build.
add_executable(example_dial
../../dial/example_dial.cc
../../dial/example_dial.cpp
)

add_executable(example_module
../../modules/example_module.cpp)

# Declaring the link dependency on the viam-cpp-sdk::viamcpp library
# will automatically wire up the required include paths and transitive
# library dependencies.
Expand All @@ -66,3 +69,7 @@ add_executable(example_dial
target_link_libraries(example_dial
viam-cpp-sdk::viamcpp
)

target_link_libraries(example_module
viam-cpp-sdk::viamcpp
)
10 changes: 8 additions & 2 deletions examples/project/pkg-config/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ CXXFLAGS = -std=c++14
CXXFLAGS += `pkg-config --cflags viam-cpp-sdk-libviamcpp`
LINKFLAGS = `pkg-config --libs viam-cpp-sdk-libviamcpp`

all: example_dial
all: example_dial example_module

example_dial.o: ../../dial/example_dial.cc
example_dial.o: ../../dial/example_dial.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $^

example_dial: example_dial.o
$(CXX) $(LINKFLAGS) -o $@ $^

example_module.o: ../../modules/example_module.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $^

example_module: example_module.o
$(CXX) $(LINKFLAGS) -o $@ $^
Loading

0 comments on commit 961544f

Please sign in to comment.