1.7. Multithreaded HTTP server

To create a multithreaded HTTP server you need to create an according server object with the number of desired threads. Then you start all the threads and after maybe doing something useful you must wait for the termination of all the threads.


  TcpIpConnection conn (true, host, port);  1
  HttpProtocol prot(&conn);
  HttpServer http_server (&prot, num_threads);

  Dispatcher rpc_server;
  http_server.setRpcDispatcher(&rpc_server);   2
  rpc_server.addMethod(&testcall,
                        Struct::getValueName(),
                        "testcall",
                        Integer::getValueName() + "," + Integer::getValueName(),
                        "Testcase with a c-function");

  http_server.addHttpHandler("get",  3
                              make_methodhandler(rpcinfo,
                                                &RpcInfoHandler::handler));
  http_server.addHttpHandler("post",
                              make_methodhandler(rpcinfo,
                                                &RpcInfoHandler::handler));

  http_server.addRealm("/", "http-root-resource"); 4
  http_server.addAuthentication("ali-baba",
                                "open-sesame",
                                "http-root-resource");

  http_server.addRealm("/RPC2", "rpc2-resource"); 5
  http_server.addAuthentication("ali-baba-rpc",
                                "open-sesame-rpc",
                                "rpc2-resource");

  CppString root_dir = "/usr/local/ulxmlrpcpp/public_html";  6
  http_server.setHttpRoot(root_dir);

  unsigned started = http_server.runPicoHttpd();  7

  do_some_work(); 8

  http_server.waitAsync(false, true); 9

1

Set up a connection and a server object.

2

Set up the XML-RPC dispatcher which is handled by the HTTP server.

3

Add some handlers for requests to html resources.

4

Add a realm and authentication data for the HTTP resource.

5

Add a realm and authentication data for the XML-RPC resource.

6

Set the HTTP root.

7

Start the threads.

8

Maybe do something other useful while the server threads respond to request.

9

Wait for all the threads to terminate.