summaryrefslogtreecommitdiff
path: root/implementation/routing/src/event.cpp
blob: ffaec8aabc25899a32f02f8f66ae8de51f5d0714 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (C) 2014 BMW Group
// Author: Lutz Bichler (lutz.bichler@bmw.de)
// 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/.

#include <iomanip>

#include <vsomeip/defines.hpp>
#include <vsomeip/logger.hpp>
#include <vsomeip/message.hpp>
#include <vsomeip/payload.hpp>
#include <vsomeip/runtime.hpp>

#include "../include/event.hpp"
#include "../include/routing_manager.hpp"
#include "../../configuration/include/internal.hpp"

namespace vsomeip {

event::event(routing_manager *_routing) :
		routing_(_routing), message_(runtime::get()->create_notification()), cycle_timer_(
				_routing->get_io()), is_updating_on_change_(true) {
}

service_t event::get_service() const {
	return (message_->get_service());
}

void event::set_service(service_t _service) {
	message_->set_service(_service);
}

instance_t event::get_instance() const {
	return (message_->get_instance());
}

void event::set_instance(instance_t _instance) {
	message_->set_instance(_instance);
}

event_t event::get_event() const {
	return (message_->get_method());
}

void event::set_event(event_t _event) {
	message_->set_method(_event); // TODO: maybe we should check for the leading 0-bit
}

bool event::is_field() const {
	return (is_field_);
}

void event::set_field(bool _is_field) {
	is_field_ = _is_field;
}

bool event::is_reliable() const {
	return is_reliable_;
}

void event::set_reliable(bool _is_reliable) {
	is_reliable_ = _is_reliable;
}

const std::shared_ptr<payload> event::get_payload() const {
	return (message_->get_payload());
}

void event::set_payload(std::shared_ptr<payload> _payload) {
	std::shared_ptr<payload> its_payload = message_->get_payload();
	bool is_change = (its_payload->get_length() != _payload->get_length());
	if (!is_change) {
		std::size_t its_pos = 0;
		const byte_t *its_old_data = its_payload->get_data();
		const byte_t *its_new_data = _payload->get_data();
		while (!is_change && its_pos < its_payload->get_length()) {
			is_change = (*its_old_data++ != *its_new_data++);
			its_pos++;
		}
	}

	if (is_change) {
		std::shared_ptr<payload> its_new_payload
			= runtime::get()->create_payload(
				_payload->get_data(), _payload->get_length());
		message_->set_payload(its_new_payload);
		if (is_updating_on_change_) {
			notify();
		}
	}
}

void event::set_update_on_change(bool _is_active) {
	is_updating_on_change_ = _is_active;
}

void event::set_update_cycle(std::chrono::milliseconds &_cycle) {
	cycle_ = _cycle;
	cycle_timer_.cancel();

	if (std::chrono::milliseconds::zero() != _cycle) {
		cycle_timer_.expires_from_now(cycle_);
		std::function<void(boost::system::error_code const &)> its_handler =
				std::bind(&event::update_cbk, shared_from_this(),
						std::placeholders::_1);
		cycle_timer_.async_wait(its_handler);
	}
}

const std::set<eventgroup_t> & event::get_eventgroups() const {
	return (eventgroups_);
}

void event::add_eventgroup(eventgroup_t _eventgroup) {
	eventgroups_.insert(_eventgroup);
}

void event::set_eventgroups(const std::set<eventgroup_t> &_eventgroups) {
	eventgroups_ = _eventgroups;
}

void event::update_cbk(boost::system::error_code const &_error) {
	if (!_error) {
		cycle_timer_.expires_from_now(cycle_);
		notify();
		std::function<void(boost::system::error_code const &)> its_handler =
				std::bind(&event::update_cbk, shared_from_this(),
						std::placeholders::_1);
		cycle_timer_.async_wait(its_handler);
	}
}

void event::notify() {
	routing_->send(VSOMEIP_ROUTING_CLIENT, message_, true, is_reliable_);
}

void event::notify_one(const std::shared_ptr<endpoint_definition> &_target) {
	routing_->send_to(_target, message_);
}

}  // namespace vsomeip