summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJürgen Gehring <Juergen.Gehring@bmw.de>2016-10-11 05:20:33 -0700
committerJürgen Gehring <Juergen.Gehring@bmw.de>2016-10-11 05:20:33 -0700
commit1375432503c0a72df7ad5c793c3e1f04e6b9e730 (patch)
tree4b229a9c5a7767db69db015f8e92e01c64614bf0 /examples
parent273814c76be4a8f906dc053492529b8d53b9e807 (diff)
downloadvSomeIP-1375432503c0a72df7ad5c793c3e1f04e6b9e730.tar.gz
vsomeip 2.4.22.4.2
Diffstat (limited to 'examples')
-rw-r--r--examples/hello_world/CMakeLists.txt6
-rw-r--r--examples/hello_world/hello_world_client.cpp18
-rw-r--r--examples/hello_world/hello_world_service.cpp28
-rw-r--r--examples/notify-sample.cpp47
-rw-r--r--examples/request-sample.cpp30
-rw-r--r--examples/response-sample.cpp41
-rw-r--r--examples/subscribe-sample.cpp27
7 files changed, 178 insertions, 19 deletions
diff --git a/examples/hello_world/CMakeLists.txt b/examples/hello_world/CMakeLists.txt
index cb32665..d6bc994 100644
--- a/examples/hello_world/CMakeLists.txt
+++ b/examples/hello_world/CMakeLists.txt
@@ -14,12 +14,14 @@ if (NOT vsomeip_FOUND)
message("vsomeip was not found. Please specify vsomeip_DIR")
endif()
+find_package(Threads REQUIRED)
+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(${VSOMEIP_INCLUDE_DIRS})
add_executable (hello_world_service hello_world_service.cpp)
-target_link_libraries(hello_world_service ${VSOMEIP_LIBRARIES})
+target_link_libraries(hello_world_service ${VSOMEIP_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
add_executable (hello_world_client hello_world_client.cpp)
-target_link_libraries(hello_world_client ${VSOMEIP_LIBRARIES})
+target_link_libraries(hello_world_client ${VSOMEIP_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
diff --git a/examples/hello_world/hello_world_client.cpp b/examples/hello_world/hello_world_client.cpp
index 8007c32..8d5b33c 100644
--- a/examples/hello_world/hello_world_client.cpp
+++ b/examples/hello_world/hello_world_client.cpp
@@ -2,7 +2,9 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
-
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+#include <csignal>
+#endif
#include <vsomeip/vsomeip.hpp>
#include <iostream>
@@ -125,9 +127,23 @@ private:
std::shared_ptr<vsomeip::application> app_;
};
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+hello_world_client *hw_cl_ptr(nullptr);
+ void handle_signal(int _signal) {
+ if (hw_cl_ptr != nullptr &&
+ (_signal == SIGINT || _signal == SIGTERM))
+ hw_cl_ptr->stop();
+ }
+#endif
+
int main(int argc, char **argv)
{
hello_world_client hw_cl;
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ hw_cl_ptr = &hw_cl;
+ signal(SIGINT, handle_signal);
+ signal(SIGTERM, handle_signal);
+#endif
hw_cl.init();
hw_cl.start();
return 0;
diff --git a/examples/hello_world/hello_world_service.cpp b/examples/hello_world/hello_world_service.cpp
index b99a401..7e6b615 100644
--- a/examples/hello_world/hello_world_service.cpp
+++ b/examples/hello_world/hello_world_service.cpp
@@ -2,7 +2,9 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
-
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+#include <csignal>
+#endif
#include <vsomeip/vsomeip.hpp>
#include <chrono>
#include <thread>
@@ -75,6 +77,12 @@ public:
app_->stop();
}
+ void terminate() {
+ std::lock_guard<std::mutex> its_lock(mutex_);
+ stop_ = true;
+ condition_.notify_one();
+ }
+
void on_state_cbk(vsomeip::state_type_e _state)
{
if(_state == vsomeip::state_type_e::ST_REGISTERED)
@@ -104,9 +112,7 @@ public:
// Send the response back
app_->send(resp, true);
// we're finished stop now
- std::lock_guard<std::mutex> its_lock(mutex_);
- stop_ = true;
- condition_.notify_one();
+ terminate();
}
private:
@@ -118,9 +124,23 @@ private:
std::thread stop_thread_;
};
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+hello_world_service *hw_srv_ptr(nullptr);
+ void handle_signal(int _signal) {
+ if (hw_srv_ptr != nullptr &&
+ (_signal == SIGINT || _signal == SIGTERM))
+ hw_srv_ptr->terminate();
+ }
+#endif
+
int main(int argc, char **argv)
{
hello_world_service hw_srv;
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ hw_srv_ptr = &hw_srv;
+ signal(SIGINT, handle_signal);
+ signal(SIGTERM, handle_signal);
+#endif
hw_srv.init();
hw_srv.start();
return 0;
diff --git a/examples/notify-sample.cpp b/examples/notify-sample.cpp
index 12c9a4c..172bf7d 100644
--- a/examples/notify-sample.cpp
+++ b/examples/notify-sample.cpp
@@ -2,7 +2,9 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
-
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+#include <csignal>
+#endif
#include <chrono>
#include <condition_variable>
#include <iomanip>
@@ -22,6 +24,7 @@ public:
use_tcp_(_use_tcp),
cycle_(_cycle),
blocked_(false),
+ running_(true),
is_offered_(false),
offer_thread_(std::bind(&service_sample::run, this)),
notify_thread_(std::bind(&service_sample::notify, this)) {
@@ -67,6 +70,21 @@ public:
app_->start();
}
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ /*
+ * Handle signal to shutdown
+ */
+ void stop() {
+ running_ = false;
+ blocked_ = true;
+ condition_.notify_one();
+ notify_condition_.notify_one();
+ offer_thread_.join();
+ notify_thread_.join();
+ app_->stop();
+ }
+#endif
+
void offer() {
std::lock_guard<std::mutex> its_lock(notify_mutex_);
app_->offer_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
@@ -117,13 +135,15 @@ public:
condition_.wait(its_lock);
bool is_offer(true);
- while (true) {
+ while (running_) {
if (is_offer)
offer();
else
stop_offer();
- std::this_thread::sleep_for(std::chrono::milliseconds(10000));
+ for (int i = 0; i < 10 && running_; i++)
+ std::this_thread::sleep_for(std::chrono::milliseconds(1000));
+
is_offer = !is_offer;
}
}
@@ -139,11 +159,11 @@ public:
vsomeip::byte_t its_data[10];
uint32_t its_size = 1;
- while (true) {
+ while (running_) {
std::unique_lock<std::mutex> its_lock(notify_mutex_);
- while (!is_offered_)
+ while (!is_offered_ && running_)
notify_condition_.wait(its_lock);
- while (is_offered_) {
+ while (is_offered_ && running_) {
if (its_size == sizeof(its_data))
its_size = 1;
@@ -171,6 +191,7 @@ private:
std::mutex mutex_;
std::condition_variable condition_;
bool blocked_;
+ bool running_;
std::mutex notify_mutex_;
std::condition_variable notify_condition_;
@@ -183,6 +204,15 @@ private:
std::thread notify_thread_;
};
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ service_sample *its_sample_ptr(nullptr);
+ void handle_signal(int _signal) {
+ if (its_sample_ptr != nullptr &&
+ (_signal == SIGINT || _signal == SIGTERM))
+ its_sample_ptr->stop();
+ }
+#endif
+
int main(int argc, char **argv) {
bool use_tcp = false;
uint32_t cycle = 1000; // default 1s
@@ -210,6 +240,11 @@ int main(int argc, char **argv) {
}
service_sample its_sample(use_tcp, cycle);
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ its_sample_ptr = &its_sample;
+ signal(SIGINT, handle_signal);
+ signal(SIGTERM, handle_signal);
+#endif
its_sample.init();
its_sample.start();
diff --git a/examples/request-sample.cpp b/examples/request-sample.cpp
index 86834d0..93b8f0e 100644
--- a/examples/request-sample.cpp
+++ b/examples/request-sample.cpp
@@ -2,7 +2,9 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
-
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+#include <csignal>
+#endif
#include <chrono>
#include <condition_variable>
#include <iomanip>
@@ -77,6 +79,19 @@ public:
app_->start();
}
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ /*
+ * Handle signal to shutdown
+ */
+ void stop() {
+ running_ = false;
+ blocked_ = true;
+ condition_.notify_one();
+ sender_.join();
+ app_->stop();
+ }
+#endif
+
void on_state(vsomeip::state_type_e _state) {
if (_state == vsomeip::state_type_e::ST_REGISTERED) {
app_->request_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
@@ -165,6 +180,14 @@ private:
std::thread sender_;
};
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ client_sample *its_sample_ptr(nullptr);
+ void handle_signal(int _signal) {
+ if (its_sample_ptr != nullptr &&
+ (_signal == SIGINT || _signal == SIGTERM))
+ its_sample_ptr->stop();
+ }
+#endif
int main(int argc, char **argv) {
bool use_tcp = false;
@@ -194,6 +217,11 @@ int main(int argc, char **argv) {
}
client_sample its_sample(use_tcp, be_quiet, cycle);
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ its_sample_ptr = &its_sample;
+ signal(SIGINT, handle_signal);
+ signal(SIGTERM, handle_signal);
+#endif
its_sample.init();
its_sample.start();
return 0;
diff --git a/examples/response-sample.cpp b/examples/response-sample.cpp
index c3ef177..883d821 100644
--- a/examples/response-sample.cpp
+++ b/examples/response-sample.cpp
@@ -2,7 +2,9 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
-
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+#include <csignal>
+#endif
#include <chrono>
#include <condition_variable>
#include <iomanip>
@@ -21,6 +23,7 @@ public:
is_registered_(false),
use_static_routing_(_use_static_routing),
blocked_(false),
+ running_(true),
offer_thread_(std::bind(&service_sample::run, this)) {
}
@@ -44,6 +47,19 @@ public:
app_->start();
}
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ /*
+ * Handle signal to shutdown
+ */
+ void stop() {
+ running_ = false;
+ blocked_ = true;
+ condition_.notify_one();
+ offer_thread_.join();
+ app_->stop();
+ }
+#endif
+
void offer() {
app_->offer_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
app_->offer_service(SAMPLE_SERVICE_ID + 1, SAMPLE_INSTANCE_ID);
@@ -101,14 +117,16 @@ public:
if (use_static_routing_) {
offer();
- while (true);
+ while (running_);
} else {
- while (true) {
+ while (running_) {
if (is_offer)
offer();
else
stop_offer();
- std::this_thread::sleep_for(std::chrono::milliseconds(10000));
+
+ for (int i = 0; i < 10 && running_; i++)
+ std::this_thread::sleep_for(std::chrono::milliseconds(1000));
is_offer = !is_offer;
}
}
@@ -122,11 +140,21 @@ private:
std::mutex mutex_;
std::condition_variable condition_;
bool blocked_;
+ bool running_;
// blocked_ must be initialized before the thread is started.
std::thread offer_thread_;
};
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ service_sample *its_sample_ptr(nullptr);
+ void handle_signal(int _signal) {
+ if (its_sample_ptr != nullptr &&
+ (_signal == SIGINT || _signal == SIGTERM))
+ its_sample_ptr->stop();
+ }
+#endif
+
int main(int argc, char **argv) {
bool use_static_routing(false);
@@ -139,6 +167,11 @@ int main(int argc, char **argv) {
}
service_sample its_sample(use_static_routing);
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ its_sample_ptr = &its_sample;
+ signal(SIGINT, handle_signal);
+ signal(SIGTERM, handle_signal);
+#endif
its_sample.init();
its_sample.start();
diff --git a/examples/subscribe-sample.cpp b/examples/subscribe-sample.cpp
index 915ea11..38b80c8 100644
--- a/examples/subscribe-sample.cpp
+++ b/examples/subscribe-sample.cpp
@@ -2,7 +2,9 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
-
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+#include <csignal>
+#endif
#include <chrono>
#include <condition_variable>
#include <iomanip>
@@ -57,6 +59,15 @@ public:
app_->start();
}
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ /*
+ * Handle signal to shutdown
+ */
+ void stop() {
+ app_->stop();
+ }
+#endif
+
void on_state(vsomeip::state_type_e _state) {
if (_state == vsomeip::state_type_e::ST_REGISTERED) {
app_->request_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
@@ -134,6 +145,15 @@ private:
bool be_quiet_;
};
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ client_sample *its_sample_ptr(nullptr);
+ void handle_signal(int _signal) {
+ if (its_sample_ptr != nullptr &&
+ (_signal == SIGINT || _signal == SIGTERM))
+ its_sample_ptr->stop();
+ }
+#endif
+
int main(int argc, char **argv) {
bool use_tcp = false;
@@ -151,6 +171,11 @@ int main(int argc, char **argv) {
}
client_sample its_sample(use_tcp);
+#ifndef VSOMEIP_ENABLE_SIGNAL_HANDLING
+ its_sample_ptr = &its_sample;
+ signal(SIGINT, handle_signal);
+ signal(SIGTERM, handle_signal);
+#endif
its_sample.init();
its_sample.start();
return 0;