Example program with HTTPServer and sensor data streaming over TCPSockets, using Donatien Garnier's Net APIs and services code on top of LWIP. Files StreamServer.h and .cpp encapsulate streaming over TCPSockets. Broadcast is done by sendToAll(), and all incoming data is echoed back to the client. Echo code can be replaced with some remote control of the streaming interface. See main() that shows how to periodically send some data to all subscribed clients. To subscribe, a client should open a socket at <mbed_ip> port 123. I used few lines in TCL code to set up a quick sink for the data. HTTP files are served on port 80 concurrently to the streaming.

Dependencies:   mbed

Revision:
1:3ee499525aa5
Parent:
0:e614f7875b60
--- a/services/http/client/HTTPClient.cpp	Sat Jun 12 06:01:50 2010 +0000
+++ b/services/http/client/HTTPClient.cpp	Mon Jun 14 03:24:33 2010 +0000
@@ -41,7 +41,7 @@
 {
   setTimeout(HTTP_REQUEST_TIMEOUT);
   m_buf = new char[CHUNK_SIZE];
-  DBG("\r\nNew HTTPClient %p\r\n",this);
+  DBG("New HTTPClient %p\r\n",this);
 }
 
 HTTPClient::~HTTPClient()
@@ -62,7 +62,7 @@
   decStr += ":";
   decStr += password;
   auth.append( Base64::encode(decStr) );
-  DBG("\r\nAuth str is %s\r\n", auth.c_str());
+  DBG("Auth str is %s\r\n", auth.c_str());
   m_reqHeaders["Authorization"] = auth; 
 }
 
@@ -164,7 +164,7 @@
     if( m_state == HTTP_DONE ) 
     {
       //All data has been read, close w/ success :)
-      DBG("\r\nDone :)!\r\n");
+      DBG("Done :)!\r\n");
       onResult(HTTP_OK);
       close();
     }
@@ -267,7 +267,7 @@
     m_server.setPort( HTTP_PORT );
   }
   
-  DBG("\r\nURL parsed,\r\nHost: %s\r\nPort: %d\r\nPath: %s\r\n", url.getHost().c_str(), url.getPort(), url.getPath().c_str());
+  DBG("URL parsed,\r\nHost: %s\r\nPort: %d\r\nPath: %s\r\n", url.getHost().c_str(), url.getPort(), url.getPath().c_str());
   
   IpAddr ip;
   if( url.getHostIp(&ip) )
@@ -277,11 +277,11 @@
   }
   else
   {
-    DBG("\r\nDNS Query...\r\n");
+    DBG("DNS Query...\r\n");
     m_pDnsReq = new DNSRequest();
     m_pDnsReq->setOnReply(this, &HTTPClient::onDNSReply);
     m_pDnsReq->resolve(&m_server);
-    DBG("\r\nHTTPClient : DNSRequest %p\r\n", m_pDnsReq);
+    DBG("HTTPClient : DNSRequest %p\r\n", m_pDnsReq);
   }
   
 }
@@ -289,7 +289,7 @@
 void HTTPClient::connect() //Start Connection
 {
   resetTimeout();
-  DBG("\r\nConnecting...\r\n");
+  DBG("Connecting...\r\n");
   m_pTCPSocket->connect(m_server);
 }
 
@@ -352,7 +352,7 @@
     m_state = HTTP_DONE;
     return;
   }
-  DBG("\r\nReading response...\r\n");
+  DBG("Reading response...\r\n");
   int len = 0;
   do
   {
@@ -360,7 +360,7 @@
     {
       if(m_dataLen==0)
       {
-        DBG("\r\nReading chunk length...\r\n");
+        DBG("Reading chunk length...\r\n");
         //New block
         static char chunkHeader[16];
         //We use m_dataPos to retain the read position in chunkHeader, it has been set to 0 before the first call of readData()
@@ -370,19 +370,19 @@
           if( chunkHeader[strlen(chunkHeader)-1] == 0x0d )
           {
             sscanf(chunkHeader, "%x%*[^\r\n]", &m_dataLen);
-            DBG("\r\nChunk length is %d\r\n", m_dataLen);
+            DBG("Chunk length is %d\r\n", m_dataLen);
             m_dataPos = 0;
           }
           else
           {
             //Wait for end of line
-            DBG("\r\nWait for CRLF\r\n");
+            DBG("Wait for CRLF\r\n");
             return;
           }
         }
         else
         {
-          DBG("\r\nWait for data\r\n");
+          DBG("Wait for data\r\n");
           //Wait for data
           return;
         }
@@ -405,17 +405,17 @@
     {  
       if(m_dataPos >= m_dataLen)
       {
-        DBG("\r\nChunk read, wait for CRLF\r\n");
+        DBG("Chunk read, wait for CRLF\r\n");
         char chunkTail[3];
         m_dataPos += readLine(chunkTail, 3);
       }
       
       if(m_dataPos >= m_dataLen + 1) //1 == strlen("\n"),
       {
-        DBG("\r\nEnd of chunk\r\n");
+        DBG("End of chunk\r\n");
         if(m_dataLen==0)
         {
-          DBG("\r\nEnd of file\r\n");
+          DBG("End of file\r\n");
           //End of file
           m_state = HTTP_DONE; //Done
         }
@@ -429,7 +429,7 @@
   
   if(!m_dataChunked && (m_dataPos >= m_dataLen)) //All Data has been received
   {
-    DBG("\r\nEnd of file\r\n");
+    DBG("End of file\r\n");
     m_state = HTTP_DONE; //Done
   }
 }
@@ -471,11 +471,11 @@
   
 void HTTPClient::onTCPSocketEvent(TCPSocketEvent e)
 {
-  DBG("\r\nEvent %d in HTTPClient::onTCPSocketEvent()\r\n", e);
+  DBG("Event %d in HTTPClient::onTCPSocketEvent()\r\n", e);
 
   if(m_closed)
   {
-    DBG("\r\nWARN: Discarded\r\n");
+    DBG("WARN: Discarded\r\n");
     return;
   }
 
@@ -498,18 +498,18 @@
           m_dataChunked = true;
           m_dataPos = 0;
           m_dataLen = 0; 
-          DBG("\r\nEncoding is chunked, Content-Type is %s\r\n", m_respHeaders["Content-Type"].c_str() );
+          DBG("Encoding is chunked, Content-Type is %s\r\n", m_respHeaders["Content-Type"].c_str() );
         }
         else
         {    
           m_dataChunked = false;
           int len = 0;
-          //DBG("\r\nPreparing read... len = %s\r\n", m_respHeaders["Content-Length"].c_str());
+          //DBG("Preparing read... len = %s\r\n", m_respHeaders["Content-Length"].c_str());
           sscanf(m_respHeaders["Content-Length"].c_str(), "%d", &len);
           m_pDataIn->setDataLen( len );
           m_dataPos = 0;
           m_dataLen = len; 
-          DBG("\r\nContent-Length is %d, Content-Type is %s\r\n", len, m_respHeaders["Content-Type"].c_str() );
+          DBG("Content-Length is %d, Content-Type is %s\r\n", len, m_respHeaders["Content-Type"].c_str() );
         }
         m_pDataIn->setDataType( m_respHeaders["Content-Type"] );
       }
@@ -525,7 +525,7 @@
  //All data has been read, close w/ success :)
     if( m_state == HTTP_DONE ) 
     {
-      DBG("\r\nDone :)!\r\n");
+      DBG("Done :)!\r\n");
       onResult(HTTP_OK);
     }
     break;
@@ -581,7 +581,7 @@
   case TCPSOCKET_CONRST:
   case TCPSOCKET_CONABRT:
   case TCPSOCKET_ERROR:
-    DBG("\r\nConnection error.\r\n");
+    DBG("Connection error.\r\n");
     onResult(HTTP_CONN);
   case TCPSOCKET_DISCONNECTED:
     //There might still be some data available for reading
@@ -590,7 +590,7 @@
     {
       onResult(HTTP_CONN);
     }
-    DBG("\r\nConnection closed by remote host.\r\n");
+    DBG("Connection closed by remote host.\r\n");
     break;
   }
 }
@@ -599,18 +599,18 @@
 {
   if(m_closed)
   {
-    DBG("\r\nWARN: Discarded\r\n");
+    DBG("WARN: Discarded\r\n");
     return;
   }
   
   if( r != DNS_FOUND )
   {
-    DBG("\r\nCould not resolve hostname.\r\n");
+    DBG("Could not resolve hostname.\r\n");
     onResult(HTTP_DNS);
     return;
   }
   
-  DBG("\r\nDNS Resolved to %d.%d.%d.%d.\r\n",m_server.getIp()[0],m_server.getIp()[1],m_server.getIp()[2],m_server.getIp()[3]);
+  DBG("DNS Resolved to %d.%d.%d.%d.\r\n",m_server.getIp()[0],m_server.getIp()[1],m_server.getIp()[2],m_server.getIp()[3]);
   //If no error, m_server has been updated by m_pDnsReq so we're set to go !
   m_pDnsReq->close();
   delete m_pDnsReq;
@@ -630,7 +630,7 @@
 
 void HTTPClient::onTimeout() //Connection has timed out
 {
-  DBG("\r\nTimed out.\n");
+  DBG("Timed out.\n");
   onResult(HTTP_TIMEOUT);
   close();
 }
@@ -668,7 +668,7 @@
       if( sscanf(line, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 )
       {
         //Cannot match string, error
-        DBG("\r\nNot a correct HTTP answer : %s\r\n", line);
+        DBG("Not a correct HTTP answer : %s\r\n", line);
         onResult(HTTP_PRTCL);
         close();
         return false;
@@ -676,7 +676,7 @@
       
       if(m_httpResponseCode != 200)
       {
-        DBG("\r\nResponse: error code %d\r\n", m_httpResponseCode);
+        DBG("Response: error code %d\r\n", m_httpResponseCode);
         HTTPResult res = HTTP_ERROR;
         switch(m_httpResponseCode)
         {
@@ -693,12 +693,12 @@
         close();
         return false;
       }
-      DBG("\r\nResponse OK\r\n");
+      DBG("Response OK\r\n");
     }
     else
     {
       //Empty packet, weird!
-      DBG("\r\nEmpty packet!\r\n");
+      DBG("Empty packet!\r\n");
       onResult(HTTP_PRTCL);
       close();
       return false;
@@ -711,7 +711,7 @@
     m_dataLen = 0;
     if( readLen <= 2 ) //if == 1 or 2, it is an empty line = end of headers
     {
-      DBG("\r\nAll headers read.\r\n");
+      DBG("All headers read.\r\n");
       m_state = HTTP_READ_DATA;
       break;
     }
@@ -720,11 +720,11 @@
       m_dataLen = readLen;//Sets data length available in buffer
       return false;
     }
-    //DBG("\r\nHeader : %s\r\n", line);
+    //DBG("Header : %s\r\n", line);
     int n = sscanf(line, "%[^:] : %[^\r\n]", key, value);
     if ( n == 2 )
     {
-      DBG("\r\nRead header : %s: %s\r\n", key, value);
+      DBG("Read header : %s: %s\r\n", key, value);
       m_respHeaders[key] = value;
     }
     //TODO: Impl n==1 case (part 2 of previous header)
@@ -741,14 +741,14 @@
   //Req
   sprintf(line, "%s %s HTTP/1.1\r\nHost: %s\r\n", HTTP_METH_STR[m_meth], m_path.c_str(), m_server.getName()); //Write request
   m_pTCPSocket->send(line, strlen(line));
-  DBG("\r\nRequest: %s\r\n", line);
+  DBG("Request: %s\r\n", line);
   
-  DBG("\r\nWriting headers:\r\n");
+  DBG("Writing headers:\r\n");
   map<string,string>::iterator it;
   for( it = m_reqHeaders.begin(); it != m_reqHeaders.end(); it++ )
   {
     sprintf(line, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() );
-    DBG("\r\n%s", line);
+    DBG("%s", line);
     m_pTCPSocket->send(line, strlen(line));
   }
   m_pTCPSocket->send("\r\n",2); //End of head