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.

Dependents:   SmartLight

Fork of HTTPServer by Henry Leinen

Revision:
17:d7186c696729
Parent:
16:cc3f5c53d0d5
--- a/HTTPServer.cpp	Sat Aug 17 16:17:55 2013 +0000
+++ b/HTTPServer.cpp	Thu May 15 16:09:51 2014 +0000
@@ -8,7 +8,7 @@
 /* Constructor */
 /* initialize all members and set the standard error handler. */
 HTTPServer::HTTPServer()
-    : m_pEthernet(NULL)
+    : m_port(80), m_pEthernet(NULL)
 {
     m_pErrorHandler = StdErrorHandler;
 }
@@ -32,91 +32,104 @@
     tcp.send((char*)szStdErrorPage, strlen(szStdErrorPage));
 }
 
+void HTTPServer::setPort(int port)
+{
+    m_port = port;
+}
 
-bool HTTPServer::start(int port, EthernetInterface* pEthernet)
+void HTTPServer::setEthernetInterface(EthernetInterface* pEthernet)
+{
+    m_pEthernet = pEthernet;
+}
+
+void HTTPServer::start()
 {
     //  If no ethernet interface is provided, instantiate own on the heap. This has to be deleted later in the destructor.
     //  If a valid pointer to an thernet interface is proveded, we can simply use it. 
-    if (pEthernet == NULL) {
+    if (m_pEthernet == NULL) {
         INFO("Creating EthernetInterface object\n");
         m_pEthernet = new EthernetInterface();
         
         if (m_pEthernet == NULL) {
             ERR("Out of memory, unable to instantiate an EthernetInterface object.");
-            return false;
+            //return false;
         }
 
         //  Initiaize the network
         INFO("Initializing network\n");
         if (m_pEthernet->init() != 0) {
-            ERR("Failed to initialize the ethernet interface !");
+            ERR("Failed to initialize the ethernet interface!");
             delete m_pEthernet;
             m_pEthernet = NULL;
-            return false;
+            //return false;
         }
         
         //  Connect to the network using DHCP
         INFO("Connecting to the network using DHCP...");
         if (m_pEthernet->connect() != 0) {
-            ERR("Failed to connect to the ethernet !");
+            ERR("Failed to connect to the ethernet!");
             delete m_pEthernet;
             m_pEthernet = NULL;
-            return false;
+            //return false;
         }
         
         INFO("Connected IP %s", m_pEthernet->getIPAddress());
         
     } else {
         //  In the case that the ethernet interface is provided, it is assumed that a connection has already been created.
-        INFO("Using connection IP %s", pEthernet->getIPAddress());
+        INFO("Using connection IP %s", m_pEthernet->getIPAddress());
     }
     
-    INFO("Binding to port %d...", port);
-    if (m_Svr.bind(port) < 0) {
-        ERR("Failed to bind to port !\n");
+    INFO("Binding to port %d...", m_port);
+    if (m_Svr.bind(m_port) < 0) {
+        ERR("Failed to bind to port!\n");
         error("Binding");
-        return false;
+        //return false;
     }
-
+    
     INFO("Listening ...");
     if (m_Svr.listen(1) < 0) {
-        ERR("Failed to listen !\n");
+        ERR("Failed to listen!\n");
         error("Listening");
-        return false;
+        //return false;
     }
+    
+    m_Svr.set_blocking(true);
 
-    INFO("Connected !");
-    //  set into non blocking operation
-    m_Svr.set_blocking(false, 100);
-
-    return true;
-}
-
+    INFO("Connected!");
+    
+    //osThreadSetPriority( Thread::gettid() ,  osPriorityBelowNormal );
+    
+    while (1)
+    {
+//    INFO("Entering polling thread");
 
-int HTTPServer::poll(bool blocking)
-{
-    //  This thread basically checks if there is a new incoming connection.
-    //  If so , a new HTTPConnection is created and the connection thread is started.
-    TCPSocketConnection Clnt;
-    if (m_Svr.accept(Clnt) < 0) {
-        return -1;
-    }
+        //  This thread basically checks if there is a new incoming connection.
+        //  If so , a new HTTPConnection is created and the connection thread is started.
+        TCPSocketConnection Clnt;
+        if (m_Svr.accept(Clnt) < 0) {
+//        INFO("Failure accepting client connection.");
+//            return -1;
+        }
 
-    //   a new connection was received
-    INFO("Client (IP=%s) is connected !\n", Clnt.get_address());
-    HTTPConnection con(Clnt);
-    int c = con.poll();
-    if (c == 0) {
-        //  Handle the request
-        INFO("Handling request !");
-        HandleRequest(con.m_Msg, Clnt);
+        //   a new connection was received
+        INFO("Client (IP=%s) is connected!\n", Clnt.get_address());
+        HTTPConnection con(Clnt);
+        int c = con.poll();
+        if (c == 0) {
+            //  Handle the request
+            INFO("Handling request!");
+            HandleRequest(con.m_Msg, Clnt);
+        }
+        if (c == -1) {
+//        break;
+        }
+
+        INFO("Leaving polling thread");
+//        return 0;
     }
-    if (c == -1) {
-//        break;
-    }
-
-    INFO("Leaving polling thread");
-    return 0;
+    
+    //return true;
 }
 
 void HTTPServer::HandleRequest(HTTPConnection::HTTPMessage& msg, TCPSocketConnection& tcp)
@@ -143,7 +156,7 @@
         INFO("Webrequest left unhandled.");
     } else {
         //  Valid handler was found
-        INFO("Routing webrequest !");
+        INFO("Routing webrequest!");
         //  Instantiate the handler object (handling will be done from withing the object's constructor
         HTTPRequestHandler *phdl = (*it->second)(it->first.c_str(), localPath.c_str(), msg, tcp);
         //  now we can delete the object, because handling is completed.