From 81fb39606b069fbece973995572fa7f90ea1950a Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Thu, 14 May 2020 20:21:02 +0200 Subject: [HICN-613] Add io_service to ConsumerSocket constructor. Change-Id: Ic1952388e1d2b1e7457c71ae8a959d97aa0cd2d6 Signed-off-by: Mauro Sardara --- .../includes/hicn/transport/http/request.h | 5 + .../hicn/transport/interfaces/socket_consumer.h | 21 ++++- .../includes/hicn/transport/utils/CMakeLists.txt | 1 + .../includes/hicn/transport/utils/event_thread.h | 101 +++++++++++++++++++++ .../includes/hicn/transport/utils/ring_buffer.h | 1 + 5 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 libtransport/includes/hicn/transport/utils/event_thread.h (limited to 'libtransport/includes') diff --git a/libtransport/includes/hicn/transport/http/request.h b/libtransport/includes/hicn/transport/http/request.h index 54904d696..b62f5b061 100644 --- a/libtransport/includes/hicn/transport/http/request.h +++ b/libtransport/includes/hicn/transport/http/request.h @@ -46,6 +46,11 @@ class HTTPRequest : public HTTPMessage { std::string getRequestString() const; + static std::size_t parseHeaders(const uint8_t *buffer, std::size_t size, + HTTPHeaders &headers, + std::string &http_version, + std::string &method, std::string &url); + private: std::string query_string_, path_, protocol_, locator_, port_; std::string request_string_; diff --git a/libtransport/includes/hicn/transport/interfaces/socket_consumer.h b/libtransport/includes/hicn/transport/interfaces/socket_consumer.h index 73cbb78b0..2447f9b5b 100644 --- a/libtransport/includes/hicn/transport/interfaces/socket_consumer.h +++ b/libtransport/includes/hicn/transport/interfaces/socket_consumer.h @@ -145,7 +145,7 @@ class ConsumerSocket { * @param protocol - The transport protocol to use. So far the following * transport are supported: * - CBR: Constant bitrate - * - Raaqm: Based on paper: Optimal multipath congestion control and request + * - RAAQM: Based on paper: Optimal multipath congestion control and request * forwarding in information-centric networks: Protocol design and * experimentation. G Carofiglio, M Gallo, L Muscariello. Computer Networks * 110, 104-117 @@ -153,6 +153,25 @@ class ConsumerSocket { */ explicit ConsumerSocket(int protocol); + /** + * @brief Create a new consumer socket, passing an io_service to it. + * Passing an io_service means that the caller must explicitely call + * io_service.run() for the consumer to start. Any call to consume won't be + * blocking. This can be used in case we want to share a single thread + * among multiple consumer sockets. The caller MUST ensure the provided + * io_service will outlive the ConsumerSocket. + * + * @param protocol - The transport protocol to use. So far the following + * transport are supported: + * - CBR: Constant bitrate + * - RAAQM: Based on paper: Optimal multipath congestion control and request + * forwarding in information-centric networks: Protocol design and + * experimentation. G Carofiglio, M Gallo, L Muscariello. Computer Networks + * 110, 104-117 + * - RTC: Real time communication + */ + explicit ConsumerSocket(int protocol, asio::io_service &io_service); + /** * @brief Destroy the consumer socket. */ diff --git a/libtransport/includes/hicn/transport/utils/CMakeLists.txt b/libtransport/includes/hicn/transport/utils/CMakeLists.txt index 38ecc3d37..f9a98dc69 100644 --- a/libtransport/includes/hicn/transport/utils/CMakeLists.txt +++ b/libtransport/includes/hicn/transport/utils/CMakeLists.txt @@ -29,6 +29,7 @@ list(APPEND HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/membuf.h ${CMAKE_CURRENT_SOURCE_DIR}/spinlock.h ${CMAKE_CURRENT_SOURCE_DIR}/fixed_block_allocator.h + ${CMAKE_CURRENT_SOURCE_DIR}/event_thread.h ) if(NOT WIN32) diff --git a/libtransport/includes/hicn/transport/utils/event_thread.h b/libtransport/includes/hicn/transport/utils/event_thread.h new file mode 100644 index 000000000..db1194821 --- /dev/null +++ b/libtransport/includes/hicn/transport/utils/event_thread.h @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2017-2019 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 +#include + +#include +#include +#include + +namespace utils { + +class EventThread { + private: + // No copies + EventThread(const EventThread&) = delete; // non construction-copyable + EventThread& operator=(const EventThread&) = delete; // non copyable + + public: + explicit EventThread(asio::io_service& io_service) + : internal_io_service_(nullptr), + io_service_(io_service), + work_(io_service_), + thread_(nullptr) { + run(); + } + + explicit EventThread() + : internal_io_service_(std::make_unique()), + io_service_(*internal_io_service_), + work_(io_service_), + thread_(nullptr) { + run(); + } + + ~EventThread() { stop(); } + + void run() { + if (stopped()) { + io_service_.reset(); + } + + thread_ = std::make_unique([this]() { io_service_.run(); }); + } + + std::thread::id getThreadId() const { + if (thread_) { + return thread_->get_id(); + } else { + throw errors::RuntimeException("Event thread is not running."); + } + } + + template + void add(Func&& f) { + // If the function f + // TODO USe post in mac os, asio->post in xenial + io_service_.post(std::forward(f)); + } + + template + void tryRunHandlerNow(Func&& f) { + io_service_.dispatch(std::forward(f)); + } + + void stop() { + io_service_.stop(); + + if (thread_ && thread_->joinable()) { + thread_->join(); + } + + thread_.reset(); + } + + bool stopped() { return io_service_.stopped(); } + + asio::io_service& getIoService() { return io_service_; } + + private: + std::unique_ptr internal_io_service_; + asio::io_service& io_service_; + asio::io_service::work work_; + std::unique_ptr thread_; +}; + +} // namespace utils \ No newline at end of file diff --git a/libtransport/includes/hicn/transport/utils/ring_buffer.h b/libtransport/includes/hicn/transport/utils/ring_buffer.h index 9babe56bd..52629b82b 100644 --- a/libtransport/includes/hicn/transport/utils/ring_buffer.h +++ b/libtransport/includes/hicn/transport/utils/ring_buffer.h @@ -17,6 +17,7 @@ #include #include +#include namespace utils { -- cgit 1.2.3-korg