diff options
Diffstat (limited to 'http-server')
-rw-r--r-- | http-server/common.h | 16 | ||||
-rw-r--r-- | http-server/configuration.h | 5 | ||||
-rw-r--r-- | http-server/content.h | 5 | ||||
-rw-r--r-- | http-server/http_server.cc | 56 | ||||
-rw-r--r-- | http-server/http_server.h | 12 | ||||
-rw-r--r-- | http-server/icn_request.h | 9 | ||||
-rw-r--r-- | http-server/icn_response.cc | 10 | ||||
-rw-r--r-- | http-server/icn_response.h | 11 | ||||
-rw-r--r-- | http-server/request.h | 7 | ||||
-rw-r--r-- | http-server/response.cc | 1 | ||||
-rw-r--r-- | http-server/response.h | 13 | ||||
-rw-r--r-- | http-server/socket_request.h | 5 | ||||
-rw-r--r-- | http-server/socket_response.h | 5 |
13 files changed, 62 insertions, 93 deletions
diff --git a/http-server/common.h b/http-server/common.h index 580f8a41..3d88fc42 100644 --- a/http-server/common.h +++ b/http-server/common.h @@ -13,20 +13,12 @@ * limitations under the License. */ -#ifndef ICN_WEB_SERVER_COMMON_H_ -#define ICN_WEB_SERVER_COMMON_H_ +#pragma once #include "config.h" -#if defined(HICNET) - #include <hicnet/hicnet_http_facade.h> - #include <hicnet/hicnet_utils_hash.h> -#elif defined(ICNET) - #include <icnet/icnet_http_facade.h> - #include <icnet/icnet_utils_hash.h> -#else - #error "No ICN tranport library to which link against." -#endif +#include <icnet/icnet_http_facade.h> +#include <icnet/icnet_utils_hash.h> #include <boost/asio.hpp> #include <boost/regex.hpp> @@ -45,5 +37,3 @@ typedef boost::asio::ip::tcp::socket socket_type; typedef std::function<void(const boost::system::error_code &)> SendCallback; - -#endif // ICN_WEB_SERVER_COMMON_H_ diff --git a/http-server/configuration.h b/http-server/configuration.h index 65d3170a..8939fb75 100644 --- a/http-server/configuration.h +++ b/http-server/configuration.h @@ -13,8 +13,7 @@ * limitations under the License. */ -#ifndef ICN_WEB_SERVER_CONFIGURATION_H_ -#define ICN_WEB_SERVER_CONFIGURATION_H_ +#pragma once #include "common.h" @@ -48,5 +47,3 @@ class Configuration { }; } // end namespace icn_httpserver - -#endif // ICN_WEB_SERVER_CONFIGURATION_H_ diff --git a/http-server/content.h b/http-server/content.h index a81ad643..4e2fe74f 100644 --- a/http-server/content.h +++ b/http-server/content.h @@ -15,8 +15,7 @@ #include "common.h" -#ifndef ICN_WEB_SERVER_CONTENT_H_ -#define ICN_WEB_SERVER_CONTENT_H_ +#pragma once namespace icn_httpserver { @@ -34,5 +33,3 @@ class Content }; } // end namespace icn_httpserver - -#endif // ICN_WEB_SERVER_CONTENT_H_ diff --git a/http-server/http_server.cc b/http-server/http_server.cc index 478d072e..a0ec905f 100644 --- a/http-server/http_server.cc +++ b/http-server/http_server.cc @@ -23,11 +23,13 @@ */ #include "http_server.h" - namespace icn_httpserver { HttpServer::HttpServer(unsigned short port, - std::string icn_name, size_t num_threads, long timeout_request, long timeout_send_or_receive) + std::string icn_name, + size_t num_threads, + long timeout_request, + long timeout_send_or_receive) : config_(port, num_threads), icn_name_(icn_name), internal_io_service_(std::make_shared<boost::asio::io_service>()), @@ -53,7 +55,8 @@ HttpServer::HttpServer(unsigned short port, void HttpServer::onIcnRequest(std::shared_ptr<libl4::http::HTTPServerPublisher> &publisher, const uint8_t *buffer, - std::size_t size) { + std::size_t size, + int request_id) { std::shared_ptr<Request> request = std::make_shared<IcnRequest>(publisher); request->getContent().rdbuf()->sputn((char*)buffer, size); @@ -61,29 +64,21 @@ void HttpServer::onIcnRequest(std::shared_ptr<libl4::http::HTTPServerPublisher> return; } - int request_id = libl4::utils::Hash::hash32(buffer, size); - - std::cout << "Request ID" << request_id << std::endl; + std::map<int, std::shared_ptr<libl4::http::HTTPServerPublisher>>& icn_publishers = icn_acceptor_->getPublishers(); std::unique_lock<std::mutex> lock(thread_list_mtx_); - if (icn_publishers_.size() < config_.getNum_threads()) { - if (icn_publishers_.find(request_id) == icn_publishers_.end()) { + if (icn_publishers.size() < config_.getNum_threads()) { std::cout << "Received request for: " << request->getPath() << std::endl; - icn_publishers_[request_id] = publisher; - icn_publishers_[request_id]->attachPublisher(); - if (request->getPath().substr(request->getPath().find_last_of(".") + 1) == "mpd") { - icn_publishers_[request_id]->setTimeout(1); - } else { - icn_publishers_[request_id]->setTimeout(5); - } + + publisher->attachPublisher(); std::cout << "Starting new thread" << std::endl; io_service_.dispatch([this, request, request_id]() { + std::map<int, std::shared_ptr<libl4::http::HTTPServerPublisher>>& icn_publishers = icn_acceptor_->getPublishers(); find_resource(nullptr, request); - icn_publishers_[request_id]->serveClients(); + icn_publishers[request_id]->serveClients(); std::unique_lock<std::mutex> lock(thread_list_mtx_); - icn_publishers_.erase(request_id); + icn_publishers.erase(request_id); }); - } } } @@ -92,7 +87,8 @@ void HttpServer::setIcnAcceptor() { this, std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3)); + std::placeholders::_3, + std::placeholders::_4)); icn_acceptor_->listen(true); } @@ -117,6 +113,7 @@ void HttpServer::spawnThreads() { accept(); + //If num_threads>1, start m_io_service.run() in (num_threads-1) threads for thread-pooling socket_threads_.clear(); for (size_t c = 1; c < config_.getNum_threads(); c++) { @@ -124,6 +121,7 @@ void HttpServer::spawnThreads() { io_service_.run(); }); } + } void HttpServer::start() { @@ -148,13 +146,13 @@ void HttpServer::start() { } spawnThreads(); + setIcnAcceptor(); // Wait for the rest of the threads, if any, to finish as well for (auto &t: socket_threads_) { t.join(); } - // for (auto &t : icn_threads) { // t.second.get(); // } @@ -162,17 +160,14 @@ void HttpServer::start() { void HttpServer::stop() { acceptor_.close(); - icn_acceptor_.reset(); + io_service_.stop(); - for (auto &p: icn_publishers_) { - p.second->stop(); - } + std::map<int, std::shared_ptr<libl4::http::HTTPServerPublisher>>& icn_publishers = icn_acceptor_->getPublishers(); - for (auto p : icn_publishers_) { - p.second.reset(); + for (auto &p : icn_publishers) { + p.second->stop(); } - } void HttpServer::accept() { @@ -251,7 +246,7 @@ void HttpServer::read_request_and_content(std::shared_ptr<socket_type> socket) { } unsigned long long content_length; try { - content_length = stoull(it->second); + content_length = atol(it->second.c_str()); } catch (const std::exception &) { return; } @@ -385,7 +380,7 @@ void HttpServer::write_response(std::shared_ptr<socket_type> socket, float http_version; try { - http_version = stof(request->getHttp_version()); + http_version = atof(request->getHttp_version().c_str()); } catch (const std::exception &) { return; } @@ -403,12 +398,15 @@ void HttpServer::write_response(std::shared_ptr<socket_type> socket, }); }); + try { resource_function(response, request); } catch (const std::exception &) { return; } + } } // end namespace icn_httpserver + diff --git a/http-server/http_server.h b/http-server/http_server.h index 704863d5..630f26b6 100644 --- a/http-server/http_server.h +++ b/http-server/http_server.h @@ -22,8 +22,7 @@ * SOFTWARE. */ -#ifndef ICN_WEB_SERVER_WEB_SERVER_H_ -#define ICN_WEB_SERVER_WEB_SERVER_H_ +#pragma once #include "common.h" #include "icn_request.h" @@ -49,6 +48,7 @@ namespace icn_httpserver { class HttpServer { public: + explicit HttpServer(unsigned short port, std::string icn_name, size_t num_threads, @@ -71,10 +71,11 @@ class HttpServer { void send(std::shared_ptr<Response> response, SendCallback callback = nullptr) const; std::unordered_map<std::string, std::unordered_map<std::string, ResourceCallback> > resource; + std::unordered_map<std::string, ResourceCallback> default_resource; + void onIcnRequest(std::shared_ptr<libl4::http::HTTPServerPublisher>& publisher, const uint8_t* buffer, std::size_t size, int request_id); private: - void onIcnRequest(std::shared_ptr<libl4::http::HTTPServerPublisher>& publisher, const uint8_t* buffer, std::size_t size); void spawnThreads(); @@ -100,11 +101,8 @@ class HttpServer { boost::asio::io_service &io_service_; boost::asio::ip::tcp::acceptor acceptor_; std::vector<std::thread> socket_threads_; - - // ICN parameters std::string icn_name_; std::shared_ptr<libl4::http::HTTPServerAcceptor> icn_acceptor_; - std::unordered_map<int, std::shared_ptr<libl4::http::HTTPServerPublisher>> icn_publishers_; std::mutex thread_list_mtx_; long timeout_request_; @@ -113,5 +111,3 @@ class HttpServer { }; } // end namespace icn_httpserver - -#endif //ICN_WEB_SERVER_WEB_SERVER_H_ diff --git a/http-server/icn_request.h b/http-server/icn_request.h index ec9ee198..d18a6e64 100644 --- a/http-server/icn_request.h +++ b/http-server/icn_request.h @@ -13,8 +13,7 @@ * limitations under the License. */ -#ifndef ICN_WEB_SERVER_ICNREQUEST_H_ -#define ICN_WEB_SERVER_ICNREQUEST_H_ +#pragma once #include "common.h" #include "request.h" @@ -32,6 +31,8 @@ class IcnRequest std::string method, std::string http_version); + ~IcnRequest() = default; + const std::string &getName() const; void setName(const std::string &name); @@ -51,6 +52,4 @@ class IcnRequest }; -} // end namespace icn_httpserver - -#endif // ICN_WEB_SERVER_ICNREQUEST_H_ +} // end namespace icn_httpserver
\ No newline at end of file diff --git a/http-server/icn_response.cc b/http-server/icn_response.cc index 9741556b..630ee001 100644 --- a/http-server/icn_response.cc +++ b/http-server/icn_response.cc @@ -25,14 +25,13 @@ IcnResponse::IcnResponse(std::shared_ptr<libl4::http::HTTPServerPublisher> publi } void IcnResponse::send(const SendCallback &callback) { + std::size_t buffer_size = this->streambuf_.size(); this->streambuf_.commit(this->streambuf_.size()); - std::cout << "Rrsponse Id " << response_id_ << std::endl; - this->publisher_->publishContent(boost::asio::buffer_cast<const uint8_t *>(this->streambuf_.data()), buffer_size, - this->response_lifetime_, + std::chrono::milliseconds(100000), this->response_id_, this->is_last_); @@ -43,4 +42,9 @@ void IcnResponse::send(const SendCallback &callback) { } } +void IcnResponse::setResponseLifetime(const std::chrono::milliseconds &response_lifetime) { + this->publisher_->setTimeout(response_lifetime, true); + Response::setResponseLifetime(response_lifetime); +} + } // end namespace icn_httpserver diff --git a/http-server/icn_response.h b/http-server/icn_response.h index 13a75629..7019590b 100644 --- a/http-server/icn_response.h +++ b/http-server/icn_response.h @@ -13,10 +13,9 @@ * limitations under the License. */ -#include "response.h" +#pragma once -#ifndef ICN_WEB_SERVER_ICNRESPONSE_H_ -#define ICN_WEB_SERVER_ICNRESPONSE_H_ +#include "response.h" namespace icn_httpserver { @@ -30,7 +29,9 @@ class IcnResponse std::string ndn_path, int response_id); - void send(const SendCallback &callback = nullptr); + void send(const SendCallback &callback = nullptr) override; + + void setResponseLifetime(const std::chrono::milliseconds &response_lifetime) override; private: std::string ndn_name_; @@ -40,5 +41,3 @@ class IcnResponse }; } // end namespace icn_httpserver - -#endif // ICN_WEB_SERVER_ICNRESPONSE_H_ diff --git a/http-server/request.h b/http-server/request.h index 6ffff296..3f61a46b 100644 --- a/http-server/request.h +++ b/http-server/request.h @@ -13,8 +13,7 @@ * limitations under the License. */ -#ifndef ICN_WEB_SERVER_REQUEST_H_ -#define ICN_WEB_SERVER_REQUEST_H_ +#pragma once #include "common.h" #include "content.h" @@ -46,6 +45,8 @@ class Request { virtual void read_remote_endpoint_data(socket_type &socket) { }; + virtual ~Request() = default; + const std::string &getMethod() const; void setMethod(const std::string &method); @@ -79,5 +80,3 @@ class Request { }; } // end namespace icn_httpserver - -#endif // ICN_WEB_SERVER_REQUEST_H_ diff --git a/http-server/response.cc b/http-server/response.cc index 21a93dd8..c4560ba9 100644 --- a/http-server/response.cc +++ b/http-server/response.cc @@ -45,6 +45,7 @@ void Response::setIsLast(bool is_last) { const std::chrono::milliseconds &Response::getResponseLifetime() const { return response_lifetime_; } + void Response::setResponseLifetime(const std::chrono::milliseconds &response_lifetime) { Response::response_lifetime_ = response_lifetime; } diff --git a/http-server/response.h b/http-server/response.h index cc8df9c6..51837556 100644 --- a/http-server/response.h +++ b/http-server/response.h @@ -13,8 +13,7 @@ * limitations under the License. */ -#ifndef ICN_WEB_SERVER_RESPONSE_H_ -#define ICN_WEB_SERVER_RESPONSE_H_ +#pragma once #include "common.h" @@ -25,8 +24,7 @@ class Response public: Response(); - virtual - ~Response(); + virtual ~Response(); size_t size(); @@ -41,8 +39,7 @@ class Response const std::chrono::milliseconds &getResponseLifetime() const; - void setResponseLifetime(const std::chrono::milliseconds &response_lifetime); - + virtual void setResponseLifetime(const std::chrono::milliseconds &response_lifetime); protected: boost::asio::streambuf streambuf_; @@ -51,6 +48,4 @@ class Response std::chrono::milliseconds response_lifetime_; }; -} // end namespace icn_httpserver - -#endif // ICN_WEB_SERVER_RESPONSE_H_ +} // end namespace icn_httpserver
\ No newline at end of file diff --git a/http-server/socket_request.h b/http-server/socket_request.h index cc77ef73..657226a3 100644 --- a/http-server/socket_request.h +++ b/http-server/socket_request.h @@ -22,8 +22,7 @@ * SOFTWARE. */ -#ifndef ICN_WEB_SERVER_SOCKETREQUEST_H_ -#define ICN_WEB_SERVER_SOCKETREQUEST_H_ +#pragma once #include "request.h" @@ -40,5 +39,3 @@ class SocketRequest }; } // end namespace icn_httpserver - -#endif // ICN_WEB_SERVER_SOCKETREQUEST_H_ diff --git a/http-server/socket_response.h b/http-server/socket_response.h index 722e8196..e1a72142 100644 --- a/http-server/socket_response.h +++ b/http-server/socket_response.h @@ -22,8 +22,7 @@ * SOFTWARE. */ -#ifndef ICN_WEB_SERVER_SOCKETRESPONSE_H_ -#define ICN_WEB_SERVER_SOCKETRESPONSE_H_ +#pragma once #include "response.h" @@ -48,5 +47,3 @@ class SocketResponse }; } // end namespace icn_httpserver - -#endif // ICN_WEB_SERVER_SOCKETRESPONSE_H_ |