summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorLutz Bichler <Lutz.Bichler@bmw.de>2014-08-01 14:19:24 +0200
committerLutz Bichler <Lutz.Bichler@bmw.de>2014-08-01 14:19:24 +0200
commit8dfb0d6a1bf29e1d4dc7ff3a0788be3a4d01bec1 (patch)
tree692560f88b37545414d15cf861a405d983dbc71b /examples
parentaf46f8a2270af686af2d8b4bcb5750539a12a52d (diff)
downloadvSomeIP-8dfb0d6a1bf29e1d4dc7ff3a0788be3a4d01bec1.tar.gz
First part of event/eventgroup implementation. Subscription works.
Second part (routing) is missing.
Diffstat (limited to 'examples')
-rw-r--r--examples/client-sample.cpp3
-rw-r--r--examples/sample-ids.hpp5
-rw-r--r--examples/service-sample.cpp271
3 files changed, 165 insertions, 114 deletions
diff --git a/examples/client-sample.cpp b/examples/client-sample.cpp
index 7d27222..c500d77 100644
--- a/examples/client-sample.cpp
+++ b/examples/client-sample.cpp
@@ -55,6 +55,9 @@ public:
this,
std::placeholders::_1));
+ app_->subscribe(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_EVENTGROUP_ID,
+ vsomeip::ANY_MAJOR, vsomeip::ANY_TTL);
+
request_->set_service(SAMPLE_SERVICE_ID);
request_->set_instance(SAMPLE_INSTANCE_ID);
request_->set_method(SAMPLE_METHOD_ID);
diff --git a/examples/sample-ids.hpp b/examples/sample-ids.hpp
index 3fc097f..45a6f85 100644
--- a/examples/sample-ids.hpp
+++ b/examples/sample-ids.hpp
@@ -9,7 +9,10 @@
#define SAMPLE_SERVICE_ID 0x1234
#define SAMPLE_INSTANCE_ID 0x5678
-#define SAMPLE_METHOD_ID 0x0421
+#define SAMPLE_METHOD_ID 0x8421
+#define SAMPLE_EVENT_ID 0x0778
+
+#define SAMPLE_EVENTGROUP_ID 0x4465
#define OTHER_SAMPLE_SERVICE_ID 0x0248
#define OTHER_SAMPLE_INSTANCE_ID 0x5422
diff --git a/examples/service-sample.cpp b/examples/service-sample.cpp
index 13f134b..98b8c66 100644
--- a/examples/service-sample.cpp
+++ b/examples/service-sample.cpp
@@ -16,120 +16,165 @@
#include "sample-ids.hpp"
class service_sample {
-public:
- service_sample(bool _use_tcp)
- : app_(vsomeip::runtime::get()->create_application()),
- is_registered_(false),
- use_tcp_(_use_tcp),
- offer_thread_(std::bind(&service_sample::run, this)) {
- }
-
- void init() {
- std::lock_guard< std::mutex > its_lock(mutex_);
-
- app_->init();
- app_->register_message_handler(
- SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_METHOD_ID,
- std::bind(&service_sample::on_message,
- this,
- std::placeholders::_1)
- );
-
- app_->register_event_handler(
- std::bind(&service_sample::on_event, this, std::placeholders::_1));
-
- blocked_ = true;
- condition_.notify_one();
- }
-
- void start() {
- app_->start();
- }
-
- void offer() {
- app_->offer_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
- }
-
- void stop_offer() {
- app_->stop_offer_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
- }
-
- void on_event(vsomeip::event_type_e _event) {
- VSOMEIP_INFO << "Application " << app_->get_name()
- << " is "
- << (_event == vsomeip::event_type_e::REGISTERED ? "registered." : "deregistered.");
-
- if (_event == vsomeip::event_type_e::REGISTERED) {
- if (!is_registered_) {
- is_registered_= true;
- }
- } else {
- is_registered_ = false;
- }
- }
-
- void on_message(std::shared_ptr< vsomeip::message > &_request) {
- VSOMEIP_INFO << "Received a message with Client/Session ["
- << std::setw(4) << std::setfill('0') << std::hex << _request->get_client()
- << "/"
- << std::setw(4) << std::setfill('0') << std::hex << _request->get_session()
- << "]";
-
- std::shared_ptr< vsomeip::message > its_response
- = vsomeip::runtime::get()->create_response(_request);
-
- std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload();
- std::vector< vsomeip::byte_t > its_payload_data;
- for (std::size_t i = 0; i < 120; ++i) its_payload_data.push_back(i % 256);
- its_payload->set_data(its_payload_data);
- its_response->set_payload(its_payload);
-
- app_->send(its_response, true, use_tcp_);
- }
-
- void run() {
- std::unique_lock< std::mutex > its_lock(mutex_);
- while (!blocked_) condition_.wait(its_lock);
-
- bool is_offer(true);
- while (true) {
- if (is_offer) offer();
- else stop_offer();
- std::this_thread::sleep_for(std::chrono::milliseconds(10000));
- is_offer = !is_offer;
- }
- }
-
-private:
- std::shared_ptr< vsomeip::application > app_;
- bool is_registered_;
- bool use_tcp_;
- std::thread offer_thread_;
- std::mutex mutex_;
- std::condition_variable condition_;
- bool blocked_;
+ public:
+ service_sample(bool _use_tcp)
+ : app_(vsomeip::runtime::get()->create_application()),
+ is_registered_(false),
+ use_tcp_(_use_tcp),
+ offer_thread_(std::bind(&service_sample::run, this)),
+ notification_thread_(std::bind(&service_sample::notify, this)) {
+ }
+
+ void init() {
+ std::lock_guard < std::mutex > its_lock(mutex_);
+
+ app_->init();
+ app_->register_message_handler(
+ SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_METHOD_ID,
+ std::bind(&service_sample::on_message, this, std::placeholders::_1));
+
+ app_->register_event_handler(
+ std::bind(&service_sample::on_event, this, std::placeholders::_1));
+
+ blocked_ = true;
+ condition_.notify_one();
+ }
+
+ void start() {
+ app_->start();
+ }
+
+ void offer() {
+ VSOMEIP_INFO << "Offering service.";
+ //std::lock_guard < std::mutex > its_lock(notification_mutex_);
+ app_->offer_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
+
+ notification_blocked_ = true;
+ //condition_.notify_one();
+ }
+
+ void stop_offer() {
+ VSOMEIP_INFO << "Stop offering service.";
+ app_->stop_offer_service(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID);
+ notification_blocked_ = false;
+ }
+
+ void update() {
+ static std::shared_ptr<vsomeip::payload> its_payload =
+ vsomeip::runtime::get()->create_payload();
+
+ static vsomeip::byte_t its_data[1000];
+ static uint32_t its_size = 0;
+
+ its_size++;
+ for (uint32_t i = 0; i < its_size; ++i)
+ its_data[i] = (i % 256);
+
+ its_payload->set_data(its_data, its_size);
+
+ VSOMEIP_INFO << "Updating event to " << its_size << " bytes.";
+ app_->set(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_EVENT_ID,
+ its_payload);
+ }
+
+ void on_event(vsomeip::event_type_e _event) {
+ VSOMEIP_INFO << "Application " << app_->get_name() << " is "
+ << (_event == vsomeip::event_type_e::REGISTERED ?
+ "registered." : "deregistered.");
+
+ if (_event == vsomeip::event_type_e::REGISTERED) {
+ if (!is_registered_) {
+ is_registered_ = true;
+ }
+ } else {
+ is_registered_ = false;
+ }
+ }
+
+ void on_message(std::shared_ptr<vsomeip::message> &_request) {
+ VSOMEIP_INFO << "Received a message with Client/Session [" << std::setw(4)
+ << std::setfill('0') << std::hex << _request->get_client() << "/"
+ << std::setw(4) << std::setfill('0') << std::hex
+ << _request->get_session() << "]";
+
+ std::shared_ptr<vsomeip::message> its_response = vsomeip::runtime::get()
+ ->create_response(_request);
+
+ std::shared_ptr<vsomeip::payload> its_payload = vsomeip::runtime::get()
+ ->create_payload();
+ std::vector<vsomeip::byte_t> its_payload_data;
+ for (std::size_t i = 0; i < 120; ++i)
+ its_payload_data.push_back(i % 256);
+ its_payload->set_data(its_payload_data);
+ its_response->set_payload(its_payload);
+
+ app_->send(its_response, true, use_tcp_);
+ }
+
+ void run() {
+ std::unique_lock < std::mutex > its_lock(mutex_);
+ while (!blocked_)
+ condition_.wait(its_lock);
+
+ bool is_offer(true);
+ while (true) {
+ if (is_offer)
+ offer();
+ else
+ stop_offer();
+ std::this_thread::sleep_for(std::chrono::milliseconds(10000));
+ is_offer = !is_offer;
+ }
+ }
+
+ void notify() {
+ while (true) {
+ //std::unique_lock < std::mutex > its_lock(notification_mutex_);
+ //while (!notification_blocked_)
+ // notification_condition_.wait(its_lock);
+ //while (notification_blocked_) {
+ std::this_thread::sleep_for(std::chrono::milliseconds(500));
+ if (notification_blocked_) update();
+ //}
+ }
+ }
+
+ private:
+ std::shared_ptr<vsomeip::application> app_;
+ bool is_registered_;
+ bool use_tcp_;
+
+ std::thread offer_thread_;
+ std::mutex mutex_;
+ std::condition_variable condition_;
+ bool blocked_;
+
+ std::thread notification_thread_;
+ std::mutex notification_mutex_;
+ std::condition_variable notification_condition_;
+ bool notification_blocked_;
};
int main(int argc, char **argv) {
- bool use_tcp = false;
-
- std::string tcp_enable("--tcp");
- std::string udp_enable("--udp");
-
- for (int i = 1; i < argc; i++) {
- if (tcp_enable == argv[i]) {
- use_tcp = true;
- break;
- }
- if (udp_enable == argv[i]) {
- use_tcp = false;
- break;
- }
- }
-
- service_sample its_sample(use_tcp);
- its_sample.init();
- its_sample.start();
-
- return 0;
+ bool use_tcp = false;
+
+ std::string tcp_enable("--tcp");
+ std::string udp_enable("--udp");
+
+ for (int i = 1; i < argc; i++) {
+ if (tcp_enable == argv[i]) {
+ use_tcp = true;
+ break;
+ }
+ if (udp_enable == argv[i]) {
+ use_tcp = false;
+ break;
+ }
+ }
+
+ service_sample its_sample(use_tcp);
+ its_sample.init();
+ its_sample.start();
+
+ return 0;
}