-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
56 changed files
with
3,616 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
centreon-broker/core/inc/com/centreon/broker/io/limit_endpoint.hh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
** Copyright 2022 Centreon | ||
** | ||
** Licensed under the Apache License, Version 2.0 (the "License"); | ||
** you may not use this file except in compliance with the License. | ||
** You may obtain a copy of the License at | ||
** | ||
** http://www.apache.org/licenses/LICENSE-2.0 | ||
** | ||
** Unless required by applicable law or agreed to in writing, software | ||
** distributed under the License is distributed on an "AS IS" BASIS, | ||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
** See the License for the specific language governing permissions and | ||
** limitations under the License. | ||
** | ||
** For more information : contact@centreon.com | ||
*/ | ||
|
||
#ifndef CCB_IO_LIMIT_ENDPOINT_HH | ||
#define CCB_IO_LIMIT_ENDPOINT_HH | ||
|
||
#include "endpoint.hh" | ||
|
||
CCB_BEGIN() | ||
|
||
namespace io { | ||
|
||
class limit_endpoint : public endpoint { | ||
protected: | ||
/* How many consecutive calls to is_ready() */ | ||
mutable int16_t _is_ready_count; | ||
/* The time of the last call to is_ready() */ | ||
mutable std::time_t _is_ready_now; | ||
|
||
public: | ||
limit_endpoint(bool is_accptr) | ||
: endpoint(is_accptr), _is_ready_count(0), _is_ready_now(0) {} | ||
|
||
std::unique_ptr<stream> open() override; | ||
bool is_ready() const override; | ||
|
||
virtual std::unique_ptr<stream> create_stream() = 0; | ||
}; | ||
|
||
} // namespace io | ||
|
||
CCB_END() | ||
|
||
#endif // !CCB_IO_LIMIT_ENDPOINT_HH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
centreon-broker/core/inc/com/centreon/broker/misc/trash.hh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
** Copyright 2022 Centreon | ||
** | ||
** Licensed under the Apache License, Version 2.0 (the "License"); | ||
** you may not use this file except in compliance with the License. | ||
** You may obtain a copy of the License at | ||
** | ||
** http://www.apache.org/licenses/LICENSE-2.0 | ||
** | ||
** Unless required by applicable law or agreed to in writing, software | ||
** distributed under the License is distributed on an "AS IS" BASIS, | ||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
** See the License for the specific language governing permissions and | ||
** limitations under the License. | ||
** | ||
** For more information : contact@centreon.com | ||
*/ | ||
|
||
#ifndef CCB_MISC_TRASH_HH | ||
#define CCB_MISC_TRASH_HH | ||
|
||
#include <forward_list> | ||
#include <ctime> | ||
#include "com/centreon/broker/namespace.hh" | ||
|
||
CCB_BEGIN() | ||
|
||
namespace misc { | ||
/** | ||
* @brief this class is a delayed trash where objects are thrown on. Then they are dereferenced after a time passed in parameter | ||
* it s can be used for objects involved in unsafe asynchronous operations where we aren t sure of objects owning by asynchronous layers | ||
* | ||
* TODO: if we used boost, replace forward_list by a multi_index_container | ||
* @tparam T objects to delete | ||
*/ | ||
template <class T> | ||
class trash { | ||
public: | ||
using element_pointer = std::shared_ptr<T>; | ||
protected: | ||
struct element_erase_pair{ | ||
element_erase_pair() {} | ||
element_erase_pair(const element_pointer & element, time_t time_to_eras) | ||
:elem(element), time_to_erase(time_to_eras) {} | ||
|
||
element_pointer elem; | ||
time_t time_to_erase; | ||
}; | ||
|
||
using trash_content = std::forward_list<element_erase_pair>; | ||
|
||
trash_content _content; | ||
std::mutex _protect; | ||
|
||
void clean(); | ||
public: | ||
void to_trash(const element_pointer & to_throw, time_t time_to_erase); | ||
void refresh_time_to_erase(const element_pointer & to_throw, time_t time_to_erase); | ||
}; | ||
|
||
/** | ||
* @brief dereference perempted objects | ||
* not mutex protected | ||
* @tparam T | ||
*/ | ||
template <class T> | ||
void trash<T>::clean() { | ||
time_t now = time(nullptr); | ||
_content.remove_if([now](const element_erase_pair & toTest) { return toTest.time_to_erase < now;}); | ||
} | ||
|
||
template <class T> | ||
void trash<T>::to_trash(const element_pointer & to_throw, time_t time_to_erase) { | ||
std::unique_lock<std::mutex> l(_protect); | ||
clean(); | ||
for (element_erase_pair toUpdate: _content) { | ||
if (toUpdate.elem == to_throw) { | ||
toUpdate.time_to_erase = time_to_erase; | ||
return; | ||
} | ||
} | ||
_content.emplace_front(to_throw, time_to_erase); | ||
} | ||
|
||
template <class T> | ||
void trash<T>::refresh_time_to_erase(const element_pointer & to_update, time_t time_to_erase) { | ||
std::unique_lock<std::mutex> l(_protect); | ||
clean(); | ||
for (element_erase_pair toUpdate: _content) { | ||
if (toUpdate.elem == to_update) { | ||
toUpdate.time_to_erase = time_to_erase; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
|
||
} // namespace misc | ||
|
||
CCB_END() | ||
|
||
#endif // !CCB_MISC_TOKENIZER_HH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
** Copyright 2022 Centreon | ||
** | ||
** Licensed under the Apache License, Version 2.0 (the "License"); | ||
** you may not use this file except in compliance with the License. | ||
** You may obtain a copy of the License at | ||
** | ||
** http://www.apache.org/licenses/LICENSE-2.0 | ||
** | ||
** Unless required by applicable law or agreed to in writing, software | ||
** distributed under the License is distributed on an "AS IS" BASIS, | ||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
** See the License for the specific language governing permissions and | ||
** limitations under the License. | ||
** | ||
** For more information : contact@centreon.com | ||
*/ | ||
#include "com/centreon/broker/io/limit_endpoint.hh" | ||
|
||
|
||
using namespace com::centreon::broker::io; | ||
|
||
/** | ||
* @brief Connect to the remote host. | ||
* | ||
* @return The connection object. | ||
*/ | ||
std::unique_ptr<stream> limit_endpoint::open() { | ||
// Launch connection process. | ||
try { | ||
std::unique_ptr<stream> retval = create_stream(); | ||
_is_ready_count = 0; | ||
return retval; | ||
} catch (const std::exception& e) { | ||
if (_is_ready_count < 30) | ||
_is_ready_count++; | ||
return nullptr; | ||
} | ||
} | ||
|
||
/** | ||
* @brief Return true when it is time to attempt a new connection. The idea is | ||
* to increase the duration between two calls each time this function is called | ||
* without connection between. So if now server is available, we should not | ||
* try to connect too often, but if the connection failed one time, it should | ||
* be fast to connect again. | ||
* | ||
* @return a boolean. | ||
*/ | ||
bool limit_endpoint::is_ready() const { | ||
time_t now; | ||
std::time(&now); | ||
if (now - _is_ready_now > (1 << _is_ready_count)) { | ||
_is_ready_now = now; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
Oops, something went wrong.