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 #include "lwipNetDnsRequest.h"
iva2k 0:e614f7875b60 25 #include "lwip/err.h" //err_t, ERR_xxx
iva2k 0:e614f7875b60 26 #include "lwip/dns.h"
iva2k 0:e614f7875b60 27
iva2k 0:e614f7875b60 28 #include "netCfg.h"
iva2k 0:e614f7875b60 29 #if NET_LWIP_STACK
iva2k 0:e614f7875b60 30
iva2k 0:e614f7875b60 31 //#define __DEBUG
iva2k 0:e614f7875b60 32 #include "dbg/dbg.h"
iva2k 0:e614f7875b60 33
iva2k 0:e614f7875b60 34 LwipNetDnsRequest::LwipNetDnsRequest(const char* hostname) : NetDnsRequest(hostname), m_state(LWIPNETDNS_START), m_cbFired(false)
iva2k 0:e614f7875b60 35 {
iva2k 0:e614f7875b60 36 DBG("New LwipNetDnsRequest %p\n", this);
iva2k 0:e614f7875b60 37 }
iva2k 0:e614f7875b60 38
iva2k 0:e614f7875b60 39 LwipNetDnsRequest::LwipNetDnsRequest(Host* pHost) : NetDnsRequest(pHost), m_state(LWIPNETDNS_START), m_cbFired(false)
iva2k 0:e614f7875b60 40 {
iva2k 0:e614f7875b60 41 DBG("New LwipNetDnsRequest %p\n", this);
iva2k 0:e614f7875b60 42 }
iva2k 0:e614f7875b60 43
iva2k 0:e614f7875b60 44 LwipNetDnsRequest::~LwipNetDnsRequest()
iva2k 0:e614f7875b60 45 {
iva2k 0:e614f7875b60 46 DBG("LwipNetDnsRequest %p destroyed\n", this);
iva2k 0:e614f7875b60 47 }
iva2k 0:e614f7875b60 48
iva2k 0:e614f7875b60 49 /*
iva2k 0:e614f7875b60 50 Main useful function here (see lwip/dns.h):
iva2k 0:e614f7875b60 51 err_t dns_gethostbyname(const char *hostname, ip_addr_t *addr,
iva2k 0:e614f7875b60 52 dns_found_callback found, void *callback_arg);
iva2k 0:e614f7875b60 53 */
iva2k 0:e614f7875b60 54
iva2k 0:e614f7875b60 55 //Execute request & return OK if found, NOTFOUND or ERROR on error, or PROCESSING if the request has not completed yet
iva2k 0:e614f7875b60 56 void LwipNetDnsRequest::poll()
iva2k 0:e614f7875b60 57 {
iva2k 0:e614f7875b60 58 err_t err;
iva2k 0:e614f7875b60 59 switch(m_state)
iva2k 0:e614f7875b60 60 {
iva2k 0:e614f7875b60 61 case LWIPNETDNS_START: //First req, let's call dns_gethostbyname
iva2k 0:e614f7875b60 62 ip_addr_t ipStruct;
iva2k 0:e614f7875b60 63 err = dns_gethostbyname(m_hostname, &ipStruct, LwipNetDnsRequest::sFoundCb, (void*) this );
iva2k 0:e614f7875b60 64 if( err == ERR_OK )
iva2k 0:e614f7875b60 65 {
iva2k 0:e614f7875b60 66 m_ip = IpAddr(&ipStruct);
iva2k 0:e614f7875b60 67 m_state = LWIPNETDNS_OK;
iva2k 0:e614f7875b60 68 DBG("DNS: Ip found in cache.\n");
iva2k 0:e614f7875b60 69 }
iva2k 0:e614f7875b60 70 else if( err == ERR_INPROGRESS)
iva2k 0:e614f7875b60 71 {
iva2k 0:e614f7875b60 72 DBG("DNS: Processing.\n");
iva2k 0:e614f7875b60 73 m_state = LWIPNETDNS_PROCESSING;
iva2k 0:e614f7875b60 74 }
iva2k 0:e614f7875b60 75 else //Likely ERR_VAL
iva2k 0:e614f7875b60 76 {
iva2k 0:e614f7875b60 77 DBG("DNS: Error on init.\n");
iva2k 0:e614f7875b60 78 m_state = LWIPNETDNS_ERROR;
iva2k 0:e614f7875b60 79 }
iva2k 0:e614f7875b60 80 break;
iva2k 0:e614f7875b60 81 case LWIPNETDNS_PROCESSING:
iva2k 0:e614f7875b60 82 break; //Nothing to do, DNS is polled on interrupt
iva2k 0:e614f7875b60 83 case LWIPNETDNS_OK:
iva2k 0:e614f7875b60 84 if(!m_cbFired)
iva2k 0:e614f7875b60 85 {
iva2k 0:e614f7875b60 86 DBG("DNS: Ip found.\n");
iva2k 0:e614f7875b60 87 m_cbFired = true;
iva2k 0:e614f7875b60 88 onReply(NETDNS_FOUND); //Raise callback
iva2k 0:e614f7875b60 89 }
iva2k 0:e614f7875b60 90 break;
iva2k 0:e614f7875b60 91 case LWIPNETDNS_NOTFOUND:
iva2k 0:e614f7875b60 92 if(!m_cbFired)
iva2k 0:e614f7875b60 93 {
iva2k 0:e614f7875b60 94 DBG("DNS: could not be resolved.\n");
iva2k 0:e614f7875b60 95 m_cbFired = true;
iva2k 0:e614f7875b60 96 onReply(NETDNS_NOTFOUND); //Raise callback
iva2k 0:e614f7875b60 97 }
iva2k 0:e614f7875b60 98 break;
iva2k 0:e614f7875b60 99 case LWIPNETDNS_ERROR:
iva2k 0:e614f7875b60 100 default:
iva2k 0:e614f7875b60 101 if(!m_cbFired)
iva2k 0:e614f7875b60 102 {
iva2k 0:e614f7875b60 103 DBG("DNS: Error.\n");
iva2k 0:e614f7875b60 104 m_cbFired = true;
iva2k 0:e614f7875b60 105 onReply(NETDNS_ERROR); //Raise callback
iva2k 0:e614f7875b60 106 }
iva2k 0:e614f7875b60 107 break;
iva2k 0:e614f7875b60 108 }
iva2k 0:e614f7875b60 109 // return m_state;
iva2k 0:e614f7875b60 110 }
iva2k 0:e614f7875b60 111
iva2k 0:e614f7875b60 112 void LwipNetDnsRequest::foundCb(const char *name, ip_addr_t *ipaddr)
iva2k 0:e614f7875b60 113 {
iva2k 0:e614f7875b60 114 if( ipaddr == NULL )
iva2k 0:e614f7875b60 115 {
iva2k 0:e614f7875b60 116 m_state = LWIPNETDNS_NOTFOUND;
iva2k 0:e614f7875b60 117 return;
iva2k 0:e614f7875b60 118 }
iva2k 0:e614f7875b60 119 m_ip = IpAddr(ipaddr);
iva2k 0:e614f7875b60 120 m_state = LWIPNETDNS_OK;
iva2k 0:e614f7875b60 121 }
iva2k 0:e614f7875b60 122
iva2k 0:e614f7875b60 123
iva2k 0:e614f7875b60 124 void LwipNetDnsRequest::sFoundCb(const char *name, ip_addr_t *ipaddr, void *arg)
iva2k 0:e614f7875b60 125 {
iva2k 0:e614f7875b60 126 LwipNetDnsRequest* pMe = (LwipNetDnsRequest*) arg;
iva2k 0:e614f7875b60 127 return pMe->foundCb( name, ipaddr );
iva2k 0:e614f7875b60 128 }
iva2k 0:e614f7875b60 129
iva2k 0:e614f7875b60 130 #endif
iva2k 0:e614f7875b60 131