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/StreamServer.cpp	Sat Jun 12 06:01:50 2010 +0000
+++ b/StreamServer.cpp	Mon Jun 14 03:24:33 2010 +0000
@@ -35,17 +35,18 @@
 
 StreamRequestDispatcher::~StreamRequestDispatcher()
 {
+//DBG("deleting\r\n");
   close();
 }
 
 int StreamRequestDispatcher::writeData(const char* buf, int len)
 {
   if(m_closed) {
-DBG("\r\nIN StreamRequestDispatcher::writeData - m_closed\r\n");
+//DBG("m_closed\r\n");
     return TCPSOCKET_RST;
   }
   int ret = m_pTcpSocket->send(buf, len);
-if (ret != len)    DBG("\r\nStreamRequestDispatcher::writeData - ret=%d\r\n", ret);
+if (ret != len)    DBG("ret=%d\r\n", ret);
   return ret;
 }
 
@@ -55,8 +56,10 @@
   string request;
   const char* buf;
   int len, ret;
-  DBG("\r\nIN StreamRequestDispatcher::dispatchRequest()\r\n");
+//  DBG("\r\n");
   
+//FIXME: here is the place to implement custom request handler
+ 
   while ( getRequest(&request) )
   {
     // Just echo it back
@@ -64,26 +67,30 @@
     len = strlen(buf);
     ret = writeData(buf,len);
 writeData("\r\n",2);
-    DBG("\r\nReceived req (%s), write ret=%d\r\n", buf, ret);
+    DBG("Received req (%s), write ret=%d\r\n", buf, ret);
   }
 }
 
 void StreamRequestDispatcher::close() //Close socket and destroy data
 {
-  if(m_closed)
+  if(m_closed) {
+//DBG("already closed\r\n");
     return;
+  }
   m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
+//DBG("removing from list\r\n");
   m_pSvr->m_lpClients.remove(this);
   if(m_pTcpSocket)
   {
+//DBG("closing socket\r\n");
     m_pTcpSocket->resetOnEvent();
-    m_pTcpSocket->close();
+    m_pTcpSocket->close();  // FIXME: in bad cause of events, getting "could not close properly, abort" log message
     delete m_pTcpSocket;
   }
+//DBG("closing service\r\n");
   NetService::close();
 }
 
-//FIXME: Need implementation for StreamServer
 bool StreamRequestDispatcher::getRequest(string* request)
 {
   const int maxLen = 64;
@@ -119,7 +126,7 @@
   }
   *p = 0;
   
-  DBG("\r\nParsing request (%s) ret=%d\r\n", req, ret);
+  DBG("Parsing request (%s) ret=%d\r\n", req, ret);
   
   *request = string(req);
   
@@ -129,11 +136,11 @@
 
 void StreamRequestDispatcher::onTcpSocketEvent(TCPSocketEvent e)
 {
-  DBG("\r\nStreamRequestDispatcher Event %d\r\n", e);
+  DBG("Event %d\r\n", e);
   
   if(m_closed)
   {
-    DBG("\r\nWARN: Discarded\r\n");
+    DBG("WARN: Discarded\r\n");
     return;
   }
 
@@ -186,29 +193,36 @@
 {
   int ret;
   int i = 0;
-  for(tClients::iterator it = m_lpClients.begin(); it != m_lpClients.end(); ++it)
+  tClients::iterator it;
+  for(it = m_lpClients.begin(); it != m_lpClients.end(); /* No increment here */ )
   {
     ret = (*it)->writeData(buf, len);
     if (
          ret == TCPSOCKET_RST // This is a safety valve. We should have self-removed from m_lpClients upon socket closure and not get here.
       || ret == TCPSOCKET_MEM // This probably means that network is not delivering packets, and we're backed up.
     ) {
-DBG("\r\nStreamServer::sendToAll(%s) - erasing socket %d\r\n", buf, i);
+      DBG("(%s) Socket error 0x%04X - erasing socket %d\r\n", buf, ret, i);
+      // delete below will remove (*it) from the list we are iterating in. Prepare for that
+      tClients::iterator it_next = it;
+      it_next++;
       delete (*it);
-//?     (*it) = NULL;
-//DBG("\r\nStreamServer::sendToAll() - erasing socket\r\n");
-//?      it = m_lpClients.erase(it);
-      continue;
+      it = it_next;
+      continue; // This ensures that for(...) above will continue properly.
     }
     i++;
+    it++; /* increment the iterator */
   }
-DBG("\r\nStreamServer::sendToAll(%s) - %d sockets\r\n", buf, i);
+}
+
+int StreamServer::countClients(void)
+{
+  return m_lpClients.size();
 }
   
 void StreamServer::onTcpSocketEvent(TCPSocketEvent e)
 {
 
-  DBG("\r\nStreamServer::onTcpSocketEvent : Event %d\r\n", e);
+  DBG("Event %d\r\n", e);
 
   if(e==TCPSOCKET_ACCEPT)
   {
@@ -217,7 +231,7 @@
 
     if( !!m_pTcpSocket->accept(&client, &pTcpSocket) )
     {
-      DBG("\r\nStreamServer::onTcpSocketEvent : Could not accept connection.\r\n");
+      DBG("Could not accept connection.\r\n");
       return; //Error in accept, discard connection
     }
     
@@ -226,3 +240,5 @@
     m_lpClients.push_back(pDispatcher);
   }
 }
+
+//END
\ No newline at end of file