diff options
Diffstat (limited to 'libtransport/src/hicn/transport/core/portal.h')
-rw-r--r-- | libtransport/src/hicn/transport/core/portal.h | 60 |
1 files changed, 39 insertions, 21 deletions
diff --git a/libtransport/src/hicn/transport/core/portal.h b/libtransport/src/hicn/transport/core/portal.h index a79717037..58406bbde 100644 --- a/libtransport/src/hicn/transport/core/portal.h +++ b/libtransport/src/hicn/transport/core/portal.h @@ -97,13 +97,10 @@ class Portal { virtual void onInterest(Interest::Ptr &&i) = 0; }; - Portal() : Portal(internal_io_service_) { - internal_work_ = std::make_unique<asio::io_service::work>(io_service_); - } + Portal() : Portal(internal_io_service_) {} Portal(asio::io_service &io_service) : io_service_(io_service), - is_running_(false), app_name_("libtransport_application"), consumer_callback_(nullptr), producer_callback_(nullptr), @@ -131,8 +128,7 @@ class Portal { } ~Portal() { - connector_.close(); - stopEventsLoop(); + stopEventsLoop(true); } TRANSPORT_ALWAYS_INLINE bool interestIsPending(const Name &name) { @@ -157,9 +153,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<PendingInterest>( + std::move(interest), std::move(on_content_object_callback), + std::move(on_interest_timeout_callback), + std::make_unique<asio::steady_timer>(io_service_)); + + pending_interest_hash_table_[name]->startCountdown( + std::bind(&Portal<ForwarderInt>::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 +187,9 @@ class Portal { std::unique_ptr<PendingInterest> 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 +208,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 +216,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( @@ -209,16 +224,17 @@ class Portal { forwarder_interface_.send(content_object); } - TRANSPORT_ALWAYS_INLINE void stopEventsLoop() { - is_running_ = false; - internal_work_.reset(); - + TRANSPORT_ALWAYS_INLINE void stopEventsLoop(bool kill_connection = false) { for (auto &pend_interest : pending_interest_hash_table_) { pend_interest.second->cancelTimer(); } clear(); + if(kill_connection) { + connector_.close(); + } + io_service_.post([this]() { io_service_.stop(); }); } @@ -242,7 +258,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 +310,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)); @@ -318,9 +339,6 @@ class Portal { private: asio::io_service &io_service_; asio::io_service internal_io_service_; - std::unique_ptr<asio::io_service::work> internal_work_; - - volatile bool is_running_; std::string app_name_; |