From 03371e2e47523dcbadb9a4a79969ecd225b3ff3d Mon Sep 17 00:00:00 2001 From: michele papalini Date: Fri, 25 Jan 2019 14:58:31 +0100 Subject: [HICN-13] sendInterest with customized callbacks Change-Id: Ie4b2aac7f5f356f8afc7aaf83b723596dcbb4532 Signed-off-by: michele papalini --- .../src/hicn/transport/core/pending_interest.cc | 40 +++++++++++++++++--- .../src/hicn/transport/core/pending_interest.h | 21 ++++++++--- libtransport/src/hicn/transport/core/portal.h | 44 ++++++++++++++++------ 3 files changed, 82 insertions(+), 23 deletions(-) (limited to 'libtransport') diff --git a/libtransport/src/hicn/transport/core/pending_interest.cc b/libtransport/src/hicn/transport/core/pending_interest.cc index 8f6de1839..a2df9ba44 100644 --- a/libtransport/src/hicn/transport/core/pending_interest.cc +++ b/libtransport/src/hicn/transport/core/pending_interest.cc @@ -20,12 +20,28 @@ namespace transport { namespace core { PendingInterest::PendingInterest() - : interest_(nullptr, nullptr), timer_(), received_(false) {} + : interest_(nullptr, nullptr), + timer_(), + on_content_object_callback_(), + on_interest_timeout_callback_(), + received_(false) {} PendingInterest::PendingInterest(Interest::Ptr &&interest, std::unique_ptr &&timer) : interest_(std::move(interest)), timer_(std::move(timer)), + on_content_object_callback_(), + on_interest_timeout_callback_(), + received_(false) {} + +PendingInterest::PendingInterest(Interest::Ptr &&interest, + const OnContentObjectCallback &&on_content_object, + const OnInterestTimeoutCallback &&on_interest_timeout, + std::unique_ptr &&timer) + : interest_(std::move(interest)), + timer_(std::move(timer)), + on_content_object_callback_(std::move(on_content_object)), + on_interest_timeout_callback_(std::move(on_interest_timeout)), received_(false) {} PendingInterest::~PendingInterest() { @@ -34,16 +50,28 @@ PendingInterest::~PendingInterest() { void PendingInterest::cancelTimer() { timer_->cancel(); } -bool PendingInterest::isReceived() const { return received_; } - void PendingInterest::setReceived() { received_ = true; } +bool PendingInterest::isReceived() const { return received_; } + Interest::Ptr &&PendingInterest::getInterest() { return std::move(interest_); } -void PendingInterest::setReceived(bool received) { - PendingInterest::received_ = received; +const OnContentObjectCallback &PendingInterest::getOnDataCallback() const { + return on_content_object_callback_; +} + +void PendingInterest::setOnDataCallback(const OnContentObjectCallback &on_content_object) { + PendingInterest::on_content_object_callback_ = on_content_object; +} + +const OnInterestTimeoutCallback &PendingInterest::getOnTimeoutCallback() const { + return on_interest_timeout_callback_; +} + +void PendingInterest::setOnTimeoutCallback(const OnInterestTimeoutCallback &on_interest_timeout) { + PendingInterest::on_interest_timeout_callback_ = on_interest_timeout; } } // end namespace core -} // end namespace transport \ No newline at end of file +} // end namespace transport diff --git a/libtransport/src/hicn/transport/core/pending_interest.h b/libtransport/src/hicn/transport/core/pending_interest.h index cbcafb5d9..58b51db79 100644 --- a/libtransport/src/hicn/transport/core/pending_interest.h +++ b/libtransport/src/hicn/transport/core/pending_interest.h @@ -34,6 +34,8 @@ class RawSocketInterface; template class Portal; +typedef std::function OnContentObjectCallback; +typedef std::function OnInterestTimeoutCallback; typedef std::function TimerCallback; class PendingInterest { @@ -47,9 +49,12 @@ class PendingInterest { PendingInterest(Interest::Ptr &&interest, std::unique_ptr &&timer); - ~PendingInterest(); + PendingInterest(Interest::Ptr &&interest, + const OnContentObjectCallback &&on_content_object, + const OnInterestTimeoutCallback &&on_interest_timeout, + std::unique_ptr &&timer); - bool isReceived() const; + ~PendingInterest(); template TRANSPORT_ALWAYS_INLINE void startCountdown(Handler &&cb) { @@ -62,17 +67,23 @@ class PendingInterest { void setReceived(); + bool isReceived() const; + Interest::Ptr &&getInterest(); - void setReceived(bool received); + const OnContentObjectCallback &getOnDataCallback() const; + + void setOnDataCallback(const OnContentObjectCallback &on_content_object); - bool isValid() const; + const OnInterestTimeoutCallback &getOnTimeoutCallback() const; - void setValid(bool valid); + void setOnTimeoutCallback(const OnInterestTimeoutCallback &on_interest_timeout); private: Interest::Ptr interest_; std::unique_ptr timer_; + OnContentObjectCallback on_content_object_callback_; + OnInterestTimeoutCallback on_interest_timeout_callback_; bool received_; }; diff --git a/libtransport/src/hicn/transport/core/portal.h b/libtransport/src/hicn/transport/core/portal.h index a79717037..52a721a35 100644 --- a/libtransport/src/hicn/transport/core/portal.h +++ b/libtransport/src/hicn/transport/core/portal.h @@ -103,7 +103,6 @@ class Portal { Portal(asio::io_service &io_service) : io_service_(io_service), - is_running_(false), app_name_("libtransport_application"), consumer_callback_(nullptr), producer_callback_(nullptr), @@ -157,9 +156,30 @@ class Portal { std::placeholders::_1, name)); } + TRANSPORT_ALWAYS_INLINE void sendInterest(Interest::Ptr &&interest, + const OnContentObjectCallback &&on_content_object_callback, + const OnInterestTimeoutCallback &&on_interest_timeout_callback) { + + const Name name(interest->getName(), true); + + // Send it + forwarder_interface_.send(*interest); + + pending_interest_hash_table_[name] = std::make_unique( + std::move(interest), std::move(on_content_object_callback), + std::move(on_interest_timeout_callback), + std::make_unique(io_service_)); + + pending_interest_hash_table_[name]->startCountdown( + std::bind(&Portal::timerHandler, + this, std::placeholders::_1, name)); + + } + TRANSPORT_ALWAYS_INLINE void timerHandler(const std::error_code &ec, const Name &name) { - if (TRANSPORT_EXPECT_FALSE(!is_running_)) { + bool is_stopped = io_service_.stopped(); + if (TRANSPORT_EXPECT_FALSE(is_stopped)) { return; } @@ -170,7 +190,9 @@ class Portal { std::unique_ptr ptr = std::move(it->second); pending_interest_hash_table_.erase(it); - if (consumer_callback_) { + if(ptr->getOnTimeoutCallback() != UNSET_CALLBACK){ + ptr->on_interest_timeout_callback_(std::move(ptr->getInterest())); + }else if (consumer_callback_) { consumer_callback_->onTimeout(std::move(ptr->getInterest())); } } @@ -189,9 +211,7 @@ class Portal { io_service_.reset(); // ensure that run()/poll() will do some work } - is_running_ = true; this->io_service_.run(); - is_running_ = false; } TRANSPORT_ALWAYS_INLINE void runOneEvent() { @@ -199,9 +219,7 @@ class Portal { io_service_.reset(); // ensure that run()/poll() will do some work } - is_running_ = true; this->io_service_.run_one(); - is_running_ = false; } TRANSPORT_ALWAYS_INLINE void sendContentObject( @@ -210,7 +228,6 @@ class Portal { } TRANSPORT_ALWAYS_INLINE void stopEventsLoop() { - is_running_ = false; internal_work_.reset(); for (auto &pend_interest : pending_interest_hash_table_) { @@ -242,7 +259,8 @@ class Portal { private: TRANSPORT_ALWAYS_INLINE void processIncomingMessages( Packet::MemBufPtr &&packet_buffer) { - if (TRANSPORT_EXPECT_FALSE(!is_running_)) { + bool is_stopped = io_service_.stopped(); + if (TRANSPORT_EXPECT_FALSE(is_stopped)) { return; } @@ -293,7 +311,11 @@ class Portal { interest_ptr->setReceived(); pending_interest_hash_table_.erase(content_object->getName()); - if (consumer_callback_) { + if(interest_ptr->getOnDataCallback() != UNSET_CALLBACK){ + interest_ptr->on_content_object_callback_( + std::move(interest_ptr->getInterest()), + std::move(content_object)); + }else if (consumer_callback_) { consumer_callback_->onContentObject( std::move(interest_ptr->getInterest()), std::move(content_object)); @@ -320,8 +342,6 @@ class Portal { asio::io_service internal_io_service_; std::unique_ptr internal_work_; - volatile bool is_running_; - std::string app_name_; PendingInterestHashTable pending_interest_hash_table_; -- cgit 1.2.3-korg