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 "HTTPFile.h"
iva2k 0:e614f7875b60 25
iva2k 0:e614f7875b60 26 HTTPFile::HTTPFile(const char* path) : HTTPData(), m_fp(NULL), m_path(path), m_len(0), m_chunked(false)
iva2k 0:e614f7875b60 27 {
iva2k 0:e614f7875b60 28
iva2k 0:e614f7875b60 29 }
iva2k 0:e614f7875b60 30
iva2k 0:e614f7875b60 31 HTTPFile::~HTTPFile()
iva2k 0:e614f7875b60 32 {
iva2k 0:e614f7875b60 33 closeFile();
iva2k 0:e614f7875b60 34 }
iva2k 0:e614f7875b60 35
iva2k 0:e614f7875b60 36 void HTTPFile::clear()
iva2k 0:e614f7875b60 37 {
iva2k 0:e614f7875b60 38 closeFile();
iva2k 0:e614f7875b60 39 //Force reopening
iva2k 0:e614f7875b60 40 }
iva2k 0:e614f7875b60 41
iva2k 0:e614f7875b60 42 int HTTPFile::read(char* buf, int len)
iva2k 0:e614f7875b60 43 {
iva2k 0:e614f7875b60 44 if(!openFile("r")) //File does not exist, or I/O error...
iva2k 0:e614f7875b60 45 return 0;
iva2k 0:e614f7875b60 46 len = fread(buf, 1, len, m_fp);
iva2k 0:e614f7875b60 47 if( feof(m_fp) )
iva2k 0:e614f7875b60 48 {
iva2k 0:e614f7875b60 49 //File read completely, we can close it
iva2k 0:e614f7875b60 50 closeFile();
iva2k 0:e614f7875b60 51 }
iva2k 0:e614f7875b60 52 return len;
iva2k 0:e614f7875b60 53 }
iva2k 0:e614f7875b60 54
iva2k 0:e614f7875b60 55 int HTTPFile::write(const char* buf, int len)
iva2k 0:e614f7875b60 56 {
iva2k 0:e614f7875b60 57 if(!openFile("w")) //File does not exist, or I/O error...
iva2k 0:e614f7875b60 58 return 0;
iva2k 0:e614f7875b60 59 len = fwrite(buf, 1, len, m_fp);
iva2k 0:e614f7875b60 60 if( (!m_chunked && (ftell(m_fp) >= m_len)) ||
iva2k 0:e614f7875b60 61 (m_chunked && !len) )
iva2k 0:e614f7875b60 62 {
iva2k 0:e614f7875b60 63 //File received completely, we can close it
iva2k 0:e614f7875b60 64 closeFile();
iva2k 0:e614f7875b60 65 }
iva2k 0:e614f7875b60 66 return len;
iva2k 0:e614f7875b60 67 }
iva2k 0:e614f7875b60 68
iva2k 0:e614f7875b60 69 string HTTPFile::getDataType() //Internet media type for Content-Type header
iva2k 0:e614f7875b60 70 {
iva2k 0:e614f7875b60 71 return ""; //Unknown
iva2k 0:e614f7875b60 72 }
iva2k 0:e614f7875b60 73
iva2k 0:e614f7875b60 74 void HTTPFile::setDataType(const string& type) //Internet media type from Content-Type header
iva2k 0:e614f7875b60 75 {
iva2k 0:e614f7875b60 76 //Do not really care here
iva2k 0:e614f7875b60 77 }
iva2k 0:e614f7875b60 78
iva2k 0:e614f7875b60 79 bool HTTPFile::getIsChunked() //For Transfer-Encoding header
iva2k 0:e614f7875b60 80 {
iva2k 0:e614f7875b60 81 return false;
iva2k 0:e614f7875b60 82 }
iva2k 0:e614f7875b60 83
iva2k 0:e614f7875b60 84 void HTTPFile::setIsChunked(bool chunked) //From Transfer-Encoding header
iva2k 0:e614f7875b60 85 {
iva2k 0:e614f7875b60 86 m_chunked = chunked;
iva2k 0:e614f7875b60 87 }
iva2k 0:e614f7875b60 88
iva2k 0:e614f7875b60 89 int HTTPFile::getDataLen() //For Content-Length header
iva2k 0:e614f7875b60 90 {
iva2k 0:e614f7875b60 91 return m_len;
iva2k 0:e614f7875b60 92 }
iva2k 0:e614f7875b60 93
iva2k 0:e614f7875b60 94 void HTTPFile::setDataLen(int len) //From Content-Length header, or if the transfer is chunked, next chunk length
iva2k 0:e614f7875b60 95 {
iva2k 0:e614f7875b60 96 if(!m_chunked)
iva2k 0:e614f7875b60 97 m_len = len; //Useful so that we can close file when last byte is written
iva2k 0:e614f7875b60 98 }
iva2k 0:e614f7875b60 99
iva2k 0:e614f7875b60 100 bool HTTPFile::openFile(const char* mode) //true on success, false otherwise
iva2k 0:e614f7875b60 101 {
iva2k 0:e614f7875b60 102 if(m_fp)
iva2k 0:e614f7875b60 103 return true;
iva2k 0:e614f7875b60 104
iva2k 0:e614f7875b60 105 m_fp = fopen(m_path.c_str(), mode);
iva2k 0:e614f7875b60 106 if(m_fp && mode[0]=='r')
iva2k 0:e614f7875b60 107 {
iva2k 0:e614f7875b60 108 //Seek EOF to get length
iva2k 0:e614f7875b60 109 fseek(m_fp, 0, SEEK_END);
iva2k 0:e614f7875b60 110 m_len = ftell(m_fp);
iva2k 0:e614f7875b60 111 fseek(m_fp, 0, SEEK_SET); //Goto SOF
iva2k 0:e614f7875b60 112 }
iva2k 0:e614f7875b60 113
iva2k 0:e614f7875b60 114 if(!m_fp)
iva2k 0:e614f7875b60 115 return false;
iva2k 0:e614f7875b60 116
iva2k 0:e614f7875b60 117 return true;
iva2k 0:e614f7875b60 118 }
iva2k 0:e614f7875b60 119
iva2k 0:e614f7875b60 120 void HTTPFile::closeFile()
iva2k 0:e614f7875b60 121 {
iva2k 0:e614f7875b60 122 if(m_fp)
iva2k 0:e614f7875b60 123 fclose(m_fp);
iva2k 0:e614f7875b60 124 }