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 #ifndef NET_H
iva2k 0:e614f7875b60 25 #define NET_H
iva2k 0:e614f7875b60 26
iva2k 0:e614f7875b60 27 class NetIf;
iva2k 0:e614f7875b60 28 class NetTcpSocket;
iva2k 0:e614f7875b60 29 class NetUdpSocket;
iva2k 0:e614f7875b60 30 class NetDnsRequest;
iva2k 0:e614f7875b60 31
iva2k 0:e614f7875b60 32 #include <list>
iva2k 0:e614f7875b60 33 using std::list;
iva2k 0:e614f7875b60 34
iva2k 0:e614f7875b60 35 #include "host.h"
iva2k 0:e614f7875b60 36 #include "ipaddr.h"
iva2k 0:e614f7875b60 37 #include "netif.h"
iva2k 0:e614f7875b60 38 #include "nettcpsocket.h"
iva2k 0:e614f7875b60 39 #include "netudpsocket.h"
iva2k 0:e614f7875b60 40 #include "netservice.h"
iva2k 0:e614f7875b60 41 #include "netdnsrequest.h"
iva2k 0:e614f7875b60 42
iva2k 0:e614f7875b60 43 class Net
iva2k 0:e614f7875b60 44 {
iva2k 0:e614f7875b60 45 private:
iva2k 0:e614f7875b60 46 Net();
iva2k 0:e614f7875b60 47 ~Net();
iva2k 0:e614f7875b60 48 public:
iva2k 0:e614f7875b60 49 static void poll(); //Poll every if & socket
iva2k 0:e614f7875b60 50
iva2k 0:e614f7875b60 51 static NetTcpSocket* tcpSocket(NetIf& netif);
iva2k 0:e614f7875b60 52 static NetTcpSocket* tcpSocket(); //Socket on default if
iva2k 0:e614f7875b60 53 static void releaseTcpSocket(NetTcpSocket* pNetTcpSocket);
iva2k 0:e614f7875b60 54
iva2k 0:e614f7875b60 55 static NetUdpSocket* udpSocket(NetIf& netif);
iva2k 0:e614f7875b60 56 static NetUdpSocket* udpSocket(); //Socket on default if
iva2k 0:e614f7875b60 57 static void releaseUdpSocket(NetUdpSocket* pNetUdpSocket);
iva2k 0:e614f7875b60 58
iva2k 0:e614f7875b60 59 static NetDnsRequest* dnsRequest(const char* hostname, NetIf& netif);
iva2k 0:e614f7875b60 60 static NetDnsRequest* dnsRequest(const char* hostname); //Create a new NetDnsRequest object from default if
iva2k 0:e614f7875b60 61
iva2k 0:e614f7875b60 62 static NetDnsRequest* dnsRequest(Host* pHost, NetIf& netif);
iva2k 0:e614f7875b60 63 static NetDnsRequest* dnsRequest(Host* pHost); //Create a new NetDnsRequest object from default if
iva2k 0:e614f7875b60 64
iva2k 0:e614f7875b60 65 static void setDefaultIf(NetIf& netif); //Deprecated
iva2k 0:e614f7875b60 66 static void setDefaultIf(NetIf* pIf);
iva2k 0:e614f7875b60 67 static NetIf* getDefaultIf();
iva2k 0:e614f7875b60 68
iva2k 0:e614f7875b60 69 //TODO: Factory functions like 'setupEthernet', 'setupPPP', 'setupTelit' ...
iva2k 0:e614f7875b60 70 #if 0
iva2k 0:e614f7875b60 71 enum NetErr //Generic errors
iva2k 0:e614f7875b60 72 {
iva2k 0:e614f7875b60 73 __NET_MIN = -0xFFFF;
iva2k 0:e614f7875b60 74 NET_OPEN, //Could not open if
iva2k 0:e614f7875b60 75 NET_CONNECT, //Could not connect
iva2k 0:e614f7875b60 76 NET_AUTH, //Could not auth
iva2k 0:e614f7875b60 77 NET_HW, //HW problem
iva2k 0:e614f7875b60 78
iva2k 0:e614f7875b60 79 NET_OK = 0
iva2k 0:e614f7875b60 80 };
iva2k 0:e614f7875b60 81
iva2k 0:e614f7875b60 82 static NetErr Ethernet();
iva2k 0:e614f7875b60 83 static NetErr PPPoverSerial(int Tx, int Rx, const char* apn, const char* user, const char* password);
iva2k 0:e614f7875b60 84 static NetErr Telit(int pwrSetPin, int pwrMonPin, int Tx, int Rx);
iva2k 0:e614f7875b60 85 #endif
iva2k 0:e614f7875b60 86
iva2k 0:e614f7875b60 87 protected:
iva2k 0:e614f7875b60 88 friend class NetIf;
iva2k 0:e614f7875b60 89 friend class NetTcpSocket;
iva2k 0:e614f7875b60 90 friend class NetUdpSocket;
iva2k 0:e614f7875b60 91
iva2k 0:e614f7875b60 92 static void registerIf(NetIf* pIf);
iva2k 0:e614f7875b60 93 static void unregisterIf(NetIf* pIf);
iva2k 0:e614f7875b60 94
iva2k 0:e614f7875b60 95 static void registerNetTcpSocket(NetTcpSocket* pNetTcpSocket);
iva2k 0:e614f7875b60 96 static void unregisterNetTcpSocket(NetTcpSocket* pNetTcpSocket);
iva2k 0:e614f7875b60 97
iva2k 0:e614f7875b60 98 static void registerNetUdpSocket(NetUdpSocket* pNetUdpSocket);
iva2k 0:e614f7875b60 99 static void unregisterNetUdpSocket(NetUdpSocket* pNetUdpSocket);
iva2k 0:e614f7875b60 100
iva2k 0:e614f7875b60 101 private:
iva2k 0:e614f7875b60 102 static Net& net(); //Return inst of singleton
iva2k 0:e614f7875b60 103
iva2k 0:e614f7875b60 104 NetIf* m_defaultIf;
iva2k 0:e614f7875b60 105
iva2k 0:e614f7875b60 106 list<NetIf*> m_lpIf;
iva2k 0:e614f7875b60 107 list<NetTcpSocket*> m_lpNetTcpSocket;
iva2k 0:e614f7875b60 108 list<NetUdpSocket*> m_lpNetUdpSocket;
iva2k 0:e614f7875b60 109 };
iva2k 0:e614f7875b60 110
iva2k 0:e614f7875b60 111 #endif