Skip to content

Commit

Permalink
Move dial logic to rust-utils (#1)
Browse files Browse the repository at this point in the history
Moves all dial/ffi logic from the `rust-sdk` repo into the `rust-utils` repo.
  • Loading branch information
stuqdog authored Oct 14, 2022
1 parent ba38468 commit 8ad52b7
Show file tree
Hide file tree
Showing 44 changed files with 18,114 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @npmenard @stuqdog
34 changes: 34 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Build and Checks

on:
workflow_dispatch:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
checks:
if: github.repository_owner == 'viamrobotics'
runs-on: [self-hosted, x64]
container:
image: ghcr.io/viamrobotics/canon:amd64
steps:
- name: Checkout PR/Push/Workflow Dispatch
uses: actions/checkout@v2
- name: Setup Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: clippy
- name: Run Clippy
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features
- name: Build
uses: actions-rs/cargo@v1
with:
command: build
33 changes: 33 additions & 0 deletions .github/workflows/license_finder.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: License Finder

on:
workflow_dispatch:
workflow_call:
pull_request:
branches: ['main']
push:

jobs:
license_finder:
name: Audit 3rd-Party Licenses
runs-on: [x64, qemu-host]
container:
image: ghcr.io/viamrobotics/canon:amd64-cache
options: --platform linux/amd64
timeout-minutes: 30

steps:
- name: Check out code
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Install latest rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
default: true
override: true

- name: Run license finder
run: license_finder
84 changes: 84 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Build release binaries

on:
workflow_dispatch:

jobs:
build_native:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: macos-latest
arch: arm64
target: aarch64-apple-darwin
platform: macosx_arm64
- os: macos-latest
arch: x86_64
target: x86_64-apple-darwin
platform: macosx_x86_64
steps:
- uses: actions/checkout@v2
- name: Setup rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.target }}
- name: Setup build directory
run: mkdir builds
- name: Build
uses: actions-rs/cargo@v1
with:
command: build
args: --release --target=${{ matrix.target }}
- name: Copy
run: cp target/${{ matrix.target }}/release/libviam.dylib builds/libviam-${{ matrix.platform }}.dylib
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: builds
path: builds

build_in_container:
runs-on: ${{ matrix.arch }}
container:
image: ${{ matrix.image }}
options: ${{ matrix.opts }}
strategy:
fail-fast: false
matrix:
include:
- arch: [arm64, qemu-host]
target: aarch64-unknown-linux-gnu
platform: linux_aarch64
ext: so
image: 'ghcr.io/viamrobotics/canon:arm64-cache'
opts: '--platform linux/arm64'
- arch: [x64, qemu-host]
target: x86_64-unknown-linux-gnu
platform: linux_x86_64
ext: so
image: 'ghcr.io/viamrobotics/canon:amd64-cache'
opts: '--platform linux/amd64'
steps:
- uses: actions/checkout@v2
- name: Setup rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.target }}
- name: Setup build directory
run: mkdir builds
- name: Build
uses: actions-rs/cargo@v1
with:
command: build
args: --release --target=${{ matrix.target }}
- name: Copy
run: cp target/${{ matrix.target }}/release/libviam.${{ matrix.ext }} builds/libviam-${{ matrix.platform }}.${{ matrix.ext }}
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: builds
path: builds
36 changes: 36 additions & 0 deletions .github/workflows/update_protos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Update Protos + Open PR

on:
workflow_dispatch:
repository_dispatch:
types:
- protos-updated

jobs:
update-protos:
if: github.repository_owner == 'viamrobotics'
runs-on: [self-hosted, x64]
container:
image: ghcr.io/viamrobotics/canon:amd64
steps:
- uses: actions/checkout@v3
- uses: bufbuild/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Generate buf
run: make buf
env:
BUF_TOKEN: ${{ secrets.BUF_TOKEN }}
- name: Add + Commit + Open PR
uses: peter-evans/create-pull-request@v3
with:
commit-message: '[WORKFLOW] Updating protos from ${{ github.event.client_payload.repo_name }}, commit: ${{ github.event.client_payload.sha }}'
branch: 'origin/update-protos'
delete-branch: true
title: Automated Protos Update
body: This is an auto-generated PR to update proto definitions. Check the commits to see which repos and commits are responsible for the changes
assignees: npmenard,stuqdog
reviewers: npmenard,stuqdog
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Cargo.lock
target/
**/*.rs.bk

*.DS_Store
45 changes: 45 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "viam-rust-utils"
version = "0.0.1"
edition = "2021"

[lib]
crate-type = ["cdylib","lib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = { version = "1.0", features = ["backtrace"]}
base64 = "0.13.0"
byteorder = "1.4.3"
bytes = "1.1.0"
chashmap = "2.2.2"
derivative = "2.2.0"
futures = {version = "0.3", default-features = false, features = ["alloc", "executor"]}
futures-core = "0.3"
futures-util = "0.3"
http = "0.2.7"
http-body = {version = "0.4.4"}
hyper = { version = "0.14.20", features = ["full"] }
interceptor = "0.8.0"
libc = {version = "0.2"}
log = "0.4.17"
prost = "0.10"
prost-types = "0.10"
rand = "0.8.5"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = {version = "1.19", features = ["rt-multi-thread", "time", "fs", "macros", "net"]}
tokio-stream = {version = "0.1", features = ["net"]}
tokio-rustls = { version = "0.23.4"}
tonic = {version = "0.7.2",features = [ "tls", "compression", "tls-roots",]}
tower = { version = "0.4" }
tower-http = { version = "0.3.3", features = ["add-extension","auth","propagate-header","set-header","sensitive-headers","trace","compression-gzip"]}
tracing = {version = "0.1.34"}
tracing-subscriber = {version = "0.3.11", features = ["env-filter"]}
webpki-roots = "0.21.1"
webrtc = "0.5.0"


[build-dependencies]
tonic-build = {version = "0.7.2",features = ["prost", "compression"]}

10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
all: build build-example
build:
cargo build
build-example:
cd examples/ && cargo build
buf-clean:
find src/gen -type f \( -iname "*.rs" ! -iname "mod.rs" \) -delete
buf: buf-clean
buf generate buf.build/viamrobotics/goutils --template buf.gen.yaml
buf generate buf.build/googleapis/googleapis --template buf.gen.yaml --path google/rpc --path google/api
6 changes: 6 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: v1
plugins:
- remote: buf.build/prost/plugins/prost:v0.1.3-2
out: src/gen/
- remote: buf.build/prost/plugins/tonic:v0.1.0-2
out: src/gen/
22 changes: 22 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
edition = "2021"
name = "example"
version = "0.0.1"

[[bin]]
name = "test-dial"
path = "src/dial/main.rs"

[[bin]]
name = "test-echo"
path = "src/echo/main.rs"

[dependencies]
viam = {package="viam-rust-utils", path = "../"}
anyhow = { version = "1.0", features = ["backtrace"]}
async-stream = "0.3.3"
futures-util = "0.3"
tokio = { version = "1.19", features = [ "rt-multi-thread", "time", "fs", "macros", "net", ] }
tonic = {version = "0.7.2",features = ["tls", "compression", "tls-roots"]}
tower = "0.4.0"
env_logger = "0.9.0"
59 changes: 59 additions & 0 deletions examples/src/echo/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use anyhow::Result;
use viam::gen::proto::rpc::examples::echo::v1::echo_service_client::EchoServiceClient;
use viam::gen::proto::rpc::examples::echo::v1::{
EchoBiDiRequest, EchoMultipleRequest, EchoRequest,
};
use viam::rpc::dial;

#[tokio::main]
/// Tests unary, server, and bidi streaming with simple echo requests. To run, simply
/// update the credentials and uri as necessary.
async fn main() -> Result<()> {
let creds = dial::CredentialsExt::new(
"robot-location-secret".to_string(),
"ytexnwei4fu1xv9csoqxfv4ckl3htsb49mzzey5t15xo9swy".to_string(),
);

let c = dial::DialOptions::builder()
.uri("webrtc-test-main.jkek76kqnh.viam.cloud")
.with_credentials(creds)
.allow_downgrade()
.connect()
.await?;

let mut service = EchoServiceClient::new(c);
let echo_request = EchoRequest {
message: "hi".to_string(),
};
let resp = service.echo(echo_request).await?.into_inner();
println!("resp: {resp:?}");

let multi_echo_request = EchoMultipleRequest {
message: "hello?".to_string(),
};
let mut resp = service
.echo_multiple(multi_echo_request)
.await?
.into_inner();

while let Some(resp) = resp.message().await? {
println!("multiple response: {resp:?}");
}

let bidi_stream = async_stream::stream! {
for i in 0..3 {
let request =
EchoBiDiRequest {
message: i.to_string()
};
yield request;
}
};

let mut bidi_resp = service.echo_bi_di(bidi_stream).await?.into_inner();
while let Some(resp) = bidi_resp.message().await? {
println!("Bidi response: {resp:?}");
}

Ok(())
}
5 changes: 5 additions & 0 deletions examples/src/ffi/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gen
ffi_echo
ffi_echo.o
ffi_robot
ffi_robot.o
23 changes: 23 additions & 0 deletions examples/src/ffi/cpp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
LDFLAGS = -L/usr/local/lib `pkg-config --libs protobuf grpc++ re2 libcares`\
-ldl -L../../../../target/debug -lviam_rust_utils

CXX = g++
CXXFLAGS += `pkg-config --cflags protobuf grpc`
CXXFLAGS += -std=c++11 -I gen/ -I gen/proto/

all: buf ffi_echo

ffi_echo: gen/google/api/http.pb.o gen/google/api/annotations.pb.o gen/proto/rpc/examples/echo/v1/echo.pb.o gen/proto/rpc/examples/echo/v1/echo.grpc.pb.o ffi_echo.o
$(CXX) $^ $(CXXFLAGS) $(LDFLAGS) -o $@

ffi_robot: gen/google/api/http.pb.o gen/google/api/annotations.pb.o gen/robot/v1/robot.pb.o gen/robot/v1/robot.grpc.pb.o gen/common/v1/common.pb.o gen/common/v1/common.grpc.pb.o ffi_robot.o
$(CXX) $^ $(CXXFLAGS) $(LDFLAGS) -o $@

buf:
@mkdir -p gen
buf generate buf.build/viamrobotics/goutils --template buf.gen.yaml
buf generate buf.build/googleapis/googleapis --template buf.gen.yaml --path google/rpc --path google/api
buf generate buf.build/viamrobotics/api --template buf.gen.yaml
clean:
rm -f ffi_echo *.o
rm -rf gen/*
6 changes: 6 additions & 0 deletions examples/src/ffi/cpp/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: v1
plugins:
- name: cpp
out: gen/
- remote: buf.build/grpc/plugins/cpp:v1.45.2-1
out: gen/
Loading

0 comments on commit 8ad52b7

Please sign in to comment.