diff options
author | Mauro Sardara <msardara@cisco.com> | 2020-05-14 20:21:02 +0200 |
---|---|---|
committer | Mauro Sardara <msardara@cisco.com> | 2020-05-20 10:45:53 +0200 |
commit | 81fb39606b069fbece973995572fa7f90ea1950a (patch) | |
tree | 10c1534707c725eb654741e5b4d280a17ef0c0dc /libtransport/includes | |
parent | 67b86555b33c641de14d3c1d0864e571370a71e6 (diff) |
[HICN-613] Add io_service to ConsumerSocket constructor.
Change-Id: Ic1952388e1d2b1e7457c71ae8a959d97aa0cd2d6
Signed-off-by: Mauro Sardara <msardara@cisco.com>
Diffstat (limited to 'libtransport/includes')
5 files changed, 128 insertions, 1 deletions
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 @@ -154,6 +154,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. */ ~ConsumerSocket(); 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 <hicn/transport/config.h> +#include <hicn/transport/errors/runtime_exception.h> + +#include <asio.hpp> +#include <memory> +#include <thread> + +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<asio::io_service>()), + io_service_(*internal_io_service_), + work_(io_service_), + thread_(nullptr) { + run(); + } + + ~EventThread() { stop(); } + + void run() { + if (stopped()) { + io_service_.reset(); + } + + thread_ = std::make_unique<std::thread>([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 <typename Func> + void add(Func&& f) { + // If the function f + // TODO USe post in mac os, asio->post in xenial + io_service_.post(std::forward<Func&&>(f)); + } + + template <typename Func> + void tryRunHandlerNow(Func&& f) { + io_service_.dispatch(std::forward<Func&&>(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<asio::io_service> internal_io_service_; + asio::io_service& io_service_; + asio::io_service::work work_; + std::unique_ptr<std::thread> 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 <atomic> #include <cstddef> +#include <utility> namespace utils { |