#include <Wt/WServer>
Public Types | |
enum | EntryPointType { Application, WidgetSet } |
Enumeration that indicates the type of an entrypoint. More... | |
Public Member Functions | |
WServer (const std::string &wtApplicationPath=std::string(), const std::string &wtConfigurationFile=std::string()) | |
Create a new server instance. | |
~WServer () | |
Destructor. | |
void | setServerConfiguration (int argc, char *argv[], const std::string &serverConfigurationFile) |
Configure the HTTP(S) server. | |
void | addEntryPoint (EntryPointType type, ApplicationCreator callback, const std::string &path=std::string()) |
Bind an entry-point to a callback function to create a new application. | |
bool | start () |
Starts the server in the background. | |
void | stop () |
Stops the server. | |
bool | isRunning () const |
Returns whether the server is running. | |
Related Functions | |
(Note that these are not member functions.) | |
int | WRun (int argc, char **argv, ApplicationCreator createApplication=0) |
Runs the Wt application server. | |
Classes | |
class | Exception |
Server Exception class. More... |
This server class represents an instance of an application server.
It can only be used when linking against wthttpd (for the built-in httpd), and it offers control over starting and stopping the server. This may be used as an alternative to using WRun(), for integrating a Wt server application in an existing application, when you want to and start and stop the server as appropriate.
As an example usage, consider the implementation of WRun(), which starts the server until a Ctrl-C is pressed or a termination signal has been received:
int WRun(int argc, char *argv[], ApplicationCreator createApplication) { try { // use argv[0] as the application name to match a suitable entry // in the Wt configuration file, and use the default configuration // file (which defaults to /etc/wt/wt_config.xml) WServer server(argv[0]); // WTHTTP_CONFIGURATION is e.g. "/etc/wt/wthttpd" server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION); // add a single entry point, at the default location (as determined // by the server configuration's deploy-path) server.addEntryPoint(WServer::Application, createApplication); if (server.start()) { // ... platform-dependent code to wait for a termination signal or Ctrl-C. std::cerr << "Shutdown (signal = " << sig << ")" << std::endl; server.stop(); } } catch (WServer::Exception& e) { std::cerr << e.what() << "\n"; return 1; } catch (std::exception& e) { std::cerr << "exception: " << e.what() << "\n"; return 1; } }
Wt::WServer::WServer | ( | const std::string & | wtApplicationPath = std::string() , |
|
const std::string & | wtConfigurationFile = std::string() | |||
) |
Create a new server instance.
The wtApplicationPath is used to match specific application-settings in the Wt configuration file. If no specific match could be found, the general settings are used (corresponding to the '*' selector).
The Wt application configuration is read from the wtConfigurationFile. If empty, this defaults to the value configured at build time.
For more information on configuring Wt applications, see Configuration.
Exception | : indicates a configuration problem. |
Wt::WServer::~WServer | ( | ) |
Destructor.
If the server was still running, it is stopped first by calling stop(). It is probably safer to call stop() first yourself, since this allows exceptions to be caught.
void Wt::WServer::setServerConfiguration | ( | int | argc, | |
char * | argv[], | |||
const std::string & | serverConfigurationFile | |||
) |
Configure the HTTP(S) server.
Configures the HTTP(S) server using command-line arguments, a configuration file, or both. The valid options are described in Built-in httpd configuration.
The applications themselves are configured using the configuration file passed to the constructor.
Exception | : indicates a configuration problem. |
void Wt::WServer::addEntryPoint | ( | EntryPointType | type, | |
ApplicationCreator | callback, | |||
const std::string & | path = std::string() | |||
) |
Bind an entry-point to a callback function to create a new application.
The path is the local URL at which the application is deployed: when a user visits this URL, the callback will be called to create a new application. If empty, the URL is inferred from the server configuration's deploy-path (see also Built-in httpd configuration).
bool Wt::WServer::start | ( | ) |
Starts the server in the background.
Returns whether the server could be successfully started.
Exception | : indicates a problem starting the server. |
void Wt::WServer::stop | ( | ) |
Stops the server.
All active application sessions are terminated cleanly, and the HTTP(S) server is shut down.
Exception | : indicates a problem while stopping the server. |
bool Wt::WServer::isRunning | ( | ) | const |
int WRun | ( | int | argc, | |
char ** | argv, | |||
ApplicationCreator | createApplication = 0 | |||
) | [related] |
Runs the Wt application server.
This function runs the application server, and should be called only once (e.g. from within your main function).
The createApplication argument is a function pointer to create new application instances for each new user surfing to your application.
When using the built-in httpd, the implementation listens for POSIX termination signals (or console CTRL-C) event. You can use the WServer class for more flexible control on starting and stopping the server.