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 /* Copyright (C) 2007 MySQL AB & Michael Widenius
iva2k 0:e614f7875b60 2
iva2k 0:e614f7875b60 3 This program is free software; you can redistribute it and/or modify
iva2k 0:e614f7875b60 4 it under the terms of the GNU General Public License as published by
iva2k 0:e614f7875b60 5 the Free Software Foundation; version 2 of the License.
iva2k 0:e614f7875b60 6
iva2k 0:e614f7875b60 7 This program is distributed in the hope that it will be useful,
iva2k 0:e614f7875b60 8 but WITHOUT ANY WARRANTY; without even the implied warranty of
iva2k 0:e614f7875b60 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
iva2k 0:e614f7875b60 10 GNU General Public License for more details.
iva2k 0:e614f7875b60 11
iva2k 0:e614f7875b60 12 You should have received a copy of the GNU General Public License
iva2k 0:e614f7875b60 13 along with this program; if not, write to the Free Software
iva2k 0:e614f7875b60 14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
iva2k 0:e614f7875b60 15
iva2k 0:e614f7875b60 16 #define SCRAMBLE_LENGTH_323 8
iva2k 0:e614f7875b60 17
iva2k 0:e614f7875b60 18 #include <string.h>
iva2k 0:e614f7875b60 19 #include <math.h>
iva2k 0:e614f7875b60 20
iva2k 0:e614f7875b60 21 typedef unsigned int uint;
iva2k 0:e614f7875b60 22 typedef unsigned long ulong;
iva2k 0:e614f7875b60 23 typedef unsigned char uchar;
iva2k 0:e614f7875b60 24
iva2k 0:e614f7875b60 25 struct my_rnd_struct {
iva2k 0:e614f7875b60 26 unsigned long seed1,seed2,max_value;
iva2k 0:e614f7875b60 27 double max_value_dbl;
iva2k 0:e614f7875b60 28 };
iva2k 0:e614f7875b60 29
iva2k 0:e614f7875b60 30 static void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2);
iva2k 0:e614f7875b60 31 static double my_rnd(struct my_rnd_struct *rand_st);
iva2k 0:e614f7875b60 32 static void hash_password(ulong *result, const char *password, uint password_len);
iva2k 0:e614f7875b60 33
iva2k 0:e614f7875b60 34 /*
iva2k 0:e614f7875b60 35 Initialize random generator
iva2k 0:e614f7875b60 36
iva2k 0:e614f7875b60 37 NOTES
iva2k 0:e614f7875b60 38 MySQL's password checks depends on this, so don't do any changes
iva2k 0:e614f7875b60 39 that changes the random numbers that are generated!
iva2k 0:e614f7875b60 40 */
iva2k 0:e614f7875b60 41
iva2k 0:e614f7875b60 42 static void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2)
iva2k 0:e614f7875b60 43 {
iva2k 0:e614f7875b60 44 rand_st->max_value= 0x3FFFFFFFL;
iva2k 0:e614f7875b60 45 rand_st->max_value_dbl=(double) rand_st->max_value;
iva2k 0:e614f7875b60 46 rand_st->seed1=seed1%rand_st->max_value ;
iva2k 0:e614f7875b60 47 rand_st->seed2=seed2%rand_st->max_value;
iva2k 0:e614f7875b60 48 }
iva2k 0:e614f7875b60 49
iva2k 0:e614f7875b60 50 /*
iva2k 0:e614f7875b60 51 Generate random number.
iva2k 0:e614f7875b60 52
iva2k 0:e614f7875b60 53 SYNOPSIS
iva2k 0:e614f7875b60 54 my_rnd()
iva2k 0:e614f7875b60 55 rand_st INOUT Structure used for number generation
iva2k 0:e614f7875b60 56
iva2k 0:e614f7875b60 57 RETURN VALUE
iva2k 0:e614f7875b60 58 generated pseudo random number
iva2k 0:e614f7875b60 59 */
iva2k 0:e614f7875b60 60
iva2k 0:e614f7875b60 61 static double my_rnd(struct my_rnd_struct *rand_st)
iva2k 0:e614f7875b60 62 {
iva2k 0:e614f7875b60 63 rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
iva2k 0:e614f7875b60 64 rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
iva2k 0:e614f7875b60 65 return (((double) rand_st->seed1)/rand_st->max_value_dbl);
iva2k 0:e614f7875b60 66 }
iva2k 0:e614f7875b60 67
iva2k 0:e614f7875b60 68 /*
iva2k 0:e614f7875b60 69 Generate binary hash from raw text string
iva2k 0:e614f7875b60 70 Used for Pre-4.1 password handling
iva2k 0:e614f7875b60 71 SYNOPSIS
iva2k 0:e614f7875b60 72 hash_password()
iva2k 0:e614f7875b60 73 result OUT store hash in this location
iva2k 0:e614f7875b60 74 password IN plain text password to build hash
iva2k 0:e614f7875b60 75 password_len IN password length (password may be not null-terminated)
iva2k 0:e614f7875b60 76 */
iva2k 0:e614f7875b60 77
iva2k 0:e614f7875b60 78 static void hash_password(ulong *result, const char *password, uint password_len)
iva2k 0:e614f7875b60 79 {
iva2k 0:e614f7875b60 80 register ulong nr=1345345333L, add=7, nr2=0x12345671L;
iva2k 0:e614f7875b60 81 ulong tmp;
iva2k 0:e614f7875b60 82 const char *password_end= password + password_len;
iva2k 0:e614f7875b60 83 for (; password < password_end; password++)
iva2k 0:e614f7875b60 84 {
iva2k 0:e614f7875b60 85 if (*password == ' ' || *password == '\t')
iva2k 0:e614f7875b60 86 continue; /* skip space in password */
iva2k 0:e614f7875b60 87 tmp= (ulong) (uchar) *password;
iva2k 0:e614f7875b60 88 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
iva2k 0:e614f7875b60 89 nr2+=(nr2 << 8) ^ nr;
iva2k 0:e614f7875b60 90 add+=tmp;
iva2k 0:e614f7875b60 91 }
iva2k 0:e614f7875b60 92 result[0]=nr & (((ulong) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
iva2k 0:e614f7875b60 93 result[1]=nr2 & (((ulong) 1L << 31) -1L);
iva2k 0:e614f7875b60 94 }
iva2k 0:e614f7875b60 95
iva2k 0:e614f7875b60 96
iva2k 0:e614f7875b60 97
iva2k 0:e614f7875b60 98 /*
iva2k 0:e614f7875b60 99 Scramble string with password.
iva2k 0:e614f7875b60 100 Used in pre 4.1 authentication phase.
iva2k 0:e614f7875b60 101 SYNOPSIS
iva2k 0:e614f7875b60 102 scramble_323()
iva2k 0:e614f7875b60 103 to OUT Store scrambled message here. Buffer must be at least
iva2k 0:e614f7875b60 104 SCRAMBLE_LENGTH_323+1 bytes long
iva2k 0:e614f7875b60 105 message IN Message to scramble. Message must be at least
iva2k 0:e614f7875b60 106 SRAMBLE_LENGTH_323 bytes long.
iva2k 0:e614f7875b60 107 password IN Password to use while scrambling
iva2k 0:e614f7875b60 108 */
iva2k 0:e614f7875b60 109
iva2k 0:e614f7875b60 110 void scramble_323(char *to, const char *message, const char *password)
iva2k 0:e614f7875b60 111 {
iva2k 0:e614f7875b60 112 struct my_rnd_struct rand_st;
iva2k 0:e614f7875b60 113 ulong hash_pass[2], hash_message[2];
iva2k 0:e614f7875b60 114
iva2k 0:e614f7875b60 115 if (password && password[0])
iva2k 0:e614f7875b60 116 {
iva2k 0:e614f7875b60 117 char extra, *to_start=to;
iva2k 0:e614f7875b60 118 const char *message_end= message + SCRAMBLE_LENGTH_323;
iva2k 0:e614f7875b60 119 hash_password(hash_pass,password, (uint) strlen(password));
iva2k 0:e614f7875b60 120 hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
iva2k 0:e614f7875b60 121 my_rnd_init(&rand_st,hash_pass[0] ^ hash_message[0],
iva2k 0:e614f7875b60 122 hash_pass[1] ^ hash_message[1]);
iva2k 0:e614f7875b60 123 for (; message < message_end; message++)
iva2k 0:e614f7875b60 124 *to++= (char) (floor(my_rnd(&rand_st)*31)+64);
iva2k 0:e614f7875b60 125 extra=(char) (floor(my_rnd(&rand_st)*31));
iva2k 0:e614f7875b60 126 while (to_start != to)
iva2k 0:e614f7875b60 127 *(to_start++)^=extra;
iva2k 0:e614f7875b60 128 }
iva2k 0:e614f7875b60 129 *to= 0;
iva2k 0:e614f7875b60 130 }
iva2k 0:e614f7875b60 131