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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*
* \file request_info.h
* \brief request information structure source file.
*
* Copyright (c) 2014, Ford Motor Company
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of the Ford Motor Company nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "application_manager/request_info.h"
#include <algorithm>
namespace application_manager {
namespace request_controller {
CREATE_LOGGERPTR_GLOBAL(logger_, "RequestController");
uint32_t RequestInfo::HmiConnectoinKey = 0;
HMIRequestInfo::HMIRequestInfo(
RequestPtr request,
const uint64_t timeout_sec):
RequestInfo(request, HMIRequest, timeout_sec) {
correlation_id_ = request_->correlation_id();
app_id_ = RequestInfo::HmiConnectoinKey;
}
HMIRequestInfo::HMIRequestInfo(
RequestPtr request,
const TimevalStruct &start_time,
const uint64_t timeout_sec):
RequestInfo(request, HMIRequest, start_time, timeout_sec) {
correlation_id_ = request_->correlation_id();
app_id_ = RequestInfo::HmiConnectoinKey;
}
MobileRequestInfo::MobileRequestInfo(
RequestPtr request,
const uint64_t timeout_sec):
RequestInfo(request, MobileRequest, timeout_sec) {
correlation_id_ = request_.get()->correlation_id();
app_id_ = request_.get()->connection_key();
}
MobileRequestInfo::MobileRequestInfo(
RequestPtr request,
const TimevalStruct &start_time,
const uint64_t timeout_sec):
RequestInfo(request, MobileRequest, start_time, timeout_sec) {
correlation_id_ = request_.get()->correlation_id();
app_id_ = request_.get()->connection_key();
}
RequestInfo::RequestInfo(RequestPtr request,
const RequestInfo::RequestType requst_type,
const TimevalStruct& start_time,
const uint64_t timeout_sec):
request_(request),
start_time_(start_time),
timeout_sec_(timeout_sec),
hmi_level_(mobile_apis::HMILevel::INVALID_ENUM) {
updateEndTime();
requst_type_ = requst_type;
correlation_id_ = request_->correlation_id();
app_id_ = request_->connection_key();
}
void application_manager::request_controller::RequestInfo::updateEndTime() {
end_time_ = date_time::DateTime::getCurrentTime();
end_time_.tv_sec += timeout_sec_;
// possible delay during IPC
const uint32_t hmi_delay_sec = 1;
end_time_.tv_sec += hmi_delay_sec;
}
void RequestInfo::updateTimeOut(const uint64_t& timeout_sec) {
timeout_sec_ = timeout_sec;
updateEndTime();
}
bool RequestInfo::isExpired() {
TimevalStruct curr_time = date_time::DateTime::getCurrentTime();
return end_time_.tv_sec <= curr_time.tv_sec;
// TODO(AKutsan) APPLINK-9711 Need to use compareTime method when timer will support millisecconds
// return date_time::GREATER == date_time::DateTime::compareTime(end_time_, curr_time);
}
uint64_t RequestInfo::hash() {
return GenerateHash(app_id(), requestId());
}
uint64_t RequestInfo::GenerateHash(uint32_t var1, uint32_t var2) {
uint64_t hash_result = 0;
hash_result = var1;
hash_result = hash_result << 32;
hash_result = hash_result | var2;
return hash_result;
}
FakeRequestInfo::FakeRequestInfo(uint32_t app_id, uint32_t correaltion_id) {
app_id_ = app_id;
correlation_id_ = correaltion_id;
}
bool RequestInfoSet::Add(RequestInfoPtr request_info) {
DCHECK_OR_RETURN(request_info, false);
LOG4CXX_DEBUG(logger_, "Add request app_id = " << request_info->app_id()
<< "; corr_id = " << request_info->requestId());
sync_primitives::AutoLock lock(this_lock_);
CheckSetSizes();
const std::pair<HashSortedRequestInfoSet::iterator, bool>& insert_resilt =
hash_sorted_pending_requests_.insert(request_info);
if (insert_resilt.second == true) {
const std::pair<TimeSortedRequestInfoSet::iterator, bool>& insert_resilt =
time_sorted_pending_requests_.insert(request_info);
DCHECK(insert_resilt.second);
if (!insert_resilt.second) {
return false;
}
CheckSetSizes();
return true;
} else {
LOG4CXX_ERROR(logger_, "Request with app_id = " << request_info->app_id()
<< "; corr_id " << request_info->requestId() << " Already exist ");
}
CheckSetSizes();
return false;
}
RequestInfoPtr RequestInfoSet::Find(const uint32_t connection_key,
const uint32_t correlation_id) {
RequestInfoPtr result;
// Request info for searching in request info set by log_n time
utils::SharedPtr<FakeRequestInfo> request_info_for_search(
new FakeRequestInfo(connection_key, correlation_id));
sync_primitives::AutoLock lock(this_lock_);
HashSortedRequestInfoSet::iterator it =
hash_sorted_pending_requests_.find(request_info_for_search);
if (it != hash_sorted_pending_requests_.end()) {
result = *it;
}
return result;
}
RequestInfoPtr RequestInfoSet::Front() {
RequestInfoPtr result;
sync_primitives::AutoLock lock(this_lock_);
TimeSortedRequestInfoSet::iterator it = time_sorted_pending_requests_.begin();
if (it != time_sorted_pending_requests_.end()) {
result = *it;
}
return result;
}
RequestInfoPtr RequestInfoSet::FrontWithNotNullTimeout() {
LOG4CXX_AUTO_TRACE(logger_);
sync_primitives::AutoLock lock(this_lock_);
RequestInfoPtr result;
TimeSortedRequestInfoSet::iterator it = time_sorted_pending_requests_.begin();
while (it != time_sorted_pending_requests_.end()) {
RequestInfoPtr tmp = *it;
if (0 == tmp ->timeout_sec()) {
++it;
} else {
result = tmp;
it = time_sorted_pending_requests_.end();
}
}
return result;
}
bool RequestInfoSet::Erase(const RequestInfoPtr request_info) {
DCHECK(request_info);
if (!request_info) {
LOG4CXX_ERROR(logger_, "NULL ponter request_info");
return false;
}
CheckSetSizes();
size_t erased_count =
hash_sorted_pending_requests_.erase(request_info);
DCHECK((erased_count <= 1));
if (1 == erased_count) {
TimeSortedRequestInfoSet::iterator it =
time_sorted_pending_requests_.find(request_info);
DCHECK(it != time_sorted_pending_requests_.end());
if (it == time_sorted_pending_requests_.end()) {
LOG4CXX_ERROR(logger_, "Can't find request in time_sorted_requests_");
return false;
}
const RequestInfoPtr found = *it;
DCHECK(request_info == found);
time_sorted_pending_requests_.erase(it);
CheckSetSizes();
return 1 == erased_count;
}
CheckSetSizes();
return false;
}
bool RequestInfoSet::RemoveRequest(const RequestInfoPtr request_info) {
sync_primitives::AutoLock lock(this_lock_);
return Erase(request_info);
}
uint32_t RequestInfoSet::RemoveRequests(const RequestInfoSet::AppIdCompararator& filter) {
LOG4CXX_AUTO_TRACE(logger_);
uint32_t erased = 0;
sync_primitives::AutoLock lock(this_lock_);
HashSortedRequestInfoSet::iterator it = std::find_if(
hash_sorted_pending_requests_.begin(),
hash_sorted_pending_requests_.end(),
filter);
while (it != hash_sorted_pending_requests_.end()) {
HashSortedRequestInfoSet::iterator to_erase = it++;
Erase(*to_erase);
it = std::find_if(it, hash_sorted_pending_requests_.end(), filter);
erased++;
}
CheckSetSizes();
return erased;
}
uint32_t RequestInfoSet::RemoveByConnectionKey(uint32_t connection_key) {
LOG4CXX_AUTO_TRACE(logger_);
return RemoveRequests(AppIdCompararator(AppIdCompararator::Equal, connection_key));
}
uint32_t RequestInfoSet::RemoveMobileRequests() {
LOG4CXX_AUTO_TRACE(logger_);
return RemoveRequests(AppIdCompararator(AppIdCompararator::NotEqual, RequestInfo::HmiConnectoinKey));
}
const size_t RequestInfoSet::Size() {
CheckSetSizes();
return time_sorted_pending_requests_.size();
}
void RequestInfoSet::CheckSetSizes() {
const ssize_t time_set_size = time_sorted_pending_requests_.size();
const ssize_t hash_set_size = hash_sorted_pending_requests_.size();
const bool set_sizes_equal = (time_set_size == hash_set_size);
DCHECK(set_sizes_equal);
}
bool RequestInfoSet::CheckTimeScaleMaxRequest(
uint32_t app_id,
uint32_t app_time_scale,
uint32_t max_request_per_time_scale) {
LOG4CXX_AUTO_TRACE(logger_);
if (max_request_per_time_scale > 0
&& app_time_scale > 0) {
TimevalStruct end = date_time::DateTime::getCurrentTime();
TimevalStruct start = {0, 0};
start.tv_sec = end.tv_sec - app_time_scale;
sync_primitives::AutoLock lock(this_lock_);
TimeScale scale(start, end, app_id);
const uint32_t count = std::count_if(time_sorted_pending_requests_.begin(),
time_sorted_pending_requests_.end(), scale);
if (count >= max_request_per_time_scale) {
LOG4CXX_WARN(logger_, "Processing requests count " << count <<
" exceed application limit " << max_request_per_time_scale);
return false;
}
LOG4CXX_DEBUG(logger_, "Requests count " << count);
} else {
LOG4CXX_DEBUG(logger_, "CheckTimeScaleMaxRequest disabled");
}
return true;
}
bool RequestInfoSet::CheckHMILevelTimeScaleMaxRequest(mobile_apis::HMILevel::eType hmi_level,
uint32_t app_id,
uint32_t app_time_scale,
uint32_t max_request_per_time_scale) {
LOG4CXX_AUTO_TRACE(logger_);
if (max_request_per_time_scale > 0 &&
app_time_scale > 0) {
TimevalStruct end = date_time::DateTime::getCurrentTime();
TimevalStruct start = {0, 0};
start.tv_sec = end.tv_sec - app_time_scale;
sync_primitives::AutoLock lock(this_lock_);
HMILevelTimeScale scale(start, end, app_id, hmi_level);
const uint32_t count = std::count_if(time_sorted_pending_requests_.begin(),
time_sorted_pending_requests_.end(), scale);
if (count >= max_request_per_time_scale) {
LOG4CXX_WARN(logger_, "Processing requests count " << count
<< " exceed application limit " << max_request_per_time_scale
<< " in hmi level " << hmi_level);
return false;
}
LOG4CXX_DEBUG(logger_, "Requests count " << count);
} else {
LOG4CXX_DEBUG(logger_, "CheckHMILevelTimeScaleMaxRequest disabled");
}
return true;
}
bool RequestInfoSet::AppIdCompararator::operator()(const RequestInfoPtr value_compare) const {
switch (compare_type_) {
case Equal:
return value_compare->app_id() == app_id_;
case NotEqual:
return value_compare->app_id() != app_id_;
default:
return false;
}
}
bool RequestInfoTimeComparator::operator()(const RequestInfoPtr lhs,
const RequestInfoPtr rhs) const {
date_time::TimeCompare compare_result =
date_time::DateTime::compareTime(lhs->end_time(), rhs->end_time());
if (compare_result == date_time::LESS) {
return true;
} else if (compare_result == date_time::GREATER) {
return false;
}
// compare_result == date_time::EQUAL
// If time is equal, sort by hash
return lhs->hash() < rhs->hash();
}
bool RequestInfoHashComparator::operator()(const RequestInfoPtr lhs,
const RequestInfoPtr rhs) const {
return lhs->hash() < rhs->hash();
}
} // namespace request_controller
} // namespace application_manager
|