| Home | Libraries | People | FAQ | More

# http/server/posix_main.cpp

Go to the documentation of this file.

    00001 #include <iostream>
    00002 #include <string>
    00003 #include <boost/asio.hpp>
    00004 #include <boost/thread.hpp>
    00005 #include <boost/bind.hpp>
    00006 #include "server.hpp"
    00007
    00008 #if !defined(_WIN32)
    00009
    00010 #include <pthread.h>
    00011 #include <signal.h>
    00012
    00013 int main(int argc, char* argv[])
    00014 {
    00015   try
    00016   {
    00017     // Check command line arguments.
    00018     if (argc != 4)
    00019     {
    00020       std::cerr << "Usage: http_server <address> <port> <doc_root>\n";
    00021       std::cerr << "  For IPv4, try:\n";
    00022       std::cerr << "    receiver 0.0.0.0 80 .\n";
    00023       std::cerr << "  For IPv6, try:\n";
    00024       std::cerr << "    receiver 0::0 80 .\n";
    00025       return 1;
    00026     }
    00027
    00028     // Block all signals for background thread.
    00029     sigset_t new_mask;
    00030     sigfillset(&new_mask);
    00031     sigset_t old_mask;
    00032     pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
    00033
    00034     // Run server in background thread.
    00035     http::server::server s(argv[1], argv[2], argv[3]);
    00036     boost::thread t(boost::bind(&http::server::server::run, &s));
    00037
    00038     // Restore previous signals.
    00039     pthread_sigmask(SIG_SETMASK, &old_mask, 0);
    00040
    00041     // Wait for signal indicating time to shut down.
    00042     sigset_t wait_mask;
    00043     sigemptyset(&wait_mask);
    00044     sigaddset(&wait_mask, SIGINT);
    00045     sigaddset(&wait_mask, SIGQUIT);
    00046     sigaddset(&wait_mask, SIGTERM);
    00047     pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
    00048     int sig = 0;
    00049     sigwait(&wait_mask, &sig);
    00050
    00051     // Stop the server.
    00052     s.stop();
    00053     t.join();
    00054   }
    00055   catch (boost::asio::error& e)
    00056   {
    00057     std::cerr << "asio error: " << e << "\n";
    00058   }
    00059   catch (std::exception& e)
    00060   {
    00061     std::cerr << "exception: " << e.what() << "\n";
    00062   }
    00063
    00064   return 0;
    00065 }
    00066
    00067 #endif // !defined(_WIN32)

| Copyright © 2003 - 2006 Christopher M. Kohlhoff