Skip to content

Commit

Permalink
person snd udp sample added
Browse files Browse the repository at this point in the history
  • Loading branch information
rex-schilasky committed Apr 26, 2024
1 parent 4462bd6 commit 82c4269
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ecal/samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ if(ECAL_CORE_PUBLISHER)
if(ECAL_CORE_HAS_PROTOBUF)
add_subdirectory(cpp/pubsub/protobuf/person_events_snd)
add_subdirectory(cpp/pubsub/protobuf/person_snd)
if(ECAL_CORE_TRANSPORT_UDP)
add_subdirectory(cpp/pubsub/protobuf/person_snd_udp)
endif()
if(ECAL_CORE_TRANSPORT_TCP)
add_subdirectory(cpp/pubsub/protobuf/person_snd_tcp)
endif()
Expand Down
50 changes: 50 additions & 0 deletions ecal/samples/cpp/pubsub/protobuf/person_snd_udp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# ========================= eCAL LICENSE =================================
#
# Copyright (C) 2016 - 2019 Continental Corporation
#
# 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.
#
# ========================= eCAL LICENSE =================================

cmake_minimum_required(VERSION 3.10)

set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)

project(person_snd_udp)

find_package(eCAL REQUIRED)
find_package(Protobuf REQUIRED)

set(person_snd_udp_src
src/person_snd_udp.cpp
)

set(person_snd_udp_proto
${CMAKE_CURRENT_SOURCE_DIR}/src/protobuf/animal.proto
${CMAKE_CURRENT_SOURCE_DIR}/src/protobuf/house.proto
${CMAKE_CURRENT_SOURCE_DIR}/src/protobuf/person.proto
)

ecal_add_sample(${PROJECT_NAME} ${person_snd_udp_src})
PROTOBUF_TARGET_CPP(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/src/protobuf ${person_snd_udp_proto})

target_link_libraries(${PROJECT_NAME}
PRIVATE
eCAL::core_protobuf
)

target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)

ecal_install_sample(${PROJECT_NAME})

set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER samples/cpp/pubsub/protobuf)
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
*
* 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.
*
* ========================= eCAL LICENSE =================================
*/

#include <ecal/ecal.h>
#include <ecal/msg/protobuf/publisher.h>

#include <iostream>

#include "person.pb.h"

int main(int argc, char **argv)
{
// initialize eCAL API
eCAL::Initialize(argc, argv, "person publisher multicast");

// set process state
eCAL::Process::SetState(proc_sev_healthy, proc_sev_level1, "I feel good !");

// create a publisher (topic name "person")
eCAL::protobuf::CPublisher<pb::People::Person> pub("person");

// switch all layer off
pub.SetLayerMode(eCAL::TLayer::tlayer_all, eCAL::TLayer::smode_off);

// switch unicast layer on
pub.SetLayerMode(eCAL::TLayer::tlayer_udp_mc, eCAL::TLayer::smode_on);

// generate a class instance of Person
pb::People::Person person;

// enter main loop
auto cnt(0);
while (eCAL::Ok())
{
// set person object content
person.set_id(++cnt);
person.set_name("Max");
person.set_stype(pb::People::Person_SType_MALE);
person.set_email("[email protected]");
person.mutable_dog()->set_name("Brandy");
person.mutable_house()->set_rooms(4);

// send the person object
pub.Send(person);

// print content
std::cout << "person id : " << person.id() << std::endl;
std::cout << "person name : " << person.name() << std::endl;
std::cout << "person stype : " << person.stype() << std::endl;
std::cout << "person email : " << person.email() << std::endl;
std::cout << "dog.name : " << person.dog().name() << std::endl;
std::cout << "house.rooms : " << person.house().rooms() << std::endl;
std::cout << std::endl;

// sleep 500 ms
eCAL::Process::SleepMS(500);
}

// finalize eCAL API
eCAL::Finalize();

return(0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
*
* 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.
*
* ========================= eCAL LICENSE =================================
*/

syntax = "proto3";

package pb.Animal;

message Dog
{
string name = 1;
string colour = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
*
* 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.
*
* ========================= eCAL LICENSE =================================
*/

syntax = "proto3";

package pb.Environment;

message House
{
int32 rooms = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
*
* 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.
*
* ========================= eCAL LICENSE =================================
*/

syntax = "proto3";

import "animal.proto";
import "house.proto";

package pb.People;

message Person
{
enum SType
{
MALE = 0;
FEMALE = 1;
}

int32 id = 1;
string name = 2;
SType stype = 3;
string email = 4;

Animal.Dog dog = 5;
Environment.House house = 6;
}

0 comments on commit 82c4269

Please sign in to comment.