From 443f9e81c2f721898c1e7edf2940172fef6f55fe Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Thu, 27 Feb 2020 16:47:49 +0100 Subject: [HICN-539] Expose portal APIs to applications Change-Id: Icbaad69981193119714f5689faf3518d2e152e11 Signed-off-by: Mauro Sardara --- .../hicn/transport/interfaces/CMakeLists.txt | 1 + .../includes/hicn/transport/interfaces/portal.h | 207 +++++++++++++++++++++ libtransport/src/CMakeLists.txt | 1 - libtransport/src/core/pending_interest.h | 7 +- libtransport/src/core/portal.h | 54 +----- libtransport/src/implementation/socket_producer.h | 2 + libtransport/src/interfaces/CMakeLists.txt | 1 + libtransport/src/interfaces/portal.cc | 100 ++++++++++ libtransport/src/protocols/protocol.h | 13 +- utils/src/ping_client.cc | 11 +- 10 files changed, 333 insertions(+), 64 deletions(-) create mode 100644 libtransport/includes/hicn/transport/interfaces/portal.h create mode 100644 libtransport/src/interfaces/portal.cc diff --git a/libtransport/includes/hicn/transport/interfaces/CMakeLists.txt b/libtransport/includes/hicn/transport/interfaces/CMakeLists.txt index 38e6143f0..7370ad1b0 100644 --- a/libtransport/includes/hicn/transport/interfaces/CMakeLists.txt +++ b/libtransport/includes/hicn/transport/interfaces/CMakeLists.txt @@ -23,6 +23,7 @@ list(APPEND HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/callbacks.h ${CMAKE_CURRENT_SOURCE_DIR}/verification_policy.h ${CMAKE_CURRENT_SOURCE_DIR}/statistics.h + ${CMAKE_CURRENT_SOURCE_DIR}/portal.h ) if (${OPENSSL_VERSION} VERSION_EQUAL "1.1.1a" OR ${OPENSSL_VERSION} VERSION_GREATER "1.1.1a") diff --git a/libtransport/includes/hicn/transport/interfaces/portal.h b/libtransport/includes/hicn/transport/interfaces/portal.h new file mode 100644 index 000000000..724cd7592 --- /dev/null +++ b/libtransport/includes/hicn/transport/interfaces/portal.h @@ -0,0 +1,207 @@ +/* + * 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 + +#ifndef ASIO_STANDALONE +#define ASIO_STANDALONE +#endif +#include + +#include + +#define UNSET_CALLBACK 0 + +namespace transport { + +namespace interface { + +template +class BasicBindConfig { + static_assert(std::is_same::value, + "Prefix must be a Prefix type."); + + const uint32_t standard_cs_reserved = 5000; + + public: + template + BasicBindConfig(T &&prefix) + : prefix_(std::forward(prefix)), + content_store_reserved_(standard_cs_reserved) {} + + template + BasicBindConfig(T &&prefix, uint32_t cs_reserved) + : prefix_(std::forward(prefix)), + content_store_reserved_(cs_reserved) {} + + TRANSPORT_ALWAYS_INLINE const PrefixType &prefix() const { return prefix_; } + + TRANSPORT_ALWAYS_INLINE uint32_t csReserved() const { + return content_store_reserved_; + } + + private: + PrefixType prefix_; + uint32_t content_store_reserved_; +}; + +using BindConfig = BasicBindConfig; + +class Portal { + public: + /** + * Consumer callback is an abstract class containing two methods to be + * implemented by a consumer application. + */ + class ConsumerCallback { + public: + virtual void onContentObject(core::Interest::Ptr &&i, + core::ContentObject::Ptr &&c) = 0; + virtual void onTimeout(core::Interest::Ptr &&i) = 0; + virtual void onError(std::error_code ec) = 0; + }; + + /** + * Producer callback is an abstract class containing two methods to be + * implemented by a producer application. + */ + class ProducerCallback { + public: + virtual void onInterest(core::Interest::Ptr &&i) = 0; + virtual void onError(std::error_code ec) = 0; + }; + + using OnContentObjectCallback = + std::function; + using OnInterestTimeoutCallback = std::function; + + Portal(); + + Portal(asio::io_service &io_service); + + /** + * Set the consumer callback. + * + * @param consumer_callback - The pointer to the ConsumerCallback object. + */ + void setConsumerCallback(ConsumerCallback *consumer_callback); + + /** + * Set the producer callback. + * + * @param producer_callback - The pointer to the ProducerCallback object. + */ + void setProducerCallback(ProducerCallback *producer_callback); + + /** + * Connect the transport to the local hicn forwarder. + * + * @param is_consumer - Boolean specifying if the application on top of portal + * is a consumer or a producer. + */ + void connect(bool is_consumer = true); + + /** + * Destructor. + */ + ~Portal(); + + /** + * Check if there is already a pending interest for a given name. + * + * @param name - The interest name. + */ + bool interestIsPending(const core::Name &name); + + /** + * Send an interest through to the local forwarder. + * + * @param interest - The pointer to the interest. The ownership of the + * interest is transferred by the caller to portal. + * + * @param on_content_object_callback - If the caller wishes to use a different + * callback to be called for this interest, it can set this parameter. + * Otherwise ConsumerCallback::onContentObject will be used. + * + * @param on_interest_timeout_callback - If the caller wishes to use a + * different callback to be called for this interest, it can set this + * parameter. Otherwise ConsumerCallback::onTimeout will be used. + */ + void sendInterest( + core::Interest::Ptr &&interest, + OnContentObjectCallback &&on_content_object_callback = UNSET_CALLBACK, + OnInterestTimeoutCallback &&on_interest_timeout_callback = + UNSET_CALLBACK); + + /** + * Register a producer name to the local forwarder and optionally set the + * content store size in a per-face manner. + * + * @param config - The configuration for the local forwarder binding. + */ + void bind(const BindConfig &config); + + void runEventsLoop(); + + /** + * Run one event and return. + */ + void runOneEvent(); + + /** + * Send a data packet to the local forwarder. As opposite to sendInterest, the + * ownership of the content object is not transferred to the portal. + * + * @param content_object - The data packet. + */ + void sendContentObject(core::ContentObject &content_object); + /** + * Stop the event loop, canceling all the pending events in the event queue. + * + * Beware that stopping the event loop DOES NOT disconnect the transport from + * the local forwarder, the connector underneath will stay connected. + */ + void stopEventsLoop(); + + /** + * Disconnect the transport from the local forwarder. + */ + void killConnection(); + + /** + * Clear the pending interest hash table. + */ + void clear(); + + /** + * Get a reference to the io_service object. + */ + asio::io_service &getIoService(); + + /** + * Register a route to the local forwarder. + */ + void registerRoute(core::Prefix &prefix); + + private: + void *implementation_; +}; + +} // namespace interface +} // namespace transport \ No newline at end of file diff --git a/libtransport/src/CMakeLists.txt b/libtransport/src/CMakeLists.txt index a182998e3..c92393775 100644 --- a/libtransport/src/CMakeLists.txt +++ b/libtransport/src/CMakeLists.txt @@ -42,7 +42,6 @@ list(INSERT LIBTRANSPORT_INTERNAL_INCLUDE_DIRS 0 ) set(LIBTRANSPORT_INCLUDE_DIRS - ${CMAKE_CURRENT_BINARY_DIR} ${LIBTRANSPORT_INCLUDE_DIRS} "" CACHE INTERNAL "" FORCE diff --git a/libtransport/src/core/pending_interest.h b/libtransport/src/core/pending_interest.h index 87fed5073..d9ec2ed40 100644 --- a/libtransport/src/core/pending_interest.h +++ b/libtransport/src/core/pending_interest.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -36,10 +37,8 @@ class RawSocketInterface; template class Portal; -typedef std::function - OnContentObjectCallback; -typedef std::function OnInterestTimeoutCallback; -typedef std::function TimerCallback; +using OnContentObjectCallback = interface::Portal::OnContentObjectCallback; +using OnInterestTimeoutCallback = interface::Portal::OnInterestTimeoutCallback; class PendingInterest { friend class Portal; diff --git a/libtransport/src/core/portal.h b/libtransport/src/core/portal.h index d7c463dfd..cf1010068 100644 --- a/libtransport/src/core/portal.h +++ b/libtransport/src/core/portal.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -39,8 +40,6 @@ #include #include -#define UNSET_CALLBACK 0 - namespace transport { namespace core { @@ -230,36 +229,7 @@ class Pool { using PendingInterestHashTable = std::unordered_map; -template -class BasicBindConfig { - static_assert(std::is_same::value, - "Prefix must be a Prefix type."); - - const uint32_t standard_cs_reserved = 5000; - - public: - template - BasicBindConfig(T &&prefix) - : prefix_(std::forward(prefix)), - content_store_reserved_(standard_cs_reserved) {} - - template - BasicBindConfig(T &&prefix, uint32_t cs_reserved) - : prefix_(std::forward(prefix)), - content_store_reserved_(cs_reserved) {} - - TRANSPORT_ALWAYS_INLINE const PrefixType &prefix() const { return prefix_; } - - TRANSPORT_ALWAYS_INLINE uint32_t csReserved() const { - return content_store_reserved_; - } - - private: - PrefixType prefix_; - uint32_t content_store_reserved_; -}; - -using BindConfig = BasicBindConfig; +using interface::BindConfig; /** * Portal is a opaque class which is used for sending/receiving interest/data @@ -290,24 +260,8 @@ class Portal { "ForwarderInt must inherit from ForwarderInterface!"); public: - /** - * Consumer callback is an abstract class containing two methods to be - * implemented by a consumer application. - */ - class ConsumerCallback { - public: - virtual void onContentObject(Interest::Ptr &&i, ContentObject::Ptr &&c) = 0; - virtual void onTimeout(Interest::Ptr &&i) = 0; - }; - - /** - * Producer callback is an abstract class containing two methods to be - * implemented by a producer application. - */ - class ProducerCallback { - public: - virtual void onInterest(Interest::Ptr &&i) = 0; - }; + using ConsumerCallback = interface::Portal::ConsumerCallback; + using ProducerCallback = interface::Portal::ProducerCallback; Portal() : Portal(internal_io_service_) {} diff --git a/libtransport/src/implementation/socket_producer.h b/libtransport/src/implementation/socket_producer.h index 1f03fe53d..a5dd578b1 100644 --- a/libtransport/src/implementation/socket_producer.h +++ b/libtransport/src/implementation/socket_producer.h @@ -426,6 +426,8 @@ class ProducerSocket : public Socket, onInterest(*interest); }; + virtual void onError(std::error_code ec) override {} + virtual int setSocketOption(int socket_option_key, uint32_t socket_option_value) { switch (socket_option_key) { diff --git a/libtransport/src/interfaces/CMakeLists.txt b/libtransport/src/interfaces/CMakeLists.txt index daf4b31e5..e1d144596 100644 --- a/libtransport/src/interfaces/CMakeLists.txt +++ b/libtransport/src/interfaces/CMakeLists.txt @@ -17,6 +17,7 @@ list(APPEND SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/rtc_socket_producer.cc ${CMAKE_CURRENT_SOURCE_DIR}/socket_producer.cc ${CMAKE_CURRENT_SOURCE_DIR}/socket_consumer.cc + ${CMAKE_CURRENT_SOURCE_DIR}/portal.cc ${CMAKE_CURRENT_SOURCE_DIR}/callbacks.cc ) diff --git a/libtransport/src/interfaces/portal.cc b/libtransport/src/interfaces/portal.cc new file mode 100644 index 000000000..36cbd0c3b --- /dev/null +++ b/libtransport/src/interfaces/portal.cc @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2020 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. + */ + +#include + +#include + +namespace transport { +namespace interface { + +using implementation::BasePortal; + +Portal::Portal() { implementation_ = new implementation::BasePortal(); } + +Portal::Portal(asio::io_service &io_service) { + implementation_ = new BasePortal(io_service); +} + +Portal::~Portal() { delete reinterpret_cast(implementation_); } + +void Portal::setConsumerCallback(ConsumerCallback *consumer_callback) { + reinterpret_cast(implementation_) + ->setConsumerCallback(consumer_callback); +} + +void Portal::setProducerCallback(ProducerCallback *producer_callback) { + reinterpret_cast(implementation_) + ->setProducerCallback(producer_callback); +} + +void Portal::connect(bool is_consumer) { + reinterpret_cast(implementation_)->connect(is_consumer); +} + +bool Portal::interestIsPending(const core::Name &name) { + return reinterpret_cast(implementation_) + ->interestIsPending(name); +} + +void Portal::sendInterest( + core::Interest::Ptr &&interest, + OnContentObjectCallback &&on_content_object_callback, + OnInterestTimeoutCallback &&on_interest_timeout_callback) { + reinterpret_cast(implementation_) + ->sendInterest(std::move(interest), std::move(on_content_object_callback), + std::move(on_interest_timeout_callback)); +} + +void Portal::bind(const BindConfig &config) { + reinterpret_cast(implementation_)->bind(config); +} + +void Portal::runEventsLoop() { + reinterpret_cast(implementation_)->runEventsLoop(); +} + +void Portal::runOneEvent() { + reinterpret_cast(implementation_)->runOneEvent(); +} + +void Portal::sendContentObject(core::ContentObject &content_object) { + reinterpret_cast(implementation_) + ->sendContentObject(content_object); +} + +void Portal::stopEventsLoop() { + reinterpret_cast(implementation_)->stopEventsLoop(); +} + +void Portal::killConnection() { + reinterpret_cast(implementation_)->killConnection(); +} + +void Portal::clear() { + reinterpret_cast(implementation_)->clear(); +} + +asio::io_service &Portal::getIoService() { + return reinterpret_cast(implementation_)->getIoService(); +} + +void Portal::registerRoute(core::Prefix &prefix) { + reinterpret_cast(implementation_)->registerRoute(prefix); +} + +} // namespace interface + +} // namespace transport \ No newline at end of file diff --git a/libtransport/src/protocols/protocol.h b/libtransport/src/protocols/protocol.h index c094adaae..949380959 100644 --- a/libtransport/src/protocols/protocol.h +++ b/libtransport/src/protocols/protocol.h @@ -67,15 +67,18 @@ class TransportProtocol : public implementation::BasePortal::ConsumerCallback, // Events generated by the indexing virtual void onContentReassembled(std::error_code ec); - virtual void onPacketDropped(Interest::Ptr &&interest, - ContentObject::Ptr &&content_object) = 0; - virtual void onReassemblyFailed(std::uint32_t missing_segment) = 0; + virtual void onPacketDropped( + Interest::Ptr &&interest, + ContentObject::Ptr &&content_object) override = 0; + virtual void onReassemblyFailed(std::uint32_t missing_segment) override = 0; protected: // Consumer Callback virtual void reset() = 0; - virtual void onContentObject(Interest::Ptr &&i, ContentObject::Ptr &&c) = 0; - virtual void onTimeout(Interest::Ptr &&i) = 0; + virtual void onContentObject(Interest::Ptr &&i, + ContentObject::Ptr &&c) override = 0; + virtual void onTimeout(Interest::Ptr &&i) override = 0; + virtual void onError(std::error_code ec) override {} protected: implementation::ConsumerSocket *socket_; diff --git a/utils/src/ping_client.cc b/utils/src/ping_client.cc index 6dea17418..67440cdc1 100644 --- a/utils/src/ping_client.cc +++ b/utils/src/ping_client.cc @@ -14,9 +14,9 @@ */ #include +#include #include - -#include +#include // Let's make the linker happy #if !TRANSPORT_LOG_EXTERN_GLOBAL_OUTPUT_LEVEL @@ -25,6 +25,7 @@ TRANSPORT_LOG_DEFINE_GLOBAL_OUTPUT_LEVEL = 0; #endif #endif +#include #include #include #include @@ -83,7 +84,7 @@ class Configuration { } }; -class Client : implementation::BasePortal::ConsumerCallback { +class Client : interface::Portal::ConsumerCallback { public: Client(Configuration *c) : portal_(), signals_(portal_.getIoService(), SIGINT) { @@ -213,6 +214,8 @@ class Client : implementation::BasePortal::ConsumerCallback { } } + void onError(std::error_code ec) override {} + void doPing() { Name interest_name(config_->name_, (uint32_t)sequence_number_); hicn_format_t format; @@ -298,7 +301,7 @@ class Client : implementation::BasePortal::ConsumerCallback { private: SendTimeMap send_timestamps_; - implementation::BasePortal portal_; + interface::Portal portal_; asio::signal_set signals_; uint64_t sequence_number_; uint64_t last_jump_; -- cgit 1.2.3-korg