Single instance HTTP Server using new Ethernet Interface. Blocking mode only; this improved stability, but the HTTP server must be started from a separate thread.

Dependents:   SmartLight

Fork of HTTPServer by Henry Leinen

Committer:
cabledev
Date:
Thu May 15 16:09:51 2014 +0000
Revision:
17:d7186c696729
Parent:
16:cc3f5c53d0d5
- converted to blocking mode & removed polling; greatly improving stability, but HTTP server must be started from a separate thread (not the main app thread); - moved ethernet interface and port assignment out of start() method; - updated log messages

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leihen 5:dc88012caef1 1 /* FsHandler.cpp */
leihen 5:dc88012caef1 2 #include "mbed.h"
leihen 5:dc88012caef1 3 #include "RpcHandler.h"
leihen 5:dc88012caef1 4 #include "mbed_rpc.h"
leihen 5:dc88012caef1 5
leihen 5:dc88012caef1 6
leihen 16:cc3f5c53d0d5 7 #define DEBUG 1
leihen 16:cc3f5c53d0d5 8 #include "hl_debug.h"
leihen 5:dc88012caef1 9
leihen 5:dc88012caef1 10 RPC rpc("rpc");
leihen 5:dc88012caef1 11
leihen 5:dc88012caef1 12
leihen 5:dc88012caef1 13 HTTPRpcRequestHandler::HTTPRpcRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp)
leihen 5:dc88012caef1 14 : HTTPRequestHandler(Msg, Tcp)
leihen 5:dc88012caef1 15 {
leihen 5:dc88012caef1 16 m_rootPath = rootPath;
leihen 5:dc88012caef1 17 m_localPath = localPath;
leihen 5:dc88012caef1 18
leihen 5:dc88012caef1 19 handleRequest();
leihen 5:dc88012caef1 20 }
leihen 5:dc88012caef1 21
leihen 5:dc88012caef1 22
leihen 5:dc88012caef1 23 HTTPRpcRequestHandler::~HTTPRpcRequestHandler()
leihen 5:dc88012caef1 24 {
leihen 5:dc88012caef1 25 }
leihen 5:dc88012caef1 26
leihen 5:dc88012caef1 27
leihen 5:dc88012caef1 28 int HTTPRpcRequestHandler::handleGetRequest()
leihen 5:dc88012caef1 29 {
leihen 5:dc88012caef1 30 char outBuf[256] = {};
leihen 5:dc88012caef1 31 bool retval = false;
leihen 5:dc88012caef1 32 int err = 404;
leihen 5:dc88012caef1 33 string rpc_args("");
leihen 5:dc88012caef1 34
cabledev 17:d7186c696729 35 INFO("Handling RPC Get Request.");
leihen 5:dc88012caef1 36 // This version of the RPC handler does only accept native RPC commands in the format
leihen 5:dc88012caef1 37 // /<class>/<method> <argument1> [<argument2> [<argument3> ...]]
leihen 5:dc88012caef1 38 // So we can simply pass our local pathg to our rpc
leihen 5:dc88012caef1 39
leihen 5:dc88012caef1 40 // Append missing slash if needed
leihen 5:dc88012caef1 41 if (m_localPath.c_str()[0] != '/') {
leihen 5:dc88012caef1 42 rpc_args = "/";
leihen 5:dc88012caef1 43 }
leihen 5:dc88012caef1 44 // replace the HTTP strings with ascii strings
leihen 5:dc88012caef1 45 for (int i = 0 ; i < m_localPath.length() ; i++) {
leihen 5:dc88012caef1 46 if (m_localPath.substr(i,3) == "%20") {
leihen 5:dc88012caef1 47 rpc_args += " ";
leihen 5:dc88012caef1 48 i+=2;
leihen 5:dc88012caef1 49 }
leihen 5:dc88012caef1 50 else {
leihen 5:dc88012caef1 51 rpc_args += m_localPath.substr(i,1);
leihen 5:dc88012caef1 52 }
leihen 5:dc88012caef1 53 }
leihen 5:dc88012caef1 54 INFO("RPC to %s", rpc_args.c_str());
leihen 5:dc88012caef1 55 retval = rpc.call(rpc_args.c_str(),outBuf);
leihen 5:dc88012caef1 56 INFO("RPC Request returned %d with args : %s", retval==true ? 1 : 0, outBuf);
leihen 5:dc88012caef1 57 if (retval) {
leihen 5:dc88012caef1 58 // No error
leihen 5:dc88012caef1 59 startResponse(retval, strlen(outBuf));
leihen 5:dc88012caef1 60 processResponse(strlen(outBuf), outBuf);
leihen 5:dc88012caef1 61 endResponse();
leihen 5:dc88012caef1 62 err = 0;
leihen 5:dc88012caef1 63 }
leihen 5:dc88012caef1 64
leihen 5:dc88012caef1 65 return err;
leihen 5:dc88012caef1 66 }
leihen 5:dc88012caef1 67
leihen 5:dc88012caef1 68 int HTTPRpcRequestHandler::handlePutRequest()
leihen 5:dc88012caef1 69 {
leihen 5:dc88012caef1 70 return 404;
leihen 5:dc88012caef1 71 }
leihen 5:dc88012caef1 72
leihen 5:dc88012caef1 73 int HTTPRpcRequestHandler::handlePostRequest()
leihen 5:dc88012caef1 74 {
leihen 5:dc88012caef1 75 return 404;
leihen 5:dc88012caef1 76 }