NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RPCHandler.cpp Source File

RPCHandler.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "RPCHandler.h"
00025 #include "rpc.h"
00026 
00027 //#define __DEBUG
00028 #include "dbg/dbg.h"
00029 
00030 #define RPC_DATA_LEN 128
00031 
00032 RPCHandler::RPCHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket)
00033 {}
00034 
00035 RPCHandler::~RPCHandler()
00036 {
00037   DBG("\r\nHandler destroyed\r\n");
00038 }
00039 
00040 void RPCHandler::doGet()
00041 {
00042   DBG("\r\nIn RPCHandler::doGet()\r\n");
00043   char resp[RPC_DATA_LEN] = {0};
00044   char req[RPC_DATA_LEN] = {0};
00045   
00046   DBG("\r\nPath : %s\r\n", path().c_str());
00047   DBG("\r\nRoot Path : %s\r\n", rootPath().c_str());
00048   
00049   //Remove path
00050   strncpy(req, path().c_str(), RPC_DATA_LEN-1);
00051   DBG("\r\nRPC req : %s\r\n", req);
00052   
00053   //Remove %20, +, from req
00054   cleanReq(req);
00055   DBG("\r\nRPC req : %s\r\n", req);
00056   
00057   //Do RPC Call
00058   mbed::rpc(req, resp); //FIXME: Use bool result
00059   
00060   //Response
00061   setContentLen( strlen(resp) );
00062   
00063   //Make sure that the browser won't cache this request
00064   respHeaders()["Cache-control"]="no-cache;no-store";
00065  // respHeaders()["Cache-control"]="no-store";
00066   respHeaders()["Pragma"]="no-cache";
00067   respHeaders()["Expires"]="0";
00068   
00069   //Write data
00070   respHeaders()["Connection"] = "close";
00071   writeData(resp, strlen(resp));
00072   DBG("\r\nExit RPCHandler::doGet()\r\n");
00073 }
00074 
00075 void RPCHandler::doPost()
00076 {
00077 
00078 }
00079 
00080 void RPCHandler::doHead()
00081 {
00082 
00083 }
00084 
00085   
00086 void RPCHandler::onReadable() //Data has been read
00087 {
00088 
00089 }
00090 
00091 void RPCHandler::onWriteable() //Data has been written & buf is free
00092 {
00093   DBG("\r\nRPCHandler::onWriteable() event\r\n");
00094   close(); //Data written, we can close the connection
00095 }
00096 
00097 void RPCHandler::onClose() //Connection is closing
00098 {
00099   //Nothing to do
00100 }
00101 
00102 void RPCHandler::cleanReq(char* data)
00103 {
00104   char* p;
00105   static const char* lGarbage[2] = {"%20", "+"};
00106   for(int i = 0; i < 2; i++)
00107   {
00108     while( p = strstr(data, lGarbage[i]) )
00109     {
00110       memset((void*) p, ' ', strlen(lGarbage[i]));
00111     }
00112   }
00113 }
00114   
00115