diff options
author | Dirk Huss <dirk_huss@mentor.com> | 2015-11-25 15:11:49 +0100 |
---|---|---|
committer | Dirk Huss <dirk_huss@mentor.com> | 2015-11-25 15:11:49 +0100 |
commit | 78be04b467566633318a277ccd2d968c1c4e46bf (patch) | |
tree | 444ef60cfdca2cc80d584f26911fb08b93a034b9 /examples/subscribe-sample.cpp | |
parent | 40d4759f262a86bcebaf3a91d9d813f4d6a3ae10 (diff) | |
download | vSomeIP-78be04b467566633318a277ccd2d968c1c4e46bf.tar.gz |
vSomeIP 2.0.02.0.0
Diffstat (limited to 'examples/subscribe-sample.cpp')
-rw-r--r-- | examples/subscribe-sample.cpp | 233 |
1 files changed, 123 insertions, 110 deletions
diff --git a/examples/subscribe-sample.cpp b/examples/subscribe-sample.cpp index 3faa4d1..9a03722 100644 --- a/examples/subscribe-sample.cpp +++ b/examples/subscribe-sample.cpp @@ -16,119 +16,132 @@ class client_sample { public: - client_sample(bool _use_tcp) : - app_(vsomeip::runtime::get()->create_application()), use_tcp_( - _use_tcp) { - } - - void init() { - app_->init(); - - VSOMEIP_INFO << "Client settings [protocol=" - << (use_tcp_ ? "TCP" : "UDP") - << "]"; - - app_->register_availability_handler(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, - std::bind(&client_sample::on_availability, - this, - std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); - - app_->register_message_handler(vsomeip::ANY_SERVICE, SAMPLE_INSTANCE_ID, - vsomeip::ANY_METHOD, - std::bind(&client_sample::on_message, this, - std::placeholders::_1)); - } - - void start() { - app_->start(); - } - - void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { - VSOMEIP_INFO << "Service [" - << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance - << "] is " - << (_is_available ? "available." : "NOT available."); - - if (_is_available && SAMPLE_SERVICE_ID == _service && SAMPLE_INSTANCE_ID == _instance) { - app_->subscribe(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_EVENTGROUP_ID); - } - } - - void on_message(const std::shared_ptr<vsomeip::message> &_response) { - std::stringstream its_message; - its_message << "Received a notification for Event [" << std::setw(4) - << std::setfill('0') << std::hex << _response->get_service() - << "." << _response->get_instance() << "." - << _response->get_method() << "] to Client/Session [" - << std::setw(4) << std::setfill('0') << std::hex - << _response->get_client() << "/" << std::setw(4) - << std::setfill('0') << std::hex << _response->get_session() - << "] = "; - std::shared_ptr<vsomeip::payload> its_payload = - _response->get_payload(); - its_message << "(" << std::dec << its_payload->get_length() << ") "; - for (uint32_t i = 0; i < its_payload->get_length(); ++i) - its_message << std::hex << std::setw(2) << std::setfill('0') - << (int) its_payload->get_data()[i] << " "; - VSOMEIP_INFO << its_message.str(); - - if (_response->get_client() == 0) { - if ((its_payload->get_length() % 5) == 0) { - std::shared_ptr<vsomeip::message> its_get - = vsomeip::runtime::get()->create_request(); - its_get->set_service(SAMPLE_SERVICE_ID); - its_get->set_instance(SAMPLE_INSTANCE_ID); - its_get->set_method(SAMPLE_GET_METHOD_ID); - its_get->set_reliable(use_tcp_); - app_->send(its_get, true); - } - - if ((its_payload->get_length() % 8) == 0) { - std::shared_ptr<vsomeip::message> its_set - = vsomeip::runtime::get()->create_request(); - its_set->set_service(SAMPLE_SERVICE_ID); - its_set->set_instance(SAMPLE_INSTANCE_ID); - its_set->set_method(SAMPLE_SET_METHOD_ID); - its_set->set_reliable(use_tcp_); - - const vsomeip::byte_t its_data[] - = { 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, - 0x48, 0x49, 0x50, 0x51, 0x52 }; - std::shared_ptr<vsomeip::payload> its_set_payload - = vsomeip::runtime::get()->create_payload(); - its_set_payload->set_data(its_data, sizeof(its_data)); - its_set->set_payload(its_set_payload); - app_->send(its_set, true); - } - } - } + client_sample(bool _use_tcp) : + app_(vsomeip::runtime::get()->create_application()), use_tcp_( + _use_tcp) { + } + + void init() { + app_->init(); + + std::cout << "Client settings [protocol=" + << (use_tcp_ ? "TCP" : "UDP") + << "]" + << std::endl; + + app_->register_message_handler( + vsomeip::ANY_SERVICE, SAMPLE_INSTANCE_ID, vsomeip::ANY_METHOD, + std::bind(&client_sample::on_message, this, + std::placeholders::_1)); + + app_->register_availability_handler(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, + std::bind(&client_sample::on_availability, + this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + + std::set<vsomeip::eventgroup_t> its_groups; + its_groups.insert(SAMPLE_EVENTGROUP_ID); + app_->request_event( + SAMPLE_SERVICE_ID, + SAMPLE_INSTANCE_ID, + SAMPLE_EVENT_ID, + its_groups, + true); + } + + void start() { + app_->start(); + } + + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "Service [" + << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance + << "] is " + << (_is_available ? "available." : "NOT available.") + << std::endl; + + if (_is_available && SAMPLE_SERVICE_ID == _service && SAMPLE_INSTANCE_ID == _instance) { + app_->subscribe(SAMPLE_SERVICE_ID, SAMPLE_INSTANCE_ID, SAMPLE_EVENTGROUP_ID); + } + } + + void on_message(const std::shared_ptr<vsomeip::message> &_response) { + std::stringstream its_message; + its_message << "Received a notification for Event [" + << std::setw(4) << std::setfill('0') << std::hex + << _response->get_service() << "." + << std::setw(4) << std::setfill('0') << std::hex + << _response->get_instance() << "." + << std::setw(4) << std::setfill('0') << std::hex + << _response->get_method() << "] to Client/Session [" + << std::setw(4) << std::setfill('0') << std::hex + << _response->get_client() << "/" + << std::setw(4) << std::setfill('0') << std::hex + << _response->get_session() + << "] = "; + std::shared_ptr<vsomeip::payload> its_payload = + _response->get_payload(); + its_message << "(" << std::dec << its_payload->get_length() << ") "; + for (uint32_t i = 0; i < its_payload->get_length(); ++i) + its_message << std::hex << std::setw(2) << std::setfill('0') + << (int) its_payload->get_data()[i] << " "; + std::cout << its_message.str() << std::endl; + + if (_response->get_client() == 0) { + if ((its_payload->get_length() % 5) == 0) { + std::shared_ptr<vsomeip::message> its_get + = vsomeip::runtime::get()->create_request(); + its_get->set_service(SAMPLE_SERVICE_ID); + its_get->set_instance(SAMPLE_INSTANCE_ID); + its_get->set_method(SAMPLE_GET_METHOD_ID); + its_get->set_reliable(use_tcp_); + app_->send(its_get, true); + } + + if ((its_payload->get_length() % 8) == 0) { + std::shared_ptr<vsomeip::message> its_set + = vsomeip::runtime::get()->create_request(); + its_set->set_service(SAMPLE_SERVICE_ID); + its_set->set_instance(SAMPLE_INSTANCE_ID); + its_set->set_method(SAMPLE_SET_METHOD_ID); + its_set->set_reliable(use_tcp_); + + const vsomeip::byte_t its_data[] + = { 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x50, 0x51, 0x52 }; + std::shared_ptr<vsomeip::payload> its_set_payload + = vsomeip::runtime::get()->create_payload(); + its_set_payload->set_data(its_data, sizeof(its_data)); + its_set->set_payload(its_set_payload); + app_->send(its_set, true); + } + } + } private: - std::shared_ptr< vsomeip::application > app_; - bool use_tcp_; - bool be_quiet_; + std::shared_ptr< vsomeip::application > app_; + bool use_tcp_; + bool be_quiet_; }; int main(int argc, char **argv) { - bool use_tcp = false; - bool be_quiet = false; - uint32_t cycle = 1000; // Default: 1s - - std::string tcp_enable("--tcp"); - std::string udp_enable("--udp"); - - int i = 1; - while (i < argc) { - if (tcp_enable == argv[i]) { - use_tcp = true; - } else if (udp_enable == argv[i]) { - use_tcp = false; - } - i++; - } - - client_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"); + + int i = 1; + while (i < argc) { + if (tcp_enable == argv[i]) { + use_tcp = true; + } else if (udp_enable == argv[i]) { + use_tcp = false; + } + i++; + } + + client_sample its_sample(use_tcp); + its_sample.init(); + its_sample.start(); + return 0; } |