Skip to content
Browse files

web server proto: unfold namespaces. run forever.

  • Loading branch information...
1 parent d9a284f commit 619f005fabd2667e02a1c1478624d6b2a5c8a754 @aalex aalex committed Feb 14, 2012
Showing with 17 additions and 23 deletions.
  1. +1 −0 .gitignore
  2. +16 −23 prototypes/web-server/main.cpp
View
1 .gitignore
@@ -58,3 +58,4 @@ tests/check_it_works
tests/stats
xml/
!prototypes/web-server/Makefile
+prototypes/web-server/main
View
39 prototypes/web-server/main.cpp
@@ -6,35 +6,27 @@
#include <pion/net/HTTPRequest.hpp>
#include <pion/net/HTTPResponseWriter.hpp>
-using namespace std;
-using namespace pion;
-using namespace pion::net;
-
-void handleRootURI(HTTPRequestPtr& http_request, TCPConnectionPtr& tcp_conn)
+void handleRootURI(pion::net::HTTPRequestPtr& http_request, pion::net::TCPConnectionPtr& tcp_conn)
{
- static const std::string kHTMLStart("<html><body>\n");
- static const std::string kHTMLEnd("</body></html>\n");
+ static const std::string html_start("<html><body>\n");
+ static const std::string html_end("</body></html>\n");
- HTTPResponseWriterPtr writer(
- HTTPResponseWriter::create(
- tcp_conn,
- *http_request,
- boost::bind(
- &TCPConnection::finish,
- tcp_conn)));
- HTTPResponse& r = writer->getResponse();
- r.setStatusCode(HTTPTypes::RESPONSE_CODE_OK);
- r.setStatusMessage(HTTPTypes::RESPONSE_MESSAGE_OK);
- HTTPTypes::QueryParams& params = http_request->getQueryParams();
+ pion::net::HTTPResponseWriterPtr writer(
+ pion::net::HTTPResponseWriter::create(tcp_conn, *http_request,
+ boost::bind(&pion::net::TCPConnection::finish, tcp_conn)));
+ pion::net::HTTPResponse& r = writer->getResponse();
+ r.setStatusCode(pion::net::HTTPTypes::RESPONSE_CODE_OK);
+ r.setStatusMessage(pion::net::HTTPTypes::RESPONSE_MESSAGE_OK);
+ pion::net::HTTPTypes::QueryParams& params = http_request->getQueryParams();
- writer->writeNoCopy(kHTMLStart);
+ writer->writeNoCopy(html_start);
writer->write("The query about resource ");
writer->write(http_request->getResource());
if (params.size() > 0)
{
writer->write(" has the following parameters: <br>");
- for (HTTPTypes::QueryParams::const_iterator i = params.begin();
+ for (pion::net::HTTPTypes::QueryParams::const_iterator i = params.begin();
i != params.end(); ++i)
{
writer->write(i->first);
@@ -47,7 +39,7 @@ void handleRootURI(HTTPRequestPtr& http_request, TCPConnectionPtr& tcp_conn)
{
writer->write(" has no parameter.");
}
- writer->writeNoCopy(kHTMLEnd);
+ writer->writeNoCopy(html_end);
writer->send();
}
@@ -56,13 +48,14 @@ int main(int argc, char *argv[])
static const unsigned int DEFAULT_PORT = 8080;
try
{
- HTTPServerPtr hello_server(new HTTPServer(DEFAULT_PORT));
+ pion::net::HTTPServerPtr hello_server(new pion::net::HTTPServer(DEFAULT_PORT));
hello_server->addResource("/", &handleRootURI);
hello_server->start(); // Spawn a thead to run the HTTP
// service, and the main thread can
// focus on the main work now.
std::cout << "Hello, the server is running now ...\n";
- sleep(10);
+ while (true)
+ sleep(1);
}
catch (std::exception& e)
{

0 comments on commit 619f005

Please sign in to comment.
Something went wrong with that request. Please try again.