From 4da1b7955fb3190c0e0646cfde99436aa140d271 Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Mon, 5 Jun 2017 18:43:52 +0200 Subject: - Added proxy function - Changed interface between library and application - Support for different build type Change-Id: I34ae75057490eb887d353e53c6d013f88bead04f Signed-off-by: Mauro Sardara --- http-server/common.h | 15 ++++- http-server/http_server.cc | 144 ++++++++++---------------------------------- http-server/http_server.h | 16 ++--- http-server/icn_request.cc | 16 ++--- http-server/icn_request.h | 10 +-- http-server/icn_response.cc | 15 ++--- http-server/icn_response.h | 4 +- 7 files changed, 72 insertions(+), 148 deletions(-) (limited to 'http-server') diff --git a/http-server/common.h b/http-server/common.h index e69706e6..580f8a41 100644 --- a/http-server/common.h +++ b/http-server/common.h @@ -16,6 +16,18 @@ #ifndef ICN_WEB_SERVER_COMMON_H_ #define ICN_WEB_SERVER_COMMON_H_ +#include "config.h" + +#if defined(HICNET) + #include + #include +#elif defined(ICNET) + #include + #include +#else + #error "No ICN tranport library to which link against." +#endif + #include #include #include @@ -31,9 +43,6 @@ #include #include -// ICN extensions -#include - typedef boost::asio::ip::tcp::socket socket_type; typedef std::function SendCallback; diff --git a/http-server/http_server.cc b/http-server/http_server.cc index ebc8503b..6bdf18f2 100644 --- a/http-server/http_server.cc +++ b/http-server/http_server.cc @@ -33,7 +33,6 @@ HttpServer::HttpServer(unsigned short port, internal_io_service_(std::make_shared()), io_service_(*internal_io_service_), acceptor_(io_service_), - acceptor_producer_(std::make_shared(icnet::Name(icn_name))), timeout_request_(timeout_request), timeout_content_(timeout_send_or_receive) { } @@ -48,124 +47,51 @@ HttpServer::HttpServer(unsigned short port, icn_name_(icn_name), io_service_(ioService), acceptor_(io_service_), - acceptor_producer_(std::make_shared(icnet::Name(icn_name))), timeout_request_(timeout_request), timeout_content_(timeout_send_or_receive) { } -void HttpServer::processIncomingInterest(icnet::ProducerSocket &p, const icnet::Interest &interest) { - icnet::Name complete_name = interest.getName(); +void HttpServer::onIcnRequest(std::shared_ptr &publisher, + const uint8_t *buffer, + std::size_t size) { + publisher->setTimeout(5); + std::shared_ptr request = std::make_shared(publisher); + request->getContent().rdbuf()->sputn((char*)buffer, size); - if (complete_name.getSegmentCount() <= 2) { - std::cerr << "Received malformed name " << complete_name << ". Ignoring it." << std::endl; + if (!parse_request(request, request->getContent())) { return; } - icnet::Name request_name = complete_name.get(-1).isSegment() ? complete_name.getPrefix(-1) : complete_name; + int request_id = libl4::utils::Hash::hash32(buffer, size); + + std::cout << "Request ID" << request_id << std::endl; std::unique_lock lock(thread_list_mtx_); - if (icn_producers_.size() < config_.getNum_threads()) { - if (icn_producers_.find(request_name) == icn_producers_.end()) { - std::cout << "Received interest name: " << request_name << std::endl; - std::shared_ptr p = makeProducer(request_name); - icn_producers_[request_name] = p; + if (icn_publishers_.size() < config_.getNum_threads()) { + if (icn_publishers_.find(request_id) == icn_publishers_.end()) { + std::cout << "Received request for: " << request->getPath() << std::endl; + icn_publishers_[request_id] = publisher; std::cout << "Starting new thread" << std::endl; - std::thread t([this, interest, request_name, p]() { - processInterest(request_name, p); + io_service_.dispatch([this, request, request_id]() { + find_resource(nullptr, request); + icn_publishers_[request_id]->serveClients(); + std::unique_lock lock(thread_list_mtx_); + icn_publishers_.erase(request_id); }); - t.detach(); - } else { - icn_producers_[request_name]->onInterest(complete_name, interest); } } } -void HttpServer::signPacket(icnet::ProducerSocket &p, icnet::ContentObject &content_object) { - // This is not really signing the packet. Signing every packet is cpu expensive. - icnet::KeyLocator keyLocator; - content_object.signWithSha256(keyLocator); -} - -void HttpServer::processInterest(icnet::Name request_name, std::shared_ptr p) { - // Create timer - std::shared_ptr portal; - p->getSocketOption(icnet::GeneralTransportOptions::PORTAL, portal); - boost::asio::io_service &ioService = portal->getIoService(); - - boost::asio::deadline_timer t(ioService, boost::posix_time::seconds(5)); - - std::function - wait_callback = [&ioService](const boost::system::error_code e) { - if (!e) { - // Be sure to delete the timer before the io_service, otherwise we'll get some strange behavior! - ioService.stop(); - } - }; - - t.async_wait(wait_callback); - - // Get the name of the HTTP method to compute - std::string method = request_name.get(1).toString(); - std::transform(method.begin(), method.end(), method.begin(), ::toupper); - std::string path; - - // This is done for getting rid of useless name components such as ccnx: or ndn: - if (request_name.getSegmentCount() > 2) { - std::string rawPath = request_name.getSubName(2).toString(); - std::size_t pos = rawPath.find("/"); - path = rawPath.substr(pos); - } - - std::function - interest_enter_callback = [this, &wait_callback, &t](icnet::ProducerSocket &p, const icnet::Interest &interest) { - t.cancel(); - t.expires_from_now(boost::posix_time::seconds(5)); - t.async_wait(wait_callback); - }; - - p->setSocketOption(icnet::ProducerCallbacksOptions::INTEREST_INPUT, - (icnet::ProducerInterestCallback) interest_enter_callback); - - // TODO The parsing of the parameters in theURL is missing! - if (method == GET) { - // Build new GET request to submit to the server - - std::shared_ptr request = std::make_shared(p, request_name.toString(), path, method, "1.0"); - - std::static_pointer_cast(request)->getHeader() - .insert(std::make_pair(std::string("Host"), std::string("localhost"))); - - p->attach(); - - find_resource(nullptr, request); - } - - p->serveForever(); - - std::unique_lock lock(thread_list_mtx_); - icn_producers_.erase(request_name); -} - -std::shared_ptr HttpServer::makeProducer(icnet::Name request_name) { - std::shared_ptr producer = std::make_shared(request_name); - // producer->setContextOption(FAST_SIGNING, true); - // producer->setContextOption(DATA_TO_SECURE, (api::ProducerDataCallback) bind(&http-server::signPacket, this, _1, _2)); - producer->setSocketOption(icnet::GeneralTransportOptions::DATA_PACKET_SIZE, PACKET_SIZE); - producer->setSocketOption(icnet::GeneralTransportOptions::OUTPUT_BUFFER_SIZE, SEND_BUFFER_SIZE); - - return producer; -} - void HttpServer::setIcnAcceptor() { - acceptor_producer_->setSocketOption(icnet::ProducerCallbacksOptions::INTEREST_INPUT, - (icnet::ProducerInterestCallback) bind(&HttpServer::processIncomingInterest, - this, - std::placeholders::_1, - std::placeholders::_2)); - acceptor_producer_->dispatch(); + icn_acceptor_ = std::make_shared(icn_name_, std::bind(&HttpServer::onIcnRequest, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3)); + icn_acceptor_->listen(true); } -void HttpServer::spawnTcpThreads() { +void HttpServer::spawnThreads() { if (io_service_.stopped()) { io_service_.reset(); } @@ -216,11 +142,9 @@ void HttpServer::start() { } } - spawnTcpThreads(); + spawnThreads(); setIcnAcceptor(); - - // Wait for the rest of the threads, if any, to finish as well for (auto &t: socket_threads_) { t.join(); @@ -233,16 +157,14 @@ void HttpServer::start() { void HttpServer::stop() { acceptor_.close(); - acceptor_producer_.reset(); + icn_acceptor_.reset(); io_service_.stop(); - for (auto p: icn_producers_) { - std::shared_ptr portalPtr; - p.second->getSocketOption(icnet::GeneralTransportOptions::PORTAL, portalPtr); - portalPtr->getIoService().stop(); + for (auto &p: icn_publishers_) { + p.second->stop(); } - for (auto p : icn_producers_) { + for (auto p : icn_publishers_) { p.second.reset(); } @@ -289,7 +211,7 @@ void HttpServer::read_request_and_content(std::shared_ptr socket) { std::shared_ptr request = std::make_shared(); request->read_remote_endpoint_data(*socket); - //Set timeout on the following boost::asio::async-read or write function + // Set timeout on the following boost::asio::async-read or write function std::shared_ptr timer; if (timeout_request_ > 0) { timer = set_timeout_on_socket(socket, timeout_request_); @@ -439,7 +361,7 @@ void HttpServer::write_response(std::shared_ptr socket, if (socket) { resp = new SocketResponse(socket); } else { - resp = new IcnResponse(std::static_pointer_cast(request)->getProducer(), + resp = new IcnResponse(std::static_pointer_cast(request)->getHttpPublisher(), std::static_pointer_cast(request)->getName(), std::static_pointer_cast(request)->getPath(), std::static_pointer_cast(request)->getRequest_id()); diff --git a/http-server/http_server.h b/http-server/http_server.h index fbc841c7..704863d5 100644 --- a/http-server/http_server.h +++ b/http-server/http_server.h @@ -74,13 +74,9 @@ class HttpServer { std::unordered_map default_resource; private: - void processInterest(icnet::Name request_name, std::shared_ptr p); + void onIcnRequest(std::shared_ptr& publisher, const uint8_t* buffer, std::size_t size); - void processIncomingInterest(icnet::ProducerSocket &p, const icnet::Interest &interest); - - void signPacket(icnet::ProducerSocket &p, icnet::ContentObject &content_object); - - void spawnTcpThreads(); + void spawnThreads(); void setIcnAcceptor(); @@ -96,8 +92,6 @@ class HttpServer { std::shared_ptr request, ResourceCallback &resource_function); - std::shared_ptr makeProducer(icnet::Name request_name); - Configuration config_; std::vector > > > opt_resource_; @@ -109,10 +103,8 @@ class HttpServer { // ICN parameters std::string icn_name_; - std::shared_ptr acceptor_producer_; - std::unordered_map> icn_threads_; - std::unordered_map> icn_producers_; - std::unordered_map> name_io_service_map_; + std::shared_ptr icn_acceptor_; + std::unordered_map> icn_publishers_; std::mutex thread_list_mtx_; long timeout_request_; diff --git a/http-server/icn_request.cc b/http-server/icn_request.cc index 07d95a22..1709e917 100644 --- a/http-server/icn_request.cc +++ b/http-server/icn_request.cc @@ -17,19 +17,19 @@ namespace icn_httpserver { -IcnRequest::IcnRequest(std::shared_ptr producer) - : producer_(producer) { +IcnRequest::IcnRequest(std::shared_ptr& publisher) + : publisher_(publisher) { time_t t; time(&t); srand((unsigned int) t); request_id_ = rand(); } -IcnRequest::IcnRequest(std::shared_ptr producer, +IcnRequest::IcnRequest(std::shared_ptr& publisher, std::string name, std::string path, std::string method, std::string http_version) - : IcnRequest(producer) { + : IcnRequest(publisher) { this->name_ = name; this->path_ = path; this->method_ = method; @@ -52,12 +52,12 @@ void IcnRequest::setRequest_id(int request_id) { IcnRequest::request_id_ = request_id; } -const std::shared_ptr &IcnRequest::getProducer() const { - return producer_; +const std::shared_ptr &IcnRequest::getHttpPublisher() const { + return publisher_; } -void IcnRequest::setProducer(const std::shared_ptr &producer) { - IcnRequest::producer_ = producer; +void IcnRequest::setProducer(const std::shared_ptr &producer) { + IcnRequest::publisher_ = producer; } } // end namespace icn_httpserver diff --git a/http-server/icn_request.h b/http-server/icn_request.h index c5aa10e4..ec9ee198 100644 --- a/http-server/icn_request.h +++ b/http-server/icn_request.h @@ -24,9 +24,9 @@ namespace icn_httpserver { class IcnRequest : public Request { public: - IcnRequest(std::shared_ptr producer); + IcnRequest(std::shared_ptr& publisher); - IcnRequest(std::shared_ptr producer, + IcnRequest(std::shared_ptr& publisher, std::string name, std::string path, std::string method, @@ -40,14 +40,14 @@ class IcnRequest void setRequest_id(int request_id); - const std::shared_ptr &getProducer() const; + const std::shared_ptr &getHttpPublisher() const; - void setProducer(const std::shared_ptr &producer); + void setProducer(const std::shared_ptr &producer); private: std::string name_; int request_id_; - std::shared_ptr producer_; + std::shared_ptr publisher_; }; diff --git a/http-server/icn_response.cc b/http-server/icn_response.cc index 6fe8b1e1..241eda51 100644 --- a/http-server/icn_response.cc +++ b/http-server/icn_response.cc @@ -17,22 +17,23 @@ namespace icn_httpserver { -IcnResponse::IcnResponse(std::shared_ptr producer, +IcnResponse::IcnResponse(std::shared_ptr publisher, std::string ndn_name, std::string ndn_path, int response_id) - : producer_(producer), ndn_name_(ndn_name), ndn_path_(ndn_path), response_id_(response_id) { + : publisher_(publisher), ndn_name_(ndn_name), ndn_path_(ndn_path), response_id_(response_id) { } void IcnResponse::send(const SendCallback &callback) { std::size_t buffer_size = this->streambuf_.size(); this->streambuf_.commit(this->streambuf_.size()); - this->producer_->produce(icnet::Name(/*this->ndn_name*/), - boost::asio::buffer_cast(this->streambuf_.data()), - buffer_size, - this->response_id_, - this->is_last_); + std::cout << "Rrsponse Id " << response_id_ << std::endl; + + this->publisher_->publishContent(boost::asio::buffer_cast(this->streambuf_.data()), + buffer_size, + this->response_id_, + this->is_last_); this->streambuf_.consume(buffer_size); diff --git a/http-server/icn_response.h b/http-server/icn_response.h index e9af0d40..13a75629 100644 --- a/http-server/icn_response.h +++ b/http-server/icn_response.h @@ -25,7 +25,7 @@ class IcnResponse public: - IcnResponse(std::shared_ptr producer, + IcnResponse(std::shared_ptr producer, std::string ndn_name, std::string ndn_path, int response_id); @@ -36,7 +36,7 @@ class IcnResponse std::string ndn_name_; std::string ndn_path_; int response_id_; - std::shared_ptr producer_; + std::shared_ptr publisher_; }; } // end namespace icn_httpserver -- cgit 1.2.3-korg