Skip to content

Commit

Permalink
adding samples (#1)
Browse files Browse the repository at this point in the history
* adding samples

* Update README.md

* Update README.md

* adding yml

* cleanup

* cleanup

* Update main_pub.cpp

* Update ci.yml

* Adding copyrights

* Update main_pub.cpp

* Update CMakeLists.txt

* Update main_rpc_client.cpp

* Update main_rpc_server.cpp

* Update CMakeLists.txt

* Update CMakeLists.txt

* Update main_pub.cpp

* Update main_sub.cpp

* Update main_rpc_server.cpp

* Update main_rpc_client.cpp

* Update main_rpc_server.cpp
  • Loading branch information
MelamudMichael authored Feb 29, 2024
1 parent 83c379b commit 0137899
Show file tree
Hide file tree
Showing 10 changed files with 763 additions and 0 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: ["**"]
workflow_call:
workflow_dispatch:

jobs:
build:
name: Build
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Install Conan
id: conan
uses: turtlebrowser/get-conan@main

- name: Conan version
run: echo "${{ steps.conan.outputs.version }}"


- name: Create default Conan profile
run: conan profile detect

- name: Install Rust toolchain
run: rustup component add rustfmt clippy

- name: Create up-cpp Conan package
shell: bash
run: |
git clone https://github.com/eclipse-uprotocol/up-cpp.git
cd up-cpp
git clone -b uprotocol-core-api-1.5.6 https://github.com/eclipse-uprotocol/up-core-api.git
git submodule update --init --recursive
conan create . --build=missing
- name: Build and install Zenoh-C
shell: bash
run: |
git clone https://github.com/eclipse-zenoh/zenoh-c.git
cd zenoh-c && mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . --target install --config Release -- -j
- name: Create up-client-zenoh-cpp Conan package
shell: bash
run: |
git clone https://github.com/eclipse-uprotocol/up-client-zenoh-cpp.git
cd up-client-zenoh-cpp
conan create . --build=missing
- name: Build up-zenoh-example-cpp
shell: bash
run: |
mkdir build_samples
cd build_samples
conan install ../
cmake ../ -DCMAKE_TOOLCHAIN_FILE=generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
cmake --build .
# NOTE: In GitHub repository settings, the "Require status checks to pass
# before merging" branch protection rule ensures that commits are only merged
# from branches where specific status checks have passed. These checks are
# specified manually as a list of workflow job names. Thus we use this extra
# job to signal whether all CI checks have passed.
ci:
name: CI status checks
runs-on: ubuntu-latest
needs: build
if: always()
steps:
- name: Check whether all jobs pass
run: echo '${{ toJson(needs) }}' | jq -e 'all(.result == "success")'
27 changes: 27 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) 2024 General Motors GTO LLC
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
# SPDX-FileType: SOURCE
# SPDX-FileCopyrightText: 2024 General Motors GTO LLC
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20)
project(samples VERSION 0.1.0 LANGUAGES CXX)

add_subdirectory(pubsub)
add_subdirectory(rpc)
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
# up-zenoh-example-cpp
C++ Example application and service that utilizes up-client-zenoh-cpp

## Getting Started
### Requirements:
- Compiler: GCC/G++ 11 or Clang 13
- Ubuntu 22.04
- conan : 1.59 or latest 2.X

#### up-client-zenoh-cpp dependencies

1. install up-client-zenoh-cpp library https://github.com/eclipse-uprotocol/up-client-zenoh-cpp

## How to Build
```
$ cd up-zenoh-example-cpp
$ mkdir build
$ cd build
$ conan install ..
$ cmake -S .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
$ cmake --build .
```
32 changes: 32 additions & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from conan import ConanFile

class HelloWorldRecipe(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps", "CMakeToolchain", "PkgConfigDeps", "VirtualRunEnv", "VirtualBuildEnv"
options = {
"shared": [True, False],
"fPIC": [True, False],
"build_testing": [True, False],
"build_unbundled": [True, False],
"build_cross_compiling": [True, False],
}

default_options = {
"shared": False,
"fPIC": False,
"build_testing": False,
"build_unbundled": True,
"build_cross_compiling": False,
}

# def configure(self):
# self.options["spdlog"].shared = self.options.shared

def requirements(self):
self.requires("up-client-zenoh-cpp/0.1.0-dev")
self.requires("protobuf/3.21.12" + ("@cross/cross" if self.options.build_cross_compiling else ""))


def imports(self):
if self.options.build_testing:
self.copy("*.so*", dst="lib", keep_path=False)
38 changes: 38 additions & 0 deletions pubsub/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (c) 2024 General Motors GTO LLC
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
# SPDX-FileType: SOURCE
# SPDX-FileCopyrightText: 2024 General Motors GTO LLC
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20)
project(pubsub VERSION 0.1.0 LANGUAGES CXX)

find_package(up-client-zenoh-cpp REQUIRED)

# sub
add_executable(sub src/main_sub.cpp)
target_link_libraries(sub
PRIVATE
up-client-zenoh-cpp::up-client-zenoh-cpp)

# pub
add_executable(pub src/main_pub.cpp)
target_link_libraries(pub
PRIVATE
up-client-zenoh-cpp::up-client-zenoh-cpp)
155 changes: 155 additions & 0 deletions pubsub/src/main_pub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2024 General Motors GTO LLC
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
* SPDX-FileType: SOURCE
* SPDX-FileCopyrightText: 2024 General Motors GTO LLC
* SPDX-License-Identifier: Apache-2.0
*/
#include <iostream>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <csignal>
#include <unistd.h> // For sleep

#include <spdlog/spdlog.h>
#include <up-client-zenoh-cpp/transport/zenohUTransport.h>
#include <up-cpp/uuid/factory/Uuidv8Factory.h>
#include <up-cpp/uri/serializer/LongUriSerializer.h>
#include <up-core-api/ustatus.pb.h>
#include <up-core-api/uri.pb.h>

using namespace uprotocol::utransport;
using namespace uprotocol::uri;
using namespace uprotocol::uuid;
using namespace uprotocol::v1;

const std::string TIME_URI_STRING = "/test.app/1/milliseconds";
const std::string RANDOM_URI_STRING = "/test.app/1/32bit";
const std::string COUNTER_URI_STRING = "/test.app/1/counter";

bool gTerminate = false;

void signalHandler(int signal) {
if (signal == SIGINT) {
std::cout << "Ctrl+C received. Exiting..." << std::endl;
gTerminate = true;
}
}

std::uint8_t* getTime() {

auto currentTime = std::chrono::system_clock::now();
auto duration = currentTime.time_since_epoch();
auto timeMilli = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
static std::uint8_t buf[8];
std::memcpy(buf, &timeMilli, sizeof(timeMilli));

return buf;
}

std::uint8_t* getRandom() {

int32_t val = std::rand();
static std::uint8_t buf[4];
std::memcpy(buf, &val, sizeof(val));

return buf;
}

std::uint8_t* getCounter() {

static std::uint8_t counter = 0;
++counter;

return &counter;
}

UCode sendMessage(ZenohUTransport *transport,
UUri &uri,
std::uint8_t *buffer,
size_t size) {

auto uuid = Uuidv8Factory::create();

UAttributesBuilder builder(uuid, UMessageType::PUBLISH, UPriority::STANDARD);
UAttributes attributes = builder.build();

UPayload payload(buffer, size, UPayloadType::VALUE);

UStatus status = transport->send(uri, payload, attributes);
if (UCode::OK != status.code()) {
spdlog::error("send.send failed");
return UCode::UNAVAILABLE;
}
return UCode::OK;
}

/* The sample pub applications demonstrates how to send data using uTransport -
* There are three topics that are published - random number, current time and a counter */
int main(int argc, char **argv) {

signal(SIGINT, signalHandler);

UStatus status;
ZenohUTransport *transport = &ZenohUTransport::instance();

/* Initialize zenoh utransport */
status = transport->init();
if (UCode::OK != status.code()) {
spdlog::error("ZenohUTransport init failed");
return -1;
}

/* Create URI objects from string URI*/
auto timeUri = LongUriSerializer::deserialize(TIME_URI_STRING);
auto randomUri = LongUriSerializer::deserialize(RANDOM_URI_STRING);
auto counterUri = LongUriSerializer::deserialize(COUNTER_URI_STRING);

while (!gTerminate) {
/* send current time in milliseconds */
if (UCode::OK != sendMessage(transport, timeUri, getTime(), 8)) {
spdlog::error("sendMessage failed");
break;
}

/* send random number */
if (UCode::OK != sendMessage(transport, randomUri, getRandom(), 4)) {
spdlog::error("sendMessage failed");
break;
}

/* send counter */
if (UCode::OK != sendMessage(transport, counterUri, getCounter(), 1)) {
spdlog::error("sendMessage failed");
break;
}

sleep(1);
}

/* Terminate zenoh utransport */
status = transport->term();
if (UCode::OK != status.code()) {
spdlog::error("ZenohUTransport term failed");
return -1;
}

return 0;
}
Loading

0 comments on commit 0137899

Please sign in to comment.