Changes made for RPC

Revision:
14:011edcd33e86
Parent:
13:aa5338a5e452
Child:
16:cc3f5c53d0d5
--- a/HTTPServer.cpp	Sat Jun 22 15:41:34 2013 +0000
+++ b/HTTPServer.cpp	Sat Aug 17 12:12:13 2013 +0000
@@ -12,20 +12,23 @@
 DigitalOut led4(LED4);
 #endif
 
-static EthernetInterface eth;
 
 /* Constructor */
 /* initialize all members and set the standard error handler. */
 HTTPServer::HTTPServer()
+    : m_pEthernet(NULL)
 {
     m_pErrorHandler = StdErrorHandler;
 }
 
 HTTPServer::~HTTPServer()
 {
+    if (m_pEthernet == NULL) {
+        INFO("Deleting EthernetInterface Object.\n");
+        delete m_pEthernet;
+    }
 }
 
-
 static const char* szStdErrorPage = "<HTML><HEAD><META content=\"text/html\" http-equiv=Content-Type></HEAD><BODY><h1>Error 404</h1><P>This resource is not available<P></BODY></HTML>\r\n\r\n";
 
 void HTTPServer::StdErrorHandler(HTTPConnection::HTTPMessage& msg, TCPSocketConnection& tcp)
@@ -38,14 +41,44 @@
 }
 
 
-bool HTTPServer::start(int port)
+bool HTTPServer::start(int port, EthernetInterface* pEthernet)
 {
-    INFO("Initializing network\n");
-    eth.init();
+    //  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) {
+        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;
+        }
 
-    INFO("Connecting to network...");
-    eth.connect();
-
+        //  Initiaize the network
+        INFO("Initializing network\n");
+        if (m_pEthernet->init() != 0) {
+            ERR("Failed to initialize the ethernet interface !");
+            delete m_pEthernet;
+            m_pEthernet = NULL;
+            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 !");
+            delete m_pEthernet;
+            m_pEthernet = NULL;
+            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("Binding to port %d...", port);
     if (m_Svr.bind(port) < 0) {
         ERR("Failed to bind to port !\n");
@@ -70,8 +103,6 @@
 
 int HTTPServer::poll(bool blocking)
 {
-    int retval = -1;
-
 #ifdef _DEBUG
     led4 = 1;   //  Indicate we are waiting for a new connection
 #endif