diff options
Diffstat (limited to 'test/internal_routing_disabled_acceptance_test/client.cpp')
-rw-r--r-- | test/internal_routing_disabled_acceptance_test/client.cpp | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/test/internal_routing_disabled_acceptance_test/client.cpp b/test/internal_routing_disabled_acceptance_test/client.cpp new file mode 100644 index 0000000..ff7be78 --- /dev/null +++ b/test/internal_routing_disabled_acceptance_test/client.cpp @@ -0,0 +1,152 @@ +#include "client.hpp" + +#include <chrono> +#include <iostream> +#include <thread> + +#include <vsomeip/constants.hpp> +#include <vsomeip/enumeration_types.hpp> +#include <vsomeip/message.hpp> +#include <vsomeip/payload.hpp> +#include <vsomeip/primitive_types.hpp> +#include <vsomeip/runtime.hpp> + +#include "config.hpp" + +client::client() : applet{"client"}, counter_event_received{}, counter_method_request{}, counter_method_response{} +{ + this->application->register_message_handler( + config::SERVICE_ID, + config::INSTANCE_ID, + vsomeip_v3::ANY_METHOD, + [this](const std::shared_ptr<vsomeip_v3::message>& message){ + std::shared_ptr runtime = vsomeip_v3::runtime::get(); + std::shared_ptr payload = message->get_payload(); + + switch(message->get_message_type()) + { + case vsomeip_v3::message_type_e::MT_RESPONSE: + std::cout + << "received:\n" + << "\tservice: " << std::hex << message->get_service() << '\n' + << "\tinstance: " << std::hex << message->get_instance() << '\n' + << "\tmethod: " << std::hex << message->get_method() << '\n' + << "\tpayload: " << payload->get_data() << '\n'; + this->counter_method_response++; + break; + + case vsomeip_v3::message_type_e::MT_NOTIFICATION: + std::cout << "GOT NOTIFICATION\n"; + this->counter_event_received++; + [[fallthrough]]; + + default: + std::cout << "unhandled message type: " << unsigned(message->get_message_type()) << '\n'; + } + } + ); + + this->application->register_availability_handler( + config::SERVICE_ID, + config::INSTANCE_ID, + [this](vsomeip_v3::service_t service, vsomeip_v3::instance_t instance, bool available){ + std::cout + << __func__ << '(' + << std::hex << service << ", " + << std::hex << instance << ", " + << std::boolalpha << available << ")\n"; + + if(service != config::SERVICE_ID) + return; + if(instance != config::INSTANCE_ID) + return; + if(!available) + return; + + std::shared_ptr runtime = vsomeip_v3::runtime::get(); + + std::shared_ptr payload = runtime->create_payload(); + constexpr vsomeip_v3::byte_t str[]{"hello world"}; + payload->set_data(str, sizeof(str)); + + std::shared_ptr request = runtime->create_request(); + request->set_service(config::SERVICE_ID); + request->set_instance(config::INSTANCE_ID); + request->set_method(config::METHOD_ID); + request->set_payload(payload); + + for(int i = 0; i < 10; i++) + { + std::cout << "sending: " << str << '\n'; + this->application->send(request); + this->counter_method_request++; + + using namespace std::chrono_literals; + std::this_thread::sleep_for(1s); + } + } + ); + + this->application->request_event( + config::SERVICE_ID, + config::INSTANCE_ID, + config::EVENT_ID, + {config::EVENTGROUP_ID}, + vsomeip_v3::event_type_e::ET_FIELD, + vsomeip_v3::reliability_type_e::RT_UNRELIABLE + ); + + this->application->subscribe( + config::SERVICE_ID, + config::INSTANCE_ID, + config::EVENTGROUP_ID + ); +} + +client::~client() +{ + this->application->unsubscribe( + config::SERVICE_ID, + config::INSTANCE_ID, + config::EVENTGROUP_ID + ); + + this->application->release_event( + config::SERVICE_ID, + config::INSTANCE_ID, + config::EVENT_ID + ); + + this->application->release_service( + config::SERVICE_ID, + config::INSTANCE_ID + ); +} + +std::size_t client::get_event_count() noexcept +{ + return this->counter_event_received; +} + +std::size_t client::get_method_request_count() noexcept +{ + return this->counter_method_request; +} + +std::size_t client::get_method_response_count() noexcept +{ + return this->counter_method_response; +} + +void client::on_state_registered() +{ + this->application->request_service( + config::SERVICE_ID, + config::INSTANCE_ID + ); +} + +void client::on_state_deregistered() +{ + std::cout << "Client is deregistered!!! Probably could not be registered!!!\n"; +} |