pion
5.0.6
|
00001 // --------------------------------------------------------------------- 00002 // pion: a Boost C++ framework for building lightweight HTTP interfaces 00003 // --------------------------------------------------------------------- 00004 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion) 00005 // 00006 // Distributed under the Boost Software License, Version 1.0. 00007 // See http://www.boost.org/LICENSE_1_0.txt 00008 // 00009 00010 #include <iostream> 00011 #include <boost/asio.hpp> 00012 #include <boost/bind.hpp> 00013 #include <pion/error.hpp> 00014 #include <pion/process.hpp> 00015 #include <pion/tcp/server.hpp> 00016 00017 using namespace std; 00018 using namespace pion; 00019 00020 00022 class HelloServer : public tcp::server { 00023 public: 00024 HelloServer(const unsigned int tcp_port) : tcp::server(tcp_port) {} 00025 virtual ~HelloServer() {} 00026 virtual void handle_connection(tcp::connection_ptr& tcp_conn) 00027 { 00028 static const std::string HELLO_MESSAGE("Hello there!\x0D\x0A"); 00029 tcp_conn->set_lifecycle(pion::tcp::connection::LIFECYCLE_CLOSE); // make sure it will get closed 00030 tcp_conn->async_write(boost::asio::buffer(HELLO_MESSAGE), 00031 boost::bind(&pion::tcp::connection::finish, tcp_conn)); 00032 } 00033 }; 00034 00035 00036 00038 int main (int argc, char *argv[]) 00039 { 00040 static const unsigned int DEFAULT_PORT = 8080; 00041 00042 // parse command line: determine port number 00043 unsigned int port = DEFAULT_PORT; 00044 if (argc == 2) { 00045 port = strtoul(argv[1], 0, 10); 00046 if (port == 0) port = DEFAULT_PORT; 00047 } else if (argc != 1) { 00048 std::cerr << "usage: helloserver [port]" << std::endl; 00049 return 1; 00050 } 00051 00052 // initialize signal handlers, etc. 00053 process::initialize(); 00054 00055 // initialize log system (use simple configuration) 00056 logger main_log(PION_GET_LOGGER("helloserver")); 00057 logger pion_log(PION_GET_LOGGER("pion")); 00058 PION_LOG_SETLEVEL_INFO(main_log); 00059 PION_LOG_SETLEVEL_INFO(pion_log); 00060 PION_LOG_CONFIG_BASIC; 00061 00062 try { 00063 00064 // create a new server to handle the Hello TCP protocol 00065 tcp::server_ptr hello_server(new HelloServer(port)); 00066 hello_server->start(); 00067 process::wait_for_shutdown(); 00068 00069 } catch (std::exception& e) { 00070 PION_LOG_FATAL(main_log, pion::diagnostic_information(e)); 00071 } 00072 00073 return 0; 00074 }