Single instance HTTP Server using new Ethernet Interface. Blocking mode only; this improved stability, but the HTTP server must be started from a separate thread.
Fork of HTTPServer by
Diff: HTTPServer.h
- Revision:
- 14:011edcd33e86
- Parent:
- 13:aa5338a5e452
- Child:
- 17:d7186c696729
--- a/HTTPServer.h Sat Jun 22 15:41:34 2013 +0000 +++ b/HTTPServer.h Sat Aug 17 12:12:13 2013 +0000 @@ -57,12 +57,11 @@ * #include "LocalFileSystem.h" * * LocalFileSystem local("local"); -* WiflyInterface wifly(p9, p10, p25, p26, "<your access point>", "<your password>", WPA); * * void main(void) * { * HTTPServer svr; - svr.mount("/local/", "/"); +* svr.mount("/local/", "/"); * svr.addHandler<HTTPFsRequestHandler>( "/" ); * svr.start(); * while(1) @@ -72,13 +71,55 @@ * } * } * @endcode +* +* An alternate approach e.g. if you need to perform additional tasks using the EthernetInterface +* there is the possibility to provide the EthernetInterface object in an initialized and connected +* state. __NOTE: You should choose this scenario for compatibility reasons.___ +* +* \b Example2: +* @code +* #include "mbed.h" +* #include "HTTPServer.h" +* #include "EthernetInterface.h" +* #include "LocalFileSystem.h" +* +* LocalFileSystem local("local"); +* EthernetInterface eth; +* +* void main(void) +* { +* HTTPServer svr; +* // Initialize the ethernet interface +* if (eth.init() != 0) { +* printf("Initialization of EthernetInterface failed !"); +* exit(0); +* } +* // Connect using DHCP +* if (eth.connect() !=0) { +* printf("Failed to connect using DHCP !"); +* exit(0); +* } +* +* // Moint the local file system and provide a handler for 'root'. +* svr.mount("/local/", "/"); +* svr.addHandler<HTTPFsRequestHandler>( "/" ); +* // Start the server on port 80, providing our own ethernet interface object. +* svr.start(80, ð); +* while(1) +* { +* if (svr.poll() < 0) +* exit(0); +* } +* } +* @endcode +* */ class HTTPServer { - TCPSocketServer m_Svr; bool m_bServerListening; - + EthernetInterface* m_pEthernet; + public: /** Constructor for HTTPServer objects. */ @@ -127,9 +168,11 @@ /** Binds server to a specific port and starts listening. This member prepares the internal variables and the server socket * and terminates after successfull initialization * @param port : port on which to listen for incoming connections - * @returns : -1 if an unrecoverable error occured, or 0 if everything was ok. + * @param pEthernet : a pointer to an existing EthernetInterface object or NULL if the HTTPServer shall allocate the object. _Please note that for compatibility reasons + * your should consider to create the EthernetInterface as a static variable. Otherwise the the object will be created on the heap._ + * @returns : false if an unrecoverable error occured or if the ethernet interface was not set or not initialized correctly, or true if everything was ok. */ - bool start(int port = 80); + bool start(int port = 80, EthernetInterface* pEthernet = NULL); /** Performs the regular polling of the server component. Needs to be called cyclically. * The function will internally check whether new connections are requested by a client and will also poll all existing client connections. @@ -138,8 +181,9 @@ * ready for processing the next request. Simply call \c poll as long as you want to serve new incoming requests. */ int poll(bool blocking = true); - + private: + /** The standard error handler function. * @param msg : Request message data. * @param tcp : Socket to be used for responding.