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