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 #define __LWIP_DEBUG
iva2k 0:e614f7875b60 25 #define __DEBUG
iva2k 0:e614f7875b60 26 #include "dbg.h"
iva2k 0:e614f7875b60 27 #include "mbed.h"
iva2k 0:e614f7875b60 28 #include <cstdarg>
iva2k 1:3ee499525aa5 29 #include "string.h"
iva2k 0:e614f7875b60 30
iva2k 0:e614f7875b60 31 //LocalFileSystem dbgfs("dbgsfs");
iva2k 0:e614f7875b60 32
iva2k 0:e614f7875b60 33 //static FILE* m_fp = NULL;
iva2k 0:e614f7875b60 34
iva2k 1:3ee499525aa5 35 static int strpos (const char *haystack, const char *seed) {
iva2k 1:3ee499525aa5 36 int pos;
iva2k 1:3ee499525aa5 37 const char *pos_char = haystack;
iva2k 1:3ee499525aa5 38 const char *seed_char = seed;
iva2k 1:3ee499525aa5 39 while (*pos_char != 0) {
iva2k 1:3ee499525aa5 40 while (*pos_char != 0 && *seed_char != 0 && *pos_char == *seed_char) {
iva2k 1:3ee499525aa5 41 pos_char++;
iva2k 1:3ee499525aa5 42 seed_char++;
iva2k 1:3ee499525aa5 43 }
iva2k 1:3ee499525aa5 44 if (*seed_char == 0) {
iva2k 1:3ee499525aa5 45 return pos; // We reached the end of seed and matched.
iva2k 1:3ee499525aa5 46 }
iva2k 1:3ee499525aa5 47 pos++;
iva2k 1:3ee499525aa5 48 pos_char = haystack + pos;
iva2k 1:3ee499525aa5 49 seed_char = seed;
iva2k 1:3ee499525aa5 50 }
iva2k 1:3ee499525aa5 51 return -1; // not found
iva2k 1:3ee499525aa5 52 }
iva2k 0:e614f7875b60 53 void DebugStream::debug(const char* format, ...)
iva2k 0:e614f7875b60 54 {
iva2k 0:e614f7875b60 55 // if(!m_fp)
iva2k 0:e614f7875b60 56 // m_fp = fopen("/dbgsfs/dbg.txt", "a");
iva2k 0:e614f7875b60 57 va_list argp;
iva2k 0:e614f7875b60 58
iva2k 0:e614f7875b60 59 va_start(argp, format);
iva2k 1:3ee499525aa5 60 #if 0 // DEBUG_FIXLF
iva2k 0:e614f7875b60 61 vprintf(format, argp);
iva2k 0:e614f7875b60 62 //vfprintf(m_fp, format, argp);
iva2k 0:e614f7875b60 63 va_end(argp);
iva2k 1:3ee499525aa5 64 #else
iva2k 1:3ee499525aa5 65 int len;
iva2k 1:3ee499525aa5 66 char buf[512];
iva2k 1:3ee499525aa5 67 len = vsprintf(buf, format, argp);
iva2k 1:3ee499525aa5 68 if (buf[len-1] == '\n' && buf[len-2] != '\r') {
iva2k 1:3ee499525aa5 69 // Fix CR without LF
iva2k 1:3ee499525aa5 70 buf[len-1] = '\r';
iva2k 1:3ee499525aa5 71 buf[len] = '\n';
iva2k 1:3ee499525aa5 72 buf[len+1] = '\0';
iva2k 1:3ee499525aa5 73 len++;
iva2k 1:3ee499525aa5 74 }
iva2k 1:3ee499525aa5 75 if (len >= (sizeof(buf)/sizeof(buf[0]))) {
iva2k 1:3ee499525aa5 76 printf("DebugStream::debug() buffer overflow (len=%d).\r\n", len);
iva2k 1:3ee499525aa5 77 }
iva2k 1:3ee499525aa5 78 if (WHERECHOP) {
iva2k 1:3ee499525aa5 79 int pos = strpos(format, WHEREFMT);
iva2k 1:3ee499525aa5 80 if (pos >= 0) {
iva2k 1:3ee499525aa5 81 char *from = buf+pos+WHERECHOP;
iva2k 1:3ee499525aa5 82 char *to = buf +WHERESKIP;
iva2k 1:3ee499525aa5 83 while (*from != 0) {
iva2k 1:3ee499525aa5 84 *(to++) = *(from++);
iva2k 1:3ee499525aa5 85 }
iva2k 1:3ee499525aa5 86 *(to) = 0;
iva2k 1:3ee499525aa5 87 }
iva2k 1:3ee499525aa5 88 }
iva2k 1:3ee499525aa5 89 va_end(argp);
iva2k 1:3ee499525aa5 90 printf(buf);
iva2k 1:3ee499525aa5 91 #endif
iva2k 0:e614f7875b60 92
iva2k 0:e614f7875b60 93 // printf("\r\n"); //Flush
iva2k 0:e614f7875b60 94 //local("local"); // Create the local filesystem under the name "local"
iva2k 0:e614f7875b60 95
iva2k 0:e614f7875b60 96 // fclose(m_fp);
iva2k 0:e614f7875b60 97 }
iva2k 0:e614f7875b60 98
iva2k 0:e614f7875b60 99 void DebugStream::release()
iva2k 0:e614f7875b60 100 {
iva2k 0:e614f7875b60 101 //fclose(m_fp);
iva2k 0:e614f7875b60 102 }
iva2k 0:e614f7875b60 103
iva2k 0:e614f7875b60 104 /*
iva2k 0:e614f7875b60 105 int snprintf(char *str, int size, const char *format, ...)
iva2k 0:e614f7875b60 106 {
iva2k 0:e614f7875b60 107 va_list argp;
iva2k 0:e614f7875b60 108
iva2k 0:e614f7875b60 109 va_start(argp, format);
iva2k 0:e614f7875b60 110 vsprintf(str, format, argp);
iva2k 0:e614f7875b60 111 va_end(argp);
iva2k 0:e614f7875b60 112
iva2k 0:e614f7875b60 113 return strlen(str);
iva2k 0:e614f7875b60 114 }
iva2k 0:e614f7875b60 115 */
iva2k 1:3ee499525aa5 116