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:
Mon Jun 14 03:24:33 2010 +0000
Revision:
1:3ee499525aa5
Parent:
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 #include "TCPSocket.h"
iva2k 0:e614f7875b60 25
iva2k 0:e614f7875b60 26 TCPSocket::TCPSocket() : m_pNetTcpSocket(NULL)
iva2k 0:e614f7875b60 27 {
iva2k 0:e614f7875b60 28
iva2k 0:e614f7875b60 29 }
iva2k 0:e614f7875b60 30
iva2k 0:e614f7875b60 31 TCPSocket::TCPSocket(NetTcpSocket* pNetTcpSocket) : m_pNetTcpSocket(pNetTcpSocket)
iva2k 0:e614f7875b60 32 {
iva2k 0:e614f7875b60 33 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent);
iva2k 0:e614f7875b60 34 }
iva2k 0:e614f7875b60 35
iva2k 0:e614f7875b60 36 TCPSocket::~TCPSocket() //close()
iva2k 0:e614f7875b60 37 {
iva2k 0:e614f7875b60 38 close();
iva2k 0:e614f7875b60 39 }
iva2k 0:e614f7875b60 40
iva2k 0:e614f7875b60 41 TCPSocketErr TCPSocket::bind(const Host& me)
iva2k 0:e614f7875b60 42 {
iva2k 0:e614f7875b60 43 TCPSocketErr tcpSocketErr = checkInst();
iva2k 0:e614f7875b60 44 if(tcpSocketErr)
iva2k 0:e614f7875b60 45 return tcpSocketErr;
iva2k 0:e614f7875b60 46 return (TCPSocketErr) m_pNetTcpSocket->bind(me);
iva2k 0:e614f7875b60 47 }
iva2k 0:e614f7875b60 48
iva2k 0:e614f7875b60 49 TCPSocketErr TCPSocket::listen()
iva2k 0:e614f7875b60 50 {
iva2k 0:e614f7875b60 51 TCPSocketErr tcpSocketErr = checkInst();
iva2k 0:e614f7875b60 52 if(tcpSocketErr)
iva2k 0:e614f7875b60 53 return tcpSocketErr;
iva2k 0:e614f7875b60 54 return (TCPSocketErr) m_pNetTcpSocket->listen();
iva2k 0:e614f7875b60 55 }
iva2k 0:e614f7875b60 56
iva2k 0:e614f7875b60 57 TCPSocketErr TCPSocket::connect(const Host& host)
iva2k 0:e614f7875b60 58 {
iva2k 0:e614f7875b60 59 TCPSocketErr tcpSocketErr = checkInst();
iva2k 0:e614f7875b60 60 if(tcpSocketErr)
iva2k 0:e614f7875b60 61 return tcpSocketErr;
iva2k 0:e614f7875b60 62 return (TCPSocketErr) m_pNetTcpSocket->connect(host);
iva2k 0:e614f7875b60 63 }
iva2k 0:e614f7875b60 64
iva2k 0:e614f7875b60 65 TCPSocketErr TCPSocket::accept(Host* pClient, TCPSocket** ppNewTCPSocket)
iva2k 0:e614f7875b60 66 {
iva2k 0:e614f7875b60 67 TCPSocketErr tcpSocketErr = checkInst();
iva2k 0:e614f7875b60 68 if(tcpSocketErr)
iva2k 0:e614f7875b60 69 return tcpSocketErr;
iva2k 0:e614f7875b60 70 NetTcpSocket* pNewNetTcpSocket;
iva2k 0:e614f7875b60 71 tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->accept(pClient, &pNewNetTcpSocket);
iva2k 0:e614f7875b60 72 if(pNewNetTcpSocket)
iva2k 0:e614f7875b60 73 *ppNewTCPSocket = new TCPSocket(pNewNetTcpSocket);
iva2k 0:e614f7875b60 74 return tcpSocketErr;
iva2k 0:e614f7875b60 75 }
iva2k 0:e614f7875b60 76
iva2k 0:e614f7875b60 77 int /*if < 0 : TCPSocketErr*/ TCPSocket::send(const char* buf, int len)
iva2k 0:e614f7875b60 78 {
iva2k 0:e614f7875b60 79 TCPSocketErr tcpSocketErr = checkInst();
iva2k 0:e614f7875b60 80 if(tcpSocketErr)
iva2k 0:e614f7875b60 81 return tcpSocketErr;
iva2k 0:e614f7875b60 82 return m_pNetTcpSocket->send(buf, len);
iva2k 0:e614f7875b60 83 }
iva2k 0:e614f7875b60 84
iva2k 0:e614f7875b60 85 int /*if < 0 : TCPSocketErr*/ TCPSocket::recv(char* buf, int len)
iva2k 0:e614f7875b60 86 {
iva2k 0:e614f7875b60 87 TCPSocketErr tcpSocketErr = checkInst();
iva2k 0:e614f7875b60 88 if(tcpSocketErr)
iva2k 0:e614f7875b60 89 return tcpSocketErr;
iva2k 0:e614f7875b60 90 return m_pNetTcpSocket->recv(buf, len);
iva2k 0:e614f7875b60 91 }
iva2k 0:e614f7875b60 92
iva2k 0:e614f7875b60 93 TCPSocketErr TCPSocket::close()
iva2k 0:e614f7875b60 94 {
iva2k 0:e614f7875b60 95 if(!m_pNetTcpSocket)
iva2k 0:e614f7875b60 96 return TCPSOCKET_SETUP;
iva2k 0:e614f7875b60 97 m_pNetTcpSocket->resetOnEvent();
iva2k 0:e614f7875b60 98 TCPSocketErr tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->close(); //Close (can already be closed)
iva2k 0:e614f7875b60 99 Net::releaseTcpSocket(m_pNetTcpSocket); //And release it so it can be freed when properly removed
iva2k 0:e614f7875b60 100 m_pNetTcpSocket = NULL;
iva2k 0:e614f7875b60 101 return tcpSocketErr;
iva2k 0:e614f7875b60 102 }
iva2k 0:e614f7875b60 103
iva2k 0:e614f7875b60 104 //Callbacks
iva2k 0:e614f7875b60 105 void TCPSocket::setOnEvent( void (*pMethod)(TCPSocketEvent) )
iva2k 0:e614f7875b60 106 {
iva2k 0:e614f7875b60 107 m_pCb = pMethod;
iva2k 0:e614f7875b60 108 }
iva2k 0:e614f7875b60 109
iva2k 0:e614f7875b60 110 #if 0 //For info only
iva2k 0:e614f7875b60 111 template<class T>
iva2k 0:e614f7875b60 112 void TCPSocket::setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
iva2k 0:e614f7875b60 113 {
iva2k 0:e614f7875b60 114 m_pCbItem = (CDummy*) pItem;
iva2k 0:e614f7875b60 115 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
iva2k 0:e614f7875b60 116 }
iva2k 0:e614f7875b60 117 #endif
iva2k 0:e614f7875b60 118
iva2k 0:e614f7875b60 119 void TCPSocket::resetOnEvent() //Disable callback
iva2k 0:e614f7875b60 120 {
iva2k 0:e614f7875b60 121 m_pCb = NULL;
iva2k 0:e614f7875b60 122 m_pCbItem = NULL;
iva2k 0:e614f7875b60 123 m_pCbMeth = NULL;
iva2k 0:e614f7875b60 124 }
iva2k 0:e614f7875b60 125
iva2k 0:e614f7875b60 126 void TCPSocket::onNetTcpSocketEvent(NetTcpSocketEvent e)
iva2k 0:e614f7875b60 127 {
iva2k 0:e614f7875b60 128 if(m_pCbItem && m_pCbMeth)
iva2k 0:e614f7875b60 129 (m_pCbItem->*m_pCbMeth)((TCPSocketEvent) e);
iva2k 0:e614f7875b60 130 else if(m_pCb)
iva2k 0:e614f7875b60 131 m_pCb((TCPSocketEvent) e);
iva2k 0:e614f7875b60 132 }
iva2k 0:e614f7875b60 133
iva2k 0:e614f7875b60 134 TCPSocketErr TCPSocket::checkInst()
iva2k 0:e614f7875b60 135 {
iva2k 0:e614f7875b60 136 if(!m_pNetTcpSocket)
iva2k 0:e614f7875b60 137 {
iva2k 0:e614f7875b60 138 m_pNetTcpSocket = Net::tcpSocket();
iva2k 0:e614f7875b60 139 if(!m_pNetTcpSocket)
iva2k 0:e614f7875b60 140 {
iva2k 0:e614f7875b60 141 return TCPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist)
iva2k 0:e614f7875b60 142 }
iva2k 0:e614f7875b60 143 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent);
iva2k 0:e614f7875b60 144 }
iva2k 0:e614f7875b60 145 return TCPSOCKET_OK;
iva2k 0:e614f7875b60 146 }