Skip to content

Commit 8ad52b7

Browse files
authored
Move dial logic to rust-utils (viamrobotics#1)
Moves all dial/ffi logic from the `rust-sdk` repo into the `rust-utils` repo.
1 parent ba38468 commit 8ad52b7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+18114
-0
lines changed

.github/CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @npmenard @stuqdog

.github/workflows/checks.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Build and Checks
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
pull_request:
9+
branches:
10+
- main
11+
12+
jobs:
13+
checks:
14+
if: github.repository_owner == 'viamrobotics'
15+
runs-on: [self-hosted, x64]
16+
container:
17+
image: ghcr.io/viamrobotics/canon:amd64
18+
steps:
19+
- name: Checkout PR/Push/Workflow Dispatch
20+
uses: actions/checkout@v2
21+
- name: Setup Rust toolchain
22+
uses: actions-rs/toolchain@v1
23+
with:
24+
toolchain: stable
25+
components: clippy
26+
- name: Run Clippy
27+
uses: actions-rs/clippy-check@v1
28+
with:
29+
token: ${{ secrets.GITHUB_TOKEN }}
30+
args: --all-features
31+
- name: Build
32+
uses: actions-rs/cargo@v1
33+
with:
34+
command: build

.github/workflows/license_finder.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: License Finder
2+
3+
on:
4+
workflow_dispatch:
5+
workflow_call:
6+
pull_request:
7+
branches: ['main']
8+
push:
9+
10+
jobs:
11+
license_finder:
12+
name: Audit 3rd-Party Licenses
13+
runs-on: [x64, qemu-host]
14+
container:
15+
image: ghcr.io/viamrobotics/canon:amd64-cache
16+
options: --platform linux/amd64
17+
timeout-minutes: 30
18+
19+
steps:
20+
- name: Check out code
21+
uses: actions/checkout@v2
22+
with:
23+
fetch-depth: 2
24+
25+
- name: Install latest rust toolchain
26+
uses: actions-rs/toolchain@v1
27+
with:
28+
toolchain: stable
29+
default: true
30+
override: true
31+
32+
- name: Run license finder
33+
run: license_finder

.github/workflows/release.yml

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Build release binaries
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
build_native:
8+
runs-on: ${{ matrix.os }}
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
include:
13+
- os: macos-latest
14+
arch: arm64
15+
target: aarch64-apple-darwin
16+
platform: macosx_arm64
17+
- os: macos-latest
18+
arch: x86_64
19+
target: x86_64-apple-darwin
20+
platform: macosx_x86_64
21+
steps:
22+
- uses: actions/checkout@v2
23+
- name: Setup rust toolchain
24+
uses: actions-rs/toolchain@v1
25+
with:
26+
toolchain: stable
27+
target: ${{ matrix.target }}
28+
- name: Setup build directory
29+
run: mkdir builds
30+
- name: Build
31+
uses: actions-rs/cargo@v1
32+
with:
33+
command: build
34+
args: --release --target=${{ matrix.target }}
35+
- name: Copy
36+
run: cp target/${{ matrix.target }}/release/libviam.dylib builds/libviam-${{ matrix.platform }}.dylib
37+
- name: Upload artifacts
38+
uses: actions/upload-artifact@v3
39+
with:
40+
name: builds
41+
path: builds
42+
43+
build_in_container:
44+
runs-on: ${{ matrix.arch }}
45+
container:
46+
image: ${{ matrix.image }}
47+
options: ${{ matrix.opts }}
48+
strategy:
49+
fail-fast: false
50+
matrix:
51+
include:
52+
- arch: [arm64, qemu-host]
53+
target: aarch64-unknown-linux-gnu
54+
platform: linux_aarch64
55+
ext: so
56+
image: 'ghcr.io/viamrobotics/canon:arm64-cache'
57+
opts: '--platform linux/arm64'
58+
- arch: [x64, qemu-host]
59+
target: x86_64-unknown-linux-gnu
60+
platform: linux_x86_64
61+
ext: so
62+
image: 'ghcr.io/viamrobotics/canon:amd64-cache'
63+
opts: '--platform linux/amd64'
64+
steps:
65+
- uses: actions/checkout@v2
66+
- name: Setup rust toolchain
67+
uses: actions-rs/toolchain@v1
68+
with:
69+
toolchain: stable
70+
target: ${{ matrix.target }}
71+
- name: Setup build directory
72+
run: mkdir builds
73+
- name: Build
74+
uses: actions-rs/cargo@v1
75+
with:
76+
command: build
77+
args: --release --target=${{ matrix.target }}
78+
- name: Copy
79+
run: cp target/${{ matrix.target }}/release/libviam.${{ matrix.ext }} builds/libviam-${{ matrix.platform }}.${{ matrix.ext }}
80+
- name: Upload artifacts
81+
uses: actions/upload-artifact@v3
82+
with:
83+
name: builds
84+
path: builds

.github/workflows/update_protos.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Update Protos + Open PR
2+
3+
on:
4+
workflow_dispatch:
5+
repository_dispatch:
6+
types:
7+
- protos-updated
8+
9+
jobs:
10+
update-protos:
11+
if: github.repository_owner == 'viamrobotics'
12+
runs-on: [self-hosted, x64]
13+
container:
14+
image: ghcr.io/viamrobotics/canon:amd64
15+
steps:
16+
- uses: actions/checkout@v3
17+
- uses: bufbuild/[email protected]
18+
with:
19+
github_token: ${{ secrets.GITHUB_TOKEN }}
20+
- uses: arduino/setup-protoc@v1
21+
with:
22+
repo-token: ${{ secrets.GITHUB_TOKEN }}
23+
- name: Generate buf
24+
run: make buf
25+
env:
26+
BUF_TOKEN: ${{ secrets.BUF_TOKEN }}
27+
- name: Add + Commit + Open PR
28+
uses: peter-evans/create-pull-request@v3
29+
with:
30+
commit-message: '[WORKFLOW] Updating protos from ${{ github.event.client_payload.repo_name }}, commit: ${{ github.event.client_payload.sha }}'
31+
branch: 'origin/update-protos'
32+
delete-branch: true
33+
title: Automated Protos Update
34+
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
35+
assignees: npmenard,stuqdog
36+
reviewers: npmenard,stuqdog

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Cargo.lock
2+
target/
3+
**/*.rs.bk
4+
5+
*.DS_Store

Cargo.toml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[package]
2+
name = "viam-rust-utils"
3+
version = "0.0.1"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib","lib"]
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
anyhow = { version = "1.0", features = ["backtrace"]}
12+
base64 = "0.13.0"
13+
byteorder = "1.4.3"
14+
bytes = "1.1.0"
15+
chashmap = "2.2.2"
16+
derivative = "2.2.0"
17+
futures = {version = "0.3", default-features = false, features = ["alloc", "executor"]}
18+
futures-core = "0.3"
19+
futures-util = "0.3"
20+
http = "0.2.7"
21+
http-body = {version = "0.4.4"}
22+
hyper = { version = "0.14.20", features = ["full"] }
23+
interceptor = "0.8.0"
24+
libc = {version = "0.2"}
25+
log = "0.4.17"
26+
prost = "0.10"
27+
prost-types = "0.10"
28+
rand = "0.8.5"
29+
serde = { version = "1.0", features = ["derive"] }
30+
serde_json = "1.0"
31+
tokio = {version = "1.19", features = ["rt-multi-thread", "time", "fs", "macros", "net"]}
32+
tokio-stream = {version = "0.1", features = ["net"]}
33+
tokio-rustls = { version = "0.23.4"}
34+
tonic = {version = "0.7.2",features = [ "tls", "compression", "tls-roots",]}
35+
tower = { version = "0.4" }
36+
tower-http = { version = "0.3.3", features = ["add-extension","auth","propagate-header","set-header","sensitive-headers","trace","compression-gzip"]}
37+
tracing = {version = "0.1.34"}
38+
tracing-subscriber = {version = "0.3.11", features = ["env-filter"]}
39+
webpki-roots = "0.21.1"
40+
webrtc = "0.5.0"
41+
42+
43+
[build-dependencies]
44+
tonic-build = {version = "0.7.2",features = ["prost", "compression"]}
45+

Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
all: build build-example
2+
build:
3+
cargo build
4+
build-example:
5+
cd examples/ && cargo build
6+
buf-clean:
7+
find src/gen -type f \( -iname "*.rs" ! -iname "mod.rs" \) -delete
8+
buf: buf-clean
9+
buf generate buf.build/viamrobotics/goutils --template buf.gen.yaml
10+
buf generate buf.build/googleapis/googleapis --template buf.gen.yaml --path google/rpc --path google/api

buf.gen.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: v1
2+
plugins:
3+
- remote: buf.build/prost/plugins/prost:v0.1.3-2
4+
out: src/gen/
5+
- remote: buf.build/prost/plugins/tonic:v0.1.0-2
6+
out: src/gen/

examples/Cargo.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
edition = "2021"
3+
name = "example"
4+
version = "0.0.1"
5+
6+
[[bin]]
7+
name = "test-dial"
8+
path = "src/dial/main.rs"
9+
10+
[[bin]]
11+
name = "test-echo"
12+
path = "src/echo/main.rs"
13+
14+
[dependencies]
15+
viam = {package="viam-rust-utils", path = "../"}
16+
anyhow = { version = "1.0", features = ["backtrace"]}
17+
async-stream = "0.3.3"
18+
futures-util = "0.3"
19+
tokio = { version = "1.19", features = [ "rt-multi-thread", "time", "fs", "macros", "net", ] }
20+
tonic = {version = "0.7.2",features = ["tls", "compression", "tls-roots"]}
21+
tower = "0.4.0"
22+
env_logger = "0.9.0"

examples/src/echo/main.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use anyhow::Result;
2+
use viam::gen::proto::rpc::examples::echo::v1::echo_service_client::EchoServiceClient;
3+
use viam::gen::proto::rpc::examples::echo::v1::{
4+
EchoBiDiRequest, EchoMultipleRequest, EchoRequest,
5+
};
6+
use viam::rpc::dial;
7+
8+
#[tokio::main]
9+
/// Tests unary, server, and bidi streaming with simple echo requests. To run, simply
10+
/// update the credentials and uri as necessary.
11+
async fn main() -> Result<()> {
12+
let creds = dial::CredentialsExt::new(
13+
"robot-location-secret".to_string(),
14+
"ytexnwei4fu1xv9csoqxfv4ckl3htsb49mzzey5t15xo9swy".to_string(),
15+
);
16+
17+
let c = dial::DialOptions::builder()
18+
.uri("webrtc-test-main.jkek76kqnh.viam.cloud")
19+
.with_credentials(creds)
20+
.allow_downgrade()
21+
.connect()
22+
.await?;
23+
24+
let mut service = EchoServiceClient::new(c);
25+
let echo_request = EchoRequest {
26+
message: "hi".to_string(),
27+
};
28+
let resp = service.echo(echo_request).await?.into_inner();
29+
println!("resp: {resp:?}");
30+
31+
let multi_echo_request = EchoMultipleRequest {
32+
message: "hello?".to_string(),
33+
};
34+
let mut resp = service
35+
.echo_multiple(multi_echo_request)
36+
.await?
37+
.into_inner();
38+
39+
while let Some(resp) = resp.message().await? {
40+
println!("multiple response: {resp:?}");
41+
}
42+
43+
let bidi_stream = async_stream::stream! {
44+
for i in 0..3 {
45+
let request =
46+
EchoBiDiRequest {
47+
message: i.to_string()
48+
};
49+
yield request;
50+
}
51+
};
52+
53+
let mut bidi_resp = service.echo_bi_di(bidi_stream).await?.into_inner();
54+
while let Some(resp) = bidi_resp.message().await? {
55+
println!("Bidi response: {resp:?}");
56+
}
57+
58+
Ok(())
59+
}

examples/src/ffi/cpp/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
gen
2+
ffi_echo
3+
ffi_echo.o
4+
ffi_robot
5+
ffi_robot.o

examples/src/ffi/cpp/Makefile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
LDFLAGS = -L/usr/local/lib `pkg-config --libs protobuf grpc++ re2 libcares`\
2+
-ldl -L../../../../target/debug -lviam_rust_utils
3+
4+
CXX = g++
5+
CXXFLAGS += `pkg-config --cflags protobuf grpc`
6+
CXXFLAGS += -std=c++11 -I gen/ -I gen/proto/
7+
8+
all: buf ffi_echo
9+
10+
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
11+
$(CXX) $^ $(CXXFLAGS) $(LDFLAGS) -o $@
12+
13+
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
14+
$(CXX) $^ $(CXXFLAGS) $(LDFLAGS) -o $@
15+
16+
buf:
17+
@mkdir -p gen
18+
buf generate buf.build/viamrobotics/goutils --template buf.gen.yaml
19+
buf generate buf.build/googleapis/googleapis --template buf.gen.yaml --path google/rpc --path google/api
20+
buf generate buf.build/viamrobotics/api --template buf.gen.yaml
21+
clean:
22+
rm -f ffi_echo *.o
23+
rm -rf gen/*

examples/src/ffi/cpp/buf.gen.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: v1
2+
plugins:
3+
- name: cpp
4+
out: gen/
5+
- remote: buf.build/grpc/plugins/cpp:v1.45.2-1
6+
out: gen/

0 commit comments

Comments
 (0)