Maturity Index:Experimental
By making argc and argv available to the Webserver class, the application can support the -d flag (debug) and the -p portnum (set a port number other than the default WWW port 80).main(int n,char **v) { id server = [Webserver argc:n argv:v]; [server run]; exit(0); }
The user may override -contents in a subclass, to print HTML onto the stdout. This will be wrapped by the Webserver class into a HTTP response, and returned to the client.
Note: contents is sent after forking a child process to handle the request. The message accepted may be overridden to make modifications to the parent process.
+ newCreates an instance of the Webserver class, listening at port number 80, with a blocking poll message, and that will run as a daemon. These defaults can be changed by setportnum:, setblocking: etc.
argc:argv:
+ argc :(int) n argv :(char**) v
Creates an instance of the Webserver class. Can be used to process some standard command line options such as -d (run the server in debug mode, without detaching from the controlling terminal) and -p (to set a port number different from 80).
setportnum:
- setportnum :(int) num
This method is normally used by +argc:argv: to set the portnumber as specified with a -p command line option. If the Webserver instance was created with +new, the program may use the setportnum: message to change the portnumber (defaults to 80).
setdaemon:
- setdaemon :(BOOL) flag
This message is normally sent by +argc:argv: to turn daemon mode off (and to turn debug mode on) when a -d command line option is given. If the Webserver instance was created with +new, the program may use the setdaemon: message to prevent the server to run in daemon mode, i.e. by default, the server will detach itself from the controlling terminal.
setblocking:
- setblocking :(BOOL) flag
By default, the poll message will block and wait for requests. By sending a setblocking: message with a NO argument, poll will immediately return if there is no request to be accepted.
run
- run
This method never returns. It repeatedly sends poll messages to itself.
if:readable:
- if :(int) fd readable : aBlock
Registers aBlock as handler for activity on the file descriptor fd. If fd is ready for reading (as indicated by the C function select()), aBlock will be evaluated.
if:writable:
- if :(int) fd writable : aBlock
Registers aBlock as handler for activity on the file descriptor fd. If fd is ready for writing (as indicated by the C function select()), aBlock will be evaluated.
poll
- poll
The first time this message is sent, the Webserver starts listening at the indicated port number (as set by setportnum:). When running in blocking mode, the poll message will then block until a request arrives. The method then sends itself an accepted message, and then forks a child process to handle the request. Subclasses should implement the contents method, to reply to HTTP GET requests.
listenfd
- (int) listenfd
File descriptor that can be used to select on, when using the Webserver in non-blocking mode.