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 "url.h"
iva2k 0:e614f7875b60 25
iva2k 0:e614f7875b60 26 Url::Url() : m_port(0)
iva2k 0:e614f7875b60 27 {
iva2k 0:e614f7875b60 28
iva2k 0:e614f7875b60 29 }
iva2k 0:e614f7875b60 30
iva2k 0:e614f7875b60 31 string Url::getProtocol()
iva2k 0:e614f7875b60 32 {
iva2k 0:e614f7875b60 33 return m_protocol;
iva2k 0:e614f7875b60 34 }
iva2k 0:e614f7875b60 35
iva2k 0:e614f7875b60 36 string Url::getHost()
iva2k 0:e614f7875b60 37 {
iva2k 0:e614f7875b60 38 return m_host;
iva2k 0:e614f7875b60 39 }
iva2k 0:e614f7875b60 40
iva2k 0:e614f7875b60 41 bool Url::getHostIp(IpAddr* ip)
iva2k 0:e614f7875b60 42 {
iva2k 0:e614f7875b60 43 unsigned int ipArr[4] = {0};
iva2k 0:e614f7875b60 44 if ( sscanf(m_host.c_str(), "%u.%u.%u.%u", &ipArr[0], &ipArr[1], &ipArr[2], &ipArr[3]) != 4 )
iva2k 0:e614f7875b60 45 {
iva2k 0:e614f7875b60 46 return false;
iva2k 0:e614f7875b60 47 }
iva2k 0:e614f7875b60 48 *ip = IpAddr(ipArr[0], ipArr[1], ipArr[2], ipArr[3]);
iva2k 0:e614f7875b60 49 return true;
iva2k 0:e614f7875b60 50 }
iva2k 0:e614f7875b60 51
iva2k 0:e614f7875b60 52 uint16_t Url::getPort()
iva2k 0:e614f7875b60 53 {
iva2k 0:e614f7875b60 54 return m_port;
iva2k 0:e614f7875b60 55 }
iva2k 0:e614f7875b60 56
iva2k 0:e614f7875b60 57 string Url::getPath()
iva2k 0:e614f7875b60 58 {
iva2k 0:e614f7875b60 59 return m_path;
iva2k 0:e614f7875b60 60 }
iva2k 0:e614f7875b60 61
iva2k 0:e614f7875b60 62 void Url::setProtocol(string protocol)
iva2k 0:e614f7875b60 63 {
iva2k 0:e614f7875b60 64 m_protocol = protocol;
iva2k 0:e614f7875b60 65 }
iva2k 0:e614f7875b60 66
iva2k 0:e614f7875b60 67 void Url::setHost(string host)
iva2k 0:e614f7875b60 68 {
iva2k 0:e614f7875b60 69 m_host = host;
iva2k 0:e614f7875b60 70 }
iva2k 0:e614f7875b60 71
iva2k 0:e614f7875b60 72 void Url::setPort(uint16_t port)
iva2k 0:e614f7875b60 73 {
iva2k 0:e614f7875b60 74 m_port = port;
iva2k 0:e614f7875b60 75 }
iva2k 0:e614f7875b60 76
iva2k 0:e614f7875b60 77 void Url::setPath(string path)
iva2k 0:e614f7875b60 78 {
iva2k 0:e614f7875b60 79 m_path = path;
iva2k 0:e614f7875b60 80 }
iva2k 0:e614f7875b60 81
iva2k 0:e614f7875b60 82 void Url::fromString(string str)
iva2k 0:e614f7875b60 83 {
iva2k 0:e614f7875b60 84 //URI form [protocol://]host[:port]/path
iva2k 0:e614f7875b60 85 int pos = 0;
iva2k 0:e614f7875b60 86 int len = str.find("://");
iva2k 0:e614f7875b60 87 if( len > 0)
iva2k 0:e614f7875b60 88 {
iva2k 0:e614f7875b60 89 m_protocol = str.substr(0, len);
iva2k 0:e614f7875b60 90 pos += len + 3;
iva2k 0:e614f7875b60 91 }
iva2k 0:e614f7875b60 92 else
iva2k 0:e614f7875b60 93 {
iva2k 0:e614f7875b60 94 m_protocol = "";
iva2k 0:e614f7875b60 95 }
iva2k 0:e614f7875b60 96
iva2k 0:e614f7875b60 97 bool isPort = false;
iva2k 0:e614f7875b60 98 int cln_pos = str.find(":", pos);
iva2k 0:e614f7875b60 99 int slash_pos = str.find("/", pos);
iva2k 0:e614f7875b60 100 if( slash_pos == -1 )
iva2k 0:e614f7875b60 101 {
iva2k 0:e614f7875b60 102 slash_pos = str.length();
iva2k 0:e614f7875b60 103 }
iva2k 0:e614f7875b60 104 if( (cln_pos != -1) && (cln_pos < slash_pos) )
iva2k 0:e614f7875b60 105 {
iva2k 0:e614f7875b60 106 isPort = true;
iva2k 0:e614f7875b60 107 len = cln_pos - pos;
iva2k 0:e614f7875b60 108 }
iva2k 0:e614f7875b60 109 else
iva2k 0:e614f7875b60 110 {
iva2k 0:e614f7875b60 111 len = slash_pos - pos;
iva2k 0:e614f7875b60 112 }
iva2k 0:e614f7875b60 113
iva2k 0:e614f7875b60 114 m_host = str.substr(pos, len);
iva2k 0:e614f7875b60 115
iva2k 0:e614f7875b60 116 pos += len;
iva2k 0:e614f7875b60 117 if( isPort )
iva2k 0:e614f7875b60 118 {
iva2k 0:e614f7875b60 119 pos+=1; //Do not keep :
iva2k 0:e614f7875b60 120 unsigned int port = 0;
iva2k 0:e614f7875b60 121 sscanf(str.substr(pos, cln_pos-pos).c_str(),"%u", &port);
iva2k 0:e614f7875b60 122 m_port = (uint16_t)port;
iva2k 0:e614f7875b60 123 pos = slash_pos;
iva2k 0:e614f7875b60 124 }
iva2k 0:e614f7875b60 125
iva2k 0:e614f7875b60 126 m_path = str.substr(pos);
iva2k 0:e614f7875b60 127 }
iva2k 0:e614f7875b60 128
iva2k 0:e614f7875b60 129 string Url::toString()
iva2k 0:e614f7875b60 130 {
iva2k 0:e614f7875b60 131 string url;
iva2k 0:e614f7875b60 132 if( !m_protocol.empty() )
iva2k 0:e614f7875b60 133 {
iva2k 0:e614f7875b60 134 url.append(m_protocol);
iva2k 0:e614f7875b60 135 url.append("://");
iva2k 0:e614f7875b60 136 }
iva2k 0:e614f7875b60 137 url.append(m_host);
iva2k 0:e614f7875b60 138 if( m_port )
iva2k 0:e614f7875b60 139 {
iva2k 0:e614f7875b60 140 char c_port[6] = {0};
iva2k 0:e614f7875b60 141 sprintf(c_port, "%d", m_port);
iva2k 0:e614f7875b60 142 url.append(":");
iva2k 0:e614f7875b60 143 url.append(c_port);
iva2k 0:e614f7875b60 144 }
iva2k 0:e614f7875b60 145 if(m_path[0]!='/')
iva2k 0:e614f7875b60 146 {
iva2k 0:e614f7875b60 147 url.append("/");
iva2k 0:e614f7875b60 148 }
iva2k 0:e614f7875b60 149 url.append(m_path);
iva2k 0:e614f7875b60 150 return url;
iva2k 0:e614f7875b60 151 }