Delta / Mbed 2 deprecated NNN40_APmodeToSTAmodeByHTTPServer

Dependencies:   WIFI_API_32kRAM mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RequestHandler.cpp Source File

RequestHandler.cpp

00001 #include "RequestHandler.h"
00002 #include "mbed_rpc.h"
00003 #include "RPCObjectManager.h"
00004 #include "RPCCommand.h"
00005 #include "mbed.h"
00006 
00007 
00008 const char* INVALID_CMD = "Invalid RPC command";
00009 const char* DELETE_ERROR = "You must send a DELETE request to remove an object ";
00010 const char* CREATE_ERROR = "You must send a PUT request to create an object";
00011 const char* FUNC_CALL_ERROR = "You must send a GET request to call a function";
00012 
00013 void GetRequestHandler::handle(const RPCCommand& cmd, char *reply)
00014 {
00015 
00016     switch(cmd.get_type()) {
00017         case DELETE:
00018             printf("Error: %s\n", DELETE_ERROR);
00019             strcat(reply, DELETE_ERROR);
00020             break;
00021         case FUNCTION_CALL:
00022             RPC::call(cmd.get_cmd(), reply);
00023             break;
00024         case CREATE:
00025             printf("Error: %s\n", CREATE_ERROR);
00026             strcat(reply, CREATE_ERROR);
00027             break;
00028         default:
00029             printf("Error: %s\n", INVALID_CMD);
00030             strcat(reply, INVALID_CMD);
00031             break;
00032     }
00033 }
00034 
00035 void PutRequestHandler::handle(const RPCCommand& cmd, char *reply)
00036 {
00037 
00038     switch(cmd.get_type()) {
00039         case DELETE:
00040             printf("Error: %s\n", DELETE_ERROR);
00041             strcat(reply, DELETE_ERROR);
00042             break;
00043         case FUNCTION_CALL:
00044             printf("Error: %s\n", FUNC_CALL_ERROR);
00045             strcat(reply, FUNC_CALL_ERROR);
00046             break;
00047         case CREATE:
00048             RPC::call(cmd.get_cmd(), reply);
00049             if(strlen(reply) > 0) {
00050                 RPCObjectManager::instance().store_object(reply);
00051                 strcat(reply, " has been created");
00052             } else {
00053                 printf("Error while creating object\n");
00054                 strcat(reply, "Error while creating object.");
00055             }
00056             break;
00057         default:
00058             printf("Error: %s\n", INVALID_CMD);
00059             strcat(reply, INVALID_CMD);
00060             break;
00061     }
00062 }
00063 
00064 void DeleteRequestHandler::handle(const RPCCommand& cmd, char *reply)
00065 {
00066 
00067     switch(cmd.get_type()) {
00068         case CREATE:
00069             printf("Error: %s\n", CREATE_ERROR);
00070             strcat(reply, CREATE_ERROR);
00071             break;
00072         case FUNCTION_CALL:
00073             printf("Error: %s\n", FUNC_CALL_ERROR);
00074             strcat(reply, FUNC_CALL_ERROR);
00075             break;
00076         case DELETE:
00077             RPC::call(cmd.get_cmd(), reply);
00078             RPCObjectManager::instance().remove_object(cmd.get_obj_name());
00079             strcat(reply, "Deleted object ");
00080             strcat(reply, cmd.get_obj_name());
00081             break;
00082         default:
00083             printf("Error: %s\n", INVALID_CMD);
00084             strcat(reply, INVALID_CMD);
00085             break;
00086     }
00087 }
00088 
00089 void ComplexRequestHandler::handle(const RPCCommand& cmd, char *reply)
00090 {
00091 
00092     switch(cmd.get_type()) {
00093         case CREATE :
00094             putHandler.handle(cmd, reply);
00095             break;
00096         case DELETE :
00097             deleteHandler.handle(cmd, reply);
00098             break;
00099         case FUNCTION_CALL :
00100             getHandler.handle(cmd, reply);
00101             break;
00102         default :
00103             printf("Error: %s\n", INVALID_CMD);
00104             strcat(reply, INVALID_CMD);
00105             break;
00106     }
00107 }