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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers StreamServer.h Source File

StreamServer.h

00001 
00002 /*
00003 Copyright (c) 2010 IVA2K
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #ifndef _STREAMSERVER_H_
00025 #define _STREAMSERVER_H_
00026 
00027 #include "if/net/net.h"
00028 #include "api/TCPSocket.h"
00029 
00030 #include <string>
00031 using std::string;
00032 
00033 #include <list>
00034 using std::list;
00035 
00036 class StreamRequestDispatcher;
00037 
00038 class StreamServer
00039 {
00040 public:
00041   StreamServer();
00042   ~StreamServer();
00043 
00044   typedef list<StreamRequestDispatcher*> tClients;
00045 
00046 //  template<typename T>
00047 //  void addHandler(const char* path) //Template decl in header
00048 //  { m_lpHandlers[path] = &T::inst; }
00049   
00050   void bind(int port = 123);
00051   void sendToAll(const char* buf, int len);
00052   int countClients(void);
00053   
00054 private:
00055   friend class StreamRequestDispatcher;
00056 
00057   void onTcpSocketEvent(TCPSocketEvent e);
00058   
00059   TCPSocket* m_pTcpSocket;
00060   tClients m_lpClients;
00061   
00062 };
00063 
00064 //BEGIN REQUEST DISPATCHER======================================================
00065 #include "mbed.h"
00066 class StreamRequestDispatcher : public NetService
00067 {
00068 public:
00069   StreamRequestDispatcher(StreamServer* pSvr, TCPSocket* pTcpSocket);
00070   virtual ~StreamRequestDispatcher();
00071   
00072   int writeData(const char* buf, int len);
00073 
00074 private:
00075 
00076   void dispatchRequest();
00077   
00078   virtual void close(); //Close TCPSocket and destroy data
00079   
00080   void onTcpSocketEvent(TCPSocketEvent e);
00081   
00082   bool getRequest(string* request);
00083   
00084   StreamServer* m_pSvr;
00085   TCPSocket* m_pTcpSocket;
00086   
00087   bool m_closed;
00088 };
00089 //END REQUEST DISPATCHER========================================================
00090 
00091 //Including handlers here for more convenience
00092 //#include "impl/RpcHandler.h"
00093 //#include "impl/FSHandler.h"
00094 //#include "impl/SimpleHandler.h"
00095 
00096 #endif  // _STREAMSERVER_H_