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
Child:
1:3ee499525aa5

        

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 "RPCHandler.h"
iva2k 0:e614f7875b60 25 #include "rpc.h"
iva2k 0:e614f7875b60 26
iva2k 0:e614f7875b60 27 //#define __DEBUG
iva2k 0:e614f7875b60 28 #include "dbg/dbg.h"
iva2k 0:e614f7875b60 29
iva2k 0:e614f7875b60 30 #define RPC_DATA_LEN 128
iva2k 0:e614f7875b60 31
iva2k 0:e614f7875b60 32 RPCHandler::RPCHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket)
iva2k 0:e614f7875b60 33 {}
iva2k 0:e614f7875b60 34
iva2k 0:e614f7875b60 35 RPCHandler::~RPCHandler()
iva2k 0:e614f7875b60 36 {
iva2k 0:e614f7875b60 37 DBG("\r\nHandler destroyed\r\n");
iva2k 0:e614f7875b60 38 }
iva2k 0:e614f7875b60 39
iva2k 0:e614f7875b60 40 void RPCHandler::doGet()
iva2k 0:e614f7875b60 41 {
iva2k 0:e614f7875b60 42 DBG("\r\nIn RPCHandler::doGet()\r\n");
iva2k 0:e614f7875b60 43 char resp[RPC_DATA_LEN] = {0};
iva2k 0:e614f7875b60 44 char req[RPC_DATA_LEN] = {0};
iva2k 0:e614f7875b60 45
iva2k 0:e614f7875b60 46 DBG("\r\nPath : %s\r\n", path().c_str());
iva2k 0:e614f7875b60 47 DBG("\r\nRoot Path : %s\r\n", rootPath().c_str());
iva2k 0:e614f7875b60 48
iva2k 0:e614f7875b60 49 //Remove path
iva2k 0:e614f7875b60 50 strncpy(req, path().c_str() + rootPath().length(), RPC_DATA_LEN-1);
iva2k 0:e614f7875b60 51 DBG("\r\nRPC req : %s\r\n", req);
iva2k 0:e614f7875b60 52
iva2k 0:e614f7875b60 53 //Remove %20, +, from req
iva2k 0:e614f7875b60 54 cleanReq(req);
iva2k 0:e614f7875b60 55 DBG("\r\nRPC req : %s\r\n", req);
iva2k 0:e614f7875b60 56
iva2k 0:e614f7875b60 57 //Do RPC Call
iva2k 0:e614f7875b60 58 mbed::rpc(req, resp); //FIXME: Use bool result
iva2k 0:e614f7875b60 59
iva2k 0:e614f7875b60 60 //Response
iva2k 0:e614f7875b60 61 setContentLen( strlen(resp) );
iva2k 0:e614f7875b60 62
iva2k 0:e614f7875b60 63 //Make sure that the browser won't cache this request
iva2k 0:e614f7875b60 64 respHeaders()["Cache-control"]="no-cache;no-store";
iva2k 0:e614f7875b60 65 // respHeaders()["Cache-control"]="no-store";
iva2k 0:e614f7875b60 66 respHeaders()["Pragma"]="no-cache";
iva2k 0:e614f7875b60 67 respHeaders()["Expires"]="0";
iva2k 0:e614f7875b60 68
iva2k 0:e614f7875b60 69 //Write data
iva2k 0:e614f7875b60 70 respHeaders()["Connection"] = "close";
iva2k 0:e614f7875b60 71 writeData(resp, strlen(resp));
iva2k 0:e614f7875b60 72 DBG("\r\nExit RPCHandler::doGet()\r\n");
iva2k 0:e614f7875b60 73 }
iva2k 0:e614f7875b60 74
iva2k 0:e614f7875b60 75 void RPCHandler::doPost()
iva2k 0:e614f7875b60 76 {
iva2k 0:e614f7875b60 77
iva2k 0:e614f7875b60 78 }
iva2k 0:e614f7875b60 79
iva2k 0:e614f7875b60 80 void RPCHandler::doHead()
iva2k 0:e614f7875b60 81 {
iva2k 0:e614f7875b60 82
iva2k 0:e614f7875b60 83 }
iva2k 0:e614f7875b60 84
iva2k 0:e614f7875b60 85
iva2k 0:e614f7875b60 86 void RPCHandler::onReadable() //Data has been read
iva2k 0:e614f7875b60 87 {
iva2k 0:e614f7875b60 88
iva2k 0:e614f7875b60 89 }
iva2k 0:e614f7875b60 90
iva2k 0:e614f7875b60 91 void RPCHandler::onWriteable() //Data has been written & buf is free
iva2k 0:e614f7875b60 92 {
iva2k 0:e614f7875b60 93 DBG("\r\nRPCHandler::onWriteable() event\r\n");
iva2k 0:e614f7875b60 94 close(); //Data written, we can close the connection
iva2k 0:e614f7875b60 95 }
iva2k 0:e614f7875b60 96
iva2k 0:e614f7875b60 97 void RPCHandler::onClose() //Connection is closing
iva2k 0:e614f7875b60 98 {
iva2k 0:e614f7875b60 99 //Nothing to do
iva2k 0:e614f7875b60 100 }
iva2k 0:e614f7875b60 101
iva2k 0:e614f7875b60 102 void RPCHandler::cleanReq(char* data)
iva2k 0:e614f7875b60 103 {
iva2k 0:e614f7875b60 104 char* p;
iva2k 0:e614f7875b60 105 static const char* lGarbage[2] = {"%20", "+"};
iva2k 0:e614f7875b60 106 for(int i = 0; i < 2; i++)
iva2k 0:e614f7875b60 107 {
iva2k 0:e614f7875b60 108 while( p = strstr(data, lGarbage[i]) )
iva2k 0:e614f7875b60 109 {
iva2k 0:e614f7875b60 110 memset((void*) p, ' ', strlen(lGarbage[i]));
iva2k 0:e614f7875b60 111 }
iva2k 0:e614f7875b60 112 }
iva2k 0:e614f7875b60 113 }
iva2k 0:e614f7875b60 114
iva2k 0:e614f7875b60 115