Webserver for peach-board

Committer:
thudt90
Date:
Thu Mar 12 08:26:27 2015 +0000
Revision:
1:f1f734dd23ee
Parent:
0:6ec14f880a00
Just test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thudt90 0:6ec14f880a00 1 #include "RequestHandler.h"
thudt90 0:6ec14f880a00 2 #include "mbed_rpc.h"
thudt90 0:6ec14f880a00 3 #include "RPCObjectManager.h"
thudt90 0:6ec14f880a00 4 #include "RPCCommand.h"
thudt90 0:6ec14f880a00 5
thudt90 0:6ec14f880a00 6 const char* INVALID_CMD = "Invalid RPC command";
thudt90 0:6ec14f880a00 7 const char* DELETE_ERROR = "You must send a DELETE request to remove an object ";
thudt90 0:6ec14f880a00 8 const char* CREATE_ERROR = "You must send a PUT request to create an object";
thudt90 0:6ec14f880a00 9 const char* FUNC_CALL_ERROR = "You must send a GET request to call a function";
thudt90 0:6ec14f880a00 10
thudt90 0:6ec14f880a00 11 void GetRequestHandler::handle(const RPCCommand& cmd, char *reply)
thudt90 0:6ec14f880a00 12 {
thudt90 0:6ec14f880a00 13 switch(cmd.get_type())
thudt90 0:6ec14f880a00 14 {
thudt90 0:6ec14f880a00 15 case DELETE:
thudt90 0:6ec14f880a00 16 printf("Error: %s\n", DELETE_ERROR);
thudt90 0:6ec14f880a00 17 strcat(reply, DELETE_ERROR);
thudt90 0:6ec14f880a00 18 break;
thudt90 0:6ec14f880a00 19 case FUNCTION_CALL:
thudt90 0:6ec14f880a00 20 RPC::call(cmd.get_cmd(), reply);
thudt90 0:6ec14f880a00 21 break;
thudt90 0:6ec14f880a00 22 case CREATE:
thudt90 0:6ec14f880a00 23 printf("Error: %s\n", CREATE_ERROR);
thudt90 0:6ec14f880a00 24 strcat(reply, CREATE_ERROR);
thudt90 0:6ec14f880a00 25 break;
thudt90 0:6ec14f880a00 26 default:
thudt90 0:6ec14f880a00 27 printf("Error: %s\n", INVALID_CMD);
thudt90 0:6ec14f880a00 28 strcat(reply, INVALID_CMD);
thudt90 0:6ec14f880a00 29 break;
thudt90 0:6ec14f880a00 30 }
thudt90 0:6ec14f880a00 31 }
thudt90 0:6ec14f880a00 32
thudt90 0:6ec14f880a00 33 void PutRequestHandler::handle(const RPCCommand& cmd, char *reply)
thudt90 0:6ec14f880a00 34 {
thudt90 0:6ec14f880a00 35 switch(cmd.get_type())
thudt90 0:6ec14f880a00 36 {
thudt90 0:6ec14f880a00 37 case DELETE:
thudt90 0:6ec14f880a00 38 printf("Error: %s\n", DELETE_ERROR);
thudt90 0:6ec14f880a00 39 strcat(reply, DELETE_ERROR);
thudt90 0:6ec14f880a00 40 break;
thudt90 0:6ec14f880a00 41 case FUNCTION_CALL:
thudt90 0:6ec14f880a00 42 printf("Error: %s\n", FUNC_CALL_ERROR);
thudt90 0:6ec14f880a00 43 strcat(reply, FUNC_CALL_ERROR);
thudt90 0:6ec14f880a00 44 break;
thudt90 0:6ec14f880a00 45 case CREATE:
thudt90 0:6ec14f880a00 46 RPC::call(cmd.get_cmd(), reply);
thudt90 0:6ec14f880a00 47 if(strlen(reply) > 0)
thudt90 0:6ec14f880a00 48 {
thudt90 0:6ec14f880a00 49 RPCObjectManager::instance().store_object(reply);
thudt90 0:6ec14f880a00 50 strcat(reply, " has been created");
thudt90 0:6ec14f880a00 51 }
thudt90 0:6ec14f880a00 52 else
thudt90 0:6ec14f880a00 53 {
thudt90 0:6ec14f880a00 54 printf("Error while creating object\n");
thudt90 0:6ec14f880a00 55 strcat(reply, "Error while creating object.");
thudt90 0:6ec14f880a00 56 }
thudt90 0:6ec14f880a00 57 break;
thudt90 0:6ec14f880a00 58 default:
thudt90 0:6ec14f880a00 59 printf("Error: %s\n", INVALID_CMD);
thudt90 0:6ec14f880a00 60 strcat(reply, INVALID_CMD);
thudt90 0:6ec14f880a00 61 break;
thudt90 0:6ec14f880a00 62 }
thudt90 0:6ec14f880a00 63 }
thudt90 0:6ec14f880a00 64
thudt90 0:6ec14f880a00 65 void DeleteRequestHandler::handle(const RPCCommand& cmd, char *reply)
thudt90 0:6ec14f880a00 66 {
thudt90 0:6ec14f880a00 67 switch(cmd.get_type())
thudt90 0:6ec14f880a00 68 {
thudt90 0:6ec14f880a00 69 case CREATE:
thudt90 0:6ec14f880a00 70 printf("Error: %s\n", CREATE_ERROR);
thudt90 0:6ec14f880a00 71 strcat(reply, CREATE_ERROR);
thudt90 0:6ec14f880a00 72 break;
thudt90 0:6ec14f880a00 73 case FUNCTION_CALL:
thudt90 0:6ec14f880a00 74 printf("Error: %s\n", FUNC_CALL_ERROR);
thudt90 0:6ec14f880a00 75 strcat(reply, FUNC_CALL_ERROR);
thudt90 0:6ec14f880a00 76 break;
thudt90 0:6ec14f880a00 77 case DELETE:
thudt90 0:6ec14f880a00 78 RPC::call(cmd.get_cmd(), reply);
thudt90 0:6ec14f880a00 79 RPCObjectManager::instance().remove_object(cmd.get_obj_name());
thudt90 0:6ec14f880a00 80 strcat(reply, "Deleted object ");
thudt90 0:6ec14f880a00 81 strcat(reply, cmd.get_obj_name());
thudt90 0:6ec14f880a00 82 break;
thudt90 0:6ec14f880a00 83 default:
thudt90 0:6ec14f880a00 84 printf("Error: %s\n", INVALID_CMD);
thudt90 0:6ec14f880a00 85 strcat(reply, INVALID_CMD);
thudt90 0:6ec14f880a00 86 break;
thudt90 0:6ec14f880a00 87 }
thudt90 0:6ec14f880a00 88 }
thudt90 0:6ec14f880a00 89
thudt90 0:6ec14f880a00 90 void ComplexRequestHandler::handle(const RPCCommand& cmd, char *reply)
thudt90 0:6ec14f880a00 91 {
thudt90 0:6ec14f880a00 92 switch(cmd.get_type())
thudt90 0:6ec14f880a00 93 {
thudt90 0:6ec14f880a00 94 case CREATE :
thudt90 0:6ec14f880a00 95 putHandler.handle(cmd, reply);
thudt90 0:6ec14f880a00 96 break;
thudt90 0:6ec14f880a00 97 case DELETE :
thudt90 0:6ec14f880a00 98 deleteHandler.handle(cmd, reply);
thudt90 0:6ec14f880a00 99 break;
thudt90 0:6ec14f880a00 100 case FUNCTION_CALL :
thudt90 0:6ec14f880a00 101 getHandler.handle(cmd, reply);
thudt90 0:6ec14f880a00 102 break;
thudt90 0:6ec14f880a00 103 default :
thudt90 0:6ec14f880a00 104 printf("Error: %s\n", INVALID_CMD);
thudt90 0:6ec14f880a00 105 strcat(reply, INVALID_CMD);
thudt90 0:6ec14f880a00 106 break;
thudt90 0:6ec14f880a00 107 }
thudt90 0:6ec14f880a00 108 }
thudt90 0:6ec14f880a00 109