From 4bd049e639d80028ccdc909c0fef2de4e810cf50 Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Fri, 28 Jul 2017 19:37:14 +0200 Subject: Adding possibility to set lifetime when publishing content using the http helper. Change-Id: I0107bb216e961c189a62f5c6bb3b22fc563919a7 Signed-off-by: Mauro Sardara --- apps/http/icnet_http_echo_server.cc | 10 ++++-- icnet/http/icnet_http_server_publisher.cc | 4 ++- icnet/http/icnet_http_server_publisher.h | 1 + icnet/transport/icnet_transport_content_store.cc | 41 ++++++++++++++-------- icnet/transport/icnet_transport_content_store.h | 7 ++-- icnet/transport/icnet_transport_socket_producer.cc | 4 +-- 6 files changed, 44 insertions(+), 23 deletions(-) diff --git a/apps/http/icnet_http_echo_server.cc b/apps/http/icnet_http_echo_server.cc index 17bdb693..223646a2 100644 --- a/apps/http/icnet_http_echo_server.cc +++ b/apps/http/icnet_http_echo_server.cc @@ -26,11 +26,17 @@ void onPayload(std::shared_ptr &publisher, const uint8_t *b std::cout << string << std::endl; std::stringstream response; - response << "HTTP/1.0 200 OK\r\n" << "Content-Length: " << size << "\r\n\r\n" << string; + response << "HTTP/1.1 200 OK\r\n" << "Content-Length: " << size << "\r\n\r\n" << string; std::string response_string = response.str(); + std::chrono::milliseconds expiry_time(std::chrono::milliseconds(4000 * 1000)); + std::thread t([publisher, response_string]() { - publisher->publishContent((uint8_t *) response_string.data(), response_string.size(), 0, true); + publisher->publishContent((uint8_t *) response_string.data(), + response_string.size(), + std::chrono::milliseconds(transport::default_values::content_object_expiry_time), + 0, + true); publisher->serveClients(); }); diff --git a/icnet/http/icnet_http_server_publisher.cc b/icnet/http/icnet_http_server_publisher.cc index 92b41e6c..e42154b2 100644 --- a/icnet/http/icnet_http_server_publisher.cc +++ b/icnet/http/icnet_http_server_publisher.cc @@ -63,13 +63,15 @@ HTTPServerPublisher &HTTPServerPublisher::setTimeout(uint32_t timeout) { return *this; } -void HTTPServerPublisher::publishContent(const uint8_t *buf, size_t buffer_size, const int response_id, bool is_last) { +void HTTPServerPublisher::publishContent(const uint8_t *buf, size_t buffer_size, std::chrono::milliseconds content_lifetime, const int response_id, bool is_last) { if (producer_) { #ifdef __ANDROID__ __android_log_print(ANDROID_LOG_DEBUG, "HTTP_SERVER_PUBLISHER", "Replying to %s", const_cast(content_name_).toString().c_str()); #else std::cout << "Replying to " << content_name_ << std::endl; #endif + producer_->setSocketOption(transport::GeneralTransportOptions::CONTENT_OBJECT_EXPIRY_TIME, static_cast(content_lifetime.count())); + producer_->produce(content_name_, buf, buffer_size, response_id, is_last); } } diff --git a/icnet/http/icnet_http_server_publisher.h b/icnet/http/icnet_http_server_publisher.h index a1c1c91a..7162f4d1 100644 --- a/icnet/http/icnet_http_server_publisher.h +++ b/icnet/http/icnet_http_server_publisher.h @@ -43,6 +43,7 @@ class HTTPServerPublisher { void publishContent(const uint8_t *buf, size_t buffer_size, + std::chrono::milliseconds content_lifetime, const int response_id, bool is_last); diff --git a/icnet/transport/icnet_transport_content_store.cc b/icnet/transport/icnet_transport_content_store.cc index a9f05658..99700f13 100644 --- a/icnet/transport/icnet_transport_content_store.cc +++ b/icnet/transport/icnet_transport_content_store.cc @@ -28,17 +28,21 @@ ContentStore::~ContentStore() { } void ContentStore::insert(const std::shared_ptr &content_object) { - std::unique_lock lock(cs_mutex_); - if (content_store_hash_table_.size() >= max_content_store_size_) { - // Evict item - content_store_hash_table_.erase(lru_list_.back()); - lru_list_.pop_back(); - } + // Check if the content can be cached + if (content_object->getExpiryTime() > 0) { + std::unique_lock lock(cs_mutex_); + if (content_store_hash_table_.size() >= max_content_store_size_) { + // Evict item + content_store_hash_table_.erase(lru_list_.back()); + lru_list_.pop_back(); + } - // Insert new item - lru_list_.push_back(std::cref(content_object->getName())); - LRUList::iterator pos = lru_list_.end(); - content_store_hash_table_[content_object->getName()] = CcnxContentStoreEntry(content_object, pos); + // Insert new item + lru_list_.push_back(std::cref(content_object->getName())); + LRUList::iterator pos = lru_list_.end(); + content_store_hash_table_[content_object->getName()] = ContentStoreEntry(ObjectTimeEntry(content_object, + std::chrono::steady_clock::now()), pos); + } } @@ -47,13 +51,20 @@ const std::shared_ptr &ContentStore::find(const Interest &interes ContentStoreHashTable::iterator it = content_store_hash_table_.find(interest.getName()); if (it != content_store_hash_table_.end()) { if (it->second.second != lru_list_.begin()) { - // Move element to the top of the LRU list - lru_list_.splice(lru_list_.begin(), lru_list_, it->second.second); + if (std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()).count() + < it->second.first.first->getExpiryTime()) { + // Move element to the top of the LRU list + lru_list_.splice(lru_list_.begin(), lru_list_, it->second.second); + return it->second.first.first; + } else { + // Stale content + content_store_hash_table_.erase(interest.getName()); + } } - return it->second.first; - } else { - return empty_reference_; } + + return empty_reference_; } void ContentStore::erase(const Name &exact_name) { diff --git a/icnet/transport/icnet_transport_content_store.h b/icnet/transport/icnet_transport_content_store.h index 8a2ed4c6..77043d70 100644 --- a/icnet/transport/icnet_transport_content_store.h +++ b/icnet/transport/icnet_transport_content_store.h @@ -24,10 +24,11 @@ namespace icnet { namespace transport { -typedef std::pair, std::list>::iterator> - CcnxContentStoreEntry; +typedef std::pair, std::chrono::steady_clock::time_point> ObjectTimeEntry; +typedef std::pair>::iterator> + ContentStoreEntry; typedef std::list> LRUList; -typedef std::unordered_map ContentStoreHashTable; +typedef std::unordered_map ContentStoreHashTable; class ContentStore { public: diff --git a/icnet/transport/icnet_transport_socket_producer.cc b/icnet/transport/icnet_transport_socket_producer.cc index 190580f8..ec5f665f 100644 --- a/icnet/transport/icnet_transport_socket_producer.cc +++ b/icnet/transport/icnet_transport_socket_producer.cc @@ -226,7 +226,7 @@ void ProducerSocket::produce(Name name, const uint8_t *buf, size_t buffer_size, Name full_name = name; content_object_segment = std::make_shared(std::move(full_name.appendSegment(current_segment))); - // content_object_segment->setExpiryTime((uint64_t) m_dataFreshness); + content_object_segment->setExpiryTime((uint64_t) content_object_expiry_time_); if (packaged_segments == number_of_segments - 1) { content_object_segment->setContent(&buf[bytes_segmented], buffer_size - bytes_segmented); @@ -277,7 +277,7 @@ void ProducerSocket::produce(Name name, const uint8_t *buf, size_t buffer_size, content_object = std::make_shared(std::move(fullName.appendSegment(current_segment))); // TODO If we set the throughput will decrease.. to investigate - // content_object->setExpiryTime((uint64_t)m_dataFreshness); + content_object->setExpiryTime((uint64_t)content_object_expiry_time_); if (is_last) { content_object->setFinalChunkNumber(current_segment + number_of_segments - packaged_segments - 1); -- cgit 1.2.3-korg