aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/includes/hicn/transport/utils
diff options
context:
space:
mode:
authorLuca Muscariello <lumuscar@cisco.com>2022-06-09 21:34:09 +0200
committerLuca Muscariello <muscariello@ieee.org>2022-06-30 10:47:50 +0200
commit6b94663b2455e212009a544ae23bb6a8c55407f8 (patch)
tree0af780ce5eeb1009fd24b8af8af08e8368eda3bd /libtransport/includes/hicn/transport/utils
parenta1ac96f497719b897793ac14b287cb8d840651c1 (diff)
refactor(lib, hicn-light, vpp, hiperf): HICN-723
- move infra data structure into the shared lib - new packet cache using double hashing and lookup on prefix suffix - testing updates - authenticated requests using interest manifests Co-authored-by: Mauro Sardara <msardara@cisco.com> Co-authored-by: Jordan Augé <jordan.auge+fdio@cisco.com> Co-authored-by: Michele Papalini <micpapal@cisco.com> Co-authored-by: Olivier Roques <oroques+fdio@cisco.com> Co-authored-by: Enrico Loparco <eloparco@cisco.com> Change-Id: Iaddebfe6aa5279ea8553433b0f519578f6b9ccd9 Signed-off-by: Luca Muscariello <muscariello@ieee.org>
Diffstat (limited to 'libtransport/includes/hicn/transport/utils')
-rw-r--r--libtransport/includes/hicn/transport/utils/chrono_typedefs.h2
-rw-r--r--libtransport/includes/hicn/transport/utils/color.h95
-rw-r--r--libtransport/includes/hicn/transport/utils/event_thread.h34
-rw-r--r--libtransport/includes/hicn/transport/utils/linux.h2
-rw-r--r--libtransport/includes/hicn/transport/utils/thread_pool.h3
5 files changed, 112 insertions, 24 deletions
diff --git a/libtransport/includes/hicn/transport/utils/chrono_typedefs.h b/libtransport/includes/hicn/transport/utils/chrono_typedefs.h
index ddfbd00cd..14234eaa1 100644
--- a/libtransport/includes/hicn/transport/utils/chrono_typedefs.h
+++ b/libtransport/includes/hicn/transport/utils/chrono_typedefs.h
@@ -79,7 +79,7 @@ class Time {
public:
using Clock = T;
using TimePoint = typename Clock::time_point;
- using Rep = double;
+ using Rep = uint64_t;
using Seconds = std::chrono::duration<Rep>;
using Milliseconds = std::chrono::duration<Rep, std::milli>;
using Microseconds = std::chrono::duration<Rep, std::micro>;
diff --git a/libtransport/includes/hicn/transport/utils/color.h b/libtransport/includes/hicn/transport/utils/color.h
new file mode 100644
index 000000000..3e8d93e14
--- /dev/null
+++ b/libtransport/includes/hicn/transport/utils/color.h
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2021 Cisco and/or its affiliates.
+ * 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.
+ */
+
+#pragma once
+
+#include <array>
+#include <ostream>
+#include <random>
+#include <sstream>
+
+namespace utils {
+
+#define foreach_modifier \
+ _(RESET, 0) \
+ _(BOLD, 1) \
+ _(FG_DEFAULT, 39) \
+ _(FG_BLACK, 30) \
+ _(FG_RED, 31) \
+ _(FG_GREEN, 32) \
+ _(FG_YELLOW, 33) \
+ _(FG_BLUE, 34) \
+ _(FG_MAGENTA, 35) \
+ _(FG_CYAN, 36) \
+ _(FG_LIGHT_GRAY, 37) \
+ _(FG_DARK_GRAY, 90) \
+ _(FG_LIGHT_RED, 91) \
+ _(FG_LIGHT_GREEN, 92) \
+ _(FG_LIGHT_YELLOW, 93) \
+ _(FG_LIGHT_BLUE, 94) \
+ _(FG_LIGHT_MAGENTA, 95) \
+ _(FG_LIGHT_CYAN, 96) \
+ _(FG_WHITE, 97) \
+ _(BG_RED, 41) \
+ _(BG_GREEN, 42) \
+ _(BG_BLUE, 44) \
+ _(BG_DEFAULT, 49)
+
+class ColorModifier {
+ static inline const std::size_t n_modifiers = 23;
+ static inline const char format_string_start[] = "\033[";
+ static inline const char format_string_end[] = "m";
+
+ public:
+ enum class Code {
+#define _(name, value) name = value,
+ foreach_modifier
+#undef _
+ };
+
+ static inline std::array<Code, n_modifiers> code_array = {
+#define _(name, value) Code::name,
+ foreach_modifier
+#undef _
+ };
+
+ static Code getRandomModifier() {
+ static std::random_device rd;
+ static std::mt19937 gen(rd());
+ static std::uniform_int_distribution<> distr(4, 17);
+
+ return code_array[distr(gen)];
+ }
+
+ ColorModifier(Code code) : code_(code), color_string_() {
+ std::stringstream ss;
+ if (std::getenv("COLORTERM") != nullptr) {
+ ss << format_string_start << static_cast<int>(code_) << format_string_end;
+ color_string_ = ss.str();
+ }
+ }
+
+ ColorModifier() : ColorModifier(getRandomModifier()) {}
+
+ friend std::ostream& operator<<(std::ostream& os, const ColorModifier& mod) {
+ return os << mod.color_string_;
+ }
+
+ private:
+ Code code_;
+ std::string color_string_;
+};
+
+} // namespace utils \ No newline at end of file
diff --git a/libtransport/includes/hicn/transport/utils/event_thread.h b/libtransport/includes/hicn/transport/utils/event_thread.h
index 2cd2f3aca..164c853a5 100644
--- a/libtransport/includes/hicn/transport/utils/event_thread.h
+++ b/libtransport/includes/hicn/transport/utils/event_thread.h
@@ -29,16 +29,16 @@ class EventThread {
EventThread(asio::io_service& io_service, bool detached = false)
: internal_io_service_(nullptr),
io_service_(std::ref(io_service)),
- work_(std::make_unique<asio::io_service::work>(io_service_)),
+ work_guard_(asio::make_work_guard(io_service_.get())),
thread_(nullptr),
detached_(detached) {
run();
}
- EventThread(bool detached = false)
+ explicit EventThread(bool detached = false)
: internal_io_service_(std::make_unique<asio::io_service>()),
io_service_(std::ref(*internal_io_service_)),
- work_(std::make_unique<asio::io_service::work>(io_service_)),
+ work_guard_(asio::make_work_guard(io_service_.get())),
thread_(nullptr),
detached_(detached) {
run();
@@ -47,22 +47,12 @@ class EventThread {
EventThread(const EventThread&) = delete;
EventThread& operator=(const EventThread&) = delete;
- EventThread(EventThread&& other)
+ EventThread(EventThread&& other) noexcept
: internal_io_service_(std::move(other.internal_io_service_)),
io_service_(std::move(other.io_service_)),
- work_(std::move(other.work_)),
+ work_guard_(std::move(other.work_guard_)),
thread_(std::move(other.thread_)),
- detached_(std::move(other.detached_)) {}
-
- EventThread& operator=(EventThread&& other) {
- internal_io_service_ = std::move(other.internal_io_service_);
- io_service_ = std::move(other.io_service_);
- work_ = std::move(other.work_);
- thread_ = std::move(other.thread_);
- detached_ = other.detached_;
-
- return *this;
- }
+ detached_(other.detached_) {}
~EventThread() { stop(); }
@@ -89,16 +79,16 @@ class EventThread {
template <typename Func>
void add(Func&& f) {
- io_service_.get().post(std::forward<Func&&>(f));
+ io_service_.get().post(std::forward<Func>(f));
}
template <typename Func>
void tryRunHandlerNow(Func&& f) {
- io_service_.get().dispatch(std::forward<Func&&>(f));
+ io_service_.get().dispatch(std::forward<Func>(f));
}
template <typename Func>
- void addAndWaitForExecution(Func&& f) {
+ void addAndWaitForExecution(Func&& f) const {
auto promise = std::promise<void>();
auto future = promise.get_future();
@@ -111,7 +101,7 @@ class EventThread {
}
void stop() {
- work_.reset();
+ add([this]() { work_guard_.reset(); });
if (thread_ && thread_->joinable()) {
thread_->join();
@@ -120,14 +110,14 @@ class EventThread {
thread_.reset();
}
- bool stopped() { return io_service_.get().stopped(); }
+ bool stopped() const { return io_service_.get().stopped(); }
asio::io_service& getIoService() { return io_service_; }
private:
std::unique_ptr<asio::io_service> internal_io_service_;
std::reference_wrapper<asio::io_service> io_service_;
- std::unique_ptr<asio::io_service::work> work_;
+ asio::executor_work_guard<asio::io_context::executor_type> work_guard_;
std::unique_ptr<std::thread> thread_;
bool detached_;
};
diff --git a/libtransport/includes/hicn/transport/utils/linux.h b/libtransport/includes/hicn/transport/utils/linux.h
index 03d29c1db..14ef179ac 100644
--- a/libtransport/includes/hicn/transport/utils/linux.h
+++ b/libtransport/includes/hicn/transport/utils/linux.h
@@ -44,7 +44,7 @@ static TRANSPORT_ALWAYS_INLINE int retrieveInterfaceAddress(
uint16_t prefix = 0;
memcpy(&prefix, tmp->sin6_addr.s6_addr, sizeof(uint16_t));
- if (htons(LINK_LOCAL_PREFIX) != prefix) {
+ if (portability::host_to_net(LINK_LOCAL_PREFIX) != prefix) {
*address = *(struct sockaddr_in6 *)ifa->ifa_addr;
getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), addr,
sizeof(addr), NULL, 0, NI_NUMERICHOST);
diff --git a/libtransport/includes/hicn/transport/utils/thread_pool.h b/libtransport/includes/hicn/transport/utils/thread_pool.h
index e4e47209c..76218ff09 100644
--- a/libtransport/includes/hicn/transport/utils/thread_pool.h
+++ b/libtransport/includes/hicn/transport/utils/thread_pool.h
@@ -29,8 +29,11 @@ class ThreadPool : public NonCopyable {
std::size_t n_threads = std::thread::hardware_concurrency())
: workers_(n_threads > 0 ? n_threads : 1) {}
+ ~ThreadPool() = default;
+
std::size_t getNThreads() const { return workers_.size(); }
EventThread &getWorker(std::size_t i) { return workers_.at(i); }
+ std::vector<EventThread> &getWorkers() { return workers_; }
private:
std::vector<EventThread> workers_;