Single instance HTTP Server using WiFly Interface.

Dependents:   WiFlyHTTPServerSample MultiThreadingHTTPServer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RpcHandler.cpp Source File

RpcHandler.cpp

00001 /* FsHandler.cpp */
00002 #include "mbed.h"
00003 #include "RpcHandler.h"
00004 #include "mbed_rpc.h"
00005 
00006 
00007 #define DEBUG
00008 #include "debug.h"
00009 
00010 
00011 RPC rpc("rpc");
00012 
00013 
00014 HTTPRpcRequestHandler::HTTPRpcRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg)
00015     : HTTPRequestHandler(Msg)
00016 {
00017     m_rootPath = rootPath;
00018     m_localPath = localPath;
00019     
00020     handleRequest();
00021 }
00022 
00023 
00024 HTTPRpcRequestHandler::~HTTPRpcRequestHandler()
00025 {
00026 }
00027 
00028 
00029 int HTTPRpcRequestHandler::handleGetRequest()
00030 {
00031     char outBuf[256] = {};
00032     bool retval = false;
00033     int err = 404;
00034     string rpc_args("");
00035     
00036     INFO("Handling RPC Get Requesst.");
00037     // This version of the RPC handler does only accept native RPC commands in the format
00038     //  /<class>/<method> <argument1> [<argument2> [<argument3> ...]]
00039     // So we can simply pass our local pathg to our rpc
00040 
00041     //  Append missing slash if needed
00042     if (m_localPath.c_str()[0] != '/') {
00043         rpc_args = "/";
00044     }
00045     // replace the HTTP strings with ascii strings
00046     for (int i = 0 ; i < m_localPath.length() ; i++) {
00047         if (m_localPath.substr(i,3) == "%20") {
00048             rpc_args += " ";
00049             i+=2;
00050         }
00051         else {
00052             rpc_args += m_localPath.substr(i,1);
00053         }
00054     }
00055     INFO("RPC to %s", rpc_args.c_str());
00056     retval = rpc.call(rpc_args.c_str(),outBuf);
00057     INFO("RPC Request returned %d with args : %s", retval==true ? 1 : 0, outBuf);
00058     if (retval) {
00059         //  No error
00060         startResponse(retval, strlen(outBuf));
00061         processResponse(strlen(outBuf), outBuf);
00062         endResponse();
00063         err = 0;
00064     }
00065     
00066     return err;
00067 }
00068 
00069 int HTTPRpcRequestHandler::handlePutRequest()
00070 {
00071     return 404;
00072 }
00073 
00074 int HTTPRpcRequestHandler::handlePostRequest()
00075 {
00076     return 404;
00077 }