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

Committer:
iva2k
Date:
Sat Jun 12 06:01:50 2010 +0000
Revision:
0:e614f7875b60

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
iva2k 0:e614f7875b60 1
iva2k 0:e614f7875b60 2 /*
iva2k 0:e614f7875b60 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
iva2k 0:e614f7875b60 4
iva2k 0:e614f7875b60 5 Permission is hereby granted, free of charge, to any person obtaining a copy
iva2k 0:e614f7875b60 6 of this software and associated documentation files (the "Software"), to deal
iva2k 0:e614f7875b60 7 in the Software without restriction, including without limitation the rights
iva2k 0:e614f7875b60 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
iva2k 0:e614f7875b60 9 copies of the Software, and to permit persons to whom the Software is
iva2k 0:e614f7875b60 10 furnished to do so, subject to the following conditions:
iva2k 0:e614f7875b60 11
iva2k 0:e614f7875b60 12 The above copyright notice and this permission notice shall be included in
iva2k 0:e614f7875b60 13 all copies or substantial portions of the Software.
iva2k 0:e614f7875b60 14
iva2k 0:e614f7875b60 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
iva2k 0:e614f7875b60 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
iva2k 0:e614f7875b60 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
iva2k 0:e614f7875b60 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
iva2k 0:e614f7875b60 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
iva2k 0:e614f7875b60 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
iva2k 0:e614f7875b60 21 THE SOFTWARE.
iva2k 0:e614f7875b60 22 */
iva2k 0:e614f7875b60 23
iva2k 0:e614f7875b60 24 #ifndef NETTCPSOCKET_H
iva2k 0:e614f7875b60 25 #define NETTCPSOCKET_H
iva2k 0:e614f7875b60 26
iva2k 0:e614f7875b60 27 #include "net.h"
iva2k 0:e614f7875b60 28 #include "host.h"
iva2k 0:e614f7875b60 29
iva2k 0:e614f7875b60 30 #include <queue>
iva2k 0:e614f7875b60 31 using std::queue;
iva2k 0:e614f7875b60 32
iva2k 0:e614f7875b60 33 //Implements a Berkeley-like socket if
iva2k 0:e614f7875b60 34 //Can be interfaced either to lwip or a Telit module
iva2k 0:e614f7875b60 35
iva2k 0:e614f7875b60 36 enum NetTcpSocketErr
iva2k 0:e614f7875b60 37 {
iva2k 0:e614f7875b60 38 __NETTCPSOCKET_MIN = -0xFFFF,
iva2k 0:e614f7875b60 39 NETTCPSOCKET_SETUP, //NetTcpSocket not properly configured
iva2k 0:e614f7875b60 40 NETTCPSOCKET_TIMEOUT,
iva2k 0:e614f7875b60 41 NETTCPSOCKET_IF, //If has problems
iva2k 0:e614f7875b60 42 NETTCPSOCKET_MEM, //Not enough mem
iva2k 0:e614f7875b60 43 NETTCPSOCKET_INUSE, //If/Port is in use
iva2k 0:e614f7875b60 44 NETTCPSOCKET_EMPTY, //Connections queue is empty
iva2k 0:e614f7875b60 45 NETTCPSOCKET_RST, // Connection was reset by remote host
iva2k 0:e614f7875b60 46 //...
iva2k 0:e614f7875b60 47 NETTCPSOCKET_OK = 0
iva2k 0:e614f7875b60 48 };
iva2k 0:e614f7875b60 49
iva2k 0:e614f7875b60 50 enum NetTcpSocketEvent
iva2k 0:e614f7875b60 51 {
iva2k 0:e614f7875b60 52 NETTCPSOCKET_CONNECTED, //Connected to host, must call accept() if we were listening
iva2k 0:e614f7875b60 53 NETTCPSOCKET_ACCEPT, //Connected to client
iva2k 0:e614f7875b60 54 NETTCPSOCKET_READABLE, //Data in buf
iva2k 0:e614f7875b60 55 NETTCPSOCKET_WRITEABLE, //Can write data to buf
iva2k 0:e614f7875b60 56 NETTCPSOCKET_CONTIMEOUT,
iva2k 0:e614f7875b60 57 NETTCPSOCKET_CONRST,
iva2k 0:e614f7875b60 58 NETTCPSOCKET_CONABRT,
iva2k 0:e614f7875b60 59 NETTCPSOCKET_ERROR,
iva2k 0:e614f7875b60 60 NETTCPSOCKET_DISCONNECTED
iva2k 0:e614f7875b60 61 };
iva2k 0:e614f7875b60 62
iva2k 0:e614f7875b60 63
iva2k 0:e614f7875b60 64 class NetTcpSocket
iva2k 0:e614f7875b60 65 {
iva2k 0:e614f7875b60 66 public:
iva2k 0:e614f7875b60 67 NetTcpSocket();
iva2k 0:e614f7875b60 68 virtual ~NetTcpSocket(); //close()
iva2k 0:e614f7875b60 69
iva2k 0:e614f7875b60 70 virtual NetTcpSocketErr bind(const Host& me) = 0;
iva2k 0:e614f7875b60 71 virtual NetTcpSocketErr listen() = 0;
iva2k 0:e614f7875b60 72 virtual NetTcpSocketErr connect(const Host& host) = 0;
iva2k 0:e614f7875b60 73 virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket) = 0;
iva2k 0:e614f7875b60 74
iva2k 0:e614f7875b60 75 virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len) = 0;
iva2k 0:e614f7875b60 76 virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len) = 0;
iva2k 0:e614f7875b60 77
iva2k 0:e614f7875b60 78 virtual NetTcpSocketErr close() = 0;
iva2k 0:e614f7875b60 79
iva2k 0:e614f7875b60 80 virtual NetTcpSocketErr poll() = 0;
iva2k 0:e614f7875b60 81
iva2k 0:e614f7875b60 82 class CDummy;
iva2k 0:e614f7875b60 83 //Callbacks
iva2k 0:e614f7875b60 84 template<class T>
iva2k 0:e614f7875b60 85 //Linker bug : Must be defined here :(
iva2k 0:e614f7875b60 86 void setOnEvent( T* pItem, void (T::*pMethod)(NetTcpSocketEvent) )
iva2k 0:e614f7875b60 87 {
iva2k 0:e614f7875b60 88 m_pCbItem = (CDummy*) pItem;
iva2k 0:e614f7875b60 89 m_pCbMeth = (void (CDummy::*)(NetTcpSocketEvent)) pMethod;
iva2k 0:e614f7875b60 90 }
iva2k 0:e614f7875b60 91
iva2k 0:e614f7875b60 92 void resetOnEvent(); //Disable callback
iva2k 0:e614f7875b60 93
iva2k 0:e614f7875b60 94 protected:
iva2k 0:e614f7875b60 95 void queueEvent(NetTcpSocketEvent e);
iva2k 0:e614f7875b60 96 void discardEvents();
iva2k 0:e614f7875b60 97 void flushEvents(); //to be called during polling
iva2k 0:e614f7875b60 98
iva2k 0:e614f7875b60 99 Host m_host;
iva2k 0:e614f7875b60 100 Host m_client;
iva2k 0:e614f7875b60 101
iva2k 0:e614f7875b60 102 friend class Net;
iva2k 0:e614f7875b60 103 int m_refs;
iva2k 0:e614f7875b60 104
iva2k 0:e614f7875b60 105 bool m_closed;
iva2k 0:e614f7875b60 106 bool m_removed;
iva2k 0:e614f7875b60 107
iva2k 0:e614f7875b60 108 private:
iva2k 0:e614f7875b60 109 //We do not want to execute user code in interrupt routines, so we queue events until the server is polled
iva2k 0:e614f7875b60 110 //If we port this to a multithreaded OS, we could avoid this (however some functions here are not thread-safe, so beware ;) )
iva2k 0:e614f7875b60 111 void onEvent(NetTcpSocketEvent e); //To be called on poll
iva2k 0:e614f7875b60 112 CDummy* m_pCbItem;
iva2k 0:e614f7875b60 113 void (CDummy::*m_pCbMeth)(NetTcpSocketEvent);
iva2k 0:e614f7875b60 114 queue<NetTcpSocketEvent> m_events;
iva2k 0:e614f7875b60 115
iva2k 0:e614f7875b60 116 };
iva2k 0:e614f7875b60 117
iva2k 0:e614f7875b60 118 #endif