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