00001
00002
00003
00004
00005
00006
00007
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);
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
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
00053 process::initialize();
00054
00055
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
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 }